From aa3c25288e44bcf082680308256f9390cba50bbc Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 23 Feb 2024 20:21:53 +0100 Subject: wip --- README.md | 10 ++++++++++ step-05/echo_client.erl | 14 ++++++++++++++ step-05/echo_server.erl | 22 ++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 step-05/echo_client.erl create mode 100644 step-05/echo_server.erl diff --git a/README.md b/README.md index a3630cb..15c5ae2 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,16 @@ apt install rebar3 # depends on erlang # Resources +* https://www.erlang.org/doc/ +* https://www.erlang.org/doc/reference_manual * https://learnyousomeerlang.com/ * https://adoptingerlang.org * https://www.jetbrains.com/help/idea/erlang-start-project.html + +# Notes + +* All nodes in system: `net_adm:names()`. +* 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-05/echo_client.erl b/step-05/echo_client.erl new file mode 100644 index 0000000..92785be --- /dev/null +++ b/step-05/echo_client.erl @@ -0,0 +1,14 @@ +-module(echo_client). + +-export([send_ping/1]). + +send_ping(Echo_Node) -> + io:format("Sending echo to ~s~n", [Echo_Node]), + io:format("self=~p~n", [self()]), + {echo, Echo_Node} ! {ping, self()}, + receive + {pong, Server} -> io:format("got pong from ~w~n", [Server]); + X -> io:format("Unexpected reply: ~w~n", [X]) + after 1000 -> io:format("timeout~n") + end, + io:format("done~n"). diff --git a/step-05/echo_server.erl b/step-05/echo_server.erl new file mode 100644 index 0000000..3f531e5 --- /dev/null +++ b/step-05/echo_server.erl @@ -0,0 +1,22 @@ +-module(echo_server). + +-export([start/0, loop/1]). + +loop(0) -> + io:format("ping finished~n", []); + +loop(N) -> + receive + {ping, Node} -> + io:format("~w: Got ping req from: ~w~n", [self(), Node]), + Node ! {pong, self()}; + X -> + io:format("Unexpected message: ~w~n", [X]) + end, + echo_server:loop(N - 1). + +start() -> + Pid = spawn(echo_server, loop, [10]), + io:format("Starting server, pid=~w~n", [Pid]), + io:format("Starting server, pid=~w~n", [Pid]), + register(echo, Pid). -- cgit v1.2.3