Repository: booksbyus/zguide (language: php)

Issue:
Erlang files for taskvent, taskwork, tasksink do not use chumak
`erlzmq` is no more developed so these examples should be migrated to use the `chumak` library.


Discussion / hints:

Fix patch:
diff --git a/examples/Erlang/tasksink.es b/examples/Erlang/tasksink.es
index 2a783e03..97072b7d 100755
--- a/examples/Erlang/tasksink.es
+++ b/examples/Erlang/tasksink.es
@@ -6,33 +6,31 @@
 %%
 
 main(_) ->
-    %% Prepare our context and socket
-    {ok, Context} = erlzmq:context(),
-    {ok, Receiver} = erlzmq:socket(Context, pull),
-    ok = erlzmq:bind(Receiver, "tcp://*:5558"),
+    application:start(chumak),
 
-    %% Wait for start of batch
-    {ok, _} = erlzmq:recv(Receiver),
+    {ok, Receiver} = chumak:socket(pull),
 
-    %% Start our clock now
-    Start = now(),
+    case chumak:connect(Receiver, tcp, "localhost", 5557) of
+        {ok, _ConnectPid} ->
+            io:format("Connection OK with Pid: ~p\n", [Receiver]);
+        {error, Reason} ->
+            io:format("Connection failed for this reason: ~p\n", [Reason])
+    end,
+
+    {ok, Sender} = chumak:socket(push),
+    case chumak:connect(Sender, tcp, "localhost", 5558) of
+        {ok, _ConnectPid1} ->
+            io:format("Connection OK with Pid: ~p\n", [Sender])
+    end,
 
-    %% Process 100 confirmations
-    process_confirmations(Receiver, 100),
+    loop(Receiver, Sender).
 
-    %% Calculate and report duration of batch
-    io:format("Total elapsed time: ~b msec~n",
-              [timer:now_diff(now(), Start) div 1000]),
+loop(Receiver, Sender) ->
+    {ok, Work} = chumak:recv(Receiver),
 
-    ok = erlzmq:close(Receiver),
-    ok = erlzmq:term(Context).
+    io:format(" . "),
 
-process_confirmations(_Receiver, 0) -> ok;
-process_confirmations(Receiver, N) when N > 0 ->
-    {ok, _} = erlzmq:recv(Receiver),
-    case N - 1 rem 10 of
-        0 -> io:format(":");
-        _ -> io:format(".")
-    end,
-    process_confirmations(Receiver, N - 1).
+    timer:sleep(list_to_integer(binary_to_list(Work))),
 
+    ok = chumak:send(Sender, <<" ">>),
+    loop(Receiver, Sender).
diff --git a/examples/Erlang/taskvent.es b/examples/Erlang/taskvent.es
index 0b703489..0966ef12 100755
--- a/examples/Erlang/taskvent.es
+++ b/examples/Erlang/taskvent.es
@@ -4,36 +4,42 @@
 %% Binds PUSH socket to tcp://localhost:5557
 %% Sends batch of tasks to workers via that socket
 %%
-
-main(_) ->
-    {ok, Context} = erlzmq:context(),
-
-    %% Socket to send messages on  
-    {ok, Sender} = erlzmq:socket(Context, push),
-    ok = erlzmq:bind(Sender, "tcp://*:5557"),
-
-    %% Socket to send start of batch message on
-    {ok, Sink} = erlzmq:socket(Context, push),
-    ok = erlzmq:connect(Sink, "tcp://localhost:5558"),
+main(_Args) ->
+    application:start(chumak),
+    {ok, Sender} = chumak:socket(push),
+    
+    case chumak:bind(Sender, tcp, "*", 5557) of
+        {ok, _BindPid} ->
+            io:format("Binding OK with Pid: ~p\n", [Sender]);
+        {error, Reason} ->
+            io:format("Connection Failed for this reason: ~p\n", [Reason]);
+        X ->
+            io:format("Unhandled reply for bind ~p\n", [X])
+    end,
+
+    {ok, Sink} = chumak:socket(push),
+
+    case chumak:connect(Sink, tcp, "localhost", 5558) of
+        {ok, _ConnectPid} ->
+            io:format("Connection OK with Pid: ~p\n", [Sink]);
+        {error, Reason2} ->
+            io:format("Connection Failed for this reason: ~p\n", [Reason2]);
+        X2 ->
+            io:format("Unhandled reply for connect  ~p\n", [X2])
+    end,
 
     {ok, _} = io:fread("Press Enter when workers are ready: ", ""),
-    io:format("Sending task to workers~n",[]),
+    io:format("Sending task to workers~n", []),
 
-    %% The first message is "0" and signals start of batch
-    ok = erlzmq:send(Sink, <<"0">>),
+    ok = chumak:send(Sink, <<"0">>),
 
     %% Send 100 tasks
     TotalCost = send_tasks(Sender, 100, 0),
-    io:format("Total expected cost: ~b msec~n", [TotalCost]),
-
-    ok = erlzmq:close(Sink),
-    ok = erlzmq:close(Sender),
+    io:format("Total expected cost: ~b msec~n", [TotalCost]).
 
-    %% Terminate with 1 second to send pending messages
-    erlzmq:term(Context, 1000).
 
 send_tasks(_Sender, 0, TotalCost) -> TotalCost;
-send_tasks(Sender, N, TotalCost) when N > 0 ->
-    Workload = random:uniform(100) + 1,
-    ok = erlzmq:send(Sender, list_to_binary(integer_to_list(Workload))),
-    send_tasks(Sender, N - 1, TotalCost + Workload).
+send_tasks(Sender, N, TotalCost) when N>0 ->
+    Workload = rand:uniform(100) + 1,
+    ok = chumak:send(Sender, list_to_binary(integer_to_list(Workload))),
+    send_tasks(Sender, N-1, TotalCost + Workload).
diff --git a/examples/Erlang/taskwork.es b/examples/Erlang/taskwork.es
index eb1554b4..7885e4bc 100755
--- a/examples/Erlang/taskwork.es
+++ b/examples/Erlang/taskwork.es
@@ -8,34 +8,31 @@
 %%
 
 main(_) ->
-    {ok,Context} = erlzmq:context(),
+    application:start(chumak),
 
-    %% Socket to receive messages on
-    {ok ,Receiver} = erlzmq:socket(Context, pull),
-    ok = erlzmq:connect(Receiver, "tcp://localhost:5557"),
+    {ok, Receiver} = chumak:socket(pull),
 
-    %% Socket to send messages to
-    {ok, Sender} = erlzmq:socket(Context, push),
-    ok = erlzmq:connect(Sender, "tcp://localhost:5558"),
+    case chumak:connect(Receiver, tcp, "localhost", 5557) of
+        {ok, _ConnectPid} ->
+            io:format("Connection OK with Pid: ~p\n", [Receiver]);
+        {error, Reason} ->
+            io:format("Connection failed for this reason: ~p\n", [Reason])
+    end,
 
-    %% Process tasks forever
-    loop(Receiver,Sender),
+    {ok, Sender} = chumak:socket(push),
+    case chumak:connect(Sender, tcp, "localhost", 5558) of
+        {ok, _ConnectPid1} ->
+            io:format("Connection OK with Pid: ~p\n", [Sender])
+    end,
 
-    %% We never get here, but
-    ok = erlzmq:close(Receiver),
-    ok = erlzmq:close(Sender),
-    ok = erlzmq:term(Context).
+    loop(Receiver, Sender).
 
-loop(Receiver,Sender) ->
-    {ok, Work} = erlzmq:recv(Receiver),
+loop(Receiver, Sender) ->
+    {ok, Work} = chumak:recv(Receiver),
 
-    %% Simple progress indicator for the viewer
-    io:format("."),
+    io:format(" . "),
 
-    %% Do the work
     timer:sleep(list_to_integer(binary_to_list(Work))),
 
-    %% Send results to sink
-    ok = erlzmq:send(Sender, <<"">>),
-
+    ok = chumak:send(Sender, <<" ">>),
     loop(Receiver, Sender).


Test patch: