aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2024-02-24 20:29:13 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2024-02-24 20:29:13 +0100
commita4c300e687600ae9c9559175362c7de6956a23e6 (patch)
tree7c1ec779cf4f8a183690ceecbd792e43edb35a26
parent83ba678d77dfb96a75f2cf069ce8bd3b47e44620 (diff)
downloaderlang-workshop-a4c300e687600ae9c9559175362c7de6956a23e6.tar.gz
erlang-workshop-a4c300e687600ae9c9559175362c7de6956a23e6.tar.bz2
erlang-workshop-a4c300e687600ae9c9559175362c7de6956a23e6.tar.xz
erlang-workshop-a4c300e687600ae9c9559175362c7de6956a23e6.zip
wip
-rw-r--r--README.md8
-rw-r--r--step-06/Emakefile1
-rw-r--r--step-06/chat_client.erl8
-rw-r--r--step-06/chat_server.erl38
4 files changed, 54 insertions, 1 deletions
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.