-fix config, shutdown issue
[oweals/gnunet.git] / src / ats / test_ats_simplistic_pref_aging.c
index 14b45f91cb59d838fe65409829d53458711eb24b..83a33d66f3c7c65a4a5cc2db441b00f775c8803d 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2010,2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2010,2011 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -26,7 +26,7 @@
  */
 /**
  * @file ats/test_ats_api_scheduling_add_address.c
- * @brief test for ats simplistic solver preference aging:
+ * @brief test for ats proportional solver preference aging:
  *     Add 2 addresses and set high preference for one. Expect higher bw for this
  *     address, wait. Preferences should age and so bw assigned should decrease.
  * @author Christian Grothoff
 
 #define DEBUG_ATS_INFO GNUNET_NO
 
-static GNUNET_SCHEDULER_TaskIdentifier die_task;
+static struct GNUNET_SCHEDULER_Task * die_task;
 
 /**
  * Scheduling handle
  */
 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
 
+/**
+ * Connectivity handle
+ */
+static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
+
 /**
  * Performance handle
  */
@@ -67,11 +72,15 @@ static struct Test_Address test_addr[2];
  */
 static struct PeerContext p[2];
 
+/**
+ * Connectivity suggestion handles.
+ */
+static struct GNUNET_ATS_ConnectivitySuggestHandle *sh[2];
 
 /**
  * HELLO address
  */
-struct GNUNET_HELLO_Address test_hello_address[2];
+static struct GNUNET_HELLO_Address test_hello_address[2];
 
 /**
  * Session
@@ -81,34 +90,39 @@ static void *test_session[2];
 /**
  * Test ats info
  */
-struct GNUNET_ATS_Information test_ats_info[2];
+static struct GNUNET_ATS_Information test_ats_info[2];
 
 /**
  * Test ats count
  */
-uint32_t test_ats_count;
+static uint32_t test_ats_count;
 
 /**
  * Configured WAN out quota
  */
-unsigned long long wan_quota_out;
+static unsigned long long wan_quota_out;
 
 /**
  * Configured WAN in quota
  */
-unsigned long long wan_quota_in;
+static unsigned long long wan_quota_in;
 
 
 static void
 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  die_task = GNUNET_SCHEDULER_NO_TASK;
+  die_task = NULL;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error! Shutting down\n");
   if (sched_ats != NULL)
   {
     GNUNET_ATS_scheduling_done (sched_ats);
     sched_ats = NULL;
   }
+  if (NULL != connect_ats)
+  {
+    GNUNET_ATS_connectivity_done (connect_ats);
+    connect_ats = NULL;
+  }
   if (perf_ats != NULL)
   {
     GNUNET_ATS_performance_done (perf_ats);
@@ -123,14 +137,14 @@ static void
 end ()
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
-  if (die_task != GNUNET_SCHEDULER_NO_TASK)
+  if (die_task != NULL)
   {
     GNUNET_SCHEDULER_cancel (die_task);
-    die_task = GNUNET_SCHEDULER_NO_TASK;
+    die_task = NULL;
   }
 
-  GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
-  GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
+  GNUNET_ATS_connectivity_suggest_cancel (sh[0]);
+  GNUNET_ATS_connectivity_suggest_cancel (sh[1]);
 
   if (NULL != sched_ats)
        GNUNET_ATS_scheduling_done (sched_ats);
@@ -144,7 +158,9 @@ end ()
 
 
 static void
-address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
+address_suggest_cb (void *cls,
+                    const struct GNUNET_PeerIdentity *peer,
+                    const struct GNUNET_HELLO_Address *address,
                     struct Session *session,
                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
@@ -166,153 +182,159 @@ address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
 
   if (0 == stage)
   {
-               /* Callback for initial suggestion */
-               if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
-               {
-                               GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 0 `%s': (in/out) %llu/%llu\n",
-                                               stage,
-                                               GNUNET_i2s (&address->peer),
-                                               ntohl (bandwidth_in.value__),
-                                               ntohl (bandwidth_out.value__));
-                               sug_p0 = GNUNET_YES;
-                               p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
-                               p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
-               }
-               if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
-               {
-                               GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 1 `%s': (in/out) %llu/%llu\n",
-                                               stage,
-                                               GNUNET_i2s (&address->peer),
-                                               ntohl (bandwidth_in.value__),
-                                               ntohl (bandwidth_out.value__));
-                               sug_p1 = GNUNET_YES;
-                               p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
-                               p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
-               }
-               if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
-               {
-                 /* Changing preference for peer 0 */
-                       stage ++;
-                       GNUNET_ATS_change_preference (perf_ats, &p[0].id, GNUNET_ATS_PREFERENCE_BANDWIDTH,(double) 1000, GNUNET_ATS_PREFERENCE_END);
-                               sug_p0 = GNUNET_NO;
-                               sug_p1 = GNUNET_NO;
-                       return;
-               }
-
+    /* Callback for initial suggestion */
+    if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                  "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
+                  stage,
+                  GNUNET_i2s (&address->peer),
+                  (unsigned int) ntohl (bandwidth_in.value__),
+                  (unsigned int) ntohl (bandwidth_out.value__));
+      sug_p0 = GNUNET_YES;
+      p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
+      p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
+    }
+    if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                  "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
+                  stage,
+                  GNUNET_i2s (&address->peer),
+                  (unsigned int) ntohl (bandwidth_in.value__),
+                  (unsigned int) ntohl (bandwidth_out.value__));
+      sug_p1 = GNUNET_YES;
+      p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
+      p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
+    }
+    if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
+    {
+      /* Changing preference for peer 0 */
+      stage ++;
+      GNUNET_ATS_performance_change_preference (perf_ats, &p[0].id, GNUNET_ATS_PREFERENCE_BANDWIDTH,(double) 1000, GNUNET_ATS_PREFERENCE_END);
+      sug_p0 = GNUNET_NO;
+      sug_p1 = GNUNET_NO;
+      return;
+    }
   }
   if (1 == stage)
   {
-               /* Callback due to preference change */
-               if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
-               {
-                               GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 0 `%s': (in/out) %llu/%llu\n",
-                                               stage,
-                                               GNUNET_i2s (&address->peer),
-                                               ntohl (bandwidth_in.value__),
-                                               ntohl (bandwidth_out.value__));
-                               sug_p0 = GNUNET_YES;
-
-                               /* Peer 0 should get more bandwidth */
-                               if (cur_bandwidth_out <= p0_last_bandwidth_out)
-                                       GNUNET_break (0);
-                               if (cur_bandwidth_in <= p0_last_bandwidth_in)
-                                       GNUNET_break (0);
-                               p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
-                               p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
-               }
-               if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
-               {
-                               GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 1 `%s': (in/out) %llu/%llu\n",
-                                               stage,
-                                               GNUNET_i2s (&address->peer),
-                                               ntohl (bandwidth_in.value__),
-                                               ntohl (bandwidth_out.value__));
-                               sug_p1 = GNUNET_YES;
-
-                               /* Peer 1 should get less bandwidth */
-                               if (cur_bandwidth_out >= p1_last_bandwidth_out)
-                               {
-                                       GNUNET_break (0);
-                                       goto error;
-                               }
-                               if (cur_bandwidth_in >= p1_last_bandwidth_in)
-                               {
-                                       GNUNET_break (0);
-                                       goto error;
-                               }
-                               p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
-                               p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
-               }
-               if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
-               {
-                       stage ++;
-                               sug_p0 = GNUNET_NO;
-                               sug_p1 = GNUNET_NO;
-                       return;
-               }
+    /* Callback due to preference change */
+    if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                  "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
+                  stage,
+                  GNUNET_i2s (&address->peer),
+                  (unsigned int) ntohl (bandwidth_in.value__),
+                  (unsigned int) ntohl (bandwidth_out.value__));
+      sug_p0 = GNUNET_YES;
+
+      /* Peer 0 should get more bandwidth */
+      if (cur_bandwidth_out <= p0_last_bandwidth_out)
+        GNUNET_break (0);
+      if (cur_bandwidth_in <= p0_last_bandwidth_in)
+        GNUNET_break (0);
+      p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
+      p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
+    }
+    if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                  "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
+                  stage,
+                  GNUNET_i2s (&address->peer),
+                  (unsigned int) ntohl (bandwidth_in.value__),
+                  (unsigned int) ntohl (bandwidth_out.value__));
+      sug_p1 = GNUNET_YES;
+
+      /* Peer 1 should get less bandwidth */
+      if (cur_bandwidth_out >= p1_last_bandwidth_out)
+      {
+        GNUNET_break (0);
+        goto error;
+      }
+      if (cur_bandwidth_in >= p1_last_bandwidth_in)
+      {
+        GNUNET_break (0);
+        goto error;
+      }
+      p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
+      p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
+    }
+    if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
+    {
+      stage ++;
+      sug_p0 = GNUNET_NO;
+      sug_p1 = GNUNET_NO;
+      return;
+    }
   }
   if (2 == stage)
   {
-               /* Callback due to preference aging */
-               if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
-               {
-                               GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 0 `%s': (in/out) %llu/%llu\n",
-                                               stage,
-                                               GNUNET_i2s (&address->peer),
-                                               ntohl (bandwidth_in.value__),
-                                               ntohl (bandwidth_out.value__));
-                               sug_p0 = GNUNET_YES;
-
-                               /* Peer 0 should get less bandwidth */
-                               if (cur_bandwidth_out <= p0_last_bandwidth_out)
-                                       GNUNET_break (0);
-                               if (cur_bandwidth_in <= p0_last_bandwidth_in)
-                                       GNUNET_break (0);
-                               p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
-                               p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
-               }
-               if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
-               {
-                               GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 1 `%s': (in/out) %llu/%llu\n",
-                                               stage,
-                                               GNUNET_i2s (&address->peer),
-                                               ntohl (bandwidth_in.value__),
-                                               ntohl (bandwidth_out.value__));
-                               sug_p1 = GNUNET_YES;
-                               /* Peer 1 should get more bandwidth */
-                               if (cur_bandwidth_out <= p1_last_bandwidth_out)
-                               {
-                                       GNUNET_break (0);
-                                       goto error;
-                               }
-                               if (cur_bandwidth_in <= p1_last_bandwidth_in)
-                               {
-                                       GNUNET_break (0);
-                                       goto error;
-                               }
-                               p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
-                               p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
-               }
-
-               if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
-               {
-                       /* Done ! */
-                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
-                       stage ++;
-                       ret = 0;
-        GNUNET_SCHEDULER_add_now (&end,NULL);
-                       return;
-               }
+    /* Callback due to preference aging */
+    if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                  "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
+                  stage,
+                  GNUNET_i2s (&address->peer),
+                  (unsigned int) ntohl (bandwidth_in.value__),
+                  (unsigned int) ntohl (bandwidth_out.value__));
+      sug_p0 = GNUNET_YES;
+
+      /* Peer 0 should get less bandwidth */
+      if (cur_bandwidth_out <= p0_last_bandwidth_out)
+        GNUNET_break (0);
+      if (cur_bandwidth_in <= p0_last_bandwidth_in)
+        GNUNET_break (0);
+      p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
+      p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
+    }
+    if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                  "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
+                  stage,
+                  GNUNET_i2s (&address->peer),
+                  (unsigned int) ntohl (bandwidth_in.value__),
+                  (unsigned int) ntohl (bandwidth_out.value__));
+      sug_p1 = GNUNET_YES;
+      /* Peer 1 should get more bandwidth */
+      if (cur_bandwidth_out <= p1_last_bandwidth_out)
+      {
+        GNUNET_break (0);
+        goto error;
+      }
+      if (cur_bandwidth_in <= p1_last_bandwidth_in)
+      {
+        GNUNET_break (0);
+        goto error;
+      }
+      p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
+      p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
+    }
+
+    if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
+    {
+      /* Done ! */
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
+      stage ++;
+      ret = 0;
+      GNUNET_SCHEDULER_add_now (&end,NULL);
+      return;
+    }
   }
   return;
 
 error:
-       /* Error ! */
-       ret = 1;
-       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Error!\n");
-       GNUNET_SCHEDULER_add_now (&end,NULL);
+  /* Error ! */
+  ret = 1;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Error!\n");
+  GNUNET_SCHEDULER_add_now (&end,NULL);
 }
 
+
 static void
 run (void *cls,
      const struct GNUNET_CONFIGURATION_Handle *cfg,
@@ -358,6 +380,7 @@ run (void *cls,
   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
 
   /* Connect to ATS scheduling */
+  connect_ats = GNUNET_ATS_connectivity_init (mycfg);
   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
   if (sched_ats == NULL)
   {
@@ -368,7 +391,7 @@ run (void *cls,
   }
 
   /* Connect to ATS performance */
-  perf_ats = GNUNET_ATS_performance_init(cfg, NULL, NULL, NULL, NULL);
+  perf_ats = GNUNET_ATS_performance_init(cfg, NULL, NULL);
   if (sched_ats == NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
@@ -431,15 +454,15 @@ run (void *cls,
   test_hello_address[1].address_length = test_addr[1].addr_len;
   GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], test_session[1], test_ats_info, test_ats_count);
 
-  GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
-  GNUNET_ATS_suggest_address (sched_ats, &p[1].id);
+  sh[0] = GNUNET_ATS_connectivity_suggest (connect_ats, &p[0].id);
+  sh[1] = GNUNET_ATS_connectivity_suggest (connect_ats, &p[1].id);
 }
 
 
 int
 main (int argc, char *argv[])
 {
-  if (0 != GNUNET_TESTING_peer_run ("test_ats_simplistic_pref_aging",
+  if (0 != GNUNET_TESTING_peer_run ("test_ats_proportional_pref_aging",
                                     "test_ats_api.conf",
                                     &run, NULL))
     return 1;