2 This file is part of GNUnet.
3 Copyright (C) 2012, 2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file util/test_mq.c
22 * @author Florian Dold
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
28 #define NUM_TRANSMISSIONS 500
31 * How long does the receiver take per message?
33 #define RECEIVER_THROTTLE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1)
35 static unsigned int received_cnt;
38 GNUNET_NETWORK_STRUCT_BEGIN
42 struct GNUNET_MessageHeader header;
43 uint32_t x GNUNET_PACKED;
46 GNUNET_NETWORK_STRUCT_END
48 static int global_ret;
50 static struct GNUNET_SCHEDULER_Task *tt;
52 static struct GNUNET_SCHEDULER_Task *dt;
54 static struct GNUNET_MQ_Handle *cmq;
58 do_shutdown (void *cls)
63 GNUNET_SCHEDULER_cancel (tt);
68 GNUNET_MQ_destroy (cmq);
75 do_timeout (void *cls)
79 GNUNET_SCHEDULER_shutdown ();
85 * Generic error handler, called with the appropriate
86 * error code and the same closure specified at the creation of
88 * Not every message queue implementation supports an error handler.
91 * @param error error code
95 enum GNUNET_MQ_Error error)
99 GNUNET_SCHEDULER_shutdown ();
104 client_continue (void *cls)
106 struct GNUNET_SERVICE_Client *c = cls;
109 GNUNET_SERVICE_client_continue (c);
114 handle_dummy (void *cls,
115 const struct MyMessage *msg)
117 struct GNUNET_SERVICE_Client *c = cls;
119 GNUNET_assert (NULL == dt);
120 /* artificially make receiver slower than sender */
121 dt = GNUNET_SCHEDULER_add_delayed (RECEIVER_THROTTLE,
124 if (received_cnt != ntohl (msg->x))
128 GNUNET_SCHEDULER_shutdown ();
135 handle_dummy2 (void *cls,
136 const struct MyMessage *msg)
138 struct GNUNET_SERVICE_Client *c = cls;
140 GNUNET_SERVICE_client_continue (c);
141 if (NUM_TRANSMISSIONS != received_cnt)
146 GNUNET_SCHEDULER_shutdown ();
151 * Function called whenever MQ has sent a message.
154 notify_sent_cb (void *cls)
156 static unsigned int seen;
157 unsigned int *cnt = cls;
163 GNUNET_SCHEDULER_shutdown ();
171 * Start running the actual test.
173 * @param cls closure passed to #GNUNET_SERVICE_MAIN
174 * @param cfg configuration to use for this service
175 * @param sh handle to the newly create service
179 const struct GNUNET_CONFIGURATION_Handle *cfg,
180 struct GNUNET_SERVICE_Handle *sh)
182 struct GNUNET_MQ_MessageHandler ch[] = {
183 GNUNET_MQ_handler_end ()
185 struct GNUNET_MQ_Envelope *env;
190 cmq = GNUNET_CLIENT_connect (cfg,
195 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
197 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
200 for (unsigned int i=0;i<NUM_TRANSMISSIONS;i++)
204 cnt = GNUNET_new (unsigned int);
206 env = GNUNET_MQ_msg (m,
207 GNUNET_MESSAGE_TYPE_DUMMY);
208 GNUNET_MQ_notify_sent (env,
215 env = GNUNET_MQ_msg (m,
216 GNUNET_MESSAGE_TYPE_DUMMY2);
223 * Callback to be called when a client connects to the service.
225 * @param cls closure for the service
226 * @param c the new client that connected to the service
227 * @param mq the message queue used to send messages to the client
228 * @return the client-specific (`internal') closure
231 connect_cb (void *cls,
232 struct GNUNET_SERVICE_Client *c,
233 struct GNUNET_MQ_Handle *mq)
242 * Callback to be called when a client disconnected from the service
244 * @param cls closure for the service
245 * @param c the client that disconnected
246 * @param internal_cls the client-specific (`internal') closure
249 disconnect_cb (void *cls,
250 struct GNUNET_SERVICE_Client *c,
262 struct GNUNET_MQ_Envelope *mqm;
263 struct MyMessage *mm;
268 mqm = GNUNET_MQ_msg (mm,
269 GNUNET_MESSAGE_TYPE_DUMMY);
270 GNUNET_assert (NULL != mqm);
271 GNUNET_assert (NULL != mm);
272 GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (mm->header.type));
273 GNUNET_assert (sizeof (struct MyMessage) == ntohs (mm->header.size));
274 GNUNET_MQ_discard (mqm);
281 struct GNUNET_MQ_Envelope *mqm;
282 struct GNUNET_MessageHeader *mh;
284 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_DUMMY);
285 /* how could the above be checked? */
287 GNUNET_MQ_discard (mqm);
289 mqm = GNUNET_MQ_msg_header_extra (mh,
291 GNUNET_MESSAGE_TYPE_DUMMY);
292 GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (mh->type));
293 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) + 20 == ntohs (mh->size));
294 GNUNET_MQ_discard (mqm);
299 main (int argc, char **argv)
301 char * test_argv[] = {
302 (char *) "test_client",
304 "test_client_data.conf",
307 struct GNUNET_MQ_MessageHandler mh[] = {
308 GNUNET_MQ_hd_fixed_size (dummy,
309 GNUNET_MESSAGE_TYPE_DUMMY,
312 GNUNET_MQ_hd_fixed_size (dummy2,
313 GNUNET_MESSAGE_TYPE_DUMMY2,
316 GNUNET_MQ_handler_end ()
321 GNUNET_log_setup ("test-mq",
327 GNUNET_SERVICE_run_ (3,
330 GNUNET_SERVICE_OPTION_NONE,