libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_disconnect_inproc.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 /// Initialize a zeromq message with a given null-terminated string
33 #define ZMQ_PREPARE_STRING(msg, data, size) \
34 zmq_msg_init(&msg) && printf("zmq_msg_init: %s\n", zmq_strerror(errno)); \
35 zmq_msg_init_size (&msg, size + 1) && printf("zmq_msg_init_size: %s\n",zmq_strerror(errno)); \
36 memcpy(zmq_msg_data(&msg), data, size + 1);
37 
38 // TODO: this code fails to meet our style guidelines, and needs rewriting
39 
40 static int publicationsReceived = 0;
41 static bool isSubscribed = false;
42 
43 int main(int, char**) {
45  void* context = zmq_ctx_new();
46  void* pubSocket;
47  void* subSocket;
48 
49  (pubSocket = zmq_socket(context, ZMQ_XPUB)) || printf("zmq_socket: %s\n", zmq_strerror(errno));
50  (subSocket = zmq_socket(context, ZMQ_SUB)) || printf("zmq_socket: %s\n", zmq_strerror(errno));
51  zmq_setsockopt(subSocket, ZMQ_SUBSCRIBE, "foo", 3) && printf("zmq_setsockopt: %s\n",zmq_strerror(errno));
52 
53  zmq_bind(pubSocket, "inproc://someInProcDescriptor") && printf("zmq_bind: %s\n", zmq_strerror(errno));
54  //zmq_bind(pubSocket, "tcp://127.0.0.1:30010") && printf("zmq_bind: %s\n", zmq_strerror(errno));
55 
56  int more;
57  size_t more_size = sizeof(more);
58  int iteration = 0;
59 
60  while (1) {
61  zmq_pollitem_t items [] = {
62  { subSocket, 0, ZMQ_POLLIN, 0 }, // read publications
63  { pubSocket, 0, ZMQ_POLLIN, 0 }, // read subscriptions
64  };
65  int rc = zmq_poll (items, 2, 100);
66 
67  if (items [1].revents & ZMQ_POLLIN) {
68  while (1) {
69  zmq_msg_t msg;
70  zmq_msg_init (&msg);
71  zmq_msg_recv (&msg, pubSocket, 0);
72  char* buffer = (char*)zmq_msg_data(&msg);
73 
74  if (buffer[0] == 0) {
75  assert(isSubscribed);
76  isSubscribed = false;
77  }
78  else {
79  assert(!isSubscribed);
80  isSubscribed = true;
81  }
82 
83  zmq_getsockopt (pubSocket, ZMQ_RCVMORE, &more, &more_size);
84  zmq_msg_close (&msg);
85 
86  if (!more)
87  break; // Last message part
88  }
89  }
90 
91  if (items[0].revents & ZMQ_POLLIN) {
92  while (1) {
93  zmq_msg_t msg;
94  zmq_msg_init (&msg);
95  zmq_msg_recv (&msg, subSocket, 0);
96  zmq_getsockopt (subSocket, ZMQ_RCVMORE, &more, &more_size);
97  zmq_msg_close (&msg);
98 
99  if (!more) {
100  publicationsReceived++;
101  break; // Last message part
102  }
103  }
104  }
105  if (iteration == 1) {
106  zmq_connect(subSocket, "inproc://someInProcDescriptor") && printf("zmq_connect: %s\n", zmq_strerror(errno));
108  }
109  if (iteration == 4) {
110  zmq_disconnect(subSocket, "inproc://someInProcDescriptor") && printf("zmq_disconnect(%d): %s\n", errno, zmq_strerror(errno));
111  }
112  if (iteration > 4 && rc == 0)
113  break;
114 
115  zmq_msg_t channelEnvlp;
116  ZMQ_PREPARE_STRING(channelEnvlp, "foo", 3);
117  zmq_msg_send (&channelEnvlp, pubSocket, ZMQ_SNDMORE) >= 0 || printf("zmq_msg_send: %s\n",zmq_strerror(errno));
118  zmq_msg_close(&channelEnvlp) && printf("zmq_msg_close: %s\n",zmq_strerror(errno));
119 
120  zmq_msg_t message;
121  ZMQ_PREPARE_STRING(message, "this is foo!", 12);
122  zmq_msg_send (&message, pubSocket, 0) >= 0 || printf("zmq_msg_send: %s\n",zmq_strerror(errno));
123  zmq_msg_close(&message) && printf("zmq_msg_close: %s\n",zmq_strerror(errno));
124 
125  iteration++;
126  }
127  assert(publicationsReceived == 3);
128  assert(!isSubscribed);
129 
130  zmq_close(pubSocket) && printf("zmq_close: %s", zmq_strerror(errno));
131  zmq_close(subSocket) && printf("zmq_close: %s", zmq_strerror(errno));
132 
133  zmq_ctx_term(context);
134  return 0;
135 }
136 
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
int main(int, char **)
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
#define ZMQ_SUB
Definition: zmq.h:248
#define ZMQ_XPUB
Definition: zmq.h:255
#define ZMQ_SUBSCRIBE
Definition: zmq.h:266
void setup_test_environment(void)
Definition: testutil.hpp:285
#define SETTLE_TIME
Definition: testutil.hpp:44
static bool isSubscribed
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_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
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
ZMQ_EXPORT const char * zmq_strerror(int errnum)
Definition: zmq.cpp:102
Definition: zmq.h:221
#define ZMQ_RCVMORE
Definition: zmq.h:272
ZMQ_EXPORT int zmq_poll(zmq_pollitem_t *items, int nitems, long timeout)
Definition: zmq.cpp:748
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
#define ZMQ_PREPARE_STRING(msg, data, size)
Initialize a zeromq message with a given null-terminated string.
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
#define ZMQ_POLLIN
Definition: zmq.h:410
ZMQ_EXPORT int zmq_disconnect(void *s, const char *addr)
Definition: zmq.cpp:353
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613
static int publicationsReceived