ack florian
[oweals/gnunet.git] / src / util / test_mq_client.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/test_mq_client.c
23  * @brief tests for mq with connection client
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27
28 #define PORT 23336
29
30 #define MY_TYPE 128
31
32
33 static struct GNUNET_SERVER_Handle *server;
34
35 static struct GNUNET_CONFIGURATION_Handle *cfg;
36
37 static int ok;
38
39 static int notify = GNUNET_NO;
40
41 static int received = 0;
42
43
44 static void
45 recv_cb (void *cls,
46          struct GNUNET_SERVER_Client *argclient,
47          const struct GNUNET_MessageHeader *message)
48 {
49   received++;
50   if (received == 2)
51   {
52     GNUNET_SERVER_receive_done (argclient,
53                                 GNUNET_NO);
54     return;
55   }
56
57   /* can happen if notify does not work */
58   GNUNET_assert (received < 2);
59
60   GNUNET_SERVER_receive_done (argclient, GNUNET_YES);
61 }
62
63
64 static void
65 clean_up (void *cls)
66 {
67   GNUNET_SERVER_destroy (server);
68   server = NULL;
69   GNUNET_CONFIGURATION_destroy (cfg);
70   cfg = NULL;
71 }
72
73
74 /**
75  * Functions with this signature are called whenever a client
76  * is disconnected on the network level.
77  *
78  * @param cls closure
79  * @param client identification of the client
80  */
81 static void
82 notify_disconnect (void *cls,
83                    struct GNUNET_SERVER_Client *client)
84 {
85   if (client == NULL)
86     return;
87   ok = 0;
88   GNUNET_SCHEDULER_add_now (&clean_up, NULL);
89 }
90
91
92 static struct GNUNET_SERVER_MessageHandler handlers[] = {
93   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
94   {NULL, NULL, 0, 0}
95 };
96
97
98 static void
99 send_cb (void *cls)
100 {
101   /* the notify should only be called once */
102   GNUNET_assert (GNUNET_NO == notify);
103   notify = GNUNET_YES;
104 }
105
106 static void
107 send_trap_cb (void *cls)
108 {
109   GNUNET_assert (0);
110 }
111
112
113 static void
114 test_mq ()
115 {
116   struct GNUNET_MQ_Handle *mq;
117   struct GNUNET_MQ_Envelope *mqm;
118
119   /* FIXME: test handling responses */
120   mq = GNUNET_CLIENT_connecT (cfg,
121                               "test",
122                               NULL, NULL, NULL);
123
124   mqm = GNUNET_MQ_msg_header (MY_TYPE);
125   GNUNET_MQ_send (mq, mqm);
126
127   mqm = GNUNET_MQ_msg_header (MY_TYPE);
128   GNUNET_MQ_notify_sent (mqm, &send_trap_cb, NULL);
129   GNUNET_MQ_send (mq, mqm);
130   GNUNET_MQ_send_cancel (mqm);
131
132   mqm = GNUNET_MQ_msg_header (MY_TYPE);
133   GNUNET_MQ_notify_sent (mqm, &send_cb, NULL);
134   GNUNET_MQ_send (mq, mqm);
135 }
136
137
138 static void
139 task (void *cls)
140 {
141   struct sockaddr_in sa;
142   struct sockaddr *sap[2];
143   socklen_t slens[2];
144
145   sap[0] = (struct sockaddr *) &sa;
146   slens[0] = sizeof (sa);
147   sap[1] = NULL;
148   slens[1] = 0;
149   memset (&sa, 0, sizeof (sa));
150 #if HAVE_SOCKADDR_IN_SIN_LEN
151   sa.sin_len = sizeof (sa);
152 #endif
153   sa.sin_family = AF_INET;
154   sa.sin_port = htons (PORT);
155   server =
156       GNUNET_SERVER_create (NULL, NULL, sap, slens,
157                             GNUNET_TIME_relative_multiply
158                             (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
159   GNUNET_assert (server != NULL);
160   handlers[0].callback_cls = cls;
161   GNUNET_SERVER_add_handlers (server, handlers);
162   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
163   cfg = GNUNET_CONFIGURATION_create ();
164   GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
165   GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost");
166   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
167                                          "localhost");
168   test_mq ();
169 }
170
171
172 int
173 main (int argc, char *argv[])
174 {
175   GNUNET_log_setup ("test-mq-client",
176                     "INFO",
177                     NULL);
178   ok = 1;
179   GNUNET_SCHEDULER_run (&task, NULL);
180   GNUNET_assert (GNUNET_YES == notify);
181   return ok;
182 }