Skip to content

Instantly share code, notes, and snippets.

@alinpopa
Created February 8, 2010 18:25
Show Gist options
  • Save alinpopa/298421 to your computer and use it in GitHub Desktop.
Save alinpopa/298421 to your computer and use it in GitHub Desktop.
-module(consumers).
-export([start/0, consumer1/0]).
start() ->
loop([]).
loop(Consumers) ->
receive
{subscribe, Consumer} ->
ConsExits = lists:member(Consumer, Consumers),
if
ConsExits =/= true ->
loop([Consumer|Consumers]);
true -> loop(Consumers)
end;
{exit, Consumer} ->
Consumer ! {exit},
loop(lists:delete(Consumer, Consumers));
{msg, Msg} ->
process_msg(Msg, Consumers),
loop(Consumers);
consumers ->
io:format("Consumers: ~p~n",[Consumers]),
loop(Consumers);
exit -> ok;
_ -> loop(Consumers)
end.
process_msg(_, []) -> ok;
process_msg(Msg, [H|T]) ->
H ! {msg, Msg},
process_msg(Msg, T).
consumer1() ->
receive
{exit} ->
io:format("[~p] - Exit - bye.~n",[self()]),
ok;
{msg, Msg} ->
io:format("[~p] - Message: ~p~n",[self(), Msg]),
consumer1()
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment