libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_connect_rid.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 
34  void *rbind, *rconn1;
35  int ret;
36  char buff[256];
37  char msg[] = "hi 1";
38  const char *bindip = "tcp://127.0.0.1:5556";
39  int disabled = 0;
40  int zero = 0;
41  void *ctx = zmq_ctx_new ();
42 
43  // Set up listener STREAM.
44  rbind = zmq_socket (ctx, ZMQ_STREAM);
45  assert (rbind);
46  ret = zmq_setsockopt (rbind, ZMQ_STREAM_NOTIFY, &disabled, sizeof (disabled));
47  assert (ret == 0);
48  ret = zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero));
49  assert (0 == ret);
50  ret = zmq_bind (rbind, bindip);
51  assert(0 == ret);
52 
53  // Set up connection stream.
54  rconn1 = zmq_socket (ctx, ZMQ_STREAM);
55  assert (rconn1);
56  ret = zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero));
57  assert (0 == ret);
58 
59  // Do the connection.
60  ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
61  assert (0 == ret);
62  ret = zmq_connect (rconn1, bindip);
63 
64 /* Uncomment to test assert on duplicate rid.
65  // Test duplicate connect attempt.
66  ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
67  assert (0 == ret);
68  ret = zmq_connect (rconn1, bindip);
69  assert (0 == ret);
70 */
71  // Send data to the bound stream.
72  ret = zmq_send (rconn1, "conn1", 6, ZMQ_SNDMORE);
73  assert (6 == ret);
74  ret = zmq_send (rconn1, msg, 5, 0);
75  assert (5 == ret);
76 
77  // Accept data on the bound stream.
78  ret = zmq_recv (rbind, buff, 256, 0);
79  assert (ret);
80  assert (0 == buff[0]);
81  ret = zmq_recv (rbind, buff+128, 128, 0);
82  assert (5 == ret);
83  assert ('h' == buff[128]);
84 
85  // Handle close of the socket.
86  ret = zmq_unbind (rbind, bindip);
87  assert(0 == ret);
88  ret = zmq_close (rbind);
89  assert(0 == ret);
90  ret = zmq_close (rconn1);
91  assert(0 == ret);
92 
93  zmq_ctx_destroy (ctx);
94 }
95 
96 void test_router_2_router(bool named){
97  void *rbind, *rconn1;
98  int ret;
99  char buff[256];
100  char msg[] = "hi 1";
101  const char *bindip = "tcp://127.0.0.1:5556";
102  int zero = 0;
103  void *ctx = zmq_ctx_new ();
104 
105  // Create bind socket.
106  rbind = zmq_socket (ctx, ZMQ_ROUTER);
107  assert (rbind);
108  ret = zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero));
109  assert (0 == ret);
110  ret = zmq_bind (rbind, bindip);
111  assert (0 == ret);
112 
113  // Create connection socket.
114  rconn1 = zmq_socket (ctx, ZMQ_ROUTER);
115  assert (rconn1);
116  ret = zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero));
117  assert (0 == ret);
118 
119  // If we're in named mode, set some identities.
120  if (named) {
121  ret = zmq_setsockopt (rbind, ZMQ_IDENTITY, "X", 1);
122  ret = zmq_setsockopt (rconn1, ZMQ_IDENTITY, "Y", 1);
123  }
124 
125  // Make call to connect using a connect_rid.
126  ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
127  assert (0 == ret);
128  ret = zmq_connect (rconn1, bindip);
129  assert (0 == ret);
130 /* Uncomment to test assert on duplicate rid
131  // Test duplicate connect attempt.
132  ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
133  assert (0 == ret);
134  ret = zmq_connect (rconn1, bindip);
135  assert (0 == ret);
136 */
137  // Send some data.
138  ret = zmq_send (rconn1, "conn1", 6, ZMQ_SNDMORE);
139  assert (6 == ret);
140  ret = zmq_send (rconn1, msg, 5, 0);
141  assert (5 == ret);
142 
143  // Receive the name.
144  ret = zmq_recv (rbind, buff, 256, 0);
145  if (named)
146  assert (ret && 'Y' == buff[0]);
147  else
148  assert (ret && 0 == buff[0]);
149 
150  // Receive the data.
151  ret = zmq_recv (rbind, buff+128, 128, 0);
152  assert(5 == ret && 'h' == buff[128]);
153 
154  // Send some data back.
155  if (named) {
156  ret = zmq_send (rbind, buff, 1, ZMQ_SNDMORE);
157  assert (1 == ret);
158  }
159  else {
160  ret = zmq_send (rbind, buff, 5, ZMQ_SNDMORE);
161  assert (5 == ret);
162  }
163  ret = zmq_send_const (rbind, "ok", 3, 0);
164  assert (3 == ret);
165 
166  // If bound socket identity naming a problem, we'll likely see something funky here.
167  ret = zmq_recv (rconn1, buff, 256, 0);
168  assert ('c' == buff[0] && 6 == ret);
169  ret = zmq_recv (rconn1, buff+128, 128, 0);
170  assert (3 == ret && 'o' == buff[128]);
171 
172  ret = zmq_unbind (rbind, bindip);
173  assert(0 == ret);
174  ret = zmq_close (rbind);
175  assert(0 == ret);
176  ret = zmq_close (rconn1);
177  assert(0 == ret);
178 
179  zmq_ctx_destroy (ctx);
180 }
181 
182 int main (void)
183 {
185 
187  test_router_2_router (false);
188  test_router_2_router (true);
189 
190  return 0;
191 }
void test_router_2_router(bool named)
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
ZMQ_EXPORT int zmq_unbind(void *s, const char *addr)
Definition: zmq.cpp:343
#define ZMQ_STREAM
Definition: zmq.h:257
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
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
ZMQ_EXPORT int zmq_send_const(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:416
#define ZMQ_STREAM_NOTIFY
Definition: zmq.h:324
#define ZMQ_ROUTER
Definition: zmq.h:252
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
#define ZMQ_LINGER
Definition: zmq.h:276
#define ZMQ_IDENTITY
Definition: zmq.h:265
void test_stream_2_stream()
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_ctx_destroy(void *context)
Definition: zmq.cpp:236
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
int main(void)
#define ZMQ_CONNECT_RID
Definition: zmq.h:311