c3018
[oweals/gnunet.git] / src / arm / test_exponential_backoff.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 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file arm/test_exponential_backoff.c
22  * @brief testcase for gnunet-service-arm.c
23  */
24 #include "platform.h"
25 #include "gnunet_arm_service.h"
26 #include "gnunet_client_lib.h"
27 #include "gnunet_configuration_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_protocols.h"
30
31 #define VERBOSE GNUNET_NO
32 #define START_ARM GNUNET_YES
33 #define LOG_BACKOFF GNUNET_NO
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
35 #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
36 #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
37
38 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 static struct GNUNET_ARM_Handle *arm;
40 static int ok = 1;
41
42 static int trialCount;
43 static struct GNUNET_TIME_Absolute startedWaitingAt;
44 struct GNUNET_TIME_Relative waitedFor;
45
46 #if LOG_BACKOFF
47 static FILE *killLogFilePtr;
48 static char *killLogFileName;
49 #endif
50
51
52 /**
53  * Context for handling the shutdown of a service.
54  */
55 struct ShutdownContext
56 {
57   /**
58    * Connection to the service that is being shutdown.
59    */
60   struct GNUNET_CLIENT_Connection *sock;
61
62   /**
63    * Time allowed for shutdown to happen.
64    */
65   struct GNUNET_TIME_Absolute timeout;
66
67   /**
68    * Task set up to cancel the shutdown request on timeout.
69    */
70   GNUNET_SCHEDULER_TaskIdentifier cancel_task;
71
72   /**
73    * Task to call once shutdown complete
74    */
75   GNUNET_CLIENT_ShutdownTask cont;
76
77   /**
78    * Closure for shutdown continuation
79    */
80   void *cont_cls;
81
82   /**
83    * We received a confirmation that the service will shut down.
84    */
85   int confirmed;
86
87 };
88
89 /**
90  * Handler receiving response to service shutdown requests.
91  * First call with NULL: service misbehaving, or something.
92  * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
93  *   - service will shutdown
94  * Second call with NULL:
95  *   - service has now really shut down.
96  *
97  * @param cls closure
98  * @param msg NULL, indicating socket closure.
99  */
100 static void
101 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
102 {
103   struct ShutdownContext *shutdown_ctx = cls;
104
105   if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
106   {
107     /* Means the other side closed the connection and never confirmed a shutdown */
108     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
109                 "Service handle shutdown before ACK!\n");
110     if (shutdown_ctx->cont != NULL)
111       shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
112     GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
113     GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
114     GNUNET_free (shutdown_ctx);
115   }
116   else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
117   {
118 #if VERBOSE
119     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
120 #endif
121     if (shutdown_ctx->cont != NULL)
122       shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
123
124     GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
125     GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
126     GNUNET_free (shutdown_ctx);
127   }
128   else
129   {
130     GNUNET_assert (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader));
131     switch (ntohs (msg->type))
132     {
133     case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
134 #if VERBOSE
135       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136                   "Received confirmation for service shutdown.\n");
137 #endif
138       shutdown_ctx->confirmed = GNUNET_YES;
139       GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
140                              shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
141       break;
142     default:                   /* Fall through */
143       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Service shutdown refused!\n");
144       if (shutdown_ctx->cont != NULL)
145         shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES);
146
147       GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
148       GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
149       GNUNET_free (shutdown_ctx);
150       break;
151     }
152   }
153 }
154
155 /**
156  * Shutting down took too long, cancel receive and return error.
157  *
158  * @param cls closure
159  * @param tc context information (why was this task triggered now)
160  */
161 void
162 service_shutdown_cancel (void *cls,
163                          const struct GNUNET_SCHEDULER_TaskContext *tc)
164 {
165   struct ShutdownContext *shutdown_ctx = cls;
166
167   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
168   shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
169   GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
170   GNUNET_free (shutdown_ctx);
171 }
172
173
174 /**
175  * If possible, write a shutdown message to the target
176  * buffer and destroy the client connection.
177  *
178  * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
179  * @param size number of bytes available in buf
180  * @param buf NULL on error, otherwise target buffer
181  * @return number of bytes written to buf
182  */
183 static size_t
184 write_shutdown (void *cls, size_t size, void *buf)
185 {
186   struct GNUNET_MessageHeader *msg;
187   struct ShutdownContext *shutdown_ctx = cls;
188
189   if (size < sizeof (struct GNUNET_MessageHeader))
190   {
191     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
192                 _("Failed to transmit shutdown request to client.\n"));
193     shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
194     GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
195     GNUNET_free (shutdown_ctx);
196     return 0;                   /* client disconnected */
197   }
198
199   GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
200                          shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
201   shutdown_ctx->cancel_task =
202       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
203                                     (shutdown_ctx->timeout),
204                                     &service_shutdown_cancel, shutdown_ctx);
205   msg = (struct GNUNET_MessageHeader *) buf;
206   msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
207   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
208   return sizeof (struct GNUNET_MessageHeader);
209 }
210
211
212 /**
213  * Request that the service should shutdown.
214  * Afterwards, the connection will automatically be
215  * disconnected.  Hence the "sock" should not
216  * be used by the caller after this call
217  * (calling this function frees "sock" after a while).
218  *
219  * @param sock the socket connected to the service
220  * @param timeout how long to wait before giving up on transmission
221  * @param cont continuation to call once the service is really down
222  * @param cont_cls closure for continuation
223  *
224  */
225 static void
226 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
227                       struct GNUNET_TIME_Relative timeout,
228                       GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
229 {
230   struct ShutdownContext *shutdown_ctx;
231
232   shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
233   shutdown_ctx->cont = cont;
234   shutdown_ctx->cont_cls = cont_cls;
235   shutdown_ctx->sock = sock;
236   shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
237   GNUNET_CLIENT_notify_transmit_ready (sock,
238                                        sizeof (struct GNUNET_MessageHeader),
239                                        timeout, GNUNET_NO, &write_shutdown,
240                                        shutdown_ctx);
241 }
242
243
244 static void
245 arm_notify_stop (void *cls, int success)
246 {
247   GNUNET_assert (success == GNUNET_NO);
248 #if START_ARM
249   GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
250 #endif
251 }
252
253
254 static void
255 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
256
257
258 static void
259 do_nothing_notify (void *cls, int success)
260 {
261   GNUNET_assert (success == GNUNET_YES);
262   ok = 1;
263   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
264 }
265
266
267 static void
268 arm_notify (void *cls, int success)
269 {
270   GNUNET_assert (success == GNUNET_YES);
271   GNUNET_ARM_start_service (arm, "do-nothing", TIMEOUT, &do_nothing_notify,
272                             NULL);
273 }
274
275
276 static void
277 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
278
279
280 static void
281 do_nothing_restarted_notify_task (void *cls,
282                                   const struct GNUNET_SCHEDULER_TaskContext *tc)
283 {
284   static char a;
285
286   trialCount++;
287
288 #if LOG_BACKOFF
289   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
290   {
291     fprintf (killLogFilePtr, "%d.Reason is shutdown!\n", trialCount);
292   }
293   else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
294   {
295     fprintf (killLogFilePtr, "%d.Reason is timeout!\n", trialCount);
296   }
297   else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0)
298   {
299     fprintf (killLogFilePtr, "%d.Service is running!\n", trialCount);
300   }
301 #endif
302   GNUNET_SCHEDULER_add_now (&kill_task, &a);
303 }
304
305
306 static void
307 do_test (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
308 {
309   GNUNET_CLIENT_service_test ("do-nothing", cfg, TIMEOUT,
310                               &do_nothing_restarted_notify_task, NULL);
311 }
312
313
314 static void
315 shutdown_cont (void *cls, int reason)
316 {
317   trialCount++;
318   startedWaitingAt = GNUNET_TIME_absolute_get ();
319   GNUNET_SCHEDULER_add_delayed (waitedFor, &do_test, NULL);
320 }
321
322
323 static void
324 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
325 {
326   static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
327
328   if (NULL != cbData)
329   {
330     waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
331
332 #if LOG_BACKOFF
333     fprintf (killLogFilePtr, "Waited for: %llu ms\n",
334              (unsigned long long) waitedFor.rel_value);
335 #endif
336   }
337   else
338   {
339     waitedFor.rel_value = 0;
340   }
341   /* Connect to the doNothing task */
342   doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
343   GNUNET_assert (doNothingConnection != NULL);
344   if (trialCount == 12)
345   {
346     GNUNET_CLIENT_disconnect (doNothingConnection, GNUNET_NO);
347     GNUNET_ARM_stop_service (arm, "do-nothing", TIMEOUT, &arm_notify_stop,
348                              NULL);
349     ok = 0;
350     return;
351   }
352   /* Use the created connection to kill the doNothingTask */
353   arm_service_shutdown (doNothingConnection, TIMEOUT, &shutdown_cont, NULL);
354 }
355
356
357 static void
358 task (void *cls, char *const *args, const char *cfgfile,
359       const struct GNUNET_CONFIGURATION_Handle *c)
360 {
361   cfg = c;
362
363   arm = GNUNET_ARM_connect (cfg, NULL);
364 #if START_ARM
365   GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify,
366                             NULL);
367 #else
368   arm_do_nothing (NULL, GNUNET_YES);
369 #endif
370 }
371
372
373 static int
374 check ()
375 {
376   char *const argv[] = {
377     "test-exponential-backoff",
378     "-c", "test_arm_api_data.conf",
379 #if VERBOSE
380     "-L", "DEBUG",
381 #endif
382     NULL
383   };
384   struct GNUNET_GETOPT_CommandLineOption options[] = {
385     GNUNET_GETOPT_OPTION_END
386   };
387
388   /* Running ARM  and running the do_nothing task */
389   GNUNET_assert (GNUNET_OK ==
390                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
391                                      argv, "test-exponential-backoff", "nohelp",
392                                      options, &task, NULL));
393
394
395   return ok;
396 }
397
398 static int
399 init ()
400 {
401 #if LOG_BACKOFF
402   killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
403   if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
404   {
405     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
406                               killLogFileName);
407     GNUNET_free (killLogFileName);
408     return GNUNET_SYSERR;
409   }
410 #endif
411   return GNUNET_OK;
412 }
413
414
415 static void
416 houseKeep ()
417 {
418 #if LOG_BACKOFF
419   GNUNET_assert (0 == fclose (killLogFilePtr));
420   GNUNET_free (killLogFileName);
421 #endif
422 }
423
424
425 int
426 main (int argc, char *argv[])
427 {
428   int ret;
429
430   GNUNET_log_setup ("test-exponential-backoff",
431 #if VERBOSE
432                     "DEBUG",
433 #else
434                     "WARNING",
435 #endif
436                     NULL);
437
438   init ();
439   ret = check ();
440   houseKeep ();
441   return ret;
442 }
443
444 /* end of test_exponential_backoff.c */