2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/test_mq_client.c
23 * @brief tests for mq with connection client
26 #include "gnunet_common.h"
27 #include "gnunet_util_lib.h"
34 static struct GNUNET_SERVER_Handle *server;
36 static struct GNUNET_CLIENT_Connection *client;
38 static struct GNUNET_CONFIGURATION_Handle *cfg;
42 static int notify = GNUNET_NO;
44 static int received = 0;
48 recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
49 const struct GNUNET_MessageHeader *message)
53 printf ("received\n");
56 if ((received == 2) && (GNUNET_YES == notify))
59 GNUNET_SERVER_receive_done (argclient, GNUNET_NO);
63 /* can happen if notify does not work */
64 GNUNET_assert (received < 2);
66 GNUNET_SERVER_receive_done (argclient, GNUNET_YES);
71 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
73 GNUNET_SERVER_destroy (server);
75 GNUNET_CONFIGURATION_destroy (cfg);
81 * Functions with this signature are called whenever a client
82 * is disconnected on the network level.
85 * @param client identification of the client
88 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
93 GNUNET_SCHEDULER_add_now (&clean_up, NULL);
97 static struct GNUNET_SERVER_MessageHandler handlers[] = {
98 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
102 void send_cb (void *cls)
104 /* the notify should only be called once */
105 GNUNET_assert (GNUNET_NO == notify);
106 printf ("notify sent\n");
110 void test_mq (struct GNUNET_CLIENT_Connection *client)
112 struct GNUNET_MQ_Handle *mq;
113 struct GNUNET_MQ_Envelope *mqm;
115 /* FIXME: test handling responses */
116 mq = GNUNET_MQ_queue_for_connection_client (client, NULL, NULL, NULL);
118 mqm = GNUNET_MQ_msg_header (MY_TYPE);
119 GNUNET_MQ_send (mq, mqm);
121 mqm = GNUNET_MQ_msg_header (MY_TYPE);
122 GNUNET_MQ_notify_sent (mqm, send_cb, NULL);
123 GNUNET_MQ_send (mq, mqm);
125 /* FIXME: add a message that will be canceled */
130 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132 struct sockaddr_in sa;
133 struct sockaddr *sap[2];
136 sap[0] = (struct sockaddr *) &sa;
137 slens[0] = sizeof (sa);
140 memset (&sa, 0, sizeof (sa));
141 #if HAVE_SOCKADDR_IN_SIN_LEN
142 sa.sin_len = sizeof (sa);
144 sa.sin_family = AF_INET;
145 sa.sin_port = htons (PORT);
147 GNUNET_SERVER_create (NULL, NULL, sap, slens,
148 GNUNET_TIME_relative_multiply
149 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
150 GNUNET_assert (server != NULL);
151 handlers[0].callback_cls = cls;
152 GNUNET_SERVER_add_handlers (server, handlers);
153 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, cls);
154 cfg = GNUNET_CONFIGURATION_create ();
155 GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
156 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost");
157 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
159 client = GNUNET_CLIENT_connect ("test", cfg);
160 GNUNET_assert (client != NULL);
167 main (int argc, char *argv[])
169 GNUNET_log_setup ("test-mq-client",
173 GNUNET_SCHEDULER_run (&task, NULL);