libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_pub_invert_matching.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 {
35  void *ctx = zmq_ctx_new ();
36  assert (ctx);
37 
38  // Create a publisher
39  void *pub = zmq_socket (ctx, ZMQ_PUB);
40  assert (pub);
41  int rc = zmq_bind (pub, "inproc://soname");
42  assert (rc == 0);
43 
44  // Create two subscribers
45  void *sub1 = zmq_socket (ctx, ZMQ_SUB);
46  assert (sub1);
47  rc = zmq_connect (sub1, "inproc://soname");
48  assert (rc == 0);
49 
50  void *sub2 = zmq_socket (ctx, ZMQ_SUB);
51  assert (sub2);
52  rc = zmq_connect (sub2, "inproc://soname");
53  assert (rc == 0);
54 
55  // Subscribe pub1 to one prefix
56  // and pub2 to another prefix.
57  const char PREFIX1[] = "prefix1";
58  const char PREFIX2[] = "p2";
59 
60  rc = zmq_setsockopt (sub1, ZMQ_SUBSCRIBE, PREFIX1, sizeof(PREFIX1));
61  assert (rc == 0);
62 
63  rc = zmq_setsockopt (sub2, ZMQ_SUBSCRIBE, PREFIX2, sizeof(PREFIX2));
64  assert (rc == 0);
65 
66  // Send a message with the first prefix
67  rc = zmq_send_const(pub, PREFIX1, sizeof(PREFIX1), 0);
68  assert (rc == sizeof(PREFIX1));
69 
70  // sub1 should receive it, but not sub2
71  rc = zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT);
72  assert (rc == sizeof(PREFIX1));
73 
74  rc = zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT);
75  assert (rc == -1);
76  assert (errno == EAGAIN);
77 
78  // Send a message with the second prefix
79  rc = zmq_send_const(pub, PREFIX2, sizeof(PREFIX2), 0);
80  assert (rc == sizeof(PREFIX2));
81 
82  // sub2 should receive it, but not sub1
83  rc = zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT);
84  assert (rc == sizeof(PREFIX2));
85 
86  rc = zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT);
87  assert (rc == -1);
88  assert (errno == EAGAIN);
89 
90  // Now invert the matching
91  int invert = 1;
92  rc = zmq_setsockopt (pub, ZMQ_INVERT_MATCHING, &invert, sizeof(invert));
93  assert (rc == 0);
94 
95  // ... on both sides, otherwise the SUB socket will filter the messages out
96  rc = zmq_setsockopt (sub1, ZMQ_INVERT_MATCHING, &invert, sizeof(invert));
97  rc = zmq_setsockopt (sub2, ZMQ_INVERT_MATCHING, &invert, sizeof(invert));
98  assert (rc == 0);
99 
100  // Send a message with the first prefix
101  rc = zmq_send_const(pub, PREFIX1, sizeof(PREFIX1), 0);
102  assert (rc == sizeof(PREFIX1));
103 
104  // sub2 should receive it, but not sub1
105  rc = zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT);
106  assert (rc == sizeof(PREFIX1));
107 
108  rc = zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT);
109  assert (rc == -1);
110  assert (errno == EAGAIN);
111 
112  // Send a message with the second prefix
113  rc = zmq_send_const(pub, PREFIX2, sizeof(PREFIX2), 0);
114  assert (rc == sizeof(PREFIX2));
115 
116  // sub1 should receive it, but not sub2
117  rc = zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT);
118  assert (rc == sizeof(PREFIX2));
119 
120  rc = zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT);
121  assert (rc == -1);
122  assert (errno == EAGAIN);
123 
124 
125  // Clean up.
126  rc = zmq_close (pub);
127  assert (rc == 0);
128  rc = zmq_close (sub1);
129  assert (rc == 0);
130  rc = zmq_close (sub2);
131  assert (rc == 0);
132  rc = zmq_ctx_term (ctx);
133  assert (rc == 0);
134 
135  return 0 ;
136 }
int main(void)
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SUB
Definition: zmq.h:248
#define ZMQ_SUBSCRIBE
Definition: zmq.h:266
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_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
#define ZMQ_INVERT_MATCHING
Definition: zmq.h:325
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
#define ZMQ_DONTWAIT
Definition: zmq.h:345