new logging functionality
[oweals/gnunet.git] / src / ats-tests / ats-testing.c
index b93a097d9dcace89e96e0ec87a98b9c9529675f9..08a61367b9796b7f86ac0445391b7870a999c689 100644 (file)
  */
 /**
  * @file ats-tests/ats-testing.c
- * @brief ats testing library: setup topology and provide logging to test ats
+ * @brief ats testing library: setup topology
  * solvers
  * @author Christian Grothoff
  * @author Matthias Wachs
  */
 #include "ats-testing.h"
 
-static struct GNUNET_ATS_TEST_Topology *top;
+
+/**
+ * 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_ATS_TEST_Topology *top;
 
 /**
  * Shutdown nicely
@@ -42,14 +65,13 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   int c_op;
   struct BenchmarkPeer *p;
 
-  if (GNUNET_YES == top->logging)
-    GNUNET_ATS_TEST_logging_stop ();
-
   top->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
-
   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];
@@ -137,8 +159,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 *
@@ -234,6 +257,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);
@@ -297,12 +323,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");
@@ -352,9 +432,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);
   }
 }
 
@@ -463,6 +540,102 @@ 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,
@@ -470,7 +643,7 @@ ats_perf_connect_adapter (void *cls,
 {
   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");
@@ -662,18 +835,23 @@ controller_event_cb (void *cls,
   }
 }
 
+
 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,
+    GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
     void *done_cb_cls,
-    struct GNUNET_CORE_MessageHandler *handlers,
     GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb,
-    GNUNET_ATS_AddressInformationCallback ats_perf_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;
   top->num_slaves = num_slaves;
@@ -682,7 +860,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));
@@ -700,7 +878,14 @@ GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
 void
 GNUNET_ATS_TEST_shutdown_topology (void)
 {
+  if (NULL == top)
+    return;
+
   GNUNET_SCHEDULER_shutdown();
 }
 
+
+
+
+
 /* end of file ats-testing.c */