cleaner handling of gnunet-peerinfo results
[oweals/gnunet.git] / src / testing / testing.c
index 6d75be6a5a635a5c9a6ee8184d8af22bb60a472d..adcc06193284c9442093d0986bf433132abdd445 100644 (file)
@@ -173,7 +173,8 @@ testing_init (void *cls,
  * @param tc unused
  */
 static void
-start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+start_fsm (void *cls, 
+          const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_TESTING_Daemon *d = cls;
   GNUNET_TESTING_NotifyDaemonRunning cb;
@@ -181,8 +182,6 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   unsigned long code;
   char *dst;
   int bytes_read;
-  static char hostkeybuf[105];
-  static const char temphostkey[104];
 
 #if DEBUG_TESTING
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -305,76 +304,71 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 #endif
       d->phase = SP_HOSTKEY_CREATE;
       d->task
-        = GNUNET_SCHEDULER_add_delayed (d->sched,
-                                        GNUNET_CONSTANTS_EXEC_WAIT,
-                                        &start_fsm, d);
+       = GNUNET_SCHEDULER_add_read_file (d->sched,
+                                         GNUNET_TIME_absolute_get_remaining(d->max_timeout),
+                                         GNUNET_DISK_pipe_handle(d->pipe_stdout, 
+                                                                 GNUNET_DISK_PIPE_END_READ),
+                                         &start_fsm, 
+                                         d);
       break;
     case SP_HOSTKEY_CREATE:
-
-      bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout, GNUNET_DISK_PIPE_END_READ), &hostkeybuf, sizeof(hostkeybuf));
-      if (bytes_read == 104) /* Success, we have read in the hostkey */
-        {
-          if (hostkeybuf[103] == '\n')
-            hostkeybuf[103] = '\0';
-          else
-            GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Malformed output from gnunet-peerinfo!\n");
-          memcpy(&temphostkey, &hostkeybuf, bytes_read);
-
-          if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (&temphostkey[0],
-                                                           &d->id.hashPubKey))
-            {
-              GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to peer identity!\n");
-            }
-          else
-            {
-              GNUNET_DISK_pipe_close(d->pipe_stdout);
-              d->pipe_stdout = NULL;
-            }
-        }
-
-      if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
-        {
-          if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
-            {
-              cb = d->cb;
-              d->cb = NULL;
-              if (NULL != cb)
-                cb (d->cb_cls,
-                    NULL,
-                    d->cfg,
-                    d,
-                    (NULL == d->hostname)
-                    ? _("`gnunet-peerinfo' does not seem to terminate.\n")
-                    : _("`ssh' does not seem to terminate.\n"));
-
-              GNUNET_DISK_pipe_close(d->pipe_stdout);
-              return;
-            }
-          /* wait some more */
+      bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout, 
+                                                                GNUNET_DISK_PIPE_END_READ),
+                                        &d->hostkeybuf[d->hostkeybufpos], 
+                                        sizeof(d->hostkeybuf) - d->hostkeybufpos);
+      if (bytes_read > 0)
+       d->hostkeybufpos += bytes_read;      
+
+      if ( (d->hostkeybufpos < 104) &&
+          (bytes_read > 0) )
+       {
+         /* keep reading */
           d->task
-            = GNUNET_SCHEDULER_add_delayed (d->sched,
-                                            GNUNET_CONSTANTS_EXEC_WAIT,
-                                            &start_fsm, d);
+            = GNUNET_SCHEDULER_add_read_file (d->sched,
+                                             GNUNET_TIME_absolute_get_remaining(d->max_timeout),
+                                             GNUNET_DISK_pipe_handle(d->pipe_stdout, 
+                                                                     GNUNET_DISK_PIPE_END_READ),
+                                             &start_fsm, 
+                                             d);
           return;
-        }
-#if DEBUG_TESTING
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Successfully got hostkey!\n");
-#endif
-      if (d->pipe_stdout != NULL)
-        {
+       }
+      d->hostkeybuf[103] = '\0';
+      if ( (bytes_read < 0) ||
+          (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
+                                                        &d->id.hashPubKey)) )
+       {
+         /* error */
+         if (bytes_read < 0)
+           GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
+                      _("Error reading from gnunet-peerinfo: %s\n"),
+                      STRERROR (errno));
+         else
+           GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
+                      _("Malformed output from gnunet-peerinfo!\n"));
           cb = d->cb;
           d->cb = NULL;
+          GNUNET_DISK_pipe_close(d->pipe_stdout);
+         d->pipe_stdout = NULL;
+         (void) PLIBC_KILL (d->pid, SIGKILL);
+         GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
+         d->pid = 0;
           if (NULL != cb)
             cb (d->cb_cls,
                 NULL,
                 d->cfg,
                 d,
                 _("`Failed to get hostkey!\n"));
-          GNUNET_DISK_pipe_close(d->pipe_stdout);
-          return;
-        }
-
+         return;
+       } 
+      GNUNET_DISK_pipe_close(d->pipe_stdout);
+      d->pipe_stdout = NULL;
+      (void) PLIBC_KILL (d->pid, SIGKILL);
+      GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
+      d->pid = 0;
+#if DEBUG_TESTING
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Successfully got hostkey!\n");
+#endif
       if (d->hostkey_callback != NULL)
         {
           d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
@@ -384,7 +378,6 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
         {
           d->phase = SP_TOPOLOGY_SETUP;
         }
-
       /* Fall through */
     case SP_HOSTKEY_CREATED:
       /* wait for topology finished */
@@ -544,7 +537,6 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
               return;
             }
           /* wait some more */
-          fprintf(stderr, "scheduling in shutdown_start\n");
           d->task
             = GNUNET_SCHEDULER_add_delayed (d->sched,
                                             GNUNET_CONSTANTS_EXEC_WAIT,
@@ -1020,7 +1012,6 @@ GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
 
   GNUNET_free_non_null(del_arg);
   d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
-  fprintf(stderr, "scheduling shutdown fsm phase\n");
   d->task
     = GNUNET_SCHEDULER_add_now (d->sched,
                                 &start_fsm, d);
@@ -1197,18 +1188,12 @@ notify_connect_result (void *cls,
   struct ConnectContext *ctx = cls;
   struct GNUNET_TIME_Relative remaining;
 
+  ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
     {
       GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
       ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
     }
-
-  if ((ctx->timeout_task != GNUNET_SCHEDULER_NO_TASK) && (tc->reason != GNUNET_SCHEDULER_REASON_TIMEOUT))
-    {
-      GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
-      ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
-    }
-
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     {
       if (ctx->d2th != NULL)
@@ -1285,10 +1270,9 @@ connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNE
     {
       ctx->connected = GNUNET_YES;
       GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
-      ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
-      GNUNET_SCHEDULER_add_now (ctx->d1->sched,
-                                &notify_connect_result,
-                                ctx);
+      ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
+                                                   &notify_connect_result,
+                                                   ctx);
     }
 
 }
@@ -1298,9 +1282,9 @@ send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct ConnectContext *ctx = cls;
 
+  ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     return;
-
   if (ctx->d1->hello != NULL)
     {
       GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
@@ -1397,7 +1381,8 @@ GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
     }
 
   ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
-                                                    GNUNET_TIME_relative_divide(ctx->relative_timeout, max_connect_attempts), /* Allow up to 8 reconnect attempts */
+                                                    GNUNET_TIME_relative_divide(ctx->relative_timeout, 
+                                                                               max_connect_attempts), 
                                                     &notify_connect_result, ctx);
 
   ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);