libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_sub_forward_tipc.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 {
34  fprintf (stderr, "test_sub_forward running...\n");
35 
36  void *ctx = zmq_init (1);
37  assert (ctx);
38 
39  // First, create an intermediate device.
40  void *xpub = zmq_socket (ctx, ZMQ_XPUB);
41  assert (xpub);
42  int rc = zmq_bind (xpub, "tipc://{5560,0,0}");
43  assert (rc == 0);
44  void *xsub = zmq_socket (ctx, ZMQ_XSUB);
45  assert (xsub);
46  rc = zmq_bind (xsub, "tipc://{5561,0,0}");
47  assert (rc == 0);
48 
49  // Create a publisher.
50  void *pub = zmq_socket (ctx, ZMQ_PUB);
51  assert (pub);
52  rc = zmq_connect (pub, "tipc://{5561,0}@0.0.0");
53  assert (rc == 0);
54 
55  // Create a subscriber.
56  void *sub = zmq_socket (ctx, ZMQ_SUB);
57  assert (sub);
58  rc = zmq_connect (sub, "tipc://{5560,0}@0.0.0");
59  assert (rc == 0);
60 
61  // Subscribe for all messages.
62  rc = zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "", 0);
63  assert (rc == 0);
64 
65  // Pass the subscription upstream through the device.
66  char buff [32];
67  rc = zmq_recv (xpub, buff, sizeof (buff), 0);
68  assert (rc >= 0);
69  rc = zmq_send (xsub, buff, rc, 0);
70  assert (rc >= 0);
71 
72  // Wait a bit till the subscription gets to the publisher.
74 
75  // Send an empty message.
76  rc = zmq_send (pub, NULL, 0, 0);
77  assert (rc == 0);
78 
79  // Pass the message downstream through the device.
80  rc = zmq_recv (xsub, buff, sizeof (buff), 0);
81  assert (rc >= 0);
82  rc = zmq_send (xpub, buff, rc, 0);
83  assert (rc >= 0);
84 
85  // Receive the message in the subscriber.
86  rc = zmq_recv (sub, buff, sizeof (buff), 0);
87  assert (rc == 0);
88 
89  // Clean up.
90  rc = zmq_close (xpub);
91  assert (rc == 0);
92  rc = zmq_close (xsub);
93  assert (rc == 0);
94  rc = zmq_close (pub);
95  assert (rc == 0);
96  rc = zmq_close (sub);
97  assert (rc == 0);
98  rc = zmq_ctx_term (ctx);
99  assert (rc == 0);
100 
101  return 0 ;
102 }
int main(void)
void msleep(int milliseconds)
Definition: testutil.hpp:316
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
#define ZMQ_SUB
Definition: zmq.h:248
#define ZMQ_XPUB
Definition: zmq.h:255
#define ZMQ_SUBSCRIBE
Definition: zmq.h:266
#define ZMQ_XSUB
Definition: zmq.h:256
ZMQ_EXPORT void * zmq_init(int io_threads)
Definition: zmq.cpp:220
#define SETTLE_TIME
Definition: testutil.hpp:44
ZMQ_EXPORT int zmq_recv(void *s, void *buf, size_t len, int flags)
Definition: zmq.cpp:507
#define ZMQ_PUB
Definition: zmq.h:247
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
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_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