Skip to content

Commit 5cf39bf

Browse files
authored
Merge pull request #4598 from brettviren/reimplement-proxy-steerable
A clean room re-implementation of zmq_proxy_steerable()
2 parents 8cdc4ed + 5712ad5 commit 5cf39bf

8 files changed

+724
-41
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Bernd Prager
2727
Bob Beaty
2828
Brandon Carpenter
2929
Brett Cameron
30+
Brett Viren
3031
Brian Buchanan
3132
Burak Arslan
3233
Carl Clemens

Makefile.am

+5
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ test_apps = \
483483
tests/test_issue_566 \
484484
tests/test_proxy_hwm \
485485
tests/test_proxy_single_socket \
486+
tests/test_proxy_steerable \
486487
tests/test_proxy_terminate \
487488
tests/test_getsockopt_memset \
488489
tests/test_setsockopt \
@@ -731,6 +732,10 @@ tests_test_proxy_single_socket_SOURCES = tests/test_proxy_single_socket.cpp
731732
tests_test_proxy_single_socket_LDADD = ${TESTUTIL_LIBS} src/libzmq.la
732733
tests_test_proxy_single_socket_CPPFLAGS = ${TESTUTIL_CPPFLAGS}
733734

735+
tests_test_proxy_steerable_SOURCES = tests/test_proxy_steerable.cpp
736+
tests_test_proxy_steerable_LDADD = ${TESTUTIL_LIBS} src/libzmq.la
737+
tests_test_proxy_steerable_CPPFLAGS = ${TESTUTIL_CPPFLAGS}
738+
734739
tests_test_proxy_terminate_SOURCES = tests/test_proxy_terminate.cpp
735740
tests_test_proxy_terminate_LDADD = ${TESTUTIL_LIBS} src/libzmq.la
736741
tests_test_proxy_terminate_CPPFLAGS = ${TESTUTIL_CPPFLAGS}

doc/zmq_proxy_steerable.txt

+97-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ zmq_proxy_steerable(3)
33

44
NAME
55
----
6-
zmq_proxy_steerable - DEPRECATED
6+
zmq_proxy_steerable - built-in 0MQ proxy with control flow
77

88

99
SYNOPSIS
@@ -14,9 +14,102 @@ SYNOPSIS
1414

1515
DESCRIPTION
1616
-----------
17-
The _zmq_proxy_steerable()_ function is an empty stub that only returns an
18-
*EOPNOTSUPP* error, as the author did not provide a relicense agreement for
19-
the Mozilla Public License v2 relicense of libzmq.
17+
18+
The _zmq_proxy_steerable()_ function is a variant of the _zmq_proxy()_ function.
19+
It accepts a fourth _control_ socket. When the _control_ socket is _NULL_ the
20+
two functions operate identically.
21+
22+
When a _control_ socket of type _REP_ is provided to the proxy function the
23+
application may send commands to the proxy. The following commands are
24+
supported.
25+
26+
_PAUSE_::
27+
The proxy will cease transferring messages between its endpoints.
28+
29+
_RESUME_::
30+
The proxy will resume transferring messages between its endpoints.
31+
32+
_TERMINATE_::
33+
The proxy function will exit with a return value of 0.
34+
35+
_STATISTICS_::
36+
The proxy behavior will remain unchanged and reply with a set of simple summary values of the messages that have been sent through the proxy as described next.
37+
38+
There are eight statistics values, each of size _uint64_t_ in the multi-part
39+
message reply to the _STATISTICS_ command. These are:
40+
41+
- number of messages received by the frontend socket
42+
43+
- number of bytes received by the frontend socket
44+
45+
- number of messages sent by the frontend socket
46+
47+
- number of bytes sent by the frontend socket
48+
49+
- number of messages received by the backend socket
50+
51+
- number of bytes received by the backend socket
52+
53+
- number of messages sent by the backend socket
54+
55+
- number of bytes sent by the backend socket
56+
57+
58+
RETURN VALUE
59+
------------
60+
The _zmq_proxy_steerable()_ function returns 0 if TERMINATE is received on its
61+
control socket. Otherwise, it returns -1 and errno set to ETERM or EINTR (the
62+
0MQ context associated with either of the specified sockets was terminated) or
63+
EFAULT (the provided frontend or backend was invalid).
64+
65+
66+
EXAMPLE
67+
-------
68+
.Create a function to run the proxy
69+
----
70+
// Create the frontend and backend sockets to be proxied
71+
void *frontend = zmq_socket (context, ZMQ_ROUTER);
72+
void *backend = zmq_socket (context, ZMQ_DEALER);
73+
74+
// Create the proxy control socket
75+
void *control = zmq_socket (context, ZMQ_REP);
76+
77+
// Bind the sockets.
78+
zmq_bind (frontend, "tcp://*:5555");
79+
zmq_bind (backend, "tcp://*:5556");
80+
zmq_bind (control, "tcp://*:5557");
81+
82+
zmq_proxy_steerable(frontend, backend, NULL, control);
83+
----
84+
.Code in another thread/process to steer the proxy.
85+
----
86+
void *control = zmq_socket (context, ZMQ_REQ);
87+
zmq_connect (control, "tcp://*:5557");
88+
89+
zmq_msg_t msg;
90+
91+
zmq_send (control, "PAUSE", 5, 0);
92+
zmq_msg_recv (&msg, control, 0));
93+
94+
zmq_send (control, "RESUME", 6, 0);
95+
zmq_msg_recv (&msg, control, 0));
96+
97+
zmq_send (control, "STATISTICS", 10, 0);
98+
while (1) {
99+
zmq_msg_recv (&msg, control, 0));
100+
printf(" %lu", *(uint64_t *)zmq_msg_data (&msg));
101+
if (!zmq_msg_get (&msg, ZMQ_MORE))
102+
break;
103+
}
104+
printf("\n");
105+
106+
zmq_send (control, "TERMINATE", 9, 0);
107+
zmq_msg_recv (&msg, control, 0));
108+
109+
zmq_close(frontend);
110+
zmq_close(backend);
111+
zmq_close(control);
112+
----
20113

21114

22115
SEE ALSO

0 commit comments

Comments
 (0)