libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_metadata.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 static void
34 {
35  uint8_t metadata [] = {
36  5, 'H', 'e', 'l', 'l', 'o',
37  0, 0, 0, 5, 'W', 'o', 'r', 'l', 'd'
38  };
39 
40  // Process ZAP requests forever
41  while (true) {
42  char *version = s_recv (handler);
43  if (!version)
44  break; // Terminating
45 
46  char *sequence = s_recv (handler);
47  char *domain = s_recv (handler);
48  char *address = s_recv (handler);
49  char *identity = s_recv (handler);
50  char *mechanism = s_recv (handler);
51 
52  assert (streq (version, "1.0"));
53  assert (streq (mechanism, "NULL"));
54 
55  s_sendmore (handler, version);
56  s_sendmore (handler, sequence);
57  if (streq (domain, "DOMAIN")) {
58  s_sendmore (handler, "200");
59  s_sendmore (handler, "OK");
60  s_sendmore (handler, "anonymous");
61  zmq_send (handler, metadata, sizeof (metadata), 0);
62  }
63  else {
64  s_sendmore (handler, "400");
65  s_sendmore (handler, "BAD DOMAIN");
66  s_sendmore (handler, "");
67  s_send (handler, "");
68  }
69  free (version);
70  free (sequence);
71  free (domain);
72  free (address);
73  free (identity);
74  free (mechanism);
75  }
76  close_zero_linger (handler);
77 }
78 
79 int main (void)
80 {
82  void *ctx = zmq_ctx_new ();
83  assert (ctx);
84 
85  // Spawn ZAP handler
86  // We create and bind ZAP socket in main thread to avoid case
87  // where child thread does not start up fast enough.
88  void *handler = zmq_socket (ctx, ZMQ_REP);
89  assert (handler);
90  int rc = zmq_bind (handler, "inproc://zeromq.zap.01");
91  assert (rc == 0);
92  void *zap_thread = zmq_threadstart (&zap_handler, handler);
93 
94  void *server = zmq_socket (ctx, ZMQ_DEALER);
95  assert (server);
96  void *client = zmq_socket (ctx, ZMQ_DEALER);
97  assert (client);
98  rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "DOMAIN", 6);
99  assert (rc == 0);
100  rc = zmq_bind (server, "tcp://127.0.0.1:9001");
101  assert (rc == 0);
102  rc = zmq_connect (client, "tcp://127.0.0.1:9001");
103  assert (rc == 0);
104 
105  s_send (client, "This is a message");
106  zmq_msg_t msg;
107  zmq_msg_init (&msg);
108  rc = zmq_msg_recv (&msg, server, 0);
109  assert (rc != -1);
110  assert (streq (zmq_msg_gets (&msg, "Hello"), "World"));
111  assert (streq (zmq_msg_gets (&msg, "Socket-Type"), "DEALER"));
112  assert (streq (zmq_msg_gets (&msg, "User-Id"), "anonymous"));
113  assert (streq (zmq_msg_gets (&msg, "Peer-Address"), "127.0.0.1"));
114 
115  assert (zmq_msg_gets (&msg, "No Such") == NULL);
116  assert (zmq_errno () == EINVAL);
117  zmq_msg_close (&msg);
118 
119  close_zero_linger (client);
120  close_zero_linger (server);
121 
122  // Shutdown
123  rc = zmq_ctx_term (ctx);
124  assert (rc == 0);
125 
126  // Wait until ZAP handler terminates
127  zmq_threadclose (zap_thread);
128 
129  return 0;
130 }
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
int s_sendmore(void *socket, const char *string)
Definition: testutil.hpp:190
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_DEALER
Definition: zmq.h:251
#define ZMQ_REP
Definition: zmq.h:250
void setup_test_environment(void)
Definition: testutil.hpp:285
static void zap_handler(void *handler)
ZMQ_EXPORT int zmq_errno(void)
Definition: zmq.cpp:107
void handler(int timer_id, void *arg)
Definition: test_timers.cpp:43
#define ZMQ_ZAP_DOMAIN
Definition: zmq.h:308
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
ZMQ_EXPORT void zmq_threadclose(void *thread)
Definition: zmq_utils.cpp:85
#define streq(s1, s2)
Definition: testutil.hpp:195
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
ZMQ_EXPORT int zmq_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
Definition: zmq.h:221
char * s_recv(void *socket)
Definition: testutil.hpp:170
ZMQ_EXPORT const char * zmq_msg_gets(zmq_msg_t *msg, const char *property)
Definition: zmq.cpp:732
ZMQ_EXPORT int zmq_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
void close_zero_linger(void *socket)
Definition: testutil.hpp:275
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
int s_send(void *socket, const char *string)
Definition: testutil.hpp:183
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
ZMQ_EXPORT void * zmq_threadstart(zmq_thread_fn *func, void *arg)
Definition: zmq_utils.cpp:78
const char * address
Definition: test_fork.cpp:32
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613
int main(void)