#! /usr/bin/env escript
%%
%% Custom routing Router to Papa (ROUTER to REP)
%%
%% We will do this all in one thread to emphasize the sequence
%% of events…
main(_) ->
{ok, Context} = erlzmq:context(),
{ok, Client} = erlzmq:socket(Context, router),
ok = erlzmq:bind(Client, "ipc://routing.ipc"),
{ok, Worker} = erlzmq:socket(Context, rep),
ok = erlzmq:setsockopt(Worker, identity, @<@@[[/span]][[span style="color:#BA2121"]]"A"[[/span]][[span style="color:#666666"]]@@>@),
ok = erlzmq:connect(Worker, "ipc://routing.ipc"),
%% Wait for the worker to connect so that when we send a message
%% with routing envelope, it will actually match the worker…
timer:sleep(1000),
%% Send papa address, address stack, empty part, and request
erlzmq:send(Client, @<@@[[/span]][[span style="color:#BA2121"]]"A"[[/span]][[span style="color:#666666"]]@@>@, [sndmore]),
erlzmq:send(Client, @<@@[[/span]][[span style="color:#BA2121"]]"address 3"[[/span]][[span style="color:#666666"]]@@>@, [sndmore]),
erlzmq:send(Client, @<@@[[/span]][[span style="color:#BA2121"]]"address 2"[[/span]][[span style="color:#666666"]]@@>@, [sndmore]),
erlzmq:send(Client, @<@@[[/span]][[span style="color:#BA2121"]]"address 1"[[/span]][[span style="color:#666666"]]@@>@, [sndmore]),
erlzmq:send(Client, @<>@, [sndmore]),
erlzmq:send(Client, @<@@[[/span]][[span style="color:#BA2121"]]"This is the workload"[[/span]][[span style="color:#666666"]]@@>@),
%% Worker should get just the workload
erlzmq_util:dump(Worker),
%% We don't play with envelopes in the Worker
erlzmq:send(Worker, @<@@[[/span]][[span style="color:#BA2121"]]"This is the reply"[[/span]][[span style="color:#666666"]]@@>@),
%% Now dump what we got off the ROUTER socket…
erlzmq_util:dump(Client),
erlzmq:close(Client),
erlzmq:close(Worker),
erlzmq:term(Context).