libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_msg_flags.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  // Create the infrastructure
36  void *ctx = zmq_ctx_new ();
37  assert (ctx);
38 
39  void *sb = zmq_socket (ctx, ZMQ_ROUTER);
40  assert (sb);
41 
42  int rc = zmq_bind (sb, "inproc://a");
43  assert (rc == 0);
44 
45  void *sc = zmq_socket (ctx, ZMQ_DEALER);
46  assert (sc);
47 
48  rc = zmq_connect (sc, "inproc://a");
49  assert (rc == 0);
50 
51  // Send 2-part message.
52  rc = zmq_send (sc, "A", 1, ZMQ_SNDMORE);
53  assert (rc == 1);
54  rc = zmq_send (sc, "B", 1, 0);
55  assert (rc == 1);
56 
57  // Identity comes first.
58  zmq_msg_t msg;
59  rc = zmq_msg_init (&msg);
60  assert (rc == 0);
61  rc = zmq_msg_recv (&msg, sb, 0);
62  assert (rc >= 0);
63  int more = zmq_msg_more (&msg);
64  assert (more == 1);
65 
66  // Then the first part of the message body.
67  rc = zmq_msg_recv (&msg, sb, 0);
68  assert (rc == 1);
69  more = zmq_msg_more (&msg);
70  assert (more == 1);
71 
72  // And finally, the second part of the message body.
73  rc = zmq_msg_recv (&msg, sb, 0);
74  assert (rc == 1);
75  more = zmq_msg_more (&msg);
76  assert (more == 0);
77 
78  // Test ZMQ_SHARED property (case 1, refcounted messages)
79  zmq_msg_t msg_a;
80  rc = zmq_msg_init_size(&msg_a, 1024); // large enough to be a type_lmsg
81  assert (rc == 0);
82 
83  // Message is not shared
84  rc = zmq_msg_get(&msg_a, ZMQ_SHARED);
85  assert (rc == 0);
86 
87  zmq_msg_t msg_b;
88  rc = zmq_msg_init(&msg_b);
89  assert (rc == 0);
90 
91  rc = zmq_msg_copy(&msg_b, &msg_a);
92  assert (rc == 0);
93 
94  // Message is now shared
95  rc = zmq_msg_get(&msg_b, ZMQ_SHARED);
96  assert (rc == 1);
97 
98  // cleanup
99  rc = zmq_msg_close(&msg_a);
100  assert (rc == 0);
101  rc = zmq_msg_close(&msg_b);
102  assert (rc == 0);
103 
104  // Test ZMQ_SHARED property (case 2, constant data messages)
105  rc = zmq_msg_init_data(&msg_a, (void*) "TEST", 5, 0, 0);
106  assert (rc == 0);
107 
108  // Message reports as shared
109  rc = zmq_msg_get(&msg_a, ZMQ_SHARED);
110  assert (rc == 1);
111 
112  // cleanup
113  rc = zmq_msg_close(&msg_a);
114  assert (rc == 0);
115 
116  // Deallocate the infrastructure.
117  rc = zmq_close (sc);
118  assert (rc == 0);
119 
120  rc = zmq_close (sb);
121  assert (rc == 0);
122 
123  rc = zmq_ctx_term (ctx);
124  assert (rc == 0);
125  return 0 ;
126 }
127 
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
#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 ZMQ_ROUTER
Definition: zmq.h:252
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
#define ZMQ_SHARED
Definition: zmq.h:342
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_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
ZMQ_EXPORT int zmq_msg_more(zmq_msg_t *msg)
Definition: zmq.cpp:676
Definition: zmq.h:221
ZMQ_EXPORT int zmq_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
ZMQ_EXPORT int zmq_msg_get(zmq_msg_t *msg, int property)
Definition: zmq.cpp:681
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_size(zmq_msg_t *msg, size_t size)
Definition: zmq.cpp:618
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613