libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_radio_dish.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 msg_send (zmq_msg_t *msg_, void *s_, const char* group_, const char* body_)
33 {
34  int rc = zmq_msg_init_size (msg_, strlen (body_));
35  if (rc != 0)
36  return rc;
37 
38  memcpy (zmq_msg_data (msg_), body_, strlen (body_));
39 
40  rc = zmq_msg_set_group (msg_, group_);
41  if (rc != 0) {
42  zmq_msg_close (msg_);
43  return rc;
44  }
45 
46  rc = zmq_msg_send (msg_, s_, 0);
47 
48  zmq_msg_close (msg_);
49 
50  return rc;
51 }
52 
53 int msg_recv_cmp (zmq_msg_t *msg_, void *s_, const char* group_, const char* body_)
54 {
55  int rc = zmq_msg_init (msg_);
56  if (rc != 0)
57  return -1;
58 
59  int recv_rc = zmq_msg_recv (msg_, s_, 0);
60  if (recv_rc == -1)
61  return -1;
62 
63  if (strcmp (zmq_msg_group (msg_), group_) != 0)
64  {
65  zmq_msg_close (msg_);
66  return -1;
67  }
68 
69  char * body = (char*) malloc (sizeof(char) * (zmq_msg_size (msg_) + 1));
70  memcpy (body, zmq_msg_data (msg_), zmq_msg_size (msg_));
71  body [zmq_msg_size (msg_)] = '\0';
72 
73  if (strcmp (body, body_) != 0)
74  {
75  zmq_msg_close (msg_);
76  return -1;
77  }
78 
79  zmq_msg_close (msg_);
80  free(body);
81  return recv_rc;
82 }
83 
84 int main (void)
85 {
87  void *ctx = zmq_ctx_new ();
88  assert (ctx);
89 
90  void *radio = zmq_socket (ctx, ZMQ_RADIO);
91  void *dish = zmq_socket (ctx, ZMQ_DISH);
92 
93  int rc = zmq_bind (radio, "tcp://127.0.0.1:5556");
94  assert (rc == 0);
95 
96  // Leaving a group which we didn't join
97  rc = zmq_leave (dish, "Movies");
98  assert (rc == -1);
99 
100  // Joining too long group
101  char too_long_group[ZMQ_GROUP_MAX_LENGTH + 2];
102  for (int index = 0; index < ZMQ_GROUP_MAX_LENGTH + 2; index++)
103  too_long_group[index] = 'A';
104  too_long_group[ZMQ_GROUP_MAX_LENGTH + 1] = '\0';
105  rc = zmq_join (dish, too_long_group);
106  assert (rc == -1);
107 
108  // Joining
109  rc = zmq_join (dish, "Movies");
110  assert (rc == 0);
111 
112  // Duplicate Joining
113  rc = zmq_join (dish, "Movies");
114  assert (rc == -1);
115 
116  // Connecting
117  rc = zmq_connect (dish, "tcp://127.0.0.1:5556");
118  assert (rc == 0);
119 
121 
122  zmq_msg_t msg;
123 
124  // This is not going to be sent as dish only subscribe to "Movies"
125  rc = msg_send (&msg, radio, "TV", "Friends");
126  assert (rc == 7);
127 
128  // This is going to be sent to the dish
129  rc = msg_send (&msg, radio, "Movies", "Godfather");
130  assert (rc == 9);
131 
132  // Check the correct message arrived
133  rc = msg_recv_cmp (&msg, dish, "Movies", "Godfather");
134  assert (rc == 9);
135 
136  // Join group during connection optvallen
137  rc = zmq_join (dish, "TV");
138  assert (rc == 0);
139 
140  zmq_sleep (1);
141 
142  // This should arrive now as we joined the group
143  rc = msg_send (&msg, radio, "TV", "Friends");
144  assert (rc == 7);
145 
146  // Check the correct message arrived
147  rc = msg_recv_cmp (&msg, dish, "TV", "Friends");
148  assert (rc == 7);
149 
150  // Leaving groupr
151  rc = zmq_leave (dish, "TV");
152  assert (rc == 0);
153 
154  zmq_sleep (1);
155 
156  // This is not going to be sent as dish only subscribe to "Movies"
157  rc = msg_send (&msg, radio, "TV", "Friends");
158  assert (rc == 7);
159 
160  // This is going to be sent to the dish
161  rc = msg_send (&msg, radio, "Movies", "Godfather");
162  assert (rc == 9);
163 
164  // Check the correct message arrived
165  rc = msg_recv_cmp (&msg, dish, "Movies", "Godfather");
166  assert (rc == 9);
167 
168  rc = zmq_close (dish);
169  assert (rc == 0);
170 
171  rc = zmq_close (radio);
172  assert (rc == 0);
173 
174  rc = zmq_ctx_term (ctx);
175  assert (rc == 0);
176 
177  return 0 ;
178 }
int msg_send(zmq_msg_t *msg_, void *s_, const char *group_, const char *body_)
void msleep(int milliseconds)
Definition: testutil.hpp:316
#define ZMQ_DISH
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_RADIO
void setup_test_environment(void)
Definition: testutil.hpp:285
#define SETTLE_TIME
Definition: testutil.hpp:44
int msg_recv_cmp(zmq_msg_t *msg_, void *s_, const char *group_, const char *body_)
#define ZMQ_GROUP_MAX_LENGTH
Definition: zmq.h:355
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_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
int zmq_join(void *s, const char *group)
Definition: zmq.cpp:299
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
ZMQ_EXPORT void zmq_sleep(int seconds_)
Definition: zmq_utils.cpp:53
const char * zmq_msg_group(zmq_msg_t *msg)
Definition: zmq.cpp:725
int zmq_leave(void *s, const char *group)
Definition: zmq.cpp:310
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_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
ZMQ_EXPORT int zmq_msg_init_size(zmq_msg_t *msg, size_t size)
Definition: zmq.cpp:618
ZMQ_EXPORT size_t zmq_msg_size(zmq_msg_t *msg)
Definition: zmq.cpp:671
int zmq_msg_set_group(zmq_msg_t *msg, const char *group)
Definition: zmq.cpp:720
int main(void)
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613