aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2024-02-23 20:37:44 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2024-02-23 20:37:44 +0100
commit34f56f81e88b12f3aedf26bc4d9d0f55788d5bf5 (patch)
tree695f1b00ef163941fc20d74c8db0352b646650c6
parentaa3c25288e44bcf082680308256f9390cba50bbc (diff)
downloaderlang-workshop-34f56f81e88b12f3aedf26bc4d9d0f55788d5bf5.tar.gz
erlang-workshop-34f56f81e88b12f3aedf26bc4d9d0f55788d5bf5.tar.bz2
erlang-workshop-34f56f81e88b12f3aedf26bc4d9d0f55788d5bf5.tar.xz
erlang-workshop-34f56f81e88b12f3aedf26bc4d9d0f55788d5bf5.zip
wip
-rw-r--r--step-05/README.md64
-rw-r--r--step-05/echo_client.erl6
-rw-r--r--step-05/echo_server.erl1
3 files changed, 66 insertions, 5 deletions
diff --git a/step-05/README.md b/step-05/README.md
new file mode 100644
index 0000000..bc5f6ed
--- /dev/null
+++ b/step-05/README.md
@@ -0,0 +1,64 @@
+*This is an alternative to `tut17`.*
+
+This example is an *echo server* to which you can send a `ping` message, and you will get a `pong` message in return.
+
+In this example session we start two Erlang nodes (doesn't have to be on the same machine), one named `es` for "echo
+server" and another one named `node2`.
+
+Start and compile/load the code on both nodes:
+
+```
+$ erl -sname es
+(es@biwia)1> c(echo_server).
+```
+
+```
+$ erl -sname node2
+(node2@biwia)1> c(echo_server).
+```
+
+Start the server:
+
+```
+(es@biwia)2> echo_server:start().
+Starting server, pid=<0.95.0>
+true
+(es@biwia)3>
+```
+
+The server uses `spawn/3` to run in the background. With the PID that `spawn` returns, it registers an `echo` name
+that the client will use. We can look at this process with `regs()`:
+
+```
+(es@biwia)3> regs().
+
+** Registered procs on node es@biwia **
+Name Pid Initial Call Reds Msgs
+application_controlle <0.44.0> erlang:apply/2 784 0
+auth <0.60.0> auth:init/1 784 0
+code_server <0.50.0> erlang:apply/2 129042 0
+echo <0.95.0> echo_server:loop/1 5 0
+...
+```
+
+On the client side we can now send a ping message with `echo_client:send_ping(es@biwia)`. The `es@biwia` atom names
+the *node* on the specific *host*. A host can run many nodes so the node name needs to be included.
+
+```
+(node2@biwia)2> echo_client:send_ping(es@biwia).
+Sending echo to es@biwia, self=<0.88.0>
+got pong from <14715.95.0>
+ok
+```
+
+On the server you will notice this output:
+
+```
+(es@biwia)4>
+<0.95.0>: Got ping req from: <14771.88.0>
+(es@biwia)4>
+```
+
+Both the server and client uses `self/0` to find their own PID and you see that the server has the PID `0.95.0` and the
+client has `0.85.0`. When the server receives the message the client pid is slightly different: `14771.88.0`, and the
+same for the client's reply from the server.
diff --git a/step-05/echo_client.erl b/step-05/echo_client.erl
index 92785be..25ddffe 100644
--- a/step-05/echo_client.erl
+++ b/step-05/echo_client.erl
@@ -3,12 +3,10 @@
-export([send_ping/1]).
send_ping(Echo_Node) ->
- io:format("Sending echo to ~s~n", [Echo_Node]),
- io:format("self=~p~n", [self()]),
+ io:format("Sending echo to ~s, self=~p~n", [Echo_Node, 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").
+ end.
diff --git a/step-05/echo_server.erl b/step-05/echo_server.erl
index 3f531e5..2dcc425 100644
--- a/step-05/echo_server.erl
+++ b/step-05/echo_server.erl
@@ -18,5 +18,4 @@ loop(N) ->
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).