Repository: booksbyus/zguide (language: php)

Issue:
hwserver and hwclient  erlang example with Chumak
Current examples of `hwserver` and `hwclient` for erlang use `erlzmq` lib which has not been updated for more than 5 years. Create examples to use the `chumak` library instead.


Discussion / hints:

Fix patch:
diff --git a/examples/Erlang/hwclient.erl b/examples/Erlang/hwclient.erl
new file mode 100644
index 000000000..2ff84a1bc
--- /dev/null
+++ b/examples/Erlang/hwclient.erl
@@ -0,0 +1,21 @@
+-module(hwclient).
+-export([main/0]).
+
+%%
+%% "Hello world" client example.
+%% Connects to tcp://localhost:5555
+%% Sends <<"Hello">> to server and prints the response
+%%
+
+main() ->
+    application:start(chumak),
+    {ok, Socket} = chumak:socket(req, "my-req"),
+    {ok, Pid} = chumak:connect(Socket, tcp, "localhost", 5555),
+    loop(Socket).
+
+
+loop(Socket) ->
+    chumak:send(Socket, "Hello"),
+    {ok, RecvMessage} = chumak:recv(Socket),
+    io:format("Recv Reply: ~p\n", [RecvMessage]),
+    loop(Socket).
diff --git a/examples/Erlang/hwclient.es b/examples/Erlang/hwclient.es
deleted file mode 100755
index 1cc795592..000000000
--- a/examples/Erlang/hwclient.es
+++ /dev/null
@@ -1,27 +0,0 @@
-#! /usr/bin/env escript
-%%
-%% "Hello world" client example.
-%% Connects to tcp://localhost:5555
-%% Sends <<"Hello">> to server and prints the response
-%%
-
-main(_) ->
-    {ok, Context} = erlzmq:context(),
-
-    %% Socket to talk to server
-    io:format("Connecting to hello world server...~n"),
-    {ok, Requester} = erlzmq:socket(Context, req),
-    ok = erlzmq:connect(Requester,"tcp://localhost:5555"),
-
-    %% Send 10 requests
-    lists:foreach(
-      fun(N) ->
-              io:format("Sending Hello ~b...~n", [N]),
-              ok = erlzmq:send(Requester, <<"Hello">>),
-
-              {ok, Reply} = erlzmq:recv(Requester),
-              io:format("Received ~s ~b~n", [Reply, N])
-      end, lists:seq(1, 10)),
-
-    ok = erlzmq:close(Requester),
-    ok = erlzmq:term(Context).
diff --git a/examples/Erlang/hwserver.erl b/examples/Erlang/hwserver.erl
new file mode 100644
index 000000000..37b209482
--- /dev/null
+++ b/examples/Erlang/hwserver.erl
@@ -0,0 +1,20 @@
+-module(hwserver).
+-export([main/0]).
+
+%% Starts a local hello server.
+%% Binds to tcp://localhost:5555
+
+main() ->
+    application:start(chumak),
+    {ok, Socket} = chumak:socket(rep, "my-rep"),
+    {ok, Pid} = chumak:bind(Socket, tcp, "localhost", 5555),
+    loop(Socket).
+
+loop(Socket) ->
+    {ok, RecvMessage} = chumak:recv(Socket),
+    io:format("Received request : ~p\n", [RecvMessage]),
+
+    timer:sleep(1000),
+
+    chumak:send(Socket, "World"),
+    loop(Socket).
diff --git a/examples/Erlang/hwserver.es b/examples/Erlang/hwserver.es
deleted file mode 100755
index edb9a5c73..000000000
--- a/examples/Erlang/hwserver.es
+++ /dev/null
@@ -1,31 +0,0 @@
-#! /usr/bin/env escript
-%%
-%% Hello World server in Erlang
-%% Binds REP socket to tcp://*:5555
-%% Accepts any message, replies with "World"
-%%
-
-main(_) ->
-    {ok, Context} = erlzmq:context(),
-
-    %% Socket to talk to clients
-    {ok, Responder} = erlzmq:socket(Context, rep),
-    ok = erlzmq:bind(Responder, "tcp://*:5555"),
-
-    loop(Responder),
-
-    %% We never get here, but if we did, this is how we end
-    ok = erlzmq:close(Responder),
-    ok = erlzmq:term(Context).
-
-loop(Responder) ->
-    %% Wait for request
-    {ok, Msg} = erlzmq:recv(Responder),
-    io:format("Received ~s~n", [Msg]),
-
-    %% Do some 'work'
-    timer:sleep(1000),
-
-    %% Send reply back to client
-    ok = erlzmq:send(Responder, <<"World">>),
-    loop(Responder).


Test patch: