Make exponential backoff test compile again
[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_util_lib.h"
27 #include "gnunet_protocols.h"
28
29 #define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
30
31 #define START_ARM GNUNET_YES
32
33 #define LOG_BACKOFF GNUNET_NO
34
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
36
37 #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
38
39 #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
40
41
42 static const struct GNUNET_CONFIGURATION_Handle *cfg;
43
44 static struct GNUNET_ARM_Handle *arm;
45
46 static struct GNUNET_ARM_MonitorHandle *mon;
47
48 static int ok = 1;
49
50 static int phase = 0;
51
52 static int trialCount;
53
54 static struct GNUNET_TIME_Absolute startedWaitingAt;
55
56 struct GNUNET_TIME_Relative waitedFor;
57
58 struct GNUNET_TIME_Relative waitedFor_prev;
59
60 #if LOG_BACKOFF
61 static FILE *killLogFilePtr;
62
63 static char *killLogFileName;
64 #endif
65
66
67 typedef void (*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
68
69 /**
70  * Context for handling the shutdown of a service.
71  */
72 struct ShutdownContext
73 {
74   /**
75    * Connection to the service that is being shutdown.
76    */
77   struct GNUNET_CLIENT_Connection *sock;
78
79   /**
80    * Time allowed for shutdown to happen.
81    */
82   struct GNUNET_TIME_Absolute timeout;
83
84   /**
85    * Task set up to cancel the shutdown request on timeout.
86    */
87   GNUNET_SCHEDULER_TaskIdentifier cancel_task;
88
89   /**
90    * Task to call once shutdown complete
91    */
92   GNUNET_CLIENT_ShutdownTask cont;
93
94   /**
95    * Closure for shutdown continuation
96    */
97   void *cont_cls;
98
99   /**
100    * We received a confirmation that the service will shut down.
101    */
102   int confirmed;
103
104 };
105
106 /**
107  * Handler receiving response to service shutdown requests.
108  * We expect it to be called with NULL, since the service that
109  * we are shutting down will just die without replying.
110  *
111  * @param cls closure
112  * @param msg NULL, indicating socket closure.
113  */
114 static void
115 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
116 {
117   struct ShutdownContext *shutdown_ctx = cls;
118
119   if (NULL == msg) 
120   {
121     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
122     if (shutdown_ctx->cont != NULL)
123       shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
124     
125     GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
126     GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
127     GNUNET_free (shutdown_ctx);
128     return;
129   }
130   GNUNET_assert (0);
131 }
132
133
134 /**
135  * Shutting down took too long, cancel receive and return error.
136  *
137  * @param cls closure
138  * @param tc context information (why was this task triggered now)
139  */
140 void
141 service_shutdown_cancel (void *cls,
142                          const struct GNUNET_SCHEDULER_TaskContext *tc)
143 {
144   struct ShutdownContext *shutdown_ctx = cls;
145
146   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
147   shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
148   GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
149   GNUNET_free (shutdown_ctx);
150 }
151
152
153 /**
154  * If possible, write a shutdown message to the target
155  * buffer and destroy the client connection.
156  *
157  * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
158  * @param size number of bytes available in buf
159  * @param buf NULL on error, otherwise target buffer
160  * @return number of bytes written to buf
161  */
162 static size_t
163 write_shutdown (void *cls, size_t size, void *buf)
164 {
165   struct GNUNET_MessageHeader *msg;
166   struct ShutdownContext *shutdown_ctx = cls;
167
168   if (size < sizeof (struct GNUNET_MessageHeader))
169   {
170     LOG ("Failed to send a shutdown request\n");
171     shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
172     GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
173     GNUNET_free (shutdown_ctx);
174     return 0;                   /* client disconnected */
175   }
176
177   GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
178                          shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
179   shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (
180       GNUNET_TIME_absolute_get_remaining (shutdown_ctx->timeout),
181       &service_shutdown_cancel, shutdown_ctx);
182   msg = (struct GNUNET_MessageHeader *) buf;
183   msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP);
184   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
185   strcpy ((char *) &msg[1], "do-nothing");
186   LOG ("Sent a shutdown request\n");
187   return sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1;
188 }
189
190
191 /**
192  * Request that the service should shutdown.
193  * Afterwards, the connection will automatically be
194  * disconnected.  Hence the "sock" should not
195  * be used by the caller after this call
196  * (calling this function frees "sock" after a while).
197  *
198  * @param sock the socket connected to the service
199  * @param timeout how long to wait before giving up on transmission
200  * @param cont continuation to call once the service is really down
201  * @param cont_cls closure for continuation
202  *
203  */
204 static void
205 do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
206                       struct GNUNET_TIME_Relative timeout,
207                       GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
208 {
209   struct ShutdownContext *shutdown_ctx;
210
211   shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
212   shutdown_ctx->cont = cont;
213   shutdown_ctx->cont_cls = cont_cls;
214   shutdown_ctx->sock = sock;
215   shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
216   GNUNET_CLIENT_notify_transmit_ready (sock,
217                                        sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1,
218                                        timeout, GNUNET_NO, &write_shutdown,
219                                        shutdown_ctx);
220 }
221
222
223 static void
224 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
225
226
227 static void
228 shutdown_cont (void *cls, int reason)
229 {
230   if (GNUNET_NO != reason)
231   {
232     /* Re-try shutdown */
233     LOG ("do-nothing didn't die, trying again\n");
234     GNUNET_SCHEDULER_add_now (kill_task, NULL);
235     return;
236   }
237   startedWaitingAt = GNUNET_TIME_absolute_get ();
238   LOG ("do-nothing is dead, starting the countdown\n");
239 }
240
241
242 static void
243 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
244 {
245   static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
246
247   if (NULL != cbData)
248   {
249     waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
250     LOG ("Waited for: %s\n", 
251          GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
252   }
253   else
254   {
255     waitedFor.rel_value_us = 0;
256   }
257   /* Connect to the doNothing task */
258   doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
259   GNUNET_assert (doNothingConnection != NULL);
260   if (trialCount == 12)
261     waitedFor_prev = waitedFor;
262   else if (trialCount == 13)
263   {
264     GNUNET_CLIENT_disconnect (doNothingConnection);
265     GNUNET_ARM_request_service_stop (arm, "do-nothing", TIMEOUT, NULL, NULL);
266     if (waitedFor_prev.rel_value_us >= waitedFor.rel_value_us)
267       ok = 9;
268     else
269       ok = 0;
270     trialCount += 1;
271     return;
272   }
273   trialCount += 1;
274   /* Use the created connection to kill the doNothingTask */
275   do_nothing_service_shutdown (doNothingConnection,
276       TIMEOUT, &shutdown_cont, NULL);
277 }
278
279
280 static void
281 trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
282 {
283   GNUNET_ARM_disconnect_and_free (arm);
284   GNUNET_ARM_monitor_disconnect_and_free (mon);
285 }
286
287
288 static void
289 arm_stop_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
290 {
291   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
292   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
293   LOG ("ARM service stopped\n");
294   GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL);
295 }
296
297
298 static void
299 srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
300 {
301   LOG ("Service %s is %u, phase %u\n", service, status, phase);
302   if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
303   {
304     phase++;
305     GNUNET_ARM_request_service_start (arm, "do-nothing",
306         GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, NULL, NULL);
307     return;
308   }
309   if (phase == 1)
310   {
311     GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
312     GNUNET_break (0 == strcasecmp (service, "do-nothing"));
313     GNUNET_break (phase == 1);
314     LOG ("do-nothing is starting\n");
315     phase++;
316     ok = 1;
317     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
318   }
319   else if ((phase == 2) && (strcasecmp ("do-nothing", service) == 0))
320   {
321     /* We passively monitor ARM for status updates. ARM should tell us
322      * when do-nothing dies (no need to run a service upness test ourselves).
323      */
324     if (status == GNUNET_ARM_SERVICE_STARTING)
325     {
326       LOG ("do-nothing is starting\n");
327       GNUNET_SCHEDULER_add_now (kill_task, &ok);
328     }
329     else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
330     {
331       phase++;
332       GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
333     }
334   }
335 }
336
337
338 static void
339 arm_start_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
340 {
341   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
342   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
343   GNUNET_break (phase == 0);
344   LOG ("Sent 'START' request for arm to ARM %s\n", 
345        (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
346 }
347
348
349 static void
350 task (void *cls, char *const *args, const char *cfgfile,
351       const struct GNUNET_CONFIGURATION_Handle *c)
352 {
353   char *armconfig;
354   cfg = c;
355   if (NULL != cfgfile)
356   {
357     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "arm",
358         "CONFIG", &armconfig))
359     {
360       GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
361                                               *) cfg, "arm", "CONFIG",
362                                              cfgfile);
363     }
364     else
365       GNUNET_free (armconfig);
366   }
367
368   arm = GNUNET_ARM_connect (cfg, NULL, NULL);
369   if (NULL != arm)
370   {
371     mon = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
372     if (NULL != mon)
373     {
374 #if START_ARM
375       GNUNET_ARM_request_service_start (arm, "arm",
376           GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL);
377 #else
378       arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_SERVICE_STARTING);
379 #endif
380     }
381     else
382     {
383       GNUNET_ARM_disconnect_and_free (arm);
384       arm = NULL;
385     }
386   }
387 }
388
389
390 static int
391 check ()
392 {
393   char *const argv[] = {
394     "test-exponential-backoff",
395     "-c", "test_arm_api_data.conf",
396     NULL
397   };
398   struct GNUNET_GETOPT_CommandLineOption options[] = {
399     GNUNET_GETOPT_OPTION_END
400   };
401
402   /* Running ARM  and running the do_nothing task */
403   GNUNET_assert (GNUNET_OK ==
404                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
405                                      argv, "test-exponential-backoff",
406                                      "nohelp", options, &task, NULL));
407
408
409   return ok;
410 }
411
412
413 static int
414 init ()
415 {
416 #if LOG_BACKOFF
417   killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
418   if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
419     {
420       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
421                                 killLogFileName);
422       GNUNET_free (killLogFileName);
423       return GNUNET_SYSERR;
424     }
425 #endif
426   return GNUNET_OK;
427 }
428
429
430 static void
431 houseKeep ()
432 {
433 #if LOG_BACKOFF
434   GNUNET_assert (0 == fclose (killLogFilePtr));
435   GNUNET_free (killLogFileName);
436 #endif
437 }
438
439
440 int
441 main (int argc, char *argv[])
442 {
443   int ret;
444
445   GNUNET_log_setup ("test-exponential-backoff",
446                     "WARNING",
447                     NULL);
448
449   init ();
450   ret = check ();
451   houseKeep ();
452   return ret;
453 }
454
455 /* end of test_exponential_backoff.c */