Should be compilable without libglpk again
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.c
index 9e388624812c62020e8cfa826c6082a3ab2ebd17..b42fadb9bef53084b111650df1b225a3069f2014 100644 (file)
 #include "gnunet-service-ats_performance.h"
 #include "gnunet-service-ats_scheduling.h"
 #include "gnunet-service-ats_reservations.h"
-#if HAVE_LIBGLPK
 #include "gnunet-service-ats_addresses_mlp.h"
-#endif
 #include "gnunet-service-ats_addresses_simplistic.h"
 
 
-#define BIG_M_VALUE (UINT32_MAX) /10
-#define BIG_M_STRING "unlimited"
-
 /**
  * Available ressource assignment modes
  */
@@ -94,11 +89,6 @@ struct GAS_Addresses_Handle
    */
   unsigned long long wan_quota_out;
 
-  /**
-   * Number of active addresses
-   */
-  unsigned int active_addr_count;
-
   /**
    * Is ATS addresses running
    */
@@ -165,7 +155,7 @@ struct GAS_Addresses_Handle *handle;
 
 
 static unsigned int
-assemble_ats_information (struct ATS_Address *aa,  struct GNUNET_ATS_Information **dest)
+assemble_ats_information (const struct ATS_Address *aa,  struct GNUNET_ATS_Information **dest)
 {
   unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
   struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
@@ -190,76 +180,6 @@ assemble_ats_information (struct ATS_Address *aa,  struct GNUNET_ATS_Information
   return ats_count;
 }
 
-static void
-send_bw_notification (struct ATS_Address *aa)
-{
-  struct GNUNET_ATS_Information *ats;
-  uint32_t ats_count;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
-              GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
-              ntohl (aa->assigned_bw_out.value__));
-  ats_count = assemble_ats_information (aa, &ats);
-
-  GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
-                                              aa->addr_len, aa->session_id,
-                                              ats, ats_count,
-                                              aa->assigned_bw_out,
-                                              aa->assigned_bw_in);
-  GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
-  GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
-                                  aa->active,
-                                  ats, ats_count, aa->assigned_bw_out,
-                                  aa->assigned_bw_in);
-  GNUNET_free (ats);
-}
-
-/**
- * Update a bandwidth assignment for a peer.  This trivial method currently
- * simply assigns the same share to all active connections.
- *
- * @param cls unused
- * @param key unused
- * @param value the 'struct ATS_Address'
- * @return GNUNET_OK (continue to iterate)
- */
-static int
-update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
-{
-  struct ATS_Address *aa = value;
-
-  if (GNUNET_YES != aa->active)
-    return GNUNET_OK;
-  GNUNET_assert (handle->active_addr_count > 0);
-
-
-  /* Simple method */
-  aa->assigned_bw_in.value__ = htonl (handle->wan_quota_in / handle->active_addr_count);
-  aa->assigned_bw_out.value__ = htonl (handle->wan_quota_out / handle->active_addr_count);
-
-  send_bw_notification (aa);
-
-  return GNUNET_OK;
-}
-
-
-/**
- * Some (significant) input changed, recalculate bandwidth assignment
- * for all peers.
- */
-static void
-recalculate_assigned_bw ()
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Recalculating bandwidth for all active connections\n");
-  GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
-                            1, GNUNET_NO);
-  GNUNET_STATISTICS_set (GSA_stats, "# active addresses", handle->active_addr_count,
-                         GNUNET_NO);
-
-  GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &update_bw_simple_it, NULL);
-}
-
 /**
  * Free the given address
  * @param addr address to destroy
@@ -317,17 +237,7 @@ destroy_address (struct ATS_Address *addr)
                                                        &addr->peer.hashPubKey,
                                                        addr));
 
-#if HAVE_LIBGLPK
-  if (handle->ats_mode == MODE_MLP)
-    GAS_mlp_address_delete (handle->solver, handle->addresses, addr);
-#endif
-
-  if (GNUNET_YES == addr->active)
-  {
-    handle->active_addr_count--;
-    addr->active = GNUNET_NO;
-    ret = GNUNET_YES;
-  }
+  handle->s_del (handle->solver, handle->addresses, addr);
   free_address (addr);
   return ret;
 }
@@ -544,7 +454,6 @@ GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
                        plugin_name,
                        plugin_addr, plugin_addr_len,
                        session_id);
-
   aa->mlp_information = NULL;
   aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
   aa->ats_count = atsi_count;
@@ -561,31 +470,34 @@ 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);
     return;
   }
 
-  if (old->session_id == 0)
+  if (old->session_id != 0)
   {
-    /* We have a base address with out an session, update this address */
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Updated existing address for peer `%s' %p with new session %u\n",
-              GNUNET_i2s (peer), old, session_id);
-    GNUNET_free_non_null (old->ats);
-    old->session_id = session_id;
-    old->ats = NULL;
-    old->ats_count = 0;
-    old->ats = aa->ats;
-    old->ats_count = aa->ats_count;
-    GNUNET_free (aa->plugin);
-    GNUNET_free (aa);
-    return;
+      /* This address and session is already existing */
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Added already existing address for peer `%s' `%s' %p with new session %u\n",
+                GNUNET_i2s (peer), plugin_name, session_id);
+      GNUNET_break (0);
+      return;
   }
 
-  /* This address and session is already existing */
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-            "Added already existing address for peer `%s' `%s' %p with new session %u\n",
-            GNUNET_i2s (peer), plugin_name, session_id);
-  GNUNET_break (0);
+  /* We have an address without an session, update this address */
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+            "Updated existing address for peer `%s' %p with new session %u\n",
+            GNUNET_i2s (peer), old, session_id);
+  GNUNET_free_non_null (old->ats);
+  old->session_id = session_id;
+  old->ats = NULL;
+  old->ats_count = 0;
+  old->ats = aa->ats;
+  old->ats_count = aa->ats_count;
+  GNUNET_free (aa->plugin);
+  GNUNET_free (aa);
+  handle->s_update (handle->solver, handle->addresses, old);
 }
 
 
@@ -649,7 +561,6 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
       GNUNET_break (0);
       break;
     }
-
   /* Tell solver about update */
   handle->s_update (handle->solver, handle->addresses, old);
 }
@@ -686,8 +597,8 @@ destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *valu
                 "Deleting address for peer `%s': `%s' %u\n",
                 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
 
-    if (GNUNET_YES == destroy_address (aa))
-      recalculate_assigned_bw ();
+    destroy_address (aa);
+      // FIXME if (GNUNET_YES == destroy_address (aa))recalculate_assigned_bw ();
     return GNUNET_OK;
   }
   /* session != 0, just remove session */
@@ -704,8 +615,7 @@ destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *valu
   if (GNUNET_YES == aa->active)
   {
     aa->active = GNUNET_NO;
-    handle->active_addr_count--;
-    recalculate_assigned_bw ();
+    //FIXME recalculate_assigned_bw ();
   }
 
   /* session == 0 and addrlen == 0 : destroy address */
@@ -750,13 +660,10 @@ GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
     return;
   }
 
-
   GNUNET_break (0 < strlen (plugin_name));
   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
-
   GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
                                               &destroy_by_session_id, aa);
-
   free_address (aa);
 }
 
@@ -807,50 +714,6 @@ GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
 }
 
 
-static void 
-request_address_mlp (const struct GNUNET_PeerIdentity *peer)
-{
-  struct ATS_Address *aa;
-  aa = NULL;
-
-#if HAVE_GLPK
-  /* Get preferred address from MODE_MLP */
-  struct ATS_PreferedAddress * paddr = NULL;
-  paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
-  aa = paddr->address;
-  aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
-  /* FIXME use bw in value */
-  paddr->bandwidth_in = paddr->bandwidth_out;
-  aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
-  GNUNET_free (paddr);
-#endif
-
-  if (aa == NULL)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
-                "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
-    return;
-  }
-  if (aa->active == GNUNET_NO)
-  {
-    aa->active = GNUNET_YES;
-    handle->active_addr_count++;
-    send_bw_notification (aa);
-  }
-  else
-  {
-    /* just to be sure... */
-    GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
-                                                aa->addr_len, aa->session_id,
-                                                aa->ats, aa->ats_count,
-                                                aa->assigned_bw_out,
-                                                aa->assigned_bw_in);
-  }
-
-}
-
-
-
 /**
  * Cancel address suggestions for a peer
  *
@@ -869,7 +732,8 @@ GAS_addresses_request_address_cancel (const struct GNUNET_PeerIdentity *peer)
 
   if (NULL == cur)
   {
-      GNUNET_break (0);
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
       return;
   }
   GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
@@ -886,7 +750,7 @@ void
 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
 {
   struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
-  struct ATS_Address *aa;
+  const struct ATS_Address *aa;
   struct GNUNET_ATS_Information *ats;
   unsigned int ats_count;
 
@@ -905,6 +769,7 @@ GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
       GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
   }
 
+  /* Get prefered address from solver */
   aa = handle->s_get (handle->solver, handle->addresses, peer);
   if (NULL == aa)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -1184,7 +1049,6 @@ GAS_addresses_destroy_all ()
 
   if (handle->addresses != NULL)
     GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, NULL);
-  GNUNET_assert (handle->active_addr_count == 0);
 }