libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_term_endpoint.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 <stdio.h>
31 #include "testutil.hpp"
32 
33 /* Use the worst case filename size for the buffer (+1 for trailing NUL) */
34 #define BUF_SIZE (FILENAME_MAX+1)
35 
36 int main (void)
37 {
39  int rc;
40  char buf[BUF_SIZE];
41  size_t buf_size;
42  const char *ep = "tcp://127.0.0.1:5560";
43  const char *ep_wc_tcp = "tcp://127.0.0.1:*";
44 #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
45  const char *ep_wc_ipc = "ipc://*";
46 #endif
47 #if defined ZMQ_HAVE_VMCI
48  const char *ep_wc_vmci = "vmci://*:*";
49 #endif
50 
51  // Create infrastructure.
52  void *ctx = zmq_ctx_new ();
53  assert (ctx);
54  void *push = zmq_socket (ctx, ZMQ_PUSH);
55  assert (push);
56  rc = zmq_bind (push, ep);
57  assert (rc == 0);
58  void *pull = zmq_socket (ctx, ZMQ_PULL);
59  assert (pull);
60  rc = zmq_connect (pull, ep);
61  assert (rc == 0);
62 
63  // Pass one message through to ensure the connection is established
64  rc = zmq_send (push, "ABC", 3, 0);
65  assert (rc == 3);
66  rc = zmq_recv (pull, buf, sizeof (buf), 0);
67  assert (rc == 3);
68 
69  // Unbind the listening endpoint
70  rc = zmq_unbind (push, ep);
71  assert (rc == 0);
72 
73  // Allow unbind to settle
75 
76  // Check that sending would block (there's no outbound connection)
77  rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
78  assert (rc == -1 && zmq_errno () == EAGAIN);
79 
80  // Clean up
81  rc = zmq_close (pull);
82  assert (rc == 0);
83  rc = zmq_close (push);
84  assert (rc == 0);
85  rc = zmq_ctx_term (ctx);
86  assert (rc == 0);
87 
88  // Create infrastructure
89  ctx = zmq_ctx_new ();
90  assert (ctx);
91  push = zmq_socket (ctx, ZMQ_PUSH);
92  assert (push);
93  rc = zmq_connect (push, ep);
94  assert (rc == 0);
95  pull = zmq_socket (ctx, ZMQ_PULL);
96  assert (pull);
97  rc = zmq_bind (pull, ep);
98  assert (rc == 0);
99 
100  // Pass one message through to ensure the connection is established.
101  rc = zmq_send (push, "ABC", 3, 0);
102  assert (rc == 3);
103  rc = zmq_recv (pull, buf, sizeof (buf), 0);
104  assert (rc == 3);
105 
106  // Disconnect the bound endpoint
107  rc = zmq_disconnect (push, ep);
108  assert (rc == 0);
109 
110  // Allow disconnect to settle
112 
113  // Check that sending would block (there's no inbound connections).
114  rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
115  assert (rc == -1 && zmq_errno () == EAGAIN);
116 
117  // Clean up.
118  rc = zmq_close (pull);
119  assert (rc == 0);
120  rc = zmq_close (push);
121  assert (rc == 0);
122  rc = zmq_ctx_term (ctx);
123  assert (rc == 0);
124 
125  // Create infrastructure (wild-card binding)
126  ctx = zmq_ctx_new ();
127  assert (ctx);
128  push = zmq_socket (ctx, ZMQ_PUSH);
129  assert (push);
130  rc = zmq_bind (push, ep_wc_tcp);
131  assert (rc == 0);
132  pull = zmq_socket(ctx, ZMQ_PULL);
133  assert(pull);
134 #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
135  rc = zmq_bind (pull, ep_wc_ipc);
136  assert (rc == 0);
137 #endif
138 #if defined ZMQ_HAVE_VMCI
139  void *req = zmq_socket (ctx, ZMQ_REQ);
140  assert (req);
141  rc = zmq_bind (req, ep_wc_vmci);
142  assert (rc == 0);
143 #endif
144 
145  // Unbind sockets binded by wild-card address
146  buf_size = sizeof(buf);
147  rc = zmq_getsockopt (push, ZMQ_LAST_ENDPOINT, buf, &buf_size);
148  assert (rc == 0);
149  rc = zmq_unbind (push, buf);
150  assert (rc == 0);
151 #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
152  buf_size = sizeof(buf);
153  rc = zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, buf, &buf_size);
154  assert (rc == 0);
155  rc = zmq_unbind (pull, buf);
156  assert (rc == 0);
157 #endif
158 #if defined ZMQ_HAVE_VMCI
159  buf_size = sizeof(buf);
160  rc = zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, buf, &buf_size);
161  assert (rc == 0);
162  rc = zmq_unbind(req, buf);
163  assert (rc == 0);
164 #endif
165 
166  // Clean up.
167  rc = zmq_close (pull);
168  assert (rc == 0);
169  rc = zmq_close (push);
170  assert (rc == 0);
171  rc = zmq_ctx_term (ctx);
172  assert (rc == 0);
173 
174  // Create infrastructure (wild-card binding)
175  ctx = zmq_ctx_new ();
176  assert (ctx);
177  push = zmq_socket (ctx, ZMQ_PUSH);
178  assert (push);
179  rc = zmq_bind (push, ep_wc_tcp);
180  assert (rc == 0);
181  pull = zmq_socket(ctx, ZMQ_PULL);
182  assert(pull);
183 #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
184  rc = zmq_bind (pull, ep_wc_ipc);
185  assert (rc == 0);
186 #endif
187 #if defined ZMQ_HAVE_VMCI
188  req = zmq_socket (ctx, ZMQ_REQ);
189  assert (req);
190  rc = zmq_bind (req, ep_wc_vmci);
191  assert (rc == 0);
192 #endif
193 
194  // Sockets binded by wild-card address can't be unbinded by wild-card address
195  rc = zmq_unbind (push, ep_wc_tcp);
196  assert (rc == -1 && zmq_errno () == ENOENT);
197 #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
198  rc = zmq_unbind (pull, ep_wc_ipc);
199  assert (rc == -1 && zmq_errno () == ENOENT);
200 #endif
201 #if defined ZMQ_HAVE_VMCI
202  rc = zmq_unbind (req, ep_wc_vmci);
203  assert (rc == -1 && zmq_errno () == ENOENT);
204 #endif
205 
206  // Clean up.
207  rc = zmq_close (pull);
208  assert (rc == 0);
209  rc = zmq_close (push);
210  assert (rc == 0);
211  rc = zmq_ctx_term (ctx);
212  assert (rc == 0);
213 
214  return 0;
215 }
void msleep(int milliseconds)
Definition: testutil.hpp:316
ZMQ_EXPORT int zmq_unbind(void *s, const char *addr)
Definition: zmq.cpp:343
#define ZMQ_LAST_ENDPOINT
Definition: zmq.h:286
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
void setup_test_environment(void)
Definition: testutil.hpp:285
#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
ZMQ_EXPORT int zmq_errno(void)
Definition: zmq.cpp:107
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
#define ZMQ_PUSH
Definition: zmq.h:254
ZMQ_EXPORT int zmq_getsockopt(void *s, int option, void *optval, size_t *optvallen)
Definition: zmq.cpp:277
#define ZMQ_REQ
Definition: zmq.h:249
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
int main(void)
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
ZMQ_EXPORT int zmq_disconnect(void *s, const char *addr)
Definition: zmq.cpp:353
#define BUF_SIZE
#define ZMQ_DONTWAIT
Definition: zmq.h:345
#define ZMQ_PULL
Definition: zmq.h:253