- changes
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.c
index 70869192ee71d8dc9f036b7096046b39b8a26109..b94a7165624726a85d3996ebb1efa54e6593662f 100644 (file)
@@ -123,6 +123,11 @@ struct GAS_Addresses_Handle
    */
   GAS_solver_init s_init;
 
+  /**
+   * Add an address to the solver
+   */
+  GAS_solver_address_add s_add;
+
   /**
    * Update address in solver
    */
@@ -205,6 +210,8 @@ disassemble_ats_information (const struct GNUNET_ATS_Information *src,
       res ++;
       break;
     case GNUNET_ATS_QUALITY_NET_DISTANCE:
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "Old ATS type %u %u\n", dest->atsp_distance, ntohl (src[i].type));
       dest->atsp_distance = ntohl (src[i].value);
       res ++;
       break;
@@ -270,6 +277,10 @@ create_address (const struct GNUNET_PeerIdentity *peer,
   memcpy (&aa[1], plugin_addr, plugin_addr_len);
   aa->plugin = GNUNET_strdup (plugin_name);
   aa->session_id = session_id;
+  aa->active = GNUNET_NO;
+  aa->solver_information = NULL;
+  aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
+  aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
   return aa;
 }
 
@@ -314,11 +325,6 @@ compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
   struct CompareAddressContext *cac = cls;
   struct ATS_Address *aa = value;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
-      GNUNET_h2s (key),
-      aa->addr_len, aa->session_id,
-      cac->search->addr_len, cac->search->session_id);
-
   /* Find an matching exact address:
    *
    * Compare by:
@@ -400,12 +406,6 @@ find_address (const struct GNUNET_PeerIdentity *peer,
   GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
                                               &compare_address_it, &cac);
 
-#if 0
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Found exact address: %s           base address: %s\n",
-              (cac.exact_address != NULL) ? "YES" : "NO",
-              (cac.base_address != NULL) ? "YES" : "NO");
-#endif
   if (cac.exact_address == NULL)
     return cac.base_address;
   return cac.exact_address;
@@ -427,8 +427,6 @@ lookup_address (const struct GNUNET_PeerIdentity *peer,
                        plugin_addr, plugin_addr_len,
                        session_id);
 
-  aa->mlp_information = NULL;
-
   /* Get existing address or address with session == 0 */
   old = find_address (peer, aa);
   free_address (aa);
@@ -444,48 +442,6 @@ lookup_address (const struct GNUNET_PeerIdentity *peer,
 }
 
 
-#if 0
-static int
-compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
-{
-  struct CompareAddressContext *cac = cls;
-  struct ATS_Address *aa = value;
-
-  if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
-  {
-      if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
-      {
-       cac->exact_address = aa;
-        return GNUNET_NO;
-      }
-  }
-  return GNUNET_YES;
-}
-
-
-/**
- * Find an existing equivalent address record.
- * Compares by peer identity and network address AND by session ID
- * (one of the two must match).
- *
- * @param peer peer to lookup addresses for
- * @param addr existing address record
- * @return existing address record, NULL for none
- */
-static struct ATS_Address *
-find_exact_address (const struct GNUNET_PeerIdentity *peer,
-              const struct ATS_Address *addr)
-{
-  struct CompareAddressContext cac;
-
-  cac.exact_address = NULL;
-  cac.search = addr;
-  GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
-                                              &compare_address_session_it, &cac);
-  return cac.exact_address;
-}
-#endif
-
 void
 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
                       const char *plugin_name, const void *plugin_addr,
@@ -511,7 +467,6 @@ GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
                        plugin_name,
                        plugin_addr, plugin_addr_len,
                        session_id);
-  aa->mlp_information = NULL;
   if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
   {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -530,10 +485,12 @@ GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
                 GNUNET_i2s (peer), session_id, aa);
-    /* Tell solver about update */
-    handle->s_update (handle->solver, handle->addresses, aa);
+    /* Tell solver about new address */
+    handle->s_add (handle->solver, handle->addresses, aa);
     return;
   }
+  GNUNET_free (aa->plugin);
+  GNUNET_free (aa);
 
   if (old->session_id != 0)
   {
@@ -556,8 +513,7 @@ GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
                 "While updating address: had %u ATS elements to add, could only add %u\n",
                 atsi_count, ats_res);
   }
-  GNUNET_free (aa->plugin);
-  GNUNET_free (aa);
+
   handle->s_update (handle->solver, handle->addresses, old);
 }
 
@@ -569,23 +525,18 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
                       const struct GNUNET_ATS_Information *atsi,
                       uint32_t atsi_count)
 {
-  struct ATS_Address *old;
+  struct ATS_Address *aa;
   uint32_t ats_res;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Received `%s' for peer `%s'\n",
-                "ADDRESS UPDATE",
-                GNUNET_i2s (peer));
-
   if (GNUNET_NO == handle->running)
     return;
 
   GNUNET_assert (NULL != handle->addresses);
 
   /* Get existing address */
-  old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
+  aa = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
                        session_id, atsi, atsi_count);
-  if (old == NULL)
+  if (aa == NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
                 GNUNET_i2s (peer), plugin_name, session_id);
@@ -593,15 +544,23 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
     return;
   }
 
-  if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, old)))
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Received `%s' for peer `%s' address \n",
+                "ADDRESS UPDATE",
+                GNUNET_i2s (peer), aa);
+
+  if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, aa)))
   {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "While adding address: had %u ATS elements to add, could only add %u\n",
                 atsi_count, ats_res);
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+            "Updated %u ATS elements for address %p\n",
+            ats_res, aa);
 
   /* Tell solver about update */
-  handle->s_update (handle->solver, handle->addresses, old);
+  handle->s_update (handle->solver, handle->addresses, aa);
 }
 
 
@@ -852,7 +811,6 @@ GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
        "Address %p ready for suggestion, block interval now %llu \n",
        aa, aa->block_interval);
 
-
   GNUNET_free (ats);
 }
 
@@ -986,6 +944,42 @@ load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *
 }
 
 
+static void
+bandwidth_changed_cb (struct ATS_Address *address)
+{
+  struct GAS_Addresses_Suggestion_Requests *cur;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
+  struct GNUNET_ATS_Information *ats;
+  unsigned int ats_count;
+
+  cur = handle->r_head;
+  while (NULL != cur)
+  {
+      if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
+        break; /* we have an address request pending*/
+      cur = cur->next;
+  }
+  if (NULL == cur)
+  {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
+      return;
+  }
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
+
+  ats_count = assemble_ats_information (address, &ats);
+  GAS_scheduling_transmit_address_suggestion (&address->peer,
+                                              address->plugin,
+                                              address->addr, address->addr_len,
+                                              address->session_id,
+                                              ats, ats_count,
+                                              address->assigned_bw_out,
+                                              address->assigned_bw_in);
+  GNUNET_free (ats);
+}
+
 
 /**
  * Initialize address subsystem.
@@ -1050,6 +1044,7 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
 #if HAVE_LIBGLPK
       ah->ats_mode = MODE_MLP;
       ah->s_init = &GAS_mlp_init;
+      ah->s_add = &GAS_mlp_address_add;
       ah->s_update = &GAS_mlp_address_update;
       ah->s_get = &GAS_mlp_get_preferred_address;
       ah->s_pref = &GAS_mlp_address_change_preference;
@@ -1064,6 +1059,7 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
       /* Init the simplistic solver with default values */
       ah->ats_mode = MODE_SIMPLISTIC;
       ah->s_init = &GAS_simplistic_init;
+      ah->s_add = &GAS_simplistic_address_add;
       ah->s_update = &GAS_simplistic_address_update;
       ah->s_get = &GAS_simplistic_get_preferred_address;
       ah->s_pref = &GAS_simplistic_address_change_preference;
@@ -1077,6 +1073,7 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   }
 
   GNUNET_assert (NULL != ah->s_init);
+  GNUNET_assert (NULL != ah->s_add);
   GNUNET_assert (NULL != ah->s_update);
   GNUNET_assert (NULL != ah->s_get);
   GNUNET_assert (NULL != ah->s_pref);
@@ -1085,7 +1082,7 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
 
   quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
 
-  ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count);
+  ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count, &bandwidth_changed_cb);
   if (NULL == ah->solver)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
@@ -1148,7 +1145,7 @@ GAS_addresses_done (struct GAS_Addresses_Handle *handle)
       GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
       GNUNET_free (cur);
   }
-
+  handle->s_done (handle->solver);
   GNUNET_free (handle);
   /* Stop configured solution method */