2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/test_mq_client.c
23 * @brief tests for mq with connection client
26 #include "gnunet_util_lib.h"
33 static struct GNUNET_SERVER_Handle *server;
35 static struct GNUNET_CONFIGURATION_Handle *cfg;
39 static int notify = GNUNET_NO;
41 static int received = 0;
46 struct GNUNET_SERVER_Client *argclient,
47 const struct GNUNET_MessageHeader *message)
52 GNUNET_SERVER_receive_done (argclient,
57 /* can happen if notify does not work */
58 GNUNET_assert (received < 2);
60 GNUNET_SERVER_receive_done (argclient, GNUNET_YES);
67 GNUNET_SERVER_destroy (server);
69 GNUNET_CONFIGURATION_destroy (cfg);
75 * Functions with this signature are called whenever a client
76 * is disconnected on the network level.
79 * @param client identification of the client
82 notify_disconnect (void *cls,
83 struct GNUNET_SERVER_Client *client)
88 GNUNET_SCHEDULER_add_now (&clean_up, NULL);
92 static struct GNUNET_SERVER_MessageHandler handlers[] = {
93 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
101 /* the notify should only be called once */
102 GNUNET_assert (GNUNET_NO == notify);
107 send_trap_cb (void *cls)
116 struct GNUNET_CLIENT_Connection *client;
117 struct GNUNET_MQ_Handle *mq;
118 struct GNUNET_MQ_Envelope *mqm;
120 client = GNUNET_CLIENT_connect ("test", cfg);
121 GNUNET_assert (client != NULL);
123 /* FIXME: test handling responses */
124 mq = GNUNET_MQ_queue_for_connection_client (client, NULL, NULL, NULL);
126 mqm = GNUNET_MQ_msg_header (MY_TYPE);
127 GNUNET_MQ_send (mq, mqm);
129 mqm = GNUNET_MQ_msg_header (MY_TYPE);
130 GNUNET_MQ_notify_sent (mqm, &send_trap_cb, NULL);
131 GNUNET_MQ_send (mq, mqm);
132 GNUNET_MQ_send_cancel (mqm);
134 mqm = GNUNET_MQ_msg_header (MY_TYPE);
135 GNUNET_MQ_notify_sent (mqm, &send_cb, NULL);
136 GNUNET_MQ_send (mq, mqm);
143 struct sockaddr_in sa;
144 struct sockaddr *sap[2];
147 sap[0] = (struct sockaddr *) &sa;
148 slens[0] = sizeof (sa);
151 memset (&sa, 0, sizeof (sa));
152 #if HAVE_SOCKADDR_IN_SIN_LEN
153 sa.sin_len = sizeof (sa);
155 sa.sin_family = AF_INET;
156 sa.sin_port = htons (PORT);
158 GNUNET_SERVER_create (NULL, NULL, sap, slens,
159 GNUNET_TIME_relative_multiply
160 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
161 GNUNET_assert (server != NULL);
162 handlers[0].callback_cls = cls;
163 GNUNET_SERVER_add_handlers (server, handlers);
164 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, cls);
165 cfg = GNUNET_CONFIGURATION_create ();
166 GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
167 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost");
168 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
175 main (int argc, char *argv[])
177 GNUNET_log_setup ("test-mq-client",
181 GNUNET_SCHEDULER_run (&task, NULL);
182 GNUNET_assert (GNUNET_YES == notify);