diff options
author | LRN <lrn1986@gmail.com> | 2013-03-13 17:49:26 +0000 |
---|---|---|
committer | LRN <lrn1986@gmail.com> | 2013-03-13 17:49:26 +0000 |
commit | 405f776bc08486af4edb80e18149c0829732b347 (patch) | |
tree | d5fc635a51641dec6b53cb2540276f34ae8f6210 /src/arm/test_exponential_backoff.c | |
parent | 3ceae682287492ecc768aea5c4c463216a35774d (diff) |
All-encompassing ARM update
Diffstat (limited to 'src/arm/test_exponential_backoff.c')
-rw-r--r-- | src/arm/test_exponential_backoff.c | 265 |
1 files changed, 128 insertions, 137 deletions
diff --git a/src/arm/test_exponential_backoff.c b/src/arm/test_exponential_backoff.c index 3395139eac..8a0dba1171 100644 --- a/src/arm/test_exponential_backoff.c +++ b/src/arm/test_exponential_backoff.c @@ -43,14 +43,20 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg; static struct GNUNET_ARM_Handle *arm; +static struct GNUNET_ARM_MonitorHandle *mon; + static int ok = 1; +static int phase = 0; + static int trialCount; static struct GNUNET_TIME_Absolute startedWaitingAt; struct GNUNET_TIME_Relative waitedFor; +struct GNUNET_TIME_Relative waitedFor_prev; + #if LOG_BACKOFF static FILE *killLogFilePtr; @@ -97,11 +103,8 @@ struct ShutdownContext /** * Handler receiving response to service shutdown requests. - * First call with NULL: service misbehaving, or something. - * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN: - * - service will shutdown - * Second call with NULL: - * - service has now really shut down. + * We expect it to be called with NULL, since the service that + * we are shutting down will just die without replying. * * @param cls closure * @param msg NULL, indicating socket closure. @@ -111,7 +114,7 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg) { struct ShutdownContext *shutdown_ctx = cls; - if (msg == NULL) + if (NULL == msg) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n"); if (shutdown_ctx->cont != NULL) @@ -122,29 +125,7 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg) GNUNET_free (shutdown_ctx); return; } - GNUNET_assert (ntohs (msg->size) == - sizeof (struct GNUNET_MessageHeader)); - switch (ntohs (msg->type)) - { - case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN: - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received confirmation for service shutdown.\n"); - shutdown_ctx->confirmed = GNUNET_YES; - GNUNET_CLIENT_receive (shutdown_ctx->sock, - &service_shutdown_handler, shutdown_ctx, - GNUNET_TIME_UNIT_FOREVER_REL); - break; - default: /* Fall through */ - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Service shutdown refused!\n"); - if (shutdown_ctx->cont != NULL) - shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES); - - GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task); - GNUNET_CLIENT_disconnect (shutdown_ctx->sock); - GNUNET_free (shutdown_ctx); - break; - } + GNUNET_assert (0); } @@ -183,25 +164,27 @@ write_shutdown (void *cls, size_t size, void *buf) struct ShutdownContext *shutdown_ctx = cls; if (size < sizeof (struct GNUNET_MessageHeader)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - _("Failed to transmit shutdown request to client.\n")); - shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR); - GNUNET_CLIENT_disconnect (shutdown_ctx->sock); - GNUNET_free (shutdown_ctx); - return 0; /* client disconnected */ - } + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + _("Failed to transmit shutdown request to client.\n")); + FPRINTF (stderr, "%s", "Failed to send a shutdown request\n"); + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR); + GNUNET_CLIENT_disconnect (shutdown_ctx->sock); + GNUNET_free (shutdown_ctx); + return 0; /* client disconnected */ + } GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler, shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL); - shutdown_ctx->cancel_task = - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining - (shutdown_ctx->timeout), - &service_shutdown_cancel, shutdown_ctx); + shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed ( + GNUNET_TIME_absolute_get_remaining (shutdown_ctx->timeout), + &service_shutdown_cancel, shutdown_ctx); msg = (struct GNUNET_MessageHeader *) buf; - msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN); + msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP); msg->size = htons (sizeof (struct GNUNET_MessageHeader)); - return sizeof (struct GNUNET_MessageHeader); + strcpy ((char *) &msg[1], "do-nothing"); + FPRINTF (stderr, "%s", "Sent a shutdown request\n"); + return sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1; } @@ -219,7 +202,7 @@ write_shutdown (void *cls, size_t size, void *buf) * */ static void -arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock, +do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock, struct GNUNET_TIME_Relative timeout, GNUNET_CLIENT_ShutdownTask cont, void *cont_cls) { @@ -231,125 +214,132 @@ arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock, shutdown_ctx->sock = sock; shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout); GNUNET_CLIENT_notify_transmit_ready (sock, - sizeof (struct GNUNET_MessageHeader), + sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1, timeout, GNUNET_NO, &write_shutdown, shutdown_ctx); } - -static void -arm_notify_stop (void *cls, enum GNUNET_ARM_ProcessStatus status) -{ - GNUNET_assert ( (status == GNUNET_ARM_PROCESS_DOWN) || - (status == GNUNET_ARM_PROCESS_ALREADY_DOWN) ); -#if START_ARM - GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL); -#endif -} - - static void kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc); - static void -do_nothing_notify (void *cls, enum GNUNET_ARM_ProcessStatus status) -{ - GNUNET_assert (status == GNUNET_ARM_PROCESS_STARTING); - ok = 1; - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL); -} - -static void -arm_notify (void *cls, enum GNUNET_ARM_ProcessStatus status) +shutdown_cont (void *cls, int reason) { - GNUNET_assert (status == GNUNET_ARM_PROCESS_STARTING); - GNUNET_ARM_start_service (arm, "do-nothing", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, &do_nothing_notify, - NULL); + if (GNUNET_NO != reason) + { + /* Re-try shutdown */ + FPRINTF (stderr, "%s", "do-nothing didn't die, trying again\n"); + GNUNET_SCHEDULER_add_now (kill_task, NULL); + return; + } + startedWaitingAt = GNUNET_TIME_absolute_get (); + FPRINTF (stderr, "%s", "do-nothing is dead, starting the countdown\n"); } - static void -kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc); - - -static void -do_nothing_restarted_notify_task (void *cls, - const struct GNUNET_SCHEDULER_TaskContext - *tc) +kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc) { - static char a; - - trialCount++; + static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL; + if (NULL != cbData) + { + waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt); + FPRINTF (stderr, "Waited for: %llu ms\n", waitedFor.rel_value); #if LOG_BACKOFF - if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) - { - FPRINTF (killLogFilePtr, "%d.Reason is shutdown!\n", trialCount); - } - else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0) - { - FPRINTF (killLogFilePtr, "%d.Reason is timeout!\n", trialCount); - } - else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0) - { - FPRINTF (killLogFilePtr, "%d.Service is running!\n", trialCount); - } + FPRINTF (killLogFilePtr, "Waited for: %llu ms\n", + (unsigned long long) waitedFor.rel_value); #endif - GNUNET_SCHEDULER_add_now (&kill_task, &a); + } + else + { + waitedFor.rel_value = 0; + } + /* Connect to the doNothing task */ + doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg); + GNUNET_assert (doNothingConnection != NULL); + if (trialCount == 12) + waitedFor_prev = waitedFor; + else if (trialCount == 13) + { + GNUNET_CLIENT_disconnect (doNothingConnection); + GNUNET_ARM_request_service_stop (arm, "do-nothing", TIMEOUT, NULL, NULL); + if (waitedFor_prev.rel_value >= waitedFor.rel_value) + ok = 9; + else + ok = 0; + trialCount += 1; + return; + } + trialCount += 1; + /* Use the created connection to kill the doNothingTask */ + do_nothing_service_shutdown (doNothingConnection, + TIMEOUT, &shutdown_cont, NULL); } - static void -do_test (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc) +trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { - GNUNET_CLIENT_service_test ("do-nothing", cfg, TIMEOUT, - &do_nothing_restarted_notify_task, NULL); + GNUNET_ARM_disconnect (arm); + GNUNET_ARM_monitor_disconnect (mon); } static void -shutdown_cont (void *cls, int reason) +arm_stop_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result) { - trialCount++; - startedWaitingAt = GNUNET_TIME_absolute_get (); - GNUNET_SCHEDULER_add_delayed (waitedFor, &do_test, NULL); + GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK); + GNUNET_break (result == GNUNET_ARM_RESULT_STOPPING); + FPRINTF (stderr, "%s", "ARM service stopped\n"); + GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL); } - -static void -kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc) +void +srv_status (void *cls, struct GNUNET_ARM_MonitorHandle *mon, const char *service, enum GNUNET_ARM_ServiceStatus status) { - static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL; - - if (NULL != cbData) - { - waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt); - -#if LOG_BACKOFF - FPRINTF (killLogFilePtr, "Waited for: %llu ms\n", - (unsigned long long) waitedFor.rel_value); -#endif - } - else + FPRINTF (stderr, "Service %s is %u, phase %u\n", service, status, phase); + if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED) + { + phase++; + GNUNET_ARM_request_service_start (arm, "do-nothing", + GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, NULL, NULL); + return; + } + if (phase == 1) + { + GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING); + GNUNET_break (0 == strcasecmp (service, "do-nothing")); + GNUNET_break (phase == 1); + FPRINTF (stderr, "%s", "do-nothing is starting\n"); + phase++; + ok = 1; + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL); + } + else if ((phase == 2) && (strcasecmp ("do-nothing", service) == 0)) + { + /* We passively monitor ARM for status updates. ARM should tell us + * when do-nothing dies (no need to run a service upness test ourselves). + */ + if (status == GNUNET_ARM_SERVICE_STARTING) { - waitedFor.rel_value = 0; + FPRINTF (stderr, "%s", "do-nothing is starting\n"); + GNUNET_SCHEDULER_add_now (kill_task, &ok); } - /* Connect to the doNothing task */ - doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg); - GNUNET_assert (doNothingConnection != NULL); - if (trialCount == 12) + else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14)) { - GNUNET_CLIENT_disconnect (doNothingConnection); - GNUNET_ARM_stop_service (arm, "do-nothing", TIMEOUT, &arm_notify_stop, - NULL); - ok = 0; - return; + phase++; + GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL); } - /* Use the created connection to kill the doNothingTask */ - arm_service_shutdown (doNothingConnection, TIMEOUT, &shutdown_cont, NULL); + } } +static void +arm_start_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result) +{ + GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK); + GNUNET_break (result == GNUNET_ARM_RESULT_STARTING); + GNUNET_break (phase == 0); + FPRINTF (stderr, "Sent 'START' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully"); +} static void task (void *cls, char *const *args, const char *cfgfile, @@ -359,9 +349,8 @@ task (void *cls, char *const *args, const char *cfgfile, cfg = c; if (NULL != cfgfile) { - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (cfg, "arm", "CONFIG", - &armconfig)) + if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "arm", + "CONFIG", &armconfig)) { GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle *) cfg, "arm", "CONFIG", @@ -371,16 +360,18 @@ task (void *cls, char *const *args, const char *cfgfile, GNUNET_free (armconfig); } - arm = GNUNET_ARM_connect (cfg, NULL); + arm = GNUNET_ARM_alloc (cfg); + GNUNET_ARM_connect (arm, NULL, NULL); + mon = GNUNET_ARM_monitor_alloc (cfg); + GNUNET_ARM_monitor (mon, srv_status, NULL); #if START_ARM - GNUNET_ARM_start_service (arm, "arm", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, &arm_notify, - NULL); + GNUNET_ARM_request_service_start (arm, "arm", + GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL); #else - arm_do_nothing (NULL, GNUNET_YES); + arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_SERVICE_STARTING); #endif } - static int check () { |