modified termination condition
[oweals/gnunet.git] / src / ats / ats_api_performance.c
index 16495b6048821931debab541112bc657ca538706..acbc18e582b499983d30a5cdb97ee834b3723ccc 100644 (file)
@@ -180,16 +180,6 @@ struct GNUNET_ATS_PerformanceHandle
    */
   void *addr_info_cb_cls;
 
-  /**
-   * Callback to invoke when a peer has performance changes.
-   */
-       GNUNET_ATS_PerformanceMonitorCb perf_monitor_cb;
-
-  /**
-   * Closure for 'perf_monitor_cb'.
-   */
-       void *perf_monitor_cb_cls;
-
   /**
    * Connection to ATS service.
    */
@@ -283,6 +273,16 @@ static void
 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
 
 
+/**
+ * Type of a function to call when we receive a message
+ * from the service.
+ *
+ * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
+ * @param msg message received, NULL on timeout or fatal error
+ */
+static void
+process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg);
+
 /**
  * We can now transmit a message to ATS. Do it.
  *
@@ -308,6 +308,10 @@ transmit_message_to_ats (void *cls, size_t size, void *buf)
     ret += p->size;
     size -= p->size;
     GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
+    if (GNUNET_YES == p->is_init)
+      GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
+                            GNUNET_TIME_UNIT_FOREVER_REL);
+
     GNUNET_free (p);
   }
   do_transmit (ph);
@@ -396,15 +400,6 @@ process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
                ph->addr_info_cb (ph->addr_info_cb_cls, &address, addr_active, pi->bandwidth_out, pi->bandwidth_in,
               atsi, ats_count);
   }
-  if ((NULL != ph->perf_monitor_cb) &&
-               (GNUNET_YES == addr_active))
-  {
-               ph->perf_monitor_cb (ph->perf_monitor_cb_cls,
-                                                                                               &pi->peer,
-                                                                                               pi->bandwidth_out,
-                                                                                               pi->bandwidth_in,
-                                                                                               atsi, ats_count);
-  }
   return GNUNET_OK;
 }
 
@@ -628,8 +623,6 @@ reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
   GNUNET_assert (NULL == ph->client);
   ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
   GNUNET_assert (NULL != ph->client);
-  GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
-                         GNUNET_TIME_UNIT_FOREVER_REL);
   if ((NULL == (p = ph->pending_head)) || (GNUNET_YES != p->is_init))
   {
     p = GNUNET_malloc (sizeof (struct PendingMessage) +
@@ -640,7 +633,7 @@ reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
     init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
     init->header.size = htons (sizeof (struct ClientStartMessage));
     init->start_flag =
-        htonl (((NULL ==ph->addr_info_cb) && (NULL == ph->perf_monitor_cb)) ?
+        htonl ((NULL ==ph->addr_info_cb) ?
                        START_FLAG_PERFORMANCE_NO_PIC : START_FLAG_PERFORMANCE_WITH_PIC);
     GNUNET_CONTAINER_DLL_insert (ph->pending_head, ph->pending_tail, p);
   }
@@ -653,9 +646,6 @@ reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
  * Get handle to access performance API of the ATS subsystem.
  *
  * @param cfg configuration to use
- * @param perf_monitor_cb callback called when performance characteristics for
- *     a peer change
- * @param perf_monitor_cb closure for the perf_monitor_cb
  * @param addr_info_cb callback called when performance characteristics for
  *     an address change
  * @param addr_info_cb_cls closure for infocb
@@ -663,8 +653,6 @@ reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
  */
 struct GNUNET_ATS_PerformanceHandle *
 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                                                                                                                GNUNET_ATS_PerformanceMonitorCb perf_monitor_cb,
-                                                                                                                void *perf_monitor_cb_cls,
                              GNUNET_ATS_AddressInformationCallback addr_info_cb,
                              void *addr_info_cb_cls)
 {
@@ -674,8 +662,6 @@ GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   ph->cfg = cfg;
   ph->addr_info_cb = addr_info_cb;
   ph->addr_info_cb_cls = addr_info_cb_cls;
-  ph->perf_monitor_cb = perf_monitor_cb;
-  ph->perf_monitor_cb_cls = perf_monitor_cb_cls;
   ph->id  = 0;
   reconnect (ph);
   return ph;
@@ -892,7 +878,7 @@ GNUNET_ATS_print_preference_type (uint32_t type)
  * @param ... 0-terminated specification of the desired changes
  */
 void
-GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
+GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
                               const struct GNUNET_PeerIdentity *peer, ...)
 {
   struct PendingMessage *p;
@@ -964,4 +950,88 @@ GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
   do_transmit (ph);
 }
 
+/**
+ * Send feedback to ATS on how good a the requirements for a peer and a
+ * preference is satisfied by ATS
+ *
+ * @param ph performance handle
+ * @param scope the time interval this valid for: [now - scope .. now]
+ * @param peer identifies the peer
+ * @param ... 0-terminated specification of the desired changes
+ */
+void
+GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
+                                                                                                                                                       const struct GNUNET_PeerIdentity *peer,
+                                                                                                                                                       const struct GNUNET_TIME_Relative scope, ...)
+{
+  struct PendingMessage *p;
+  struct FeedbackPreferenceMessage *m;
+  size_t msize;
+  uint32_t count;
+  struct PreferenceInformation *pi;
+  va_list ap;
+  enum GNUNET_ATS_PreferenceKind kind;
+
+  count = 0;
+  va_start (ap, scope);
+  while (GNUNET_ATS_PREFERENCE_END !=
+         (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
+  {
+    switch (kind)
+    {
+    case GNUNET_ATS_PREFERENCE_BANDWIDTH:
+      count++;
+      (void) va_arg (ap, double);
+
+      break;
+    case GNUNET_ATS_PREFERENCE_LATENCY:
+      count++;
+      (void) va_arg (ap, double);
+
+      break;
+    default:
+      GNUNET_assert (0);
+    }
+  }
+  va_end (ap);
+  msize =
+      count * sizeof (struct PreferenceInformation) +
+      sizeof (struct FeedbackPreferenceMessage);
+  p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
+  p->size = msize;
+  p->is_init = GNUNET_NO;
+  m = (struct FeedbackPreferenceMessage *) &p[1];
+  m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK);
+  m->header.size = htons (msize);
+  m->scope = GNUNET_TIME_relative_hton (scope);
+  m->num_feedback = htonl (count);
+  m->peer = *peer;
+  pi = (struct PreferenceInformation *) &m[1];
+  count = 0;
+  va_start (ap, scope);
+  while (GNUNET_ATS_PREFERENCE_END !=
+         (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
+  {
+    pi[count].preference_kind = htonl (kind);
+    switch (kind)
+    {
+    case GNUNET_ATS_PREFERENCE_BANDWIDTH:
+      pi[count].preference_value = (float) va_arg (ap, double);
+
+      count++;
+      break;
+    case GNUNET_ATS_PREFERENCE_LATENCY:
+      pi[count].preference_value = (float) va_arg (ap, double);
+
+      count++;
+      break;
+    default:
+      GNUNET_assert (0);
+    }
+  }
+  va_end (ap);
+  GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
+  do_transmit (ph);
+}
+
 /* end of ats_api_performance.c */