load hosts from file, implementation in testing
authorNathan S. Evans <evans@in.tum.de>
Tue, 26 Jul 2011 12:39:23 +0000 (12:39 +0000)
committerNathan S. Evans <evans@in.tum.de>
Tue, 26 Jul 2011 12:39:23 +0000 (12:39 +0000)
src/include/gnunet_testing_lib.h
src/nse/nse-profiler.c
src/testing/testing_group.c

index 34920bbab38d0aef3b8d5add5b81da02caade4f2..0b998cf9599ed0cf77aad8260234de120f593dba 100644 (file)
@@ -602,31 +602,17 @@ GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
                                     struct GNUNET_TIME_Relative timeout,
                                     GNUNET_TESTING_NotifyCompletion cb, void *cb_cls);
 
-
 /**
- * Establish a connection between two GNUnet daemons.
+ * Read a testing hosts file based on a configuration.
+ * Returns a DLL of hosts (caller must free!) on success
+ * or NULL on failure.
  *
- * @param d1 handle for the first daemon
- * @param d2 handle for the second daemon
- * @param timeout how long is the connection attempt
- *        allowed to take?
- * @param max_connect_attempts how many times should we try to reconnect
- *        (within timeout)
- * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
- *                   the HELLO has already been exchanged
- * @param cb function to call at the end
- * @param cb_cls closure for cb
+ * @param cfg a configuration with a testing section
+ *
+ * @return DLL of hosts on success, NULL on failure
  */
-void
-GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
-                                struct GNUNET_TESTING_Daemon *d2,
-                                struct GNUNET_TIME_Relative timeout,
-                                unsigned int max_connect_attempts,
-                                int send_hello,
-                                GNUNET_TESTING_NotifyConnection cb,
-                                void *cb_cls);
-
-
+struct GNUNET_TESTING_Host *
+GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg);
 
 
 /**
@@ -679,6 +665,31 @@ GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
 void
 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg);
 
+
+/**
+ * Establish a connection between two GNUnet daemons.
+ *
+ * @param d1 handle for the first daemon
+ * @param d2 handle for the second daemon
+ * @param timeout how long is the connection attempt
+ *        allowed to take?
+ * @param max_connect_attempts how many times should we try to reconnect
+ *        (within timeout)
+ * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
+ *                   the HELLO has already been exchanged
+ * @param cb function to call at the end
+ * @param cb_cls closure for cb
+ */
+void
+GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
+                                struct GNUNET_TESTING_Daemon *d2,
+                                struct GNUNET_TIME_Relative timeout,
+                                unsigned int max_connect_attempts,
+                                int send_hello,
+                                GNUNET_TESTING_NotifyConnection cb,
+                                void *cb_cls);
+
+
 /**
  * Restart all peers in the given group.
  *
index 8d90ca26045acef4a27af20609505a1a0f46c117..3194979a5dbae01c44bd01cd1f55bac66da29149 100644 (file)
@@ -580,6 +580,7 @@ run (void *cls,
 {
   char *temp_str;
   unsigned long long temp_wait;
+  struct GNUNET_TESTING_Host *hosts;
 
   ok = 1;
   testing_cfg = GNUNET_CONFIGURATION_create();
@@ -673,12 +674,14 @@ run (void *cls,
     }
   GNUNET_free_non_null(temp_str);
 
+  hosts = GNUNET_TESTING_hosts_load (testing_cfg);
+
   pg = GNUNET_TESTING_peergroup_start(testing_cfg,
                                       num_peers,
                                       TIMEOUT,
                                       &connect_cb,
                                       &my_cb, NULL,
-                                      NULL);
+                                      hosts);
   GNUNET_assert (pg != NULL);
   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever(),
                                                  &shutdown_task,
index 5b598170781a01ffdcf219642469e92f675d7187..89e866d108f2dd9a5bde3c81fc8c0f4c036410ca 100644 (file)
@@ -6811,6 +6811,105 @@ schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 }
 
+/**
+ * Read a testing hosts file based on a configuration.
+ * Returns a DLL of hosts (caller must free!) on success
+ * or NULL on failure.
+ *
+ * @param cfg a configuration with a testing section
+ *
+ * @return DLL of hosts on success, NULL on failure
+ */
+struct GNUNET_TESTING_Host *
+GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+  struct GNUNET_TESTING_Host *hosts;
+  struct GNUNET_TESTING_Host *temphost;
+  char *data;
+  char *buf;
+  char *hostfile;
+  struct stat frstat;
+  int count;
+  int ret;
+
+  /* Check for a hostfile containing user@host:port triples */
+  if (GNUNET_OK
+      != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
+                                                &hostfile))
+    return NULL;
+
+  hosts = NULL;
+  temphost = NULL;
+  data = NULL;
+  if (hostfile != NULL)
+    {
+      if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
+        GNUNET_DISK_fn_write (hostfile, NULL, 0, GNUNET_DISK_PERM_USER_READ
+            | GNUNET_DISK_PERM_USER_WRITE);
+      if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                      "Could not open file specified for host list, ending test!");
+          GNUNET_free(hostfile);
+          return NULL;
+        }
+
+      data = GNUNET_malloc_large (frstat.st_size);
+      GNUNET_assert(data != NULL);
+      if (frstat.st_size
+          != GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
+        {
+          GNUNET_log (
+                      GNUNET_ERROR_TYPE_ERROR,
+                      "Could not read file %s specified for host list, ending test!",
+                      hostfile);
+          GNUNET_free (hostfile);
+          GNUNET_free (data);
+          return NULL;
+        }
+
+      GNUNET_free_non_null(hostfile);
+
+      buf = data;
+      count = 0;
+      while (count < frstat.st_size - 1)
+        {
+          count++;
+          if (((data[count] == '\n')) && (buf != &data[count]))
+            {
+              data[count] = '\0';
+              temphost = GNUNET_malloc(sizeof(struct GNUNET_TESTING_Host));
+              ret = sscanf (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd",
+                            &temphost->username, &temphost->hostname,
+                            &temphost->port);
+              if (3 == ret)
+                {
+                  GNUNET_log (
+                              GNUNET_ERROR_TYPE_DEBUG,
+                              "Successfully read host %s, port %d and user %s from file\n",
+                              temphost->hostname, temphost->port,
+                              temphost->username);
+                }
+              else
+                {
+                  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                              "Error reading line `%s' in hostfile\n", buf);
+                  GNUNET_free(temphost);
+                  buf = &data[count + 1];
+                  continue;
+                }
+              temphost->next = hosts;
+              hosts = temphost;
+              buf = &data[count + 1];
+            }
+          else if ((data[count] == '\n') || (data[count] == '\0'))
+            buf = &data[count + 1];
+        }
+    }
+  GNUNET_free_non_null(data);
+
+  return hosts;
+}
 
 /**
  * Shutdown all peers started in the given group.