-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring.erl
48 lines (39 loc) · 1.1 KB
/
ring.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-module(ring).
%%Interface
-export([create/1,send/2,quit/0]).
-export([loop/1]).
create(N) when is_integer(N), N > 2 ->
register(last, spawn(ring, loop, [first])),
Pid = spawn(ring, loop, [last]),
create_loop(N-2, Pid).
create_loop(1, Pid) ->
register(first, spawn(ring, loop, [Pid])),
ok;
create_loop(N, Pid) ->
NewPid = spawn(ring, loop, [Pid]),
create_loop(N-1, NewPid).
loop(ProcessToMsg) ->
Last = whereis(last),
receive
stop when self() == Last ->
stopped;
stop ->
ProcessToMsg ! stop,
io:format("Stop sent from ~p to ~p.~n", [self(), ProcessToMsg]);
{_, 1} when self()==Last ->
loop(ProcessToMsg);
{Msg, Loops} when self()==Last ->
ProcessToMsg ! {Msg, Loops-1},
io:format("~p sent from ~p to ~p.~n", [Msg, self(), ProcessToMsg]),
loop(ProcessToMsg);
{Msg, Loops} ->
ProcessToMsg ! {Msg, Loops},
io:format("~p sent from ~p to ~p.~n", [Msg, self(), ProcessToMsg]),
loop(ProcessToMsg)
end.
send(Msg, Loops) when is_integer(Loops), Loops > 0 ->
first ! {Msg, Loops},
ok.
quit() ->
first ! stop,
ok.