-improving shutdown behavior
authorChristian Grothoff <christian@grothoff.org>
Thu, 6 Dec 2012 13:53:33 +0000 (13:53 +0000)
committerChristian Grothoff <christian@grothoff.org>
Thu, 6 Dec 2012 13:53:33 +0000 (13:53 +0000)
src/arm/gnunet-arm.c
src/consensus/gnunet-consensus.c
src/util/client.c
src/util/speedup.c

index a37a78a26a2a61d12c0b8c6ddd8e155d37f2a8f4..ca4193837e2fa7cae82667d3bb7fd7a70c62b76a 100644 (file)
@@ -128,12 +128,12 @@ static struct GNUNET_TIME_Relative timeout;
 /**
  * Do we want to give our stdout to gnunet-service-arm?
  */
-static unsigned int no_stdout = 0;
+static unsigned int no_stdout;
 
 /**
  * Do we want to give our stderr to gnunet-service-arm?
  */
-static unsigned int no_stderr = 0;
+static unsigned int no_stderr;
 
 
 /**
@@ -206,8 +206,7 @@ confirm_cb (void *cls,
     FPRINTF (stderr, "%s",  _("Unknown response code from ARM.\n"));
     break;
   }
-  GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
-                                    GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+  GNUNET_SCHEDULER_add_now (&cps_loop, NULL);
 }
 
 
@@ -236,6 +235,50 @@ list_cb (void *cls, int result, unsigned int count, const char *const*list)
 }
 
 
+/**
+ * Attempts to delete configuration file and SERVICEHOME
+ * on arm shutdown provided the end and delete options
+ * were specified when gnunet-arm was run.
+ */
+static void
+delete_files ()
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Will attempt to remove configuration file %s and service directory %s\n",
+             config_file, dir);
+
+  if (UNLINK (config_file) != 0)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 _("Failed to remove configuration file %s\n"), config_file);
+    }
+
+  if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 _("Failed to remove servicehome directory %s\n"), dir);
+
+    }
+}
+
+
+/**
+ * Main continuation-passing-style loop.  Runs the various
+ * jobs that we've been asked to do in order.
+ *
+ * @param cls closure, unused
+ * @param tc context, unused
+ */
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_ARM_disconnect (h);
+  h = NULL;
+  if ((end == GNUNET_YES) && (delete == GNUNET_YES))
+    delete_files ();   
+}
+
+
 /**
  * Main function that will be run by the scheduler.
  *
@@ -264,35 +307,9 @@ run (void *cls, char *const *args, const char *cfgfile,
     ret = 1;
     return;
   }
-  GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
-                                    GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-}
-
-
-/**
- * Attempts to delete configuration file and SERVICEHOME
- * on arm shutdown provided the end and delete options
- * were specified when gnunet-arm was run.
- */
-static void
-delete_files ()
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Will attempt to remove configuration file %s and service directory %s\n",
-             config_file, dir);
-
-  if (UNLINK (config_file) != 0)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Failed to remove configuration file %s\n"), config_file);
-    }
-
-  if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Failed to remove servicehome directory %s\n"), dir);
-
-    }
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
+                               &shutdown_task, NULL);
+  GNUNET_SCHEDULER_add_now (&cps_loop, NULL);
 }
 
 
@@ -306,6 +323,10 @@ delete_files ()
 static void
 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  if (NULL == h)
+    return;
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
   while (1)
   {
     switch (phase++)
@@ -393,9 +414,7 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       }
       /* Fall through */
     default:           /* last phase */
-      GNUNET_ARM_disconnect (h);
-      if ((end == GNUNET_YES) && (delete == GNUNET_YES))
-       delete_files ();
+      GNUNET_SCHEDULER_shutdown ();
       return;
     }
   }
index 12d0965e94ac8e52b7f1683bbf4c2615722caee8..bc518657e311032028131d8fddb05c69c3ab0d51 100644 (file)
@@ -48,12 +48,6 @@ static struct GNUNET_DISK_FileHandle *stdin_fh;
  */
 static GNUNET_SCHEDULER_TaskIdentifier stdin_tid = GNUNET_SCHEDULER_NO_TASK;
 
-/**
- * Element currently being sent to the service
- */
-static struct GNUNET_CONSENSUS_Element *element;
-
-
 
 static void
 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
@@ -76,23 +70,22 @@ conclude_cb (void *cls,
 }
 
 
-
 static void
 insert_done_cb (void *cls,
                 int success)
 {
+  struct GNUNET_CONSENSUS_Element *element = cls;
+
+  GNUNET_free (element);
   if (GNUNET_YES != success)
   {
     printf ("insert failed\n");
     GNUNET_SCHEDULER_shutdown ();
+    return;
   }
-
-  GNUNET_free (element);
-
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == stdin_tid);
-
   stdin_tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fh,
-                                        &stdin_cb, NULL);    
+                                             &stdin_cb, NULL);    
 }
 
 
@@ -107,10 +100,12 @@ stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   char buf[1024];
   char *ret;
-  ret = fgets (buf, 1024, stdin);
+  struct GNUNET_CONSENSUS_Element *element;
 
   stdin_tid = GNUNET_SCHEDULER_NO_TASK;
-
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return; /* we're done here */
+  ret = fgets (buf, 1024, stdin);
   if (NULL == ret)
   {
     if (feof (stdin))
@@ -118,10 +113,6 @@ stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       printf ("concluding ...\n");
       GNUNET_CONSENSUS_conclude (consensus, GNUNET_TIME_UNIT_FOREVER_REL, conclude_cb, NULL);
     }
-    else
-    {
-      GNUNET_SCHEDULER_shutdown ();
-    }
     return;
   }
 
@@ -131,11 +122,11 @@ stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   element->type = 0;
   element->size = strlen(buf) + 1;
   element->data = &element[1];
-  strcpy((char *) &element[1], buf);
-
-  GNUNET_CONSENSUS_insert (consensus, element, insert_done_cb, NULL);
+  strcpy ((char *) &element[1], buf);
+  GNUNET_CONSENSUS_insert (consensus, element, &insert_done_cb, element); 
 }
 
+
 /**
  * Called when a new element was received from another peer, or an error occured.
  *
@@ -157,6 +148,7 @@ cb (void *cls,
   return GNUNET_YES;
 }
 
+
 /**
  * Function run on shutdown to clean up.
  *
@@ -166,14 +158,12 @@ cb (void *cls,
 static void
 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "shutting down\n");
-  if (NULL == consensus)
+  if (NULL != consensus)
   {
-    return;
+    GNUNET_CONSENSUS_destroy (consensus);
+    consensus = NULL;
   }
-
-  GNUNET_CONSENSUS_destroy (consensus);
 }
 
 
index 81c696a7b679bc7856a81ea4f415272640681d08..69380c9b03ae1233f60d8c7d85b73cfc38c8d643 100644 (file)
@@ -943,8 +943,16 @@ client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_CLIENT_TransmitHandle *th = cls;
   struct GNUNET_TIME_Relative delay;
-
+  
   th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+  {
+    /* give up, was shutdown */
+    th->client->th = NULL;
+    th->notify (th->notify_cls, 0, NULL);
+    GNUNET_free (th);
+    return;
+  }
   th->client->connection =
       do_connect (th->client->service_name, th->client->cfg, th->client->attempts++);
   th->client->first_message = GNUNET_YES;
@@ -1006,7 +1014,8 @@ client_notify (void *cls, size_t size, void *buf)
     delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
     delay.rel_value /= 2;
     if ((GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
-        (delay.rel_value < 1))
+        (delay.rel_value < 1)||
+       (0 != (GNUNET_SCHEDULER_get_reason() & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG,
            "Transmission failed %u times, giving up.\n",
index 0a005c035c6ca9adb7e0f728f37f2ff2a377b93f..e5f71d063b2779927b72337dedd72f02f8d3844f 100644 (file)
@@ -83,8 +83,9 @@ GNUNET_SPEEDUP_stop_ ( )
     GNUNET_SCHEDULER_cancel (speedup_task);
     speedup_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Stopped execution speed up\n");
+  if ((0 != interval.rel_value) && (0 != delta.rel_value))
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "Stopped execution speed up\n");
 }