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