do not print two messages, one is enough
[oweals/gnunet.git] / src / ats-tests / ats-testing.c
index a8e451673c0ebe3059ead4242949a1a92c94dce0..f64fa1822b2d0a06859189d6eff8fd63cb533980 100644 (file)
  Boston, MA 02111-1307, USA.
  */
 /**
- * @file ats/perf_ats.c
- * @brief ats benchmark: start peers and modify preferences, monitor change over time
+ * @file ats-tests/ats-testing.c
+ * @brief ats testing library: setup topology
+ * solvers
  * @author Christian Grothoff
  * @author Matthias Wachs
  */
-#include "platform.h"
-#include "gnunet_util_lib.h"
-#include "gnunet_testbed_service.h"
-#include "gnunet_ats_service.h"
-#include "gnunet_core_service.h"
 #include "ats-testing.h"
 
 
+/**
+ * Connect peers with testbed
+ */
+struct TestbedConnectOperation
+{
+  /**
+   * The benchmarking master initiating this connection
+   */
+  struct BenchmarkPeer *master;
+
+  /**
+   * The benchmarking slave to connect to
+   */
+  struct BenchmarkPeer *slave;
+
+  /**
+   * Testbed operation to connect peers
+   */
+  struct GNUNET_TESTBED_Operation *connect_op;
+};
 
+struct GNUNET_CONFIGURATION_Handle *cfg;
 
-static struct GNUNET_ATS_TEST_Topology *top;
+struct GNUNET_ATS_TEST_Topology *top;
 
 /**
  * Shutdown nicely
@@ -48,24 +65,14 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   int c_s;
   int c_op;
   struct BenchmarkPeer *p;
-/*
-  if (GNUNET_YES == logging)
-    perf_logging_stop();
-*/
-  top->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
-/*
-  if (GNUNET_SCHEDULER_NO_TASK != progress_task)
-  {
-    fprintf (stderr, "0\n");
-    GNUNET_SCHEDULER_cancel (progress_task);
-  }
-  progress_task = GNUNET_SCHEDULER_NO_TASK;
-*/
-  //evaluate ();
 
+  top->shutdown_task = NULL;
   top->state.benchmarking = GNUNET_NO;
+
   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
 
+  GNUNET_ATS_TEST_generate_traffic_stop_all ();
+
   for (c_m = 0; c_m < top->num_masters; c_m++)
   {
     p = &top->mps[c_m];
@@ -75,9 +82,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++)
     {
@@ -153,8 +160,9 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_free(p->partners);
     p->partners = NULL;
   }
-
   GNUNET_SCHEDULER_shutdown ();
+  GNUNET_free (top);
+  top = NULL;
 }
 
 static struct BenchmarkPartner *
@@ -250,6 +258,9 @@ comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
           "All master peers connected all slave peers\n", id,
           GNUNET_i2s (peer));
       top->state.connected_CORE = GNUNET_YES;
+      /* Notify about setup done */
+      if (NULL != top->done_cb)
+        top->done_cb (top->done_cb_cls, top->mps, top->sps);
     }
   }
   GNUNET_free(id);
@@ -313,12 +324,66 @@ core_disconnect_adapter (void *cls, void *op_result)
 }
 
 
+
+
+static int
+comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
+    const struct GNUNET_MessageHeader *message)
+{
+  struct BenchmarkPeer *me = cls;
+  struct BenchmarkPartner *p = NULL;
+
+  if (NULL == (p = find_partner (me, other)))
+  {
+    GNUNET_break(0);
+    return GNUNET_SYSERR;
+  }
+
+  GNUNET_ATS_TEST_traffic_handle_pong (p);
+
+  return GNUNET_OK;
+}
+
+static int
+comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
+    const struct GNUNET_MessageHeader *message)
+{
+  struct BenchmarkPeer *me = cls;
+  struct BenchmarkPartner *p = NULL;
+
+  if (NULL == (p = find_partner(me, other)))
+  {
+    GNUNET_break(0);
+    return GNUNET_SYSERR;
+  }
+  GNUNET_ATS_TEST_traffic_handle_ping (p);
+  return GNUNET_OK;
+}
+
+static void
+test_recv_cb (void *cls,
+                     const struct GNUNET_PeerIdentity * peer,
+                     const struct GNUNET_MessageHeader * message)
+{
+  if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
+      (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
+     TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
+  {
+    return;
+  }
+  if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
+    comm_handle_ping (cls, peer, message);
+  if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
+    comm_handle_pong (cls, peer, message);
+}
+
+
 static void *
 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct BenchmarkPeer *me = cls;
 
-  me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, top->transport_recv_cb,
+  me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, &test_recv_cb,
       &comm_connect_cb, &comm_disconnect_cb);
   if (NULL == me->th)
     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
@@ -354,7 +419,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)
+    if (NULL != top->shutdown_task)
       GNUNET_SCHEDULER_cancel (top->shutdown_task);
     top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
   }
@@ -368,9 +433,6 @@ connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
   if (ops == top->num_masters * top->num_slaves)
   {
     top->state.connected_PEERS = GNUNET_YES;
-    /* Notify about setup done */
-    if (NULL != top->done_cb)
-      top->done_cb (top->done_cb_cls, top->mps, top->sps);
   }
 }
 
@@ -408,7 +470,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)
+        if (NULL != top->shutdown_task)
           GNUNET_SCHEDULER_cancel (top->shutdown_task);
         top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
         return;
@@ -427,7 +489,7 @@ comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
   {
     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
     GNUNET_break(0);
-    if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
+    if (NULL != top->shutdown_task)
       GNUNET_SCHEDULER_cancel (top->shutdown_task);
     top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
     return;
@@ -480,13 +542,115 @@ 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)
+{
+  struct BenchmarkPeer *me = cls;
+  struct BenchmarkPartner *p;
+  int c_a;
+  int log;
+  char *peer_id;
+
+  p = find_partner (me, &address->peer);
+  if (NULL == p)
+  {
+    /* This is not one of my partners
+     * Will happen since the peers will connect to each other due to gossiping
+     */
+    return;
+  }
+  peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
+
+  log = GNUNET_NO;
+  if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
+      (p->bandwidth_out != ntohl (bandwidth_out.value__)))
+      log = GNUNET_YES;
+  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;
+     }
+   }
+  if (GNUNET_YES == log)
+    top->ats_perf_cb (cls, address,
+                      address_active,
+                      bandwidth_out,
+                      bandwidth_in,
+                      ats, ats_count);
+  GNUNET_free(peer_id);
+}
+
+
 static void *
 ats_perf_connect_adapter (void *cls,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct BenchmarkPeer *me = cls;
 
-  me->ats_perf_handle = GNUNET_ATS_performance_init (cfg, top->ats_perf_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");
@@ -512,7 +676,7 @@ ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
   {
     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
     GNUNET_break(0);
-    if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
+    if (NULL != top->shutdown_task)
       GNUNET_SCHEDULER_cancel (top->shutdown_task);
     top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
     return;
@@ -583,6 +747,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
@@ -604,10 +769,9 @@ main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h,
   GNUNET_assert(NULL != peers_);
 
   top->shutdown_task = GNUNET_SCHEDULER_add_delayed (
-      GNUNET_TIME_relative_multiply (TEST_TIMEOUT, top->num_masters + top->num_slaves),
-      &do_shutdown, top );
+      GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown, top);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Setting up %u masters and %u slaves\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Setting up %u masters and %u slaves\n",
       top->num_masters, top->num_slaves);
 
   /* Setup master peers */
@@ -618,7 +782,7 @@ main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h,
     top->mps[c_m].no = c_m;
     top->mps[c_m].master = GNUNET_YES;
     top->mps[c_m].pref_partner = &top->sps[c_m];
-    top->mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
+    top->mps[c_m].pref_value = TEST_ATS_PREFERENCE_DEFAULT;
     top->mps[c_m].partners =
         GNUNET_malloc (top->num_slaves * sizeof (struct BenchmarkPartner));
     top->mps[c_m].num_partners = top->num_slaves;
@@ -679,17 +843,52 @@ controller_event_cb (void *cls,
   }
 }
 
+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_TESTING_TopologySetupDoneCallback done_cb,
-    void *done_cb_cls,
-    struct GNUNET_CORE_MessageHandler *handlers,
-    GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb,
-    GNUNET_ATS_AddressInformationCallback ats_perf_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 },
+      { NULL, 0, 0 } };
 
   top = GNUNET_new (struct GNUNET_ATS_TEST_Topology);
   top->num_masters = num_masters;
@@ -699,7 +898,7 @@ GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
   top->done_cb_cls = done_cb_cls;
   top->test_core = test_core;
   top->transport_recv_cb = transport_recv_cb;
-  top->ats_perf_cb = ats_perf_cb;
+  top->ats_perf_cb = log_request_cb;
 
   top->mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
   top->sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
@@ -710,14 +909,26 @@ 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();
 }
 
-/* end of file perf_ats.c */
+
+
+
+
+/* end of file ats-testing.c */