-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.