libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_udp.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  zmq_msg_close(msg_);
62  return -1;
63  }
64 
65  if (strcmp (zmq_msg_group (msg_), group_) != 0)
66  {
67  zmq_msg_close (msg_);
68  return -1;
69  }
70 
71  char * body = (char*) malloc (sizeof(char) * (zmq_msg_size (msg_) + 1));
72  memcpy (body, zmq_msg_data (msg_), zmq_msg_size (msg_));
73  body [zmq_msg_size (msg_)] = '\0';
74 
75  if (strcmp (body, body_) != 0)
76  {
77  zmq_msg_close (msg_);
78  free(body);
79  return -1;
80  }
81 
82  zmq_msg_close (msg_);
83  free (body);
84  return recv_rc;
85 }
86 
87 int main (void)
88 {
90  void *ctx = zmq_ctx_new ();
91  assert (ctx);
92 
93  zmq_msg_t msg;
94 
95  void *radio = zmq_socket (ctx, ZMQ_RADIO);
96  void *dish = zmq_socket (ctx, ZMQ_DISH);
97 
98  int rc = zmq_bind (dish, "udp://*:5556");
99  assert (rc == 0);
100 
101  rc = zmq_connect (radio, "udp://127.0.0.1:5556");
102  assert (rc == 0);
103 
105 
106  rc = zmq_join (dish, "TV");
107  assert (rc == 0);
108 
109  rc = msg_send (&msg, radio, "TV", "Friends");
110  assert (rc != -1);
111 
112  rc = msg_recv_cmp (&msg, dish, "TV", "Friends");
113  assert (rc != -1);
114 
115  rc = zmq_close (dish);
116  assert (rc == 0);
117 
118  rc = zmq_close (radio);
119  assert (rc == 0);
120 
121  rc = zmq_ctx_term (ctx);
122  assert (rc == 0);
123 
124  return 0 ;
125 }
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
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
int msg_send(zmq_msg_t *msg_, void *s_, const char *group_, const char *body_)
Definition: test_udp.cpp:32
ZMQ_EXPORT int zmq_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
Definition: zmq.h:221
const char * zmq_msg_group(zmq_msg_t *msg)
Definition: zmq.cpp:725
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
int main(void)
Definition: test_udp.cpp:87
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 msg_recv_cmp(zmq_msg_t *msg_, void *s_, const char *group_, const char *body_)
Definition: test_udp.cpp:53
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613