add function conv param string
[oweals/gnunet.git] / src / ats-tests / ats-testing.c
index 08a61367b9796b7f86ac0445391b7870a999c689..c894f4445fd3390a9cddd97f5057527f9ddfbe8c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  This file is part of GNUnet.
- (C) 2010-2013 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2010-2013 GNUnet e.V.
 
  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
  */
 /**
  * @file ats-tests/ats-testing.c
@@ -48,24 +48,24 @@ struct TestbedConnectOperation
   struct GNUNET_TESTBED_Operation *connect_op;
 };
 
+struct GNUNET_CONFIGURATION_Handle *cfg;
 
 struct GNUNET_ATS_TEST_Topology *top;
 
+
 /**
  * Shutdown nicely
  *
  * @param cls NULL
- * @param tc the task context
  */
 static void
-do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_shutdown (void *cls)
 {
   int c_m;
   int c_s;
   int c_op;
   struct BenchmarkPeer *p;
 
-  top->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
   top->state.benchmarking = GNUNET_NO;
 
   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
@@ -81,9 +81,9 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       p->peer_id_op = NULL;
     }
 
-    if (GNUNET_SCHEDULER_NO_TASK != p->ats_task)
+    if (NULL != p->ats_task)
       GNUNET_SCHEDULER_cancel (p->ats_task);
-    p->ats_task = GNUNET_SCHEDULER_NO_TASK;
+    p->ats_task = NULL;
 
     for (c_op = 0; c_op < p->num_partners; c_op++)
     {
@@ -242,8 +242,9 @@ comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
 
     if (me->core_slave_connections == top->num_slaves)
     {
-      GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
-          me->no);
+      GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+                 "Master [%u] connected all slaves\n",
+                 me->no);
     }
     completed = GNUNET_YES;
     for (c = 0; c < top->num_masters; c++)
@@ -253,9 +254,8 @@ comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
     }
     if (GNUNET_YES == completed)
     {
-      GNUNET_log(GNUNET_ERROR_TYPE_INFO,
-          "All master peers connected all slave peers\n", id,
-          GNUNET_i2s (peer));
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                  "All master peers connected all slave peers\n");
       top->state.connected_CORE = GNUNET_YES;
       /* Notify about setup done */
       if (NULL != top->done_cb)
@@ -378,7 +378,8 @@ test_recv_cb (void *cls,
 
 
 static void *
-transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
+transport_connect_adapter (void *cls,
+                           const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct BenchmarkPeer *me = cls;
 
@@ -389,6 +390,7 @@ transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *
   return me->th;
 }
 
+
 static void
 transport_disconnect_adapter (void *cls, void *op_result)
 {
@@ -400,8 +402,9 @@ transport_disconnect_adapter (void *cls, void *op_result)
 
 
 static void
-connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
-    const char *emsg)
+connect_completion_callback (void *cls,
+                             struct GNUNET_TESTBED_Operation *op,
+                             const char *emsg)
 {
   struct TestbedConnectOperation *cop = cls;
   static int ops = 0;
@@ -409,8 +412,9 @@ connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
   if (NULL == emsg)
   {
     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
-        _("Connected master [%u] with slave [%u]\n"), cop->master->no,
-        cop->slave->no);
+               _("Connected master [%u] with slave [%u]\n"),
+               cop->master->no,
+               cop->slave->no);
   }
   else
   {
@@ -418,9 +422,7 @@ connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
         _("Failed to connect master peer [%u] with slave [%u]\n"),
         cop->master->no, cop->slave->no);
     GNUNET_break(0);
-    if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
-      GNUNET_SCHEDULER_cancel (top->shutdown_task);
-    top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
+    GNUNET_SCHEDULER_shutdown ();
   }
   GNUNET_TESTBED_operation_done (op);
   ops++;
@@ -435,8 +437,9 @@ connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
   }
 }
 
+
 static void
-do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_connect_peers (void *cls)
 {
   int c_m;
   int c_s;
@@ -469,9 +472,7 @@ do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
             _("Could not connect master [%u] and slave [%u]\n"), p->no,
             top->sps[c_s].no);
         GNUNET_break(0);
-        if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
-          GNUNET_SCHEDULER_cancel (top->shutdown_task);
-        top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
+        GNUNET_SCHEDULER_shutdown ();
         return;
       }
     }
@@ -480,17 +481,19 @@ do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 
 static void
-comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
-    void *ca_result, const char *emsg)
+comm_connect_completion_cb (void *cls,
+                            struct GNUNET_TESTBED_Operation *op,
+                            void *ca_result,
+                            const char *emsg)
 {
   static int comm_done = 0;
+
   if ((NULL != emsg) || (NULL == ca_result))
   {
-    GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
+    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+               "Initialization failed, shutdown\n");
     GNUNET_break(0);
-    if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
-      GNUNET_SCHEDULER_cancel (top->shutdown_task);
-    top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
+    GNUNET_SCHEDULER_shutdown ();
     return;
   }
   comm_done++;
@@ -504,8 +507,9 @@ comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
   }
 }
 
+
 static void
-do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_comm_connect (void *cls)
 {
   int c_s;
   int c_m;
@@ -540,21 +544,27 @@ do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
 }
 
+
 static void
 ats_performance_info_cb (void *cls,
-    const struct GNUNET_HELLO_Address *address,
-    int address_active,
-    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
-    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
-    const struct GNUNET_ATS_Information *ats,
-    uint32_t ats_count)
+                         const struct GNUNET_HELLO_Address *address,
+                         int address_active,
+                         struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
+                         struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
+                         const struct GNUNET_ATS_Properties *ats_prop)
 {
   struct BenchmarkPeer *me = cls;
   struct BenchmarkPartner *p;
-  int c_a;
   int log;
   char *peer_id;
 
+  if (NULL == address)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer %u: ATS Service disconnected!\n",
+        me->no);
+    return;
+  }
+
   p = find_partner (me, &address->peer);
   if (NULL == p)
   {
@@ -572,67 +582,23 @@ ats_performance_info_cb (void *cls,
   p->bandwidth_in = ntohl (bandwidth_in.value__);
   p->bandwidth_out = ntohl (bandwidth_out.value__);
 
-  for (c_a = 0; c_a < ats_count; c_a++)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
-        (GNUNET_YES == p->me->master) ? "Master" : "Slave",
-        p->me->no,
-        GNUNET_i2s (&p->dest->id),
-        GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
-        ntohl(ats[c_a].value));
-    switch (ntohl (ats[c_a].type ))
-    {
-      case GNUNET_ATS_ARRAY_TERMINATOR:
-        break;
-      case GNUNET_ATS_UTILIZATION_OUT:
-        if (p->ats_utilization_up != ntohl (ats[c_a].value))
-             log = GNUNET_YES;
-         p->ats_utilization_up = ntohl (ats[c_a].value);
-
-         break;
-       case GNUNET_ATS_UTILIZATION_IN:
-         if (p->ats_utilization_down != ntohl (ats[c_a].value))
-             log = GNUNET_YES;
-         p->ats_utilization_down = ntohl (ats[c_a].value);
-         break;
-       case GNUNET_ATS_NETWORK_TYPE:
-         if (p->ats_network_type != ntohl (ats[c_a].value))
-             log = GNUNET_YES;
-         p->ats_network_type = ntohl (ats[c_a].value);
-         break;
-       case GNUNET_ATS_QUALITY_NET_DELAY:
-         if (p->ats_delay != ntohl (ats[c_a].value))
-             log = GNUNET_YES;
-         p->ats_delay = ntohl (ats[c_a].value);
-         break;
-       case GNUNET_ATS_QUALITY_NET_DISTANCE:
-         if (p->ats_distance != ntohl (ats[c_a].value))
-             log = GNUNET_YES;
-         p->ats_distance = ntohl (ats[c_a].value);
-         GNUNET_break (0);
-         break;
-       case GNUNET_ATS_COST_WAN:
-         if (p->ats_cost_wan != ntohl (ats[c_a].value))
-             log = GNUNET_YES;
-         p->ats_cost_wan = ntohl (ats[c_a].value);
-         break;
-       case GNUNET_ATS_COST_LAN:
-         if (p->ats_cost_lan != ntohl (ats[c_a].value))
-             log = GNUNET_YES;
-         p->ats_cost_lan = ntohl (ats[c_a].value);
-         break;
-       case GNUNET_ATS_COST_WLAN:
-         if (p->ats_cost_wlan != ntohl (ats[c_a].value))
-             log = GNUNET_YES;
-         p->ats_cost_wlan = ntohl (ats[c_a].value);
-         break;
-       default:
-         break;
-     }
-   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s\n",
+      (GNUNET_YES == p->me->master) ? "Master" : "Slave",
+      p->me->no,
+      GNUNET_i2s (&p->dest->id));
+
+  p->props.utilization_out = ats_prop->utilization_out;
+  p->props.utilization_in = ats_prop->utilization_in;
+  p->props.scope = ats_prop->scope;
+  p->props.delay = ats_prop->delay;
+  p->props.distance = ats_prop->distance;
+
   if (GNUNET_YES == log)
-    top->ats_perf_cb (cls, address, address_active, bandwidth_out, bandwidth_in,
-      ats, ats_count);
+    top->ats_perf_cb (cls, address,
+                      address_active,
+                      bandwidth_out,
+                      bandwidth_in,
+                      ats_prop);
   GNUNET_free(peer_id);
 }
 
@@ -643,7 +609,8 @@ ats_perf_connect_adapter (void *cls,
 {
   struct BenchmarkPeer *me = cls;
 
-  me->ats_perf_handle = GNUNET_ATS_performance_init (cfg, ats_performance_info_cb, me);
+  me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
+      &ats_performance_info_cb, me);
   if (NULL == me->ats_perf_handle)
     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
         "Failed to create ATS performance handle \n");
@@ -667,11 +634,10 @@ ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
 
   if ((NULL != emsg) || (NULL == ca_result))
   {
-    GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
+    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+               "Initialization failed, shutdown\n");
     GNUNET_break(0);
-    if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
-      GNUNET_SCHEDULER_cancel (top->shutdown_task);
-    top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
+    GNUNET_SCHEDULER_shutdown ();
     return;
   }
   op_done++;
@@ -685,7 +651,7 @@ ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
 
 
 static void
-do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_connect_ats (void *cls)
 {
   int c_m;
   int c_s;
@@ -740,6 +706,7 @@ peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
  * Signature of a main function for a testcase.
  *
  * @param cls closure
+ * @param h testbed handle
  * @param num_peers number of peers in 'peers'
  * @param peers_ handle to peers run in the testbed
  * @param links_succeeded the number of overlay link connection attempts that
@@ -756,15 +723,18 @@ main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h,
 {
   int c_m;
   int c_s;
+
   GNUNET_assert(NULL == cls);
   GNUNET_assert(top->num_masters + top->num_slaves == num_peers);
   GNUNET_assert(NULL != peers_);
 
-  top->shutdown_task = GNUNET_SCHEDULER_add_delayed (
-      GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown, top);
+  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+                                 top);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Setting up %u masters and %u slaves\n",
-      top->num_masters, top->num_slaves);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Setting up %u masters and %u slaves\n",
+              top->num_masters,
+              top->num_slaves);
 
   /* Setup master peers */
   for (c_m = 0; c_m < top->num_masters; c_m++)
@@ -804,6 +774,13 @@ main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h,
     {
       top->sps[c_s].partners[c_m].me = &top->sps[c_s];
       top->sps[c_s].partners[c_m].dest = &top->mps[c_m];
+
+      /* Initialize properties */
+      top->sps[c_s].partners[c_m].props.delay = GNUNET_TIME_UNIT_ZERO;
+      top->sps[c_s].partners[c_m].props.distance = 0;
+      top->sps[c_s].partners[c_m].props.scope = GNUNET_ATS_NET_UNSPECIFIED;
+      top->sps[c_s].partners[c_m].props.utilization_in = 0;
+      top->sps[c_s].partners[c_m].props.utilization_out = 0;
     }
     /* Get configuration */
     top->sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (top->sps[c_s].peer,
@@ -819,9 +796,8 @@ main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h,
  */
 static void
 controller_event_cb (void *cls,
-    const struct GNUNET_TESTBED_EventInformation *event)
+                     const struct GNUNET_TESTBED_EventInformation *event)
 {
-  struct GNUNET_ATS_TEST_Topology *top = cls;
   switch (event->type)
   {
   case GNUNET_TESTBED_ET_CONNECT:
@@ -830,23 +806,54 @@ controller_event_cb (void *cls,
     break;
   default:
     GNUNET_break(0);
-    GNUNET_SCHEDULER_cancel (top->shutdown_task);
-    top->shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
+    GNUNET_SCHEDULER_shutdown ();
   }
 }
 
 
+struct BenchmarkPeer *
+GNUNET_ATS_TEST_get_peer (int src)
+{
+  if (src > top->num_masters)
+    return NULL;
+  return &top->mps[src];
+}
+
+
+struct BenchmarkPartner *
+GNUNET_ATS_TEST_get_partner (int src, int dest)
+{
+  if (src > top->num_masters)
+    return NULL;
+  if (dest > top->num_slaves)
+    return NULL;
+  return &top->mps[src].partners[dest];
+}
+
+
+/**
+ * Create a topology for ats testing
+ *
+ * @param name test name
+ * @param cfg_file configuration file to use for the peers
+ * @param num_slaves number of slaves
+ * @param num_masters number of masters
+ * @param test_core connect to CORE service (GNUNET_YES) or transport (GNUNET_NO)
+ * @param done_cb function to call when topology is setup
+ * @param done_cb_cls cls for callback
+ * @param transport_recv_cb callback to call when data are received
+ * @param log_request_cb callback to call when logging is required
+ */
 void
 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
-    unsigned int num_slaves,
-    unsigned int num_masters,
-    int test_core,
-    GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
-    void *done_cb_cls,
-    GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb,
-    GNUNET_ATS_AddressInformationCallback log_request_cb)
+                                 unsigned int num_slaves,
+                                 unsigned int num_masters,
+                                 int test_core,
+                                 GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
+                                 void *done_cb_cls,
+                                 GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb,
+                                 GNUNET_ATS_AddressInformationCallback log_request_cb)
 {
-
   static struct GNUNET_CORE_MessageHandler handlers[] = {
       {&comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 },
       {&comm_handle_pong, TEST_MESSAGE_TYPE_PONG, 0 },
@@ -871,16 +878,21 @@ GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
   (void) GNUNET_TESTBED_test_run (name, cfg_file,
-      num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
-      &main_run, NULL);
+                                  num_slaves + num_masters,
+                                  event_mask,
+                                  &controller_event_cb, NULL,
+                                  &main_run, NULL);
 }
 
+
+/**
+ * Shutdown topology
+ */
 void
 GNUNET_ATS_TEST_shutdown_topology (void)
 {
   if (NULL == top)
     return;
-
   GNUNET_SCHEDULER_shutdown();
 }