libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_msg_ffn.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 void ffn(void *data, void *hint) {
33  // Signal that ffn has been called by writing "freed" to hint
34  (void) data; // Suppress 'unused' warnings at compile time
35  memcpy(hint, (void *) "freed", 5);
36 }
37 
38 int main (void) {
40  // Create the infrastructure
41  void *ctx = zmq_ctx_new ();
42  assert (ctx);
43 
44  void *router = zmq_socket (ctx, ZMQ_ROUTER);
45  assert (router);
46 
47  int rc = zmq_bind (router, "tcp://127.0.0.1:5555");
48  assert (rc == 0);
49 
50  void *dealer = zmq_socket (ctx, ZMQ_DEALER);
51  assert (dealer);
52 
53  rc = zmq_connect (dealer, "tcp://127.0.0.1:5555");
54  assert (rc == 0);
55 
56  // Test that creating and closing a message triggers ffn
57  zmq_msg_t msg;
58  char hint[5];
59  char data[255];
60  memset(data, 0, 255);
61  memcpy(data, (void *) "data", 4);
62  memcpy(hint, (void *) "hint", 4);
63  rc = zmq_msg_init_data(&msg, (void *)data, 255, ffn, (void*)hint);
64  assert (rc == 0);
65  rc = zmq_msg_close(&msg);
66  assert (rc == 0);
67 
69  assert (memcmp(hint, "freed", 5) == 0);
70  memcpy(hint, (void *) "hint", 4);
71 
72  // Making and closing a copy triggers ffn
73  zmq_msg_t msg2;
74  zmq_msg_init(&msg2);
75  rc = zmq_msg_init_data(&msg, (void *)data, 255, ffn, (void *)hint);
76  assert (rc == 0);
77  rc = zmq_msg_copy(&msg2, &msg);
78  assert (rc == 0);
79  rc = zmq_msg_close(&msg2);
80  assert (rc == 0);
81  rc = zmq_msg_close(&msg);
82  assert (rc == 0);
83 
85  assert (memcmp(hint, "freed", 5) == 0);
86  memcpy(hint, (void *) "hint", 4);
87 
88  // Test that sending a message triggers ffn
89  rc = zmq_msg_init_data(&msg, (void *)data, 255, ffn, (void *)hint);
90  assert (rc == 0);
91 
92  zmq_msg_send(&msg, dealer, 0);
93  char buf[255];
94  rc = zmq_recv(router, buf, 255, 0);
95  assert (rc > -1);
96  rc = zmq_recv(router, buf, 255, 0);
97  assert (rc == 255);
98  assert (memcmp(data, buf, 4) == 0);
99 
101  assert (memcmp(hint, "freed", 5) == 0);
102  memcpy(hint, (void *) "hint", 4);
103  rc = zmq_msg_close(&msg);
104  assert (rc == 0);
105 
106  // Sending a copy of a message triggers ffn
107  rc = zmq_msg_init(&msg2);
108  assert (rc == 0);
109  rc = zmq_msg_init_data(&msg, (void *)data, 255, ffn, (void *)hint);
110  assert (rc == 0);
111  rc = zmq_msg_copy(&msg2, &msg);
112  assert (rc == 0);
113 
114  zmq_msg_send(&msg, dealer, 0);
115  rc = zmq_recv(router, buf, 255, 0);
116  assert (rc > -1);
117  rc = zmq_recv(router, buf, 255, 0);
118  assert (rc == 255);
119  assert (memcmp(data, buf, 4) == 0);
120  rc = zmq_msg_close(&msg2);
121  assert (rc == 0);
122  rc = zmq_msg_close(&msg);
123  assert (rc == 0);
124 
126  assert (memcmp(hint, "freed", 5) == 0);
127  memcpy(hint, (void *) "hint", 4);
128 
129  // Deallocate the infrastructure.
130  rc = zmq_close (router);
131  assert (rc == 0);
132 
133  rc = zmq_close (dealer);
134  assert (rc == 0);
135 
136  rc = zmq_ctx_term (ctx);
137  assert (rc == 0);
138  return 0 ;
139 }
140 
void msleep(int milliseconds)
Definition: testutil.hpp:316
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_DEALER
Definition: zmq.h:251
ZMQ_EXPORT int zmq_msg_init_data(zmq_msg_t *msg, void *data, size_t size, zmq_free_fn *ffn, void *hint)
Definition: zmq.cpp:623
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT int zmq_msg_copy(zmq_msg_t *dest, zmq_msg_t *src)
Definition: zmq.cpp:661
#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_ROUTER
Definition: zmq.h:252
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
ZMQ_EXPORT int zmq_msg_send(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:629
void ffn(void *data, void *hint)
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)
Definition: zmq.h:221
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_msg_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613