libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_stream.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 // ZMTP protocol greeting structure
33 
34 typedef unsigned char byte;
35 typedef struct {
36  byte signature [10]; // 0xFF 8*0x00 0x7F
37  byte version [2]; // 0x03 0x00 for ZMTP/3.0
38  byte mechanism [20]; // "NULL"
40  byte filler [31];
42 
43 #define ZMTP_DEALER 5 // Socket type constants
44 
45 // This is a greeting matching what 0MQ will send us; note the
46 // 8-byte size is set to 1 for backwards compatibility
47 
48 static zmtp_greeting_t
49  greeting = { { 0xFF, 0, 0, 0, 0, 0, 0, 0, 1, 0x7F },
50  { 3, 0 },
51  { 'N', 'U', 'L', 'L'},
52  0,
53  { 0 }
54  };
55 
56 static void
58 {
59  int rc;
60 
61  // Set up our context and sockets
62  void *ctx = zmq_ctx_new ();
63  assert (ctx);
64 
65  // We'll be using this socket in raw mode
66  void *stream = zmq_socket (ctx, ZMQ_STREAM);
67  assert (stream);
68 
69  int zero = 0;
70  rc = zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero));
71  assert (rc == 0);
72  int enabled = 1;
73  rc = zmq_setsockopt (stream, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled));
74  assert (rc == 0);
75  rc = zmq_bind (stream, "tcp://127.0.0.1:5556");
76  assert (rc == 0);
77 
78  // We'll be using this socket as the other peer
79  void *dealer = zmq_socket (ctx, ZMQ_DEALER);
80  assert (dealer);
81  rc = zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero));
82  assert (rc == 0);
83  rc = zmq_connect (dealer, "tcp://localhost:5556");
84 
85  // Send a message on the dealer socket
86  rc = zmq_send (dealer, "Hello", 5, 0);
87  assert (rc == 5);
88 
89  // Connecting sends a zero message
90  // First frame is identity
91  zmq_msg_t identity;
92  rc = zmq_msg_init (&identity);
93  assert (rc == 0);
94  rc = zmq_msg_recv (&identity, stream, 0);
95  assert (rc > 0);
96  assert (zmq_msg_more (&identity));
97 
98  // Verify the existence of Peer-Address metadata
99  char const *peer_address = zmq_msg_gets (&identity, "Peer-Address");
100  assert (peer_address != 0);
101  assert (streq (peer_address, "127.0.0.1"));
102 
103  // Second frame is zero
104  byte buffer [255];
105  rc = zmq_recv (stream, buffer, 255, 0);
106  assert (rc == 0);
107 
108  // Verify the existence of Peer-Address metadata
109  peer_address = zmq_msg_gets (&identity, "Peer-Address");
110  assert (peer_address != 0);
111  assert (streq (peer_address, "127.0.0.1"));
112 
113  // Real data follows
114  // First frame is identity
115  rc = zmq_msg_recv (&identity, stream, 0);
116  assert (rc > 0);
117  assert (zmq_msg_more (&identity));
118 
119  // Verify the existence of Peer-Address metadata
120  peer_address = zmq_msg_gets (&identity, "Peer-Address");
121  assert (peer_address != 0);
122  assert (streq (peer_address, "127.0.0.1"));
123 
124  // Second frame is greeting signature
125  rc = zmq_recv (stream, buffer, 255, 0);
126  assert (rc == 10);
127  assert (memcmp (buffer, greeting.signature, 10) == 0);
128 
129  // Send our own protocol greeting
130  rc = zmq_msg_send (&identity, stream, ZMQ_SNDMORE);
131  assert (rc > 0);
132  rc = zmq_send (stream, &greeting, sizeof (greeting), 0);
133  assert (rc == sizeof (greeting));
134 
135  // Now we expect the data from the DEALER socket
136  // We want the rest of greeting along with the Ready command
137  int bytes_read = 0;
138  while (bytes_read < 97) {
139  // First frame is the identity of the connection (each time)
140  rc = zmq_msg_recv (&identity, stream, 0);
141  assert (rc > 0);
142  assert (zmq_msg_more (&identity));
143  // Second frame contains the next chunk of data
144  rc = zmq_recv (stream, buffer + bytes_read, 255 - bytes_read, 0);
145  assert (rc >= 0);
146  bytes_read += rc;
147  }
148 
149  // First two bytes are major and minor version numbers.
150  assert (buffer [0] == 3); // ZMTP/3.0
151  assert (buffer [1] == 0);
152 
153  // Mechanism is "NULL"
154  assert (memcmp (buffer + 2, "NULL\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0);
155  assert (memcmp (buffer + 54, "\4\51\5READY", 8) == 0);
156  assert (memcmp (buffer + 62, "\13Socket-Type\0\0\0\6DEALER", 22) == 0);
157  assert (memcmp (buffer + 84, "\10Identity\0\0\0\0", 13) == 0);
158 
159  // Announce we are ready
160  memcpy (buffer, "\4\51\5READY", 8);
161  memcpy (buffer + 8, "\13Socket-Type\0\0\0\6ROUTER", 22);
162  memcpy (buffer + 30, "\10Identity\0\0\0\0", 13);
163 
164  // Send Ready command
165  rc = zmq_msg_send (&identity, stream, ZMQ_SNDMORE);
166  assert (rc > 0);
167  rc = zmq_send (stream, buffer, 43, 0);
168  assert (rc == 43);
169 
170  // Now we expect the data from the DEALER socket
171  // First frame is, again, the identity of the connection
172  rc = zmq_msg_recv (&identity, stream, 0);
173  assert (rc > 0);
174  assert (zmq_msg_more (&identity));
175 
176  // Third frame contains Hello message from DEALER
177  rc = zmq_recv (stream, buffer, sizeof buffer, 0);
178  assert (rc == 7);
179 
180  // Then we have a 5-byte message "Hello"
181  assert (buffer [0] == 0); // Flags = 0
182  assert (buffer [1] == 5); // Size = 5
183  assert (memcmp (buffer + 2, "Hello", 5) == 0);
184 
185  // Send "World" back to DEALER
186  rc = zmq_msg_send (&identity, stream, ZMQ_SNDMORE);
187  assert (rc > 0);
188  byte world [] = { 0, 5, 'W', 'o', 'r', 'l', 'd' };
189  rc = zmq_send (stream, world, sizeof (world), 0);
190  assert (rc == sizeof (world));
191 
192  // Expect response on DEALER socket
193  rc = zmq_recv (dealer, buffer, 255, 0);
194  assert (rc == 5);
195  assert (memcmp (buffer, "World", 5) == 0);
196 
197  // Test large messages over STREAM socket
198 # define size 64000
199  uint8_t msgout [size];
200  memset (msgout, 0xAB, size);
201  zmq_send (dealer, msgout, size, 0);
202 
203  uint8_t msgin [9 + size];
204  memset (msgin, 0, 9 + size);
205  bytes_read = 0;
206  while (bytes_read < 9 + size) {
207  // Get identity frame
208  rc = zmq_recv (stream, buffer, 256, 0);
209  assert (rc > 0);
210  // Get next chunk
211  rc = zmq_recv (stream, msgin + bytes_read, 9 + size - bytes_read, 0);
212  assert (rc > 0);
213  bytes_read += rc;
214  }
215  int byte_nbr;
216  for (byte_nbr = 0; byte_nbr < size; byte_nbr++) {
217  if (msgin [9 + byte_nbr] != 0xAB)
218  assert (false);
219  }
220  rc = zmq_close (dealer);
221  assert (rc == 0);
222 
223  rc = zmq_close (stream);
224  assert (rc == 0);
225 
226  rc = zmq_ctx_term (ctx);
227  assert (rc == 0);
228 }
229 
230 
231 static void
233 {
234  int rc;
235  // Set-up our context and sockets
236  void *ctx = zmq_ctx_new ();
237  assert (ctx);
238 
239  void *server = zmq_socket (ctx, ZMQ_STREAM);
240  assert (server);
241  int enabled = 1;
242  rc = zmq_setsockopt (server, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled));
243  assert (rc == 0);
244  rc = zmq_bind (server, "tcp://127.0.0.1:9070");
245  assert (rc == 0);
246 
247  void *client = zmq_socket (ctx, ZMQ_STREAM);
248  assert (client);
249  rc = zmq_setsockopt (client, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled));
250  assert (rc == 0);
251  rc = zmq_connect (client, "tcp://localhost:9070");
252  assert (rc == 0);
253  uint8_t id [256];
254  size_t id_size = 256;
255  uint8_t buffer [256];
256 
257  // Connecting sends a zero message
258  // Server: First frame is identity, second frame is zero
259  id_size = zmq_recv (server, id, 256, 0);
260  assert (id_size > 0);
261  rc = zmq_recv (server, buffer, 256, 0);
262  assert (rc == 0);
263  // Client: First frame is identity, second frame is zero
264  id_size = zmq_recv (client, id, 256, 0);
265  assert (id_size > 0);
266  rc = zmq_recv (client, buffer, 256, 0);
267  assert (rc == 0);
268 
269  // Sent HTTP request on client socket
270  // Get server identity
271  rc = zmq_getsockopt (client, ZMQ_IDENTITY, id, &id_size);
272  assert (rc == 0);
273  // First frame is server identity
274  rc = zmq_send (client, id, id_size, ZMQ_SNDMORE);
275  assert (rc == (int) id_size);
276  // Second frame is HTTP GET request
277  rc = zmq_send (client, "GET /\n\n", 7, 0);
278  assert (rc == 7);
279 
280  // Get HTTP request; ID frame and then request
281  id_size = zmq_recv (server, id, 256, 0);
282  assert (id_size > 0);
283  rc = zmq_recv (server, buffer, 256, 0);
284  assert (rc != -1);
285  assert (memcmp (buffer, "GET /\n\n", 7) == 0);
286 
287  // Send reply back to client
288  char http_response [] =
289  "HTTP/1.0 200 OK\r\n"
290  "Content-Type: text/plain\r\n"
291  "\r\n"
292  "Hello, World!";
293  rc = zmq_send (server, id, id_size, ZMQ_SNDMORE);
294  assert (rc != -1);
295  rc = zmq_send (server, http_response, sizeof (http_response), ZMQ_SNDMORE);
296  assert (rc != -1);
297 
298  // Send zero to close connection to client
299  rc = zmq_send (server, id, id_size, ZMQ_SNDMORE);
300  assert (rc != -1);
301  rc = zmq_send (server, NULL, 0, ZMQ_SNDMORE);
302  assert (rc != -1);
303 
304  // Get reply at client and check that it's complete
305  id_size = zmq_recv (client, id, 256, 0);
306  assert (id_size > 0);
307  rc = zmq_recv (client, buffer, 256, 0);
308  assert (rc == sizeof (http_response));
309  assert (memcmp (buffer, http_response, sizeof (http_response)) == 0);
310 
311  // // Get disconnection notification
312  // FIXME: why does this block? Bug in STREAM disconnect notification?
313  // id_size = zmq_recv (client, id, 256, 0);
314  // assert (id_size > 0);
315  // rc = zmq_recv (client, buffer, 256, 0);
316  // assert (rc == 0);
317 
318  rc = zmq_close (server);
319  assert (rc == 0);
320 
321  rc = zmq_close (client);
322  assert (rc == 0);
323 
324  rc = zmq_ctx_term (ctx);
325  assert (rc == 0);
326 }
327 
328 int main (void)
329 {
333 }
#define size
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
#define ZMQ_STREAM
Definition: zmq.h:257
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
static void test_stream_to_stream(void)
#define ZMQ_SNDMORE
Definition: zmq.h:346
#define ZMQ_DEALER
Definition: zmq.h:251
byte signature[10]
Definition: test_stream.cpp:36
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_STREAM_NOTIFY
Definition: zmq.h:324
static void test_stream_to_dealer(void)
Definition: test_stream.cpp:57
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
#define streq(s1, s2)
Definition: testutil.hpp:195
ZMQ_EXPORT int zmq_getsockopt(void *s, int option, void *optval, size_t *optvallen)
Definition: zmq.cpp:277
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
unsigned char byte
Definition: test_stream.cpp:34
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
int main(void)
static zmtp_greeting_t greeting
Definition: test_stream.cpp:49
ZMQ_EXPORT int zmq_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
ZMQ_EXPORT int zmq_msg_more(zmq_msg_t *msg)
Definition: zmq.cpp:676
Definition: zmq.h:221
ZMQ_EXPORT const char * zmq_msg_gets(zmq_msg_t *msg, const char *property)
Definition: zmq.cpp:732
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