-add tests
[oweals/gnunet.git] / src / ats / plugin_ats_ril.c
index 76212e83512cd619204a474ad1bbe4449b793dfb..ea79207790872c58a9b0357d984ceebf978b8d71 100644 (file)
@@ -1,6 +1,6 @@
 /*
  This file is part of GNUnet.
- Copyright (C) 2011-2014 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2011-2014 GNUnet e.V.
 
  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
  */
 
 /**
@@ -477,23 +477,20 @@ struct GAS_RIL_Handle
  */
 static double
 agent_q (struct RIL_Peer_Agent *agent,
-         const double *state, int action)
+         const double *state,
+         int action)
 {
-  int i;
-  double result = 0;
+  unsigned int i;
+  double result = 0.0;
 
   for (i = 0; i < agent->m; i++)
-  {
     result += state[i] * agent->W[action][i];
-  }
 
-  GNUNET_assert(!isnan(result));
-
-  //prevent crash when learning diverges
+  /* prevent crashes if learning diverges */
+  if (isnan(result))
+      return isnan(result) * UINT32_MAX;
   if (isinf(result))
-  {
     return isinf(result) * UINT32_MAX;
-  }
   return result;
 }
 
@@ -937,29 +934,6 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
   return state;
 }
 
-/**
- * Retrieves an ATS information value of an address
- *
- * @param address the address in question
- * @param type the ATS information type
- * @return the value
- */
-static unsigned int
-ril_get_atsi (struct ATS_Address *address, uint32_t type)
-{
-  int c1;
-  GNUNET_assert(NULL != address);
-
-  if ((NULL == address->atsi) || (0 == address->atsi_count))
-    return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
-
-  for (c1 = 0; c1 < address->atsi_count; c1++)
-  {
-    if (ntohl (address->atsi[c1].type) == type)
-      return ntohl (address->atsi[c1].value);
-  }
-  return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
-}
 
 /**
  * Returns the utility value of the connection an agent manages
@@ -978,16 +952,12 @@ agent_get_utility (struct RIL_Peer_Agent *agent)
   preferences = agent->envi->env->get_preferences (agent->envi->env->cls,
                                                    &agent->peer);
 
-  delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
+  delay_atsi = agent->address_inuse->norm_delay.norm;
   delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
 
   pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
   pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
       sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
-//      sqrt((double) (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)/RIL_MIN_BW) * (double) (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)/RIL_MIN_BW));
-
-//  return (double) (agent->bw_in/RIL_MIN_BW);
-//  return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
   return pref_match;
 }
 
@@ -1543,10 +1513,9 @@ ril_step (struct GAS_RIL_Handle *solver);
  * no further step is scheduled.
  *
  * @param cls the solver handle
- * @param tc the task context for the scheduler
  */
 static void
-ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+ril_step_scheduler_task (void *cls)
 {
   struct GAS_RIL_Handle *solver = cls;
 
@@ -1802,9 +1771,9 @@ ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network
       if (net->type == type)
       {
         if (direction_in)
-          sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
+          sum += cur->address_inuse->norm_utilization_in.norm;
         else
-          sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
+          sum += cur->address_inuse->norm_utilization_out.norm;
       }
     }
   }
@@ -1866,7 +1835,7 @@ ril_step_schedule_next (struct GAS_RIL_Handle *solver)
   GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
   GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
 
-  time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
+  time_next = GNUNET_TIME_relative_saturating_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
 
 //  LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
 //      used_ratio,
@@ -2120,8 +2089,8 @@ ril_cut_from_vector (void **old,
   else
   {
     tmpptr = GNUNET_malloc (size);
-    memcpy (tmpptr, oldptr, bytes_before);
-    memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
+    GNUNET_memcpy (tmpptr, oldptr, bytes_before);
+    GNUNET_memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
   }
   if (NULL != *old)
   {
@@ -2257,12 +2226,10 @@ GAS_ril_address_add (void *solver,
  *
  * @param solver the solver handle
  * @param address the address to remove
- * @param session_only delete only session not whole address
  */
 static void
 GAS_ril_address_delete (void *solver,
-                       struct ATS_Address *address,
-                       int session_only)
+                       struct ATS_Address *address)
 {
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
@@ -2274,8 +2241,9 @@ GAS_ril_address_delete (void *solver,
   struct RIL_Scope *net;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
-       session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
+       "API_address_delete() Delete %s %s address %s for peer '%s'\n",
+       address->active ? "active" : "inactive",
+       address->plugin,
        address->addr,
        GNUNET_i2s (&address->peer));
 
@@ -2377,27 +2345,16 @@ GAS_ril_address_delete (void *solver,
  *
  * @param solver solver handle
  * @param address the address
- * @param type the ATSI type
- * @param abs_value the absolute value of the property
- * @param rel_value the normalized value
  */
 static void
 GAS_ril_address_property_changed (void *solver,
-                                 struct ATS_Address *address,
-                                 enum GNUNET_ATS_Property type,
-                                 uint32_t abs_value,
-                                 double rel_value)
+                                 struct ATS_Address *address)
 {
   struct GAS_RIL_Handle *s = solver;
 
   LOG(GNUNET_ERROR_TYPE_DEBUG,
-      "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
-          "to %.2f \n",
-      GNUNET_ATS_print_property_type (type),
-      GNUNET_i2s (&address->peer),
-      address->addr, rel_value);
-
-
+      "Properties for peer '%s' address changed\n",
+      GNUNET_i2s (&address->peer));
   s->parameters.temperature = s->parameters.temperature_init;
   s->parameters.epsilon = s->parameters.epsilon_init;
   ril_step (s);
@@ -2416,7 +2373,7 @@ GAS_ril_address_property_changed (void *solver,
  */
 static void
 GAS_ril_address_preference_feedback (void *solver,
-                                     struct GNUNET_SERVER_Client *application,
+                                     struct GNUNET_SERVICE_Client *application,
                                      const struct GNUNET_PeerIdentity *peer,
                                      const struct GNUNET_TIME_Relative scope,
                                      enum GNUNET_ATS_PreferenceKind kind,
@@ -2488,7 +2445,7 @@ GAS_ril_bulk_stop (void *solver)
  * @param solver the solver handle
  * @param peer the identity of the peer
  */
-static const struct ATS_Address *
+static void
 GAS_ril_get_preferred_address (void *solver,
                               const struct GNUNET_PeerIdentity *peer)
 {
@@ -2518,9 +2475,12 @@ GAS_ril_get_preferred_address (void *solver,
     s->parameters.temperature = s->parameters.temperature_init;
     s->parameters.epsilon = s->parameters.epsilon_init;
   }
-  return agent->address_inuse;
+  if (NULL != agent->address_inuse)
+    s->env->bandwidth_changed_cb (s->env->cls,
+                                  agent->address_inuse);
 }
 
+
 /**
  * Tell solver stop notifying ATS about changes for this peers
  *