aboutsummaryrefslogtreecommitdiff
path: root/step-06/chat_server.erl
diff options
context:
space:
mode:
Diffstat (limited to 'step-06/chat_server.erl')
-rw-r--r--step-06/chat_server.erl38
1 files changed, 38 insertions, 0 deletions
diff --git a/step-06/chat_server.erl b/step-06/chat_server.erl
new file mode 100644
index 0000000..f5cdaf9
--- /dev/null
+++ b/step-06/chat_server.erl
@@ -0,0 +1,38 @@
+-module(chat_server).
+
+-export([start/0, stop/0, loop/1]).
+
+-record(chat_state, {
+ nicks = [],
+ rooms = []
+}).
+
+loop(State) ->
+ receive
+ dump ->
+ io:format("Server state:~n~p~n", [State]);
+ stop ->
+ io:format("Exiting~n"),
+ exit(normal);
+ X ->
+ io:format("unexpected message: ~p~n", [X])
+% after 1000 ->
+ end,
+ chat_server:loop(State).
+
+start() ->
+ InitialState = #chat_state{},
+ Pid = spawn(chat_server, loop, [InitialState]),
+ case global:register_name(chat, Pid) of
+ yes ->
+ io:format("Server started, pid=~p~n", [Pid]);
+ X ->
+ io:format("Register name failed: ~p~n", [X]),
+ Pid ! stop
+ end.
+
+stop() ->
+ case global:whereis_name(chat) of
+ undefined -> io:format("Server not running~n");
+ Pid -> Pid ! stop
+ end.