libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_proxy_single_socket.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "testutil.hpp"
31 
32 
33 
34 // This is our server task.
35 // It runs a proxy with a single REP socket as both frontend and backend.
36 
37 void
38 server_task (void *ctx)
39 {
40  void *rep = zmq_socket (ctx, ZMQ_REP);
41  assert (rep);
42  int rc = zmq_bind (rep, "tcp://127.0.0.1:5563");
43  assert (rc == 0);
44 
45  // Control socket receives terminate command from main over inproc
46  void *control = zmq_socket (ctx, ZMQ_SUB);
47  assert (control);
48  rc = zmq_setsockopt (control, ZMQ_SUBSCRIBE, "", 0);
49  assert (rc == 0);
50  rc = zmq_connect (control, "inproc://control");
51  assert (rc == 0);
52 
53  // Use rep as both frontend and backend
54  rc = zmq_proxy_steerable (rep, rep, NULL, control);
55  assert (rc == 0);
56 
57  rc = zmq_close (rep);
58  assert (rc == 0);
59  rc = zmq_close (control);
60  assert (rc == 0);
61 }
62 
63 
64 // The main thread simply starts several clients and a server, and then
65 // waits for the server to finish.
66 
67 int main (void)
68 {
70 
71  void *ctx = zmq_ctx_new ();
72  assert (ctx);
73  // client socket pings proxy over tcp
74  void *req = zmq_socket (ctx, ZMQ_REQ);
75  assert (req);
76  int rc = zmq_connect (req, "tcp://127.0.0.1:5563");
77  assert (rc == 0);
78 
79  // Control socket receives terminate command from main over inproc
80  void *control = zmq_socket (ctx, ZMQ_PUB);
81  assert (control);
82  rc = zmq_bind (control, "inproc://control");
83  assert (rc == 0);
84 
85  void *server_thread = zmq_threadstart(&server_task, ctx);
86 
87  char buf[255];
88  rc = zmq_send(req, "msg1", 4, 0);
89  assert (rc == 4);
90  rc = zmq_recv(req, buf, 255, 0);
91  assert (rc == 4);
92  assert (memcmp (buf, "msg1", 4) == 0);
93 
94  rc = zmq_send(req, "msg22", 5, 0);
95  assert (rc == 5);
96  rc = zmq_recv(req, buf, 255, 0);
97  assert (rc == 5);
98  assert (memcmp (buf, "msg22", 5) == 0);
99 
100  rc = zmq_send (control, "TERMINATE", 9, 0);
101  assert (rc == 9);
102 
103  rc = zmq_close (control);
104  assert (rc == 0);
105  rc = zmq_close (req);
106  assert (rc == 0);
107 
108  zmq_threadclose (server_thread);
109 
110  rc = zmq_ctx_term (ctx);
111  assert (rc == 0);
112  return 0;
113 }
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
int main(void)
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SUB
Definition: zmq.h:248
#define ZMQ_SUBSCRIBE
Definition: zmq.h:266
#define ZMQ_REP
Definition: zmq.h:250
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT int zmq_recv(void *s, void *buf, size_t len, int flags)
Definition: zmq.cpp:507
#define ZMQ_PUB
Definition: zmq.h:247
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
ZMQ_EXPORT void zmq_threadclose(void *thread)
Definition: zmq_utils.cpp:85
#define ZMQ_REQ
Definition: zmq.h:249
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
ZMQ_EXPORT int zmq_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
void server_task(void *ctx)
ZMQ_EXPORT void * zmq_threadstart(zmq_thread_fn *func, void *arg)
Definition: zmq_utils.cpp:78
ZMQ_EXPORT int zmq_proxy_steerable(void *frontend, void *backend, void *capture, void *control)
Definition: zmq.cpp:1335