changes
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses_simplistic.c
index 670b9edf221eacb302c4159e856e067b6ce379d6..f92c292476dae563f5cdb1af6071fb20aa3d9bca 100644 (file)
  */
 struct GAS_SIMPLISTIC_Handle
 {
+
   unsigned int active_addresses;
+
+  unsigned int networks;
+
+  /**
+   * Network type array
+   *
+   * quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
+   *
+   */
   int *quota_net;
-  unsigned long long *quota_in;
-  unsigned long long *quota_out;
-};
 
+  /**
+   * Array of inbound quotas
+   *
+   */
+  unsigned long long *total_quota_in;
+
+  /**
+   * Array of outbound quotas
+   *
+   */
+  unsigned long long *total_quota_out;
+
+  /**
+   * Active addresses per network type
+   */
+  unsigned int *active_addresses_per_net;
+};
 
 /**
  * Init the simplistic problem solving component
  *
+ * Quotas:
+ * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
+ * out_quota[i] contains outbound quota for network type i
+ * in_quota[i] contains inbound quota for network type i
+ *
+ * Example
+ * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
+ * network[2]   == GNUNET_ATS_NET_LAN
+ * out_quota[2] == 65353
+ * in_quota[2]  == 65353
+ *
  * @param cfg configuration handle
  * @param stats the GNUNET_STATISTICS handle
+ * @param network array of GNUNET_ATS_NetworkType with length dest_length
+ * @param out_quota array of outbound quotas
+ * param in_quota array of outbound quota
  * @return handle for the solver on success, NULL on fail
  */
 void *
 GAS_simplistic_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
                      const struct GNUNET_STATISTICS_Handle *stats,
                      int *network,
-                     unsigned long long *out_dest,
-                     unsigned long long *in_dest,
+                     unsigned long long *out_quota,
+                     unsigned long long *in_quota,
                      int dest_length)
 {
   struct GAS_SIMPLISTIC_Handle *solver = GNUNET_malloc (sizeof (struct GAS_SIMPLISTIC_Handle));
 
+  solver->networks = dest_length;
+
   solver->quota_net = GNUNET_malloc (dest_length * sizeof (int));
   memcpy (solver->quota_net, network, dest_length * sizeof (int));
 
-  solver->quota_in  = GNUNET_malloc (dest_length * sizeof (unsigned long long));
-  memcpy (solver->quota_in, out_dest, dest_length * sizeof (int));
+  solver->total_quota_in  = GNUNET_malloc (dest_length * sizeof (unsigned long long));
+  memcpy (solver->total_quota_in, in_quota, dest_length * sizeof (int));
+
+  solver->total_quota_out = GNUNET_malloc (dest_length * sizeof (unsigned long long));
+  memcpy (solver->total_quota_out, out_quota, dest_length * sizeof (unsigned long long));
 
-  solver->quota_out = GNUNET_malloc (dest_length * sizeof (unsigned long long));
-  memcpy (solver->quota_out, out_dest, dest_length * sizeof (unsigned long long));
+  solver->active_addresses_per_net = GNUNET_malloc (dest_length * sizeof (unsigned int));
 
   return solver;
 }
@@ -84,11 +126,58 @@ GAS_simplistic_done (void *solver)
   struct GAS_SIMPLISTIC_Handle *s = solver;
   GNUNET_assert (s != NULL);
   GNUNET_free (s->quota_net);
-  GNUNET_free (s->quota_in);
-  GNUNET_free (s->quota_out);
+  GNUNET_free (s->total_quota_in);
+  GNUNET_free (s->total_quota_out);
+  GNUNET_free (s->active_addresses_per_net);
   GNUNET_free (s);
 }
 
+static void
+update_quota (struct GAS_SIMPLISTIC_Handle *s, unsigned int net)
+{
+  unsigned long long quota_in;
+  unsigned long long quota_out;
+
+  quota_in = s->total_quota_in[net] / s->active_addresses_per_net[net];
+  quota_out = s->total_quota_out[net] / s->active_addresses_per_net[net];
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+              "New quota for network type %u (in/out): %llu/%llu \n",
+              net, quota_in, quota_out);
+}
+
+/**
+ * Add a single address to the solve
+ *
+ * @param solver the solver Handle
+ * @param addresses the address hashmap containing all addresses
+ * @param address the address to add
+ */
+void
+GAS_simplistic_address_add (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
+{
+  struct GAS_SIMPLISTIC_Handle *s = solver;
+  GNUNET_assert (NULL != s);
+  int c;
+  for (c = 0; c < s->networks; c++)
+  {
+      if (address->atsp_network_type == s->quota_net[c])
+      {
+          s->active_addresses_per_net[c] ++;
+          LOG (GNUNET_ERROR_TYPE_DEBUG,
+                      "Adding new address for network type %u (now %u total)\n",
+                      address->atsp_network_type,
+                      s->active_addresses_per_net[c]);
+          break;
+      }
+  }
+
+  /* Update quota for this network type */
+  update_quota (s, c);
+}
+
+
+
 /**
  * Updates a single address in the solve
  *
@@ -99,7 +188,25 @@ GAS_simplistic_done (void *solver)
 void
 GAS_simplistic_address_update (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
 {
+#if 0
+  struct GAS_SIMPLISTIC_Handle *s = solver;
+  GNUNET_assert (NULL != s);
+  int c;
+  for (c = 0; c < s->networks; c++)
+  {
+      if (address->atsp_network_type == s->quota_net[c])
+      {
+          LOG (GNUNET_ERROR_TYPE_DEBUG,
+                      "Updating address for network type %u (%u total)\n",
+                      address->atsp_network_type,
+                      s->active_addresses_per_net[c]);
+          break;
+      }
+  }
 
+  /* Update quota for this network type */
+  update_quota (s, c);
+#endif
 }
 
 
@@ -113,7 +220,27 @@ GAS_simplistic_address_update (void *solver, struct GNUNET_CONTAINER_MultiHashMa
 void
 GAS_simplistic_address_delete (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
 {
+#if 0
+  struct GAS_SIMPLISTIC_Handle *s = solver;
+  GNUNET_assert (NULL != s);
+  int c;
+  for (c = 0; c < s->networks; c++)
+  {
+      if (address->atsp_network_type == s->quota_net[c])
+      {
+          GNUNET_assert (s->active_addresses_per_net[c] > 0);
+          s->active_addresses_per_net[c] --;
+          LOG (GNUNET_ERROR_TYPE_DEBUG,
+                      "Deleting address for network type %u (now %u total)\n",
+                      address->atsp_network_type,
+                      s->active_addresses_per_net[c]);
+          break;
+      }
+  }
 
+  /* Update quota for this network type */
+  update_quota (s, c);
+#endif
 }
 
 
@@ -148,13 +275,6 @@ find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
     return GNUNET_OK;
   }
 
-  current->block_interval = GNUNET_TIME_relative_add (current->block_interval, ATS_BLOCKING_DELTA);
-  current->blocked_until = GNUNET_TIME_absolute_add (now, current->block_interval);
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Address %p ready for suggestion, block interval now %llu \n",
-       current, current->block_interval);
-
   if (NULL != previous)
   {
     if ((0 == strcmp (previous->plugin, "tcp")) &&
@@ -219,8 +339,6 @@ update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
   aa->assigned_bw_in.value__ = htonl (UINT32_MAX / s->active_addresses);
   aa->assigned_bw_out.value__ = htonl (UINT32_MAX / s->active_addresses);
 
-  //send_bw_notification (aa);
-
   return GNUNET_OK;
 }
 
@@ -262,13 +380,15 @@ GAS_simplistic_get_preferred_address (void *solver,
   if (NULL == aa)
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
   else
+  {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
 
-  if (GNUNET_NO == aa->active)
-  {
-    aa->active = GNUNET_YES;
-    s->active_addresses++;
-    recalculate_assigned_bw (s, addresses);
+    if (GNUNET_NO == aa->active)
+    {
+      aa->active = GNUNET_YES;
+      s->active_addresses++;
+      recalculate_assigned_bw (s, addresses);
+    }
   }
 
   return aa;
@@ -289,7 +409,7 @@ GAS_simplistic_address_change_preference (void *solver,
                                    enum GNUNET_ATS_PreferenceKind kind,
                                    float score)
 {
-
+  /* FIXME : implement this */
 }
 
 /* end of gnunet-service-ats_addresses_simplistic.c */