libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_req_correlate.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  void *req = zmq_socket (ctx, ZMQ_REQ);
39  assert (req);
40 
41  void *router = zmq_socket (ctx, ZMQ_ROUTER);
42  assert (router);
43 
44  int enabled = 1;
45  int rc = zmq_setsockopt (req, ZMQ_REQ_CORRELATE, &enabled, sizeof (int));
46  assert (rc == 0);
47 
48  int rcvtimeo = 100;
49  rc = zmq_setsockopt (req, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int));
50  assert (rc == 0);
51 
52  rc = zmq_connect (req, "tcp://localhost:5555");
53  assert (rc == 0);
54 
55  rc = zmq_bind (router, "tcp://127.0.0.1:5555");
56  assert (rc == 0);
57 
58  // Send a multi-part request.
59  s_send_seq (req, "ABC", "DEF", SEQ_END);
60 
61  zmq_msg_t msg;
62  zmq_msg_init (&msg);
63 
64  // Receive peer identity
65  rc = zmq_msg_recv (&msg, router, 0);
66  assert (rc != -1);
67  assert (zmq_msg_size (&msg) > 0);
68  zmq_msg_t peer_id_msg;
69  zmq_msg_init (&peer_id_msg);
70  zmq_msg_copy (&peer_id_msg, &msg);
71 
72  int more = 0;
73  size_t more_size = sizeof (more);
74  rc = zmq_getsockopt (router, ZMQ_RCVMORE, &more, &more_size);
75  assert (rc == 0);
76  assert (more);
77 
78  // Receive request id 1
79  rc = zmq_msg_recv (&msg, router, 0);
80  assert (rc != -1);
81  assert (zmq_msg_size (&msg) == sizeof(uint32_t));
82  uint32_t req_id = *static_cast<uint32_t *> (zmq_msg_data (&msg));
83  zmq_msg_t req_id_msg;
84  zmq_msg_init (&req_id_msg);
85  zmq_msg_copy (&req_id_msg, &msg);
86 
87  more = 0;
88  more_size = sizeof (more);
89  rc = zmq_getsockopt (router, ZMQ_RCVMORE, &more, &more_size);
90  assert (rc == 0);
91  assert (more);
92 
93  // Receive the rest.
94  s_recv_seq (router, 0, "ABC", "DEF", SEQ_END);
95 
96  uint32_t bad_req_id = req_id + 1;
97 
98  // Send back a bad reply: wrong req id, 0, data
99  zmq_msg_copy (&msg, &peer_id_msg);
100  rc = zmq_msg_send (&msg, router, ZMQ_SNDMORE);
101  assert (rc != -1);
102  zmq_msg_init_data (&msg, &bad_req_id, sizeof (uint32_t), NULL, NULL);
103  rc = zmq_msg_send (&msg, router, ZMQ_SNDMORE);
104  assert (rc != -1);
105  s_send_seq (router, 0, "DATA", SEQ_END);
106 
107  // Send back a good reply: good req id, 0, data
108  zmq_msg_copy (&msg, &peer_id_msg);
109  rc = zmq_msg_send (&msg, router, ZMQ_SNDMORE);
110  assert (rc != -1);
111  zmq_msg_copy (&msg, &req_id_msg);
112  rc = zmq_msg_send (&msg, router, ZMQ_SNDMORE);
113  assert (rc != -1);
114  s_send_seq (router, 0, "GHI", SEQ_END);
115 
116  // Receive reply. If bad reply got through, we wouldn't see
117  // this particular data.
118  s_recv_seq (req, "GHI", SEQ_END);
119 
120  rc = zmq_msg_close (&msg);
121  assert (rc == 0);
122 
123  rc = zmq_msg_close (&peer_id_msg);
124  assert (rc == 0);
125 
126  rc = zmq_msg_close (&req_id_msg);
127  assert (rc == 0);
128 
129  close_zero_linger (req);
130  close_zero_linger (router);
131 
132  rc = zmq_ctx_term (ctx);
133  assert (rc == 0);
134 
135  return 0;
136 }
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
ZMQ_EXPORT int zmq_msg_init_data(zmq_msg_t *msg, void *data, size_t size, zmq_free_fn *ffn, void *hint)
Definition: zmq.cpp:623
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT int zmq_msg_copy(zmq_msg_t *dest, zmq_msg_t *src)
Definition: zmq.cpp:661
#define ZMQ_ROUTER
Definition: zmq.h:252
ZMQ_EXPORT void * zmq_msg_data(zmq_msg_t *msg)
Definition: zmq.cpp:666
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_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_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
void close_zero_linger(void *socket)
Definition: testutil.hpp:275
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
void s_recv_seq(void *socket,...)
Definition: testutil.hpp:236
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
const char * SEQ_END
Definition: testutil.hpp:198
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
int main(void)
void s_send_seq(void *socket,...)
Definition: testutil.hpp:205
ZMQ_EXPORT size_t zmq_msg_size(zmq_msg_t *msg)
Definition: zmq.cpp:671
#define ZMQ_RCVTIMEO
Definition: zmq.h:284
#define ZMQ_REQ_CORRELATE
Definition: zmq.h:305
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613