giving client API option for auto-retry, making more often use of transmit_and_get_re...
[oweals/gnunet.git] / src / util / test_server_disconnect.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file util/test_server_disconnect.c
22  * @brief tests for server.c and client.c,
23  *       specifically client_disconnect
24  */
25 #include "platform.h"
26 #include "gnunet_common.h"
27 #include "gnunet_scheduler_lib.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_time_lib.h"
31
32 #define VERBOSE GNUNET_NO
33
34 #define PORT 22335
35
36 #define MY_TYPE 128
37
38
39 static struct GNUNET_SERVER_Handle *server;
40
41 static struct GNUNET_CLIENT_Connection *client;
42
43 static struct GNUNET_SCHEDULER_Handle *sched;
44
45 static struct GNUNET_CONFIGURATION_Handle *cfg;
46
47 static int ok;
48
49 static void
50 send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
51 {
52   struct GNUNET_SERVER_Client *argclient = cls;
53   GNUNET_assert (ok == 3);
54   ok++;
55   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
56 }
57
58 static void
59 server_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
60 {
61   struct GNUNET_SERVER_Client *argclient = cls;
62   GNUNET_assert (ok == 5);
63   ok++;
64   GNUNET_SERVER_client_disconnect (argclient);
65 }
66
67
68 static void
69 recv_cb (void *cls,
70          struct GNUNET_SERVER_Client *argclient,
71          const struct GNUNET_MessageHeader *message)
72 {
73   void *addr;
74   size_t addrlen;
75   struct sockaddr_in sa;
76   struct sockaddr_in *have;
77
78   GNUNET_assert (GNUNET_OK ==
79                  GNUNET_SERVER_client_get_address (argclient,
80                                                    &addr, &addrlen));
81
82   GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
83   have = addr;
84   memset (&sa, 0, sizeof (sa));
85 #if HAVE_SOCKADDR_IN_SIN_LEN
86   sa.sin_len = sizeof (sa);
87 #endif
88   sa.sin_family = AF_INET;
89   sa.sin_port = have->sin_port;
90   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
91   GNUNET_assert (0 == memcmp (&sa, addr, addrlen));
92   GNUNET_free (addr);
93   switch (ok)
94     {
95     case 2:
96       ok++;
97       GNUNET_SCHEDULER_add_delayed (sched,
98                                     GNUNET_YES,
99                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
100                                     GNUNET_SCHEDULER_NO_TASK,
101                                     GNUNET_TIME_relative_multiply
102                                     (GNUNET_TIME_UNIT_MILLISECONDS, 50),
103                                     &send_done, argclient);
104       break;
105     case 4:
106       ok++;
107       GNUNET_SCHEDULER_add_delayed (sched,
108                                     GNUNET_YES,
109                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
110                                     GNUNET_SCHEDULER_NO_TASK,
111                                     GNUNET_TIME_relative_multiply
112                                     (GNUNET_TIME_UNIT_MILLISECONDS, 50),
113                                     &server_disconnect, argclient);
114       GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
115       break;
116     default:
117       GNUNET_assert (0);
118     }
119
120 }
121
122 static void
123 disconnect_notify (void *cls, const struct GNUNET_MessageHeader *msg)
124 {
125   GNUNET_assert (msg == NULL);
126   GNUNET_assert (ok == 7);
127   ok = 0;
128   GNUNET_CLIENT_disconnect (client);
129   GNUNET_SCHEDULER_shutdown (sched);
130   GNUNET_CONFIGURATION_destroy (cfg);
131 }
132
133
134 /**
135  * Functions with this signature are called whenever a client
136  * is disconnected on the network level.
137  *
138  * @param cls closure
139  * @param client identification of the client
140  */
141 static void
142 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *clientarg)
143 {
144   GNUNET_assert (ok == 6);
145   ok++;
146   GNUNET_CLIENT_receive (client,
147                          &disconnect_notify,
148                          NULL, GNUNET_TIME_UNIT_FOREVER_REL);
149 }
150
151
152 static size_t
153 notify_ready (void *cls, size_t size, void *buf)
154 {
155   struct GNUNET_MessageHeader *msg;
156
157   GNUNET_assert (size >= 256);
158   GNUNET_assert (1 == ok);
159   ok++;
160   msg = buf;
161   msg->type = htons (MY_TYPE);
162   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
163   msg++;
164   msg->type = htons (MY_TYPE);
165   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
166   return 2 * sizeof (struct GNUNET_MessageHeader);
167 }
168
169
170 static struct GNUNET_SERVER_MessageHandler handlers[] = {
171   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
172   {NULL, NULL, 0, 0}
173 };
174
175
176 static void
177 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 {
179   struct sockaddr_in sa;
180
181   sched = tc->sched;
182   memset (&sa, 0, sizeof (sa));
183 #if HAVE_SOCKADDR_IN_SIN_LEN
184   sa.sin_len = sizeof (sa);
185 #endif
186   sa.sin_family = AF_INET;
187   sa.sin_port = htons (PORT);
188   server = GNUNET_SERVER_create (tc->sched,
189                                  NULL,
190                                  NULL,
191                                  (const struct sockaddr *) &sa,
192                                  sizeof (sa),
193                                  1024,
194                                  GNUNET_TIME_relative_multiply
195                                  (GNUNET_TIME_UNIT_MILLISECONDS, 250),
196                                  GNUNET_NO);
197   GNUNET_assert (server != NULL);
198   handlers[0].callback_cls = cls;
199   GNUNET_SERVER_add_handlers (server, handlers);
200   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
201   cfg = GNUNET_CONFIGURATION_create ();
202   GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
203   GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME",
204                                          "localhost");
205   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
206                                          "localhost");
207   client = GNUNET_CLIENT_connect (tc->sched, "test", cfg);
208   GNUNET_assert (client != NULL);
209   GNUNET_CLIENT_notify_transmit_ready (client,
210                                        256,
211                                        GNUNET_TIME_relative_multiply
212                                        (GNUNET_TIME_UNIT_MILLISECONDS, 250),
213                                        GNUNET_NO,
214                                        &notify_ready, NULL);
215 }
216
217
218 /**
219  * Main method, starts scheduler with task1,
220  * checks that "ok" is correct at the end.
221  */
222 static int
223 check ()
224 {
225
226   ok = 1;
227   GNUNET_SCHEDULER_run (&task, NULL);
228   return ok;
229 }
230
231
232 int
233 main (int argc, char *argv[])
234 {
235   int ret = 0;
236
237   GNUNET_log_setup ("test_server_disconnect",
238 #if VERBOSE
239                     "DEBUG",
240 #else
241                     "WARNING",
242 #endif
243                     NULL);
244   ret += check ();
245
246   return ret;
247 }
248
249 /* end of test_server_disconnect.c */