-code cleanup
authorChristian Grothoff <christian@grothoff.org>
Sat, 14 Jul 2012 18:49:49 +0000 (18:49 +0000)
committerChristian Grothoff <christian@grothoff.org>
Sat, 14 Jul 2012 18:49:49 +0000 (18:49 +0000)
src/testbed/gnunet-testbed-helper.c
src/testbed/test_gnunet_testbed_helper.c

index 371ac8951dbd93a2a0e9df1ad55c6c62d48e7006..edef3a52cd373ae6e6727b79b3cfbe62e32d2c2b 100644 (file)
  *          should the connection from the remote controller is dropped
  * @author Sree Harsha Totakura <sreeharsha@totakura.in> 
  */
-
-
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_testing_lib-new.h"
-
 #include "testbed_helper.h"
 
 
@@ -59,25 +56,16 @@ struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
  */
 static struct GNUNET_DISK_FileHandle *stdin_fd;
 
-/**
- * Message receive buffer
- */
-static void *buf;
-
-/**
- * The size of the above buffer
- */
-static size_t buf_size;
-
 /**
  * Task identifier for the read task
  */
 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
 
 /**
- * Task identifier for the shutdown task
+ * Are we done reading messages from stdin?
  */
-static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
+static int done_reading;
+
 
 /**
  * Task to shutting down nicely
@@ -89,12 +77,18 @@ static void
 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   if (GNUNET_SCHEDULER_NO_TASK != read_task_id)
+  {
     GNUNET_SCHEDULER_cancel (read_task_id);
+    read_task_id = GNUNET_SCHEDULER_NO_TASK;
+  }
   (void) GNUNET_DISK_file_close (stdin_fd);
-  GNUNET_free_non_null (buf);  
   GNUNET_SERVER_mst_destroy (tokenizer);  
+  tokenizer = NULL;
   if (NULL != test_system)
+  {
     GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
+    test_system = NULL;
+  }
 }
 
 
@@ -115,6 +109,8 @@ tokenizer_cb (void *cls, void *client,
               const struct GNUNET_MessageHeader *message)
 {
   GNUNET_break (0);
+  // FIXME: write config & start gnunet-service-testbed
+  done_reading = GNUNET_YES;
   return GNUNET_OK;
 }
 
@@ -128,35 +124,38 @@ tokenizer_cb (void *cls, void *client,
 static void
 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
   ssize_t sread;
-  static int ignore_reading = GNUNET_NO;
-  int ret;
 
   read_task_id = GNUNET_SCHEDULER_NO_TASK;
-  if (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)
+  if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
     return;  
-  sread = GNUNET_DISK_file_read (stdin_fd, buf, buf_size);
+  sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
   if (GNUNET_SYSERR == sread)
   {
     GNUNET_break (0);           /* FIXME: stdin closed - kill child */
-    GNUNET_SCHEDULER_cancel (shutdown_task_id);
-    shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  if (GNUNET_YES == done_reading)
+  {
+    /* didn't expect any more data! */
+    GNUNET_break (0);
+    GNUNET_SCHEDULER_shutdown ();
     return;
   }
   LOG_DEBUG ("Read %u bytes\n", sread);
+  if (GNUNET_OK !=
+      GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread,
+                                GNUNET_NO, GNUNET_NO))
+  {
+    GNUNET_break (0);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
   read_task_id =                /* No timeout while reading */
     GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
                                     stdin_fd, &read_task, NULL);
-  if (GNUNET_YES == ignore_reading)
-    return;
-  ret = GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread,
-                                   GNUNET_NO, GNUNET_NO);
-  GNUNET_assert (GNUNET_SYSERR != ret);
-  if (GNUNET_NO == ret)
-  {
-    LOG_DEBUG ("We only listen for 1 message -- ignoring others\n");
-    ignore_reading = GNUNET_YES;
-  }
 }
 
 
@@ -175,14 +174,11 @@ run (void *cls, char *const *args, const char *cfgfile,
   LOG_DEBUG ("Starting testbed helper...\n");
   tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
   stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
-  buf_size = sizeof (struct GNUNET_TESTBED_HelperInit) + 8 * 1024;
-  buf = GNUNET_malloc (buf_size);
-  read_task_id =                /* No timeout while reading */
+  read_task_id =
     GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
                                     stdin_fd, &read_task, NULL);
-  shutdown_task_id = 
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                  &shutdown_task, NULL);
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
+                               &shutdown_task, NULL);
 }
 
 
@@ -195,14 +191,15 @@ run (void *cls, char *const *args, const char *cfgfile,
  */
 int main (int argc, char **argv)
 {
-   struct GNUNET_GETOPT_CommandLineOption options[] = {
+  struct GNUNET_GETOPT_CommandLineOption options[] = {
     GNUNET_GETOPT_OPTION_END
   };
-
-   if (GNUNET_OK != 
-       GNUNET_PROGRAM_run ( argc, argv, "gnunet-testbed-helper",
-                            "Helper for starting gnunet-service-testbed",
-                            options, &run, NULL))
-     return 1;
-  else return 0;
+  if (GNUNET_OK != 
+      GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-helper",
+                         "Helper for starting gnunet-service-testbed",
+                         options, &run, NULL))
+    return 1;
+  return 0;
 }
+
+/* end of gnunet-testbed-helper.c */
index b41410ce9ed8737479eddf049ada2bf097b92395..30f562bad5a49fdbff82850fd208b4e8f5caa771 100644 (file)
@@ -45,7 +45,6 @@ static struct GNUNET_HELPER_Handle *helper;
  */
 static struct GNUNET_TESTBED_HelperInit msg;
 
-
 /**
  * Message send handle
  */
@@ -115,7 +114,6 @@ cont_cb (void *cls, int result)
 }
 
 
-
 /**
  * Main function that will be run.
  *
@@ -128,12 +126,12 @@ static void
 run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle * cfg)
 {
-  char * const binary_argv[] = {
+  static char * const binary_argv[] = {
     "gnunet-testbed-helper",
     NULL
     };
-
-  helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv,
+  helper = GNUNET_HELPER_start ("gnunet-testbed-helper", 
+                               binary_argv,
                                 NULL, NULL);
   GNUNET_assert (NULL != helper);
   msg.header.size = htons (sizeof (struct GNUNET_TESTBED_HelperInit));
@@ -141,8 +139,8 @@ run (void *cls, char *const *args, const char *cfgfile,
   msg.cname_size = htons (0);
   msg.config_size = htons (0);
   shandle = GNUNET_HELPER_send (helper,
-                                (const struct GNUNET_MessageHeader *) &msg,
-                                GNUNET_NO, cont_cb, NULL);
+                                &msg.header,
+                                GNUNET_NO, &cont_cb, NULL);
   GNUNET_assert (NULL != shandle);
   abort_task = GNUNET_SCHEDULER_add_delayed 
     (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1), &do_abort, NULL);
@@ -162,10 +160,12 @@ int main (int argc, char **argv)
     GNUNET_GETOPT_OPTION_END
   };
 
-   if (GNUNET_OK != 
-       GNUNET_PROGRAM_run (argc, argv, "test_gnunet_testbed_helper",
-                           "Testcase for testing gnunet-testbed-helper.c",
-                           options, &run, NULL))
-     return 1;
-  else return 0;
+  if (GNUNET_OK != 
+      GNUNET_PROGRAM_run (argc, argv, "test_gnunet_testbed_helper",
+                         "Testcase for testing gnunet-testbed-helper.c",
+                         options, &run, NULL))
+    return 1;
+  return 0;
 }
+
+/* end of test_gnunet_testbed_helper.c */