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