2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file arm/test_exponential_backoff.c
22 * @brief testcase for gnunet-service-arm.c
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"
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)
38 static struct GNUNET_SCHEDULER_Handle *sched;
39 static const struct GNUNET_CONFIGURATION_Handle *cfg;
40 static struct GNUNET_ARM_Handle *arm;
43 static int trialCount;
44 static struct GNUNET_TIME_Absolute startedWaitingAt;
45 struct GNUNET_TIME_Relative waitedFor;
48 static FILE *killLogFilePtr;
49 static char *killLogFileName;
54 * Context for handling the shutdown of a service.
56 struct ShutdownContext
59 * Scheduler to be used to call continuation
61 struct GNUNET_SCHEDULER_Handle *sched;
63 * Connection to the service that is being shutdown.
65 struct GNUNET_CLIENT_Connection *sock;
68 * Time allowed for shutdown to happen.
70 struct GNUNET_TIME_Absolute timeout;
73 * Task set up to cancel the shutdown request on timeout.
75 GNUNET_SCHEDULER_TaskIdentifier cancel_task;
78 * Task to call once shutdown complete
80 GNUNET_CLIENT_ShutdownTask cont;
83 * Closure for shutdown continuation
88 * We received a confirmation that the service will shut down.
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.
103 * @param msg NULL, indicating socket closure.
106 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
108 struct ShutdownContext *shutdown_ctx = cls;
110 if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
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);
121 else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
124 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
125 "Service shutdown complete.\n");
127 if (shutdown_ctx->cont != NULL)
128 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO);
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);
136 GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
137 switch (ntohs(msg->type))
139 case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
141 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
142 "Received confirmation for service shutdown.\n");
144 shutdown_ctx->confirmed = GNUNET_YES;
145 GNUNET_CLIENT_receive (shutdown_ctx->sock,
146 &service_shutdown_handler,
148 GNUNET_TIME_UNIT_FOREVER_REL);
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);
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);
165 * Shutting down took too long, cancel receive and return error.
168 * @param tc context information (why was this task triggered now)
170 void service_shutdown_cancel (void *cls,
171 const struct GNUNET_SCHEDULER_TaskContext * tc)
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);
182 * If possible, write a shutdown message to the target
183 * buffer and destroy the client connection.
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
191 write_shutdown (void *cls, size_t size, void *buf)
193 struct GNUNET_MessageHeader *msg;
194 struct ShutdownContext *shutdown_ctx = cls;
196 if (size < sizeof (struct GNUNET_MessageHeader))
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 */
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,
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);
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).
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
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,
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,
250 GNUNET_MessageHeader),
253 &write_shutdown, shutdown_ctx);
258 arm_notify_stop (void *cls, int success)
260 GNUNET_assert (success == GNUNET_NO);
262 GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
268 kill_task (void *cbData,
269 const struct GNUNET_SCHEDULER_TaskContext *tc);
273 do_nothing_notify (void *cls, int success)
275 GNUNET_assert (success == GNUNET_YES);
277 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_UNIT_SECONDS,
283 arm_notify (void *cls, int success)
285 GNUNET_assert (success == GNUNET_YES);
286 GNUNET_ARM_start_service (arm,
287 "do-nothing", TIMEOUT,
288 &do_nothing_notify, NULL);
293 kill_task (void *cbData,
294 const struct GNUNET_SCHEDULER_TaskContext *tc);
298 do_nothing_restarted_notify_task (void *cls,
299 const struct GNUNET_SCHEDULER_TaskContext *tc)
306 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
308 fprintf(killLogFilePtr,
309 "%d.Reason is shutdown!\n",
312 else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
314 fprintf(killLogFilePtr,
315 "%d.Reason is timeout!\n",
318 else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0)
320 fprintf(killLogFilePtr,
321 "%d.Service is running!\n",
325 GNUNET_SCHEDULER_add_now (sched, &kill_task, &a);
330 do_test (void *cbData,
331 const struct GNUNET_SCHEDULER_TaskContext *tc)
333 GNUNET_CLIENT_service_test(sched, "do-nothing",
335 &do_nothing_restarted_notify_task, NULL);
340 shutdown_cont (void *cls, int reason)
343 startedWaitingAt = GNUNET_TIME_absolute_get();
344 GNUNET_SCHEDULER_add_delayed (sched,
352 kill_task (void *cbData,
353 const struct GNUNET_SCHEDULER_TaskContext *tc)
355 static struct GNUNET_CLIENT_Connection * doNothingConnection = NULL;
359 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
362 fprintf(killLogFilePtr,
363 "Waited for: %llu ms\n",
364 (unsigned long long) waitedFor.value);
371 /* Connect to the doNothing task */
372 doNothingConnection = GNUNET_CLIENT_connect (sched, "do-nothing", cfg);
374 if (NULL == doNothingConnection)
375 fprintf(killLogFilePtr,
376 "Unable to connect to do-nothing process!\n");
378 if (trialCount == 12) {
379 GNUNET_ARM_stop_service (arm,
382 &arm_notify_stop, NULL);
387 /* Use the created connection to kill the doNothingTask */
388 arm_service_shutdown(sched,
391 &shutdown_cont, NULL);
397 struct GNUNET_SCHEDULER_Handle *s,
400 const struct GNUNET_CONFIGURATION_Handle *c)
405 arm = GNUNET_ARM_connect (cfg, sched, NULL);
407 GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify, NULL);
409 arm_do_nothing (NULL, GNUNET_YES);
416 char *const argv[] = {
418 "-c", "test_arm_api_data.conf",
424 struct GNUNET_GETOPT_CommandLineOption options[] = {
425 GNUNET_GETOPT_OPTION_END
428 /* Running ARM and running the do_nothing task */
429 GNUNET_assert (GNUNET_OK ==
430 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
432 "test-exponential-backoff",
433 "nohelp", options, &task, NULL));
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;
458 GNUNET_assert (0 == fclose (killLogFilePtr));
459 GNUNET_free(killLogFileName);
465 main (int argc, char *argv[])
469 GNUNET_log_setup ("test-exponential-backoff",
483 /* end of test_exponential_backoff.c */