aboutsummaryrefslogtreecommitdiff
path: root/src/arm/test_exponential_backoff.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-12-08 15:45:10 +0000
committerChristian Grothoff <christian@grothoff.org>2011-12-08 15:45:10 +0000
commitcbd09ba94fde1331cb4fe6ea29f5f164adc8db25 (patch)
tree7d6d205b2f0fbaf894770334733c589c34fb4864 /src/arm/test_exponential_backoff.c
parent7ecae6c47bc658cce69de75c1addb0c6db75ced9 (diff)
downloadgnunet-cbd09ba94fde1331cb4fe6ea29f5f164adc8db25.tar.gz
gnunet-cbd09ba94fde1331cb4fe6ea29f5f164adc8db25.zip
major rewrite of ARM service and a bit of the ARM IPC to take advantage of the simplifications possible now that we no longer intercept traffic; the new code in particular is better at communicating what exactly ARM was doing in response to requests. A major change is that gnunet-arm -i/-k now only impacts if a service is running by-default, on-demand starting is no longer impacted, option -t from gnunet-arm was removed
Diffstat (limited to 'src/arm/test_exponential_backoff.c')
-rw-r--r--src/arm/test_exponential_backoff.c221
1 files changed, 118 insertions, 103 deletions
diff --git a/src/arm/test_exponential_backoff.c b/src/arm/test_exponential_backoff.c
index 3ff4d284f..029ed145a 100644
--- a/src/arm/test_exponential_backoff.c
+++ b/src/arm/test_exponential_backoff.c
@@ -29,22 +29,33 @@
29#include "gnunet_protocols.h" 29#include "gnunet_protocols.h"
30 30
31#define VERBOSE GNUNET_EXTRA_LOGGING 31#define VERBOSE GNUNET_EXTRA_LOGGING
32
32#define START_ARM GNUNET_YES 33#define START_ARM GNUNET_YES
34
33#define LOG_BACKOFF GNUNET_NO 35#define LOG_BACKOFF GNUNET_NO
36
34#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) 37#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
38
35#define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL 39#define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
40
36#define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5) 41#define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
37 42
43
38static const struct GNUNET_CONFIGURATION_Handle *cfg; 44static const struct GNUNET_CONFIGURATION_Handle *cfg;
45
39static struct GNUNET_ARM_Handle *arm; 46static struct GNUNET_ARM_Handle *arm;
47
40static int ok = 1; 48static int ok = 1;
41 49
42static int trialCount; 50static int trialCount;
51
43static struct GNUNET_TIME_Absolute startedWaitingAt; 52static struct GNUNET_TIME_Absolute startedWaitingAt;
53
44struct GNUNET_TIME_Relative waitedFor; 54struct GNUNET_TIME_Relative waitedFor;
45 55
46#if LOG_BACKOFF 56#if LOG_BACKOFF
47static FILE *killLogFilePtr; 57static FILE *killLogFilePtr;
58
48static char *killLogFileName; 59static char *killLogFileName;
49#endif 60#endif
50 61
@@ -89,7 +100,7 @@ struct ShutdownContext
89/** 100/**
90 * Handler receiving response to service shutdown requests. 101 * Handler receiving response to service shutdown requests.
91 * First call with NULL: service misbehaving, or something. 102 * First call with NULL: service misbehaving, or something.
92 * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK: 103 * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN:
93 * - service will shutdown 104 * - service will shutdown
94 * Second call with NULL: 105 * Second call with NULL:
95 * - service has now really shut down. 106 * - service has now really shut down.
@@ -103,53 +114,56 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
103 struct ShutdownContext *shutdown_ctx = cls; 114 struct ShutdownContext *shutdown_ctx = cls;
104 115
105 if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES)) 116 if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
106 { 117 {
107 /* Means the other side closed the connection and never confirmed a shutdown */ 118 /* Means the other side closed the connection and never confirmed a shutdown */
108 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
109 "Service handle shutdown before ACK!\n"); 120 "Service handle shutdown before ACK!\n");
110 if (shutdown_ctx->cont != NULL) 121 if (shutdown_ctx->cont != NULL)
111 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR); 122 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
112 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task); 123 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
113 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); 124 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
114 GNUNET_free (shutdown_ctx); 125 GNUNET_free (shutdown_ctx);
115 } 126 }
116 else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES)) 127 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 { 128 {
133 case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
134#if VERBOSE 129#if VERBOSE
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
136 "Received confirmation for service shutdown.\n");
137#endif 131#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) 132 if (shutdown_ctx->cont != NULL)
145 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES); 133 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
146 134
147 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task); 135 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
148 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); 136 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
149 GNUNET_free (shutdown_ctx); 137 GNUNET_free (shutdown_ctx);
150 break;
151 } 138 }
152 } 139 else
140 {
141 GNUNET_assert (ntohs (msg->size) ==
142 sizeof (struct GNUNET_MessageHeader));
143 switch (ntohs (msg->type))
144 {
145 case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN:
146#if VERBOSE
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Received confirmation for service shutdown.\n");
149#endif
150 shutdown_ctx->confirmed = GNUNET_YES;
151 GNUNET_CLIENT_receive (shutdown_ctx->sock,
152 &service_shutdown_handler, shutdown_ctx,
153 GNUNET_TIME_UNIT_FOREVER_REL);
154 break;
155 default: /* Fall through */
156 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
157 "Service shutdown refused!\n");
158 if (shutdown_ctx->cont != NULL)
159 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES);
160
161 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
162 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
163 GNUNET_free (shutdown_ctx);
164 break;
165 }
166 }
153} 167}
154 168
155/** 169/**
@@ -160,7 +174,7 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
160 */ 174 */
161void 175void
162service_shutdown_cancel (void *cls, 176service_shutdown_cancel (void *cls,
163 const struct GNUNET_SCHEDULER_TaskContext *tc) 177 const struct GNUNET_SCHEDULER_TaskContext *tc)
164{ 178{
165 struct ShutdownContext *shutdown_ctx = cls; 179 struct ShutdownContext *shutdown_ctx = cls;
166 180
@@ -187,21 +201,21 @@ write_shutdown (void *cls, size_t size, void *buf)
187 struct ShutdownContext *shutdown_ctx = cls; 201 struct ShutdownContext *shutdown_ctx = cls;
188 202
189 if (size < sizeof (struct GNUNET_MessageHeader)) 203 if (size < sizeof (struct GNUNET_MessageHeader))
190 { 204 {
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 _("Failed to transmit shutdown request to client.\n")); 206 _("Failed to transmit shutdown request to client.\n"));
193 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR); 207 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
194 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); 208 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
195 GNUNET_free (shutdown_ctx); 209 GNUNET_free (shutdown_ctx);
196 return 0; /* client disconnected */ 210 return 0; /* client disconnected */
197 } 211 }
198 212
199 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler, 213 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
200 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL); 214 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
201 shutdown_ctx->cancel_task = 215 shutdown_ctx->cancel_task =
202 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining 216 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
203 (shutdown_ctx->timeout), 217 (shutdown_ctx->timeout),
204 &service_shutdown_cancel, shutdown_ctx); 218 &service_shutdown_cancel, shutdown_ctx);
205 msg = (struct GNUNET_MessageHeader *) buf; 219 msg = (struct GNUNET_MessageHeader *) buf;
206 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN); 220 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
207 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 221 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
@@ -224,8 +238,8 @@ write_shutdown (void *cls, size_t size, void *buf)
224 */ 238 */
225static void 239static void
226arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock, 240arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
227 struct GNUNET_TIME_Relative timeout, 241 struct GNUNET_TIME_Relative timeout,
228 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls) 242 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
229{ 243{
230 struct ShutdownContext *shutdown_ctx; 244 struct ShutdownContext *shutdown_ctx;
231 245
@@ -235,16 +249,17 @@ arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
235 shutdown_ctx->sock = sock; 249 shutdown_ctx->sock = sock;
236 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout); 250 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
237 GNUNET_CLIENT_notify_transmit_ready (sock, 251 GNUNET_CLIENT_notify_transmit_ready (sock,
238 sizeof (struct GNUNET_MessageHeader), 252 sizeof (struct GNUNET_MessageHeader),
239 timeout, GNUNET_NO, &write_shutdown, 253 timeout, GNUNET_NO, &write_shutdown,
240 shutdown_ctx); 254 shutdown_ctx);
241} 255}
242 256
243 257
244static void 258static void
245arm_notify_stop (void *cls, int success) 259arm_notify_stop (void *cls, enum GNUNET_ARM_ProcessStatus status)
246{ 260{
247 GNUNET_assert (success == GNUNET_NO); 261 GNUNET_assert ( (status == GNUNET_ARM_PROCESS_DOWN) ||
262 (status == GNUNET_ARM_PROCESS_ALREADY_DOWN) );
248#if START_ARM 263#if START_ARM
249 GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL); 264 GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
250#endif 265#endif
@@ -256,20 +271,19 @@ kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
256 271
257 272
258static void 273static void
259do_nothing_notify (void *cls, int success) 274do_nothing_notify (void *cls, enum GNUNET_ARM_ProcessStatus status)
260{ 275{
261 GNUNET_assert (success == GNUNET_YES); 276 GNUNET_assert (status == GNUNET_ARM_PROCESS_STARTING);
262 ok = 1; 277 ok = 1;
263 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL); 278 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
264} 279}
265 280
266
267static void 281static void
268arm_notify (void *cls, int success) 282arm_notify (void *cls, enum GNUNET_ARM_ProcessStatus status)
269{ 283{
270 GNUNET_assert (success == GNUNET_YES); 284 GNUNET_assert (status == GNUNET_ARM_PROCESS_STARTING);
271 GNUNET_ARM_start_service (arm, "do-nothing", TIMEOUT, &do_nothing_notify, 285 GNUNET_ARM_start_service (arm, "do-nothing", TIMEOUT, &do_nothing_notify,
272 NULL); 286 NULL);
273} 287}
274 288
275 289
@@ -279,7 +293,8 @@ kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
279 293
280static void 294static void
281do_nothing_restarted_notify_task (void *cls, 295do_nothing_restarted_notify_task (void *cls,
282 const struct GNUNET_SCHEDULER_TaskContext *tc) 296 const struct GNUNET_SCHEDULER_TaskContext
297 *tc)
283{ 298{
284 static char a; 299 static char a;
285 300
@@ -287,17 +302,17 @@ do_nothing_restarted_notify_task (void *cls,
287 302
288#if LOG_BACKOFF 303#if LOG_BACKOFF
289 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 304 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
290 { 305 {
291 fprintf (killLogFilePtr, "%d.Reason is shutdown!\n", trialCount); 306 fprintf (killLogFilePtr, "%d.Reason is shutdown!\n", trialCount);
292 } 307 }
293 else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0) 308 else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
294 { 309 {
295 fprintf (killLogFilePtr, "%d.Reason is timeout!\n", trialCount); 310 fprintf (killLogFilePtr, "%d.Reason is timeout!\n", trialCount);
296 } 311 }
297 else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0) 312 else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0)
298 { 313 {
299 fprintf (killLogFilePtr, "%d.Service is running!\n", trialCount); 314 fprintf (killLogFilePtr, "%d.Service is running!\n", trialCount);
300 } 315 }
301#endif 316#endif
302 GNUNET_SCHEDULER_add_now (&kill_task, &a); 317 GNUNET_SCHEDULER_add_now (&kill_task, &a);
303} 318}
@@ -307,7 +322,7 @@ static void
307do_test (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc) 322do_test (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
308{ 323{
309 GNUNET_CLIENT_service_test ("do-nothing", cfg, TIMEOUT, 324 GNUNET_CLIENT_service_test ("do-nothing", cfg, TIMEOUT,
310 &do_nothing_restarted_notify_task, NULL); 325 &do_nothing_restarted_notify_task, NULL);
311} 326}
312 327
313 328
@@ -326,29 +341,29 @@ kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
326 static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL; 341 static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
327 342
328 if (NULL != cbData) 343 if (NULL != cbData)
329 { 344 {
330 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt); 345 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
331 346
332#if LOG_BACKOFF 347#if LOG_BACKOFF
333 fprintf (killLogFilePtr, "Waited for: %llu ms\n", 348 fprintf (killLogFilePtr, "Waited for: %llu ms\n",
334 (unsigned long long) waitedFor.rel_value); 349 (unsigned long long) waitedFor.rel_value);
335#endif 350#endif
336 } 351 }
337 else 352 else
338 { 353 {
339 waitedFor.rel_value = 0; 354 waitedFor.rel_value = 0;
340 } 355 }
341 /* Connect to the doNothing task */ 356 /* Connect to the doNothing task */
342 doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg); 357 doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
343 GNUNET_assert (doNothingConnection != NULL); 358 GNUNET_assert (doNothingConnection != NULL);
344 if (trialCount == 12) 359 if (trialCount == 12)
345 { 360 {
346 GNUNET_CLIENT_disconnect (doNothingConnection, GNUNET_NO); 361 GNUNET_CLIENT_disconnect (doNothingConnection, GNUNET_NO);
347 GNUNET_ARM_stop_service (arm, "do-nothing", TIMEOUT, &arm_notify_stop, 362 GNUNET_ARM_stop_service (arm, "do-nothing", TIMEOUT, &arm_notify_stop,
348 NULL); 363 NULL);
349 ok = 0; 364 ok = 0;
350 return; 365 return;
351 } 366 }
352 /* Use the created connection to kill the doNothingTask */ 367 /* Use the created connection to kill the doNothingTask */
353 arm_service_shutdown (doNothingConnection, TIMEOUT, &shutdown_cont, NULL); 368 arm_service_shutdown (doNothingConnection, TIMEOUT, &shutdown_cont, NULL);
354} 369}
@@ -363,7 +378,7 @@ task (void *cls, char *const *args, const char *cfgfile,
363 arm = GNUNET_ARM_connect (cfg, NULL); 378 arm = GNUNET_ARM_connect (cfg, NULL);
364#if START_ARM 379#if START_ARM
365 GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify, 380 GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify,
366 NULL); 381 NULL);
367#else 382#else
368 arm_do_nothing (NULL, GNUNET_YES); 383 arm_do_nothing (NULL, GNUNET_YES);
369#endif 384#endif
@@ -387,9 +402,9 @@ check ()
387 402
388 /* Running ARM and running the do_nothing task */ 403 /* Running ARM and running the do_nothing task */
389 GNUNET_assert (GNUNET_OK == 404 GNUNET_assert (GNUNET_OK ==
390 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 405 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
391 argv, "test-exponential-backoff", "nohelp", 406 argv, "test-exponential-backoff",
392 options, &task, NULL)); 407 "nohelp", options, &task, NULL));
393 408
394 409
395 return ok; 410 return ok;
@@ -401,12 +416,12 @@ init ()
401#if LOG_BACKOFF 416#if LOG_BACKOFF
402 killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log"); 417 killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
403 if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w"))) 418 if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
404 { 419 {
405 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", 420 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
406 killLogFileName); 421 killLogFileName);
407 GNUNET_free (killLogFileName); 422 GNUNET_free (killLogFileName);
408 return GNUNET_SYSERR; 423 return GNUNET_SYSERR;
409 } 424 }
410#endif 425#endif
411 return GNUNET_OK; 426 return GNUNET_OK;
412} 427}
@@ -429,11 +444,11 @@ main (int argc, char *argv[])
429 444
430 GNUNET_log_setup ("test-exponential-backoff", 445 GNUNET_log_setup ("test-exponential-backoff",
431#if VERBOSE 446#if VERBOSE
432 "DEBUG", 447 "DEBUG",
433#else 448#else
434 "WARNING", 449 "WARNING",
435#endif 450#endif
436 NULL); 451 NULL);
437 452
438 init (); 453 init ();
439 ret = check (); 454 ret = check ();