libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_reqrep_device.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 int main (void)
33 {
35  void *ctx = zmq_ctx_new ();
36  assert (ctx);
37 
38  // Create a req/rep device.
39  void *dealer = zmq_socket (ctx, ZMQ_DEALER);
40  assert (dealer);
41  int rc = zmq_bind (dealer, "tcp://127.0.0.1:5560");
42  assert (rc == 0);
43  void *router = zmq_socket (ctx, ZMQ_ROUTER);
44  assert (router);
45  rc = zmq_bind (router, "tcp://127.0.0.1:5561");
46  assert (rc == 0);
47 
48  // Create a worker.
49  void *rep = zmq_socket (ctx, ZMQ_REP);
50  assert (rep);
51  rc = zmq_connect (rep, "tcp://127.0.0.1:5560");
52  assert (rc == 0);
53 
54  // Create a client.
55  void *req = zmq_socket (ctx, ZMQ_REQ);
56  assert (req);
57  rc = zmq_connect (req, "tcp://127.0.0.1:5561");
58  assert (rc == 0);
59 
60  // Send a request.
61  rc = zmq_send (req, "ABC", 3, ZMQ_SNDMORE);
62  assert (rc == 3);
63  rc = zmq_send (req, "DEF", 3, 0);
64  assert (rc == 3);
65 
66  // Pass the request through the device.
67  for (int i = 0; i != 4; i++) {
68  zmq_msg_t msg;
69  rc = zmq_msg_init (&msg);
70  assert (rc == 0);
71  rc = zmq_msg_recv (&msg, router, 0);
72  assert (rc >= 0);
73  int rcvmore;
74  size_t sz = sizeof (rcvmore);
75  rc = zmq_getsockopt (router, ZMQ_RCVMORE, &rcvmore, &sz);
76  assert (rc == 0);
77  rc = zmq_msg_send (&msg, dealer, rcvmore? ZMQ_SNDMORE: 0);
78  assert (rc >= 0);
79  }
80 
81  // Receive the request.
82  char buff [3];
83  rc = zmq_recv (rep, buff, 3, 0);
84  assert (rc == 3);
85  assert (memcmp (buff, "ABC", 3) == 0);
86  int rcvmore;
87  size_t sz = sizeof (rcvmore);
88  rc = zmq_getsockopt (rep, ZMQ_RCVMORE, &rcvmore, &sz);
89  assert (rc == 0);
90  assert (rcvmore);
91  rc = zmq_recv (rep, buff, 3, 0);
92  assert (rc == 3);
93  assert (memcmp (buff, "DEF", 3) == 0);
94  rc = zmq_getsockopt (rep, ZMQ_RCVMORE, &rcvmore, &sz);
95  assert (rc == 0);
96  assert (!rcvmore);
97 
98  // Send the reply.
99  rc = zmq_send (rep, "GHI", 3, ZMQ_SNDMORE);
100  assert (rc == 3);
101  rc = zmq_send (rep, "JKL", 3, 0);
102  assert (rc == 3);
103 
104  // Pass the reply through the device.
105  for (int i = 0; i != 4; i++) {
106  zmq_msg_t msg;
107  rc = zmq_msg_init (&msg);
108  assert (rc == 0);
109  rc = zmq_msg_recv (&msg, dealer, 0);
110  assert (rc >= 0);
111  int rcvmore;
112  rc = zmq_getsockopt (dealer, ZMQ_RCVMORE, &rcvmore, &sz);
113  assert (rc == 0);
114  rc = zmq_msg_send (&msg, router, rcvmore? ZMQ_SNDMORE: 0);
115  assert (rc >= 0);
116  }
117 
118  // Receive the reply.
119  rc = zmq_recv (req, buff, 3, 0);
120  assert (rc == 3);
121  assert (memcmp (buff, "GHI", 3) == 0);
122  rc = zmq_getsockopt (req, ZMQ_RCVMORE, &rcvmore, &sz);
123  assert (rc == 0);
124  assert (rcvmore);
125  rc = zmq_recv (req, buff, 3, 0);
126  assert (rc == 3);
127  assert (memcmp (buff, "JKL", 3) == 0);
128  rc = zmq_getsockopt (req, ZMQ_RCVMORE, &rcvmore, &sz);
129  assert (rc == 0);
130  assert (!rcvmore);
131 
132  // Clean up.
133  rc = zmq_close (req);
134  assert (rc == 0);
135  rc = zmq_close (rep);
136  assert (rc == 0);
137  rc = zmq_close (router);
138  assert (rc == 0);
139  rc = zmq_close (dealer);
140  assert (rc == 0);
141  rc = zmq_ctx_term (ctx);
142  assert (rc == 0);
143 
144  return 0 ;
145 }
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
#define ZMQ_DEALER
Definition: zmq.h:251
#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_ROUTER
Definition: zmq.h:252
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
ZMQ_EXPORT int zmq_getsockopt(void *s, int option, void *optval, size_t *optvallen)
Definition: zmq.cpp:277
#define ZMQ_REQ
Definition: zmq.h:249
int main(void)
ZMQ_EXPORT int zmq_msg_send(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:629
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_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
Definition: zmq.h:221
#define ZMQ_RCVMORE
Definition: zmq.h:272
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
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613