Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Created December 8, 2008 04:49
Show Gist options
  • Save ngerakines/33346 to your computer and use it in GitHub Desktop.
Save ngerakines/33346 to your computer and use it in GitHub Desktop.
A simple erlang server demo.
-module(bottles).
-export([start/0, start/1, server_loop/1, take/0, info/0, stop/0]).
start() ->
start(99).
start(State) ->
Pid = spawn(bottles, server_loop, [State]),
pg2:create(bottles_server),
pg2:join(bottles_server, Pid),
Pid.
server_loop(State) ->
NewState = receive
{From, stop} ->
From ! stopped,
exit(normal);
{From, info} ->
From ! State,
State;
{From, take_one_down} ->
From ! ok,
State - 1
end,
bottles:server_loop(NewState).
take() ->
pg2:get_closest_pid(bottles_server) ! {self(), take_one_down},
receive X -> X end.
info() ->
pg2:get_closest_pid(bottles_server) ! {self(), info},
receive X -> X end.
stop() ->
pg2:get_closest_pid(bottles_server) ! {self(), stop},
stopped == receive X -> X end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment