-
Notifications
You must be signed in to change notification settings - Fork 48
/
router_with_dealer.erl
53 lines (42 loc) · 1.58 KB
/
router_with_dealer.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at http://mozilla.org/MPL/2.0/.
-module(router_with_dealer).
-export([main/0]).
start_worker(Identity) ->
Parent = self(),
spawn_link(
fun () ->
{ok, Socket} = chumak:socket(dealer, Identity),
{ok, _PeerPid} = chumak:connect(Socket, tcp, "localhost", 5585),
worker_loop(Socket, Identity, Parent)
end
).
worker_loop(Socket, Identity, Parent) ->
{ok, Multipart} = chumak:recv_multipart(Socket),
case Multipart of
[<<"EXIT">>] ->
ok;
_ ->
Parent ! {recv, Identity, Multipart}
end.
main() ->
application:ensure_started(chumak),
{ok, Socket} = chumak:socket(router),
{ok, _BindPid} = chumak:bind(Socket, tcp, "localhost", 5585),
start_worker("A"),
start_worker("B"),
timer:sleep(100), %% wait workers to be established
ok = chumak:send_multipart(Socket, [<<"A">>, <<"My message one">>]),
ok = chumak:send_multipart(Socket, [<<"B">>, <<"My message two">>]),
ok = chumak:send_multipart(Socket, [<<"A">>, <<"EXIT">>]),
ok = chumak:send_multipart(Socket, [<<"B">>, <<"EXIT">>]),
MessageA = receive
{recv, "A", MultipartA} ->
MultipartA
end,
MessageB = receive
{recv, "B", MultipartB} ->
MultipartB
end,
io:format("Received: ~p...~p\n", [MessageA, MessageB]).