diff options
Diffstat (limited to 'step-06')
-rw-r--r-- | step-06/Emakefile | 1 | ||||
-rw-r--r-- | step-06/chat_client.erl | 8 | ||||
-rw-r--r-- | step-06/chat_server.erl | 38 |
3 files changed, 47 insertions, 0 deletions
diff --git a/step-06/Emakefile b/step-06/Emakefile new file mode 100644 index 0000000..4670287 --- /dev/null +++ b/step-06/Emakefile @@ -0,0 +1 @@ +{'*',[debug_info]}. diff --git a/step-06/chat_client.erl b/step-06/chat_client.erl new file mode 100644 index 0000000..fc2461c --- /dev/null +++ b/step-06/chat_client.erl @@ -0,0 +1,8 @@ +-module(chat_client). + +-export([run/0]). + +run() -> + Server = global:whereis_name(chat), + Server ! hello, + io:format("bye bye!~n"). 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. |