- distribute peers equally among island nodes on SuperMUC
[oweals/gnunet.git] / src / testbed / testbed_api_hosts.c
index f61129b859ec4a1c5d924c25f125f65fdcfcbda2..99625cba05b55f51865193d20d669191d8a806f0 100644 (file)
@@ -486,7 +486,7 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
     {
       data[offset] = '\0';
       ret =
-          SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd", username,
+          SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.-]:%5hd", username,
                   hostname, &port);
       if (3 == ret)
       {
@@ -556,10 +556,22 @@ simple_resolve (const char *host)
   in_addr = (const struct sockaddr_in *) res->ai_addr;
   hostip = inet_ntoa (in_addr->sin_addr);
   GNUNET_assert (NULL != hostip);
+  freeaddrinfo (res);
   LOG_DEBUG ("Resolved [%s] to [%s]\n", host, hostip);
   return hostip;
 }
 
+#if ENABLE_LL
+static int
+cmpstringp(const void *p1, const void *p2)
+{
+  /* The actual arguments to this function are "pointers to
+     pointers to char", but strcmp(3) arguments are "pointers
+     to char", hence the following cast plus dereference */
+  
+  return strcmp(* (char * const *) p1, * (char * const *) p2);
+}
+#endif
 
 /**
  * Loads the set of host allocated by the LoadLeveler Job Scheduler.  This
@@ -588,8 +600,6 @@ GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
   char *buf;
   char *hostname;
   char **hostnames;
-  char **hostaddrs;
-  const char *hostip;
   struct GNUNET_TESTBED_Host **host_list;
   ssize_t rsize;
   uint64_t size;
@@ -602,7 +612,6 @@ GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
   } pstep;
   unsigned int host;
   unsigned int nhosts;
-  unsigned int nhostaddrs;
   
   if (NULL == (hostfile = getenv ("MP_SAVEHOSTFILE")))
   {
@@ -633,9 +642,7 @@ GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
   pstep = SCAN;
   hostname = NULL;
   hostnames = NULL;
-  hostaddrs = NULL;
   nhosts = 0;
-  nhostaddrs = 0;
   while (offset < size)
   {
     switch (pstep)
@@ -691,31 +698,18 @@ GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
   GNUNET_free_non_null (buf);
   if (NULL == hostnames)
     return 0;
-  for (host = 0; host < nhosts; host++)
-  {
-    hostip = simple_resolve (hostnames[host]);
-    if (NULL == hostip)
-    {
-      nhosts = 0;
-      goto cleanup;
-    }
-    GNUNET_array_append (hostaddrs, nhostaddrs, GNUNET_strdup (hostip));
-  }
-  GNUNET_assert (nhostaddrs == nhosts);
   if (NULL == hosts)
     goto cleanup;
-  host_list = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * nhostaddrs);
+  qsort (hostnames, nhosts, sizeof (hostnames[0]), cmpstringp);
+  host_list = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * nhosts);
   for (host = 0; host < nhosts; host++)
-    host_list[host] = GNUNET_TESTBED_host_create (hostaddrs[host], NULL, cfg, 0);
+    host_list[host] = GNUNET_TESTBED_host_create (hostnames[host], NULL, cfg, 0);
   *hosts = host_list;
 
  cleanup:
   for (host = 0; host < nhosts; host++)
     GNUNET_free (hostnames[host]);
   GNUNET_free(hostnames);
-  for (host = 0; (NULL != hostaddrs) && (host < nhostaddrs); host++)
-    GNUNET_free (hostaddrs[host]);
-  GNUNET_free (hostaddrs);
   return nhosts;
 #endif
 }
@@ -1068,6 +1062,7 @@ helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
   struct GNUNET_TESTBED_ControllerProc *cp = cls;
   const struct GNUNET_TESTBED_HelperReply *msg;
   const char *hostname;
+  const char *hostip;
   char *config;
   uLongf config_size;
   uLongf xconfig_size;
@@ -1095,9 +1090,12 @@ helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
   if ((NULL == cp->host) ||
       (NULL == (hostname = GNUNET_TESTBED_host_get_hostname (cp->host))))
     hostname = "localhost";
+  hostip = simple_resolve (hostname);
+  if (NULL == hostip)
+    hostip = "127.0.0.1";  
   /* Change the hostname so that we can connect to it */
   GNUNET_CONFIGURATION_set_value_string (cp->host->cfg, "testbed", "hostname",
-                                         hostname);
+                                         hostip);
   cp->host->locked = GNUNET_NO;
   cp->host->controller_started = GNUNET_YES;
   cp->cb (cp->cls, cp->host->cfg, GNUNET_OK);
@@ -1261,20 +1259,33 @@ GNUNET_TESTBED_controller_start (const char *trusted_ip,
 
 
 /**
- * Stop the controller process (also will terminate all peers and controllers
- * dependent on this controller).  This function blocks until the testbed has
- * been fully terminated (!). The controller status cb from
- * GNUNET_TESTBED_controller_start() will not be called.
+ * Sends termination signal to the controller's helper process
  *
- * @param cproc the controller process handle
+ * @param cproc the handle to the controller's helper process
  */
 void
-GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
+GNUNET_TESTBED_controller_kill_ (struct GNUNET_TESTBED_ControllerProc *cproc)
 {
   if (NULL != cproc->shandle)
     GNUNET_HELPER_send_cancel (cproc->shandle);
   if (NULL != cproc->helper)
-    GNUNET_HELPER_soft_stop (cproc->helper);
+    GNUNET_HELPER_kill (cproc->helper, GNUNET_YES);
+}
+
+
+/**
+ * Cleans-up the controller's helper process handle
+ *
+ * @param cproc the handle to the controller's helper process
+ */
+void
+GNUNET_TESTBED_controller_destroy_ (struct GNUNET_TESTBED_ControllerProc *cproc)
+{
+  if (NULL != cproc->helper)
+  {
+    GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (cproc->helper));
+    GNUNET_HELPER_destroy (cproc->helper);
+  }
   if (NULL != cproc->helper_argv)
     free_argv (cproc->helper_argv);
   cproc->host->controller_started = GNUNET_NO;
@@ -1283,6 +1294,22 @@ GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
 }
 
 
+/**
+ * Stop the controller process (also will terminate all peers and controllers
+ * dependent on this controller).  This function blocks until the testbed has
+ * been fully terminated (!). The controller status cb from
+ * GNUNET_TESTBED_controller_start() will not be called.
+ *
+ * @param cproc the controller process handle
+ */
+void
+GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
+{
+  GNUNET_TESTBED_controller_kill_ (cproc);
+  GNUNET_TESTBED_controller_destroy_ (cproc);
+}
+
+
 /**
  * The handle for whether a host is habitable or not
  */