Created
July 5, 2012 14:27
-
-
Save GeertVL-zz/3053984 to your computer and use it in GitHub Desktop.
Erlang 8.11.2.3 (refactor round 1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(ring2). | |
-export([start/2]). | |
start(Msg,Max) -> | |
Head = clasp(Max), | |
shackle(shackle(shackle(shackle(Head))), Head), | |
send(Head, Msg). | |
shackle(LPid) -> | |
RPid = ring(), | |
LPid ! {connect, LPid, RPid}, | |
RPid. | |
shackle(Left, Right) -> | |
Left ! {connect, Left, Right}. | |
clasp(Max) -> | |
spawn(fun() -> loop(0,0,0,Max) end). | |
ring() -> | |
spawn(fun() -> loop(0,0,-1,0) end). | |
send(StartPid, Msg) -> | |
StartPid ! {send, Msg}. | |
loop(LPid, RPid, Beacon, Max) -> | |
receive | |
{connect, Left, Right} -> | |
io:format("shackle connected~n"), | |
loop(Left, Right, Beacon, Max); | |
{send, Msg} when (Beacon > -1) and (Beacon < Max) -> | |
io:format("Message passed clasp: ~p~n", [Msg]), | |
RPid ! {send, Msg}, | |
loop(LPid, RPid, Beacon + 1, Max); | |
{send, Msg} when Beacon =:= -1 -> | |
io:format("Message sent: ~p~n", [Msg]), | |
RPid ! {send, Msg}, | |
loop(LPid, RPid, Beacon, Max); | |
{send, Msg} when Beacon =:= 5 -> | |
io:format("Ring round done.~p~n", [Msg]) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment