-need to do mkdir before testing UNIXPATH, not sure how this worked before
[oweals/gnunet.git] / src / ats / libgnunet_plugin_ats_ril.c
index 0f2a4c73baa8c7254ac9d6dcb24e804d7b5b179f..2a35007f3b175161afdd3a894d2a677f0b549544 100755 (executable)
@@ -151,7 +151,7 @@ struct RIL_Peer_Agent
   /**
    * Whether the agent is active or not
    */
-  int active; //TODO? rename into something better. It reflects the state whether get_preferred_address() has been called for the according peer or not
+  int active;
 
   /**
    * Number of performed time-steps
@@ -572,7 +572,7 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
       solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
           new_address);
     }
-    else
+    else if (agent->address_inuse)
     {
       GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
       GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
@@ -696,7 +696,8 @@ static double
 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 {
   /*
-   * Match the preferences of the peer with the current assignment.
+   * - Match the preferences of the peer with the current assignment
+   * - Validity of the solution
    */
   const double *preferences;
   const double *properties;
@@ -710,7 +711,7 @@ envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
   properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
       agent->address_inuse);
   prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
-  pref_match += preferences[GNUNET_ATS_PREFERENCE_LATENCY] * properties[prop_index];
+  pref_match += preferences[GNUNET_ATS_PREFERENCE_LATENCY] * (3 - properties[prop_index]); //invert property as we want to maximize for lower latencies
   bw_norm = GNUNET_MAX(2, (((
                   ((double) agent->bw_in / (double) ril_get_max_bw(agent, GNUNET_YES)) +
                   ((double) agent->bw_out / (double) ril_get_max_bw(agent, GNUNET_NO))
@@ -719,7 +720,7 @@ envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
   pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm;
 
   net = agent->address_inuse->solver_information;
-  if ((net->bw_in_assigned > net->bw_in_available) || net->bw_out_assigned > net->bw_out_available)
+  if ((net->bw_in_assigned > net->bw_in_available) || (net->bw_out_assigned > net->bw_out_available))
   {
     return -1;
   }
@@ -884,7 +885,7 @@ envi_action_address_switch (struct GAS_RIL_Handle *solver,
 static void
 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
 {
-  unsigned int address_index;
+  int address_index;
 
   switch (action)
   {
@@ -1050,7 +1051,7 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
   agent->active = GNUNET_NO;
   agent->n = RIL_ACTION_TYPE_NUM;
   agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
-  agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
+  agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
   for (i = 0; i < agent->n; i++)
   {
     agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
@@ -1429,7 +1430,7 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
 
   //increase size of old state vector
   agent->m = m_old;
-  GNUNET_array_grow(agent->s_old, agent->m, m_new); //TODO random instead of zero-initialization of state features
+  GNUNET_array_grow(agent->s_old, agent->m, m_new);
 
   agent->m = m_old;
   GNUNET_array_grow(agent->e, agent->m, m_new);
@@ -1697,7 +1698,6 @@ GAS_ril_address_preference_feedback (void *solver,
     enum GNUNET_ATS_PreferenceKind kind,
     double score)
 {
-  //TODO! collect feedback
   LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
           "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
@@ -1750,11 +1750,18 @@ GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *p
    */
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
+  struct RIL_Network *net;
 
   agent = ril_get_agent (s, peer, GNUNET_YES);
 
   agent->active = GNUNET_YES;
 
+  if (agent->address_inuse)
+  {
+    net = agent->address_inuse->solver_information;
+    net->bw_in_assigned += agent->bw_in;
+    net->bw_out_assigned += agent->bw_out;
+  }
   envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
       GNUNET_YES);
 
@@ -1788,6 +1795,7 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
 {
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
+  struct RIL_Network *net;
 
   agent = ril_get_agent (s, peer, GNUNET_NO);
 
@@ -1803,6 +1811,12 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
   }
 
   agent->active = GNUNET_NO;
+  if (agent->address_inuse)
+  {
+    net = agent->address_inuse->solver_information;
+    net->bw_in_assigned -= agent->bw_in;
+    net->bw_out_assigned -= agent->bw_out;
+  }
   envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
       GNUNET_YES);