From a4c300e687600ae9c9559175362c7de6956a23e6 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 24 Feb 2024 20:29:13 +0100 Subject: wip --- README.md | 8 +++++++- step-06/Emakefile | 1 + step-06/chat_client.erl | 8 ++++++++ step-06/chat_server.erl | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 step-06/Emakefile create mode 100644 step-06/chat_client.erl create mode 100644 step-06/chat_server.erl diff --git a/README.md b/README.md index 15c5ae2..8f58dd8 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,12 @@ apt install rebar3 # depends on erlang * https://www.erlang.org/downloads#prebuilt +# Tips + +``` +export ERL_AFLAGS="-kernel shell_history enabled" +``` + # Resources * https://www.erlang.org/doc/ @@ -21,7 +27,7 @@ apt install rebar3 # depends on erlang # Notes * All nodes in system: `net_adm:names()`. +* All nodes in system: `net_kernel:nodes_info()`. * Show all processes and registered names: `i()`. * ... on all nodes `ni()`. * Show all registered processes: `regs()`. -* \ No newline at end of file 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. -- cgit v1.2.3