libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_term_endpoint_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  int rc;
35  char buf[32];
36  const char *ep = "tipc://{5560,0,0}";
37  const char *name = "tipc://{5560,0}@0.0.0";
38 
39  fprintf (stderr, "unbind endpoint test running...\n");
40 
41  // Create infrastructure.
42  void *ctx = zmq_init (1);
43  assert (ctx);
44  void *push = zmq_socket (ctx, ZMQ_PUSH);
45  assert (push);
46  rc = zmq_bind (push, ep);
47  assert (rc == 0);
48  void *pull = zmq_socket (ctx, ZMQ_PULL);
49  assert (pull);
50  rc = zmq_connect (pull, name);
51  assert (rc == 0);
52 
53  // Pass one message through to ensure the connection is established.
54  rc = zmq_send (push, "ABC", 3, 0);
55  assert (rc == 3);
56  rc = zmq_recv (pull, buf, sizeof (buf), 0);
57  assert (rc == 3);
58 
59  // Unbind the lisnening endpoint
60  rc = zmq_unbind (push, ep);
61  assert (rc == 0);
62 
63  // Let events some time
65 
66  // Check that sending would block (there's no outbound connection).
67  rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
68  assert (rc == -1 && zmq_errno () == EAGAIN);
69 
70  // Clean up.
71  rc = zmq_close (pull);
72  assert (rc == 0);
73  rc = zmq_close (push);
74  assert (rc == 0);
75  rc = zmq_ctx_term (ctx);
76  assert (rc == 0);
77 
78 
79  // Now the other way round.
80  fprintf (stderr, "disconnect endpoint test running...\n");
81 
82 
83  // Create infrastructure.
84  ctx = zmq_init (1);
85  assert (ctx);
86  push = zmq_socket (ctx, ZMQ_PUSH);
87  assert (push);
88  rc = zmq_connect (push, name);
89  assert (rc == 0);
90  pull = zmq_socket (ctx, ZMQ_PULL);
91  assert (pull);
92  rc = zmq_bind (pull, ep);
93  assert (rc == 0);
94 
95  // Pass one message through to ensure the connection is established.
96  rc = zmq_send (push, "ABC", 3, 0);
97  assert (rc == 3);
98  rc = zmq_recv (pull, buf, sizeof (buf), 0);
99  assert (rc == 3);
100 
101  // Disconnect the bound endpoint
102  rc = zmq_disconnect (push, name);
103  assert (rc == 0);
104 
106 
107  // Check that sending would block (there's no inbound connections).
108  rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
109  assert (rc == -1 && zmq_errno () == EAGAIN);
110 
111  // Clean up.
112  rc = zmq_close (pull);
113  assert (rc == 0);
114  rc = zmq_close (push);
115  assert (rc == 0);
116  rc = zmq_ctx_term (ctx);
117  assert (rc == 0);
118 
119  return 0;
120 }
void msleep(int milliseconds)
Definition: testutil.hpp:316
ZMQ_EXPORT int zmq_unbind(void *s, const char *addr)
Definition: zmq.cpp:343
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
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
int main(void)
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
ZMQ_EXPORT int zmq_disconnect(void *s, const char *addr)
Definition: zmq.cpp:353
#define ZMQ_DONTWAIT
Definition: zmq.h:345
#define ZMQ_PULL
Definition: zmq.h:253