- cleanup
[oweals/gnunet.git] / src / ats / ats_api_peer_change_preference.c
index 84fc2bf6c2307b62e5e15f9569cde9cfabfa0a13..bff9306e4a359e3fd8313852d84bad6bff1c0e95 100644 (file)
@@ -40,12 +40,7 @@ struct GNUNET_ATS_InformationRequestContext
   /**
    * Our connection to the service.
    */
-  struct GNUNET_ATS_Handle *h;
-
-  /**
-   * Link to peer record.
-   */
-  struct AllocationRecord *ar;
+  struct GNUNET_ATS_SchedulingHandle *h;
 
   int32_t amount;
 
@@ -54,25 +49,35 @@ struct GNUNET_ATS_InformationRequestContext
   GNUNET_ATS_PeerConfigurationInfoCallback info;
 
   void *info_cls;
-  
+
   struct GNUNET_PeerIdentity peer;
-  
+
   GNUNET_SCHEDULER_TaskIdentifier task;
 
 };
 
 
 static void
-exec_pcp (void *cls,
-         const struct GNUNET_SCHEDULER_TaskContext *tc)
+exec_pcp (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_ATS_InformationRequestContext *irc = cls;
   int32_t want_reserv;
   int32_t got_reserv;
   struct GNUNET_TIME_Relative rdelay;
+  struct AllocationRecord *ar;
 
   rdelay = GNUNET_TIME_UNIT_ZERO;
   want_reserv = irc->amount;
+  ar = GNUNET_CONTAINER_multihashmap_get (irc->h->peers, &irc->peer.hashPubKey);
+  if (NULL == ar)
+  {
+    /* attempt to change preference on peer that is not connected */
+    /* FIXME: this can happen if the 'service' didn't yet tell us about
+     * a new connection, fake it! */
+    irc->info (irc->info_cls, &irc->peer, want_reserv, rdelay);
+    GNUNET_free (irc);
+    return;
+  }
   if (want_reserv < 0)
   {
     got_reserv = want_reserv;
@@ -80,21 +85,18 @@ exec_pcp (void *cls,
   else if (want_reserv > 0)
   {
     rdelay =
-      GNUNET_BANDWIDTH_tracker_get_delay (&irc->ar->available_recv_window,
-                                         want_reserv);
+        GNUNET_BANDWIDTH_tracker_get_delay (&ar->available_recv_window,
+                                            want_reserv);
     if (rdelay.rel_value == 0)
       got_reserv = want_reserv;
     else
-      got_reserv = 0;         /* all or nothing */
+      got_reserv = 0;           /* all or nothing */
   }
   else
     got_reserv = 0;
-  GNUNET_BANDWIDTH_tracker_consume (&irc->ar->available_recv_window, got_reserv);
+  GNUNET_BANDWIDTH_tracker_consume (&ar->available_recv_window, got_reserv);
 
-  irc->info (irc->info_cls,
-            &irc->peer,
-            got_reserv,
-            rdelay);
+  irc->info (irc->info_cls, &irc->peer, got_reserv, rdelay);
   GNUNET_free (irc);
 }
 
@@ -115,26 +117,17 @@ exec_pcp (void *cls,
  * @return NULL on error
  */
 struct GNUNET_ATS_InformationRequestContext *
-GNUNET_ATS_peer_change_preference (struct GNUNET_ATS_Handle *h,
-                                  const struct GNUNET_PeerIdentity *peer,
-                                    int32_t amount, uint64_t preference,
-                                    GNUNET_ATS_PeerConfigurationInfoCallback
-                                    info, void *info_cls)
+GNUNET_ATS_peer_change_preference (struct GNUNET_ATS_SchedulingHandle *h,
+                                   const struct GNUNET_PeerIdentity *peer,
+                                   int32_t amount, uint64_t preference,
+                                   GNUNET_ATS_PeerConfigurationInfoCallback
+                                   info, void *info_cls)
 {
   struct GNUNET_ATS_InformationRequestContext *irc;
-  struct AllocationRecord *ar;
 
-  ar = GNUNET_CONTAINER_multihashmap_get (h->peers, &peer->hashPubKey);
-  if (NULL == ar)
-  {
-    /* attempt to change preference on peer that is not connected */
-    GNUNET_assert (0);
-    return NULL;
-  }
   irc = GNUNET_malloc (sizeof (struct GNUNET_ATS_InformationRequestContext));
   irc->h = h;
   irc->peer = *peer;
-  irc->ar = ar;
   irc->amount = amount;
   irc->preference = preference;
   irc->info = info;
@@ -157,8 +150,8 @@ GNUNET_ATS_peer_change_preference (struct GNUNET_ATS_Handle *h,
  */
 void
 GNUNET_ATS_peer_change_preference_cancel (struct
-                                           GNUNET_ATS_InformationRequestContext
-                                           *irc)
+                                          GNUNET_ATS_InformationRequestContext
+                                          *irc)
 {
   GNUNET_SCHEDULER_cancel (irc->task);
   GNUNET_free (irc);
@@ -173,7 +166,7 @@ struct GNUNET_ATS_InformationRequestContext
   /**
    * Our connection to the service.
    */
-  struct GNUNET_ATS_Handle *h;
+  struct GNUNET_ATS_SchedulingHandle *h;
 
   /**
    * Link to control message, NULL if CM was sent.
@@ -218,11 +211,11 @@ change_preference_send_continuation (void *cls, int success)
  * @return NULL on error
  */
 struct GNUNET_ATS_InformationRequestContext *
-GNUNET_ATS_peer_change_preference (struct GNUNET_ATS_Handle *h,
-                                    const struct GNUNET_PeerIdentity *peer,
-                                    int32_t amount, uint64_t preference,
-                                    GNUNET_ATS_PeerConfigurationInfoCallback
-                                    info, void *info_cls)
+GNUNET_ATS_peer_change_preference (struct GNUNET_ATS_SchedulingHandle *h,
+                                   const struct GNUNET_PeerIdentity *peer,
+                                   int32_t amount, uint64_t preference,
+                                   GNUNET_ATS_PeerConfigurationInfoCallback
+                                   info, void *info_cls)
 {
   struct GNUNET_ATS_InformationRequestContext *irc;
   struct PeerRecord *pr;
@@ -258,11 +251,9 @@ GNUNET_ATS_peer_change_preference (struct GNUNET_ATS_Handle *h,
   rim->reserve_inbound = htonl (amount);
   rim->preference_change = GNUNET_htonll (preference);
   rim->peer = *peer;
-#if DEBUG_ATS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Queueing CHANGE PREFERENCE request for peer `%s' with RIM %u\n",
               GNUNET_i2s (peer), (unsigned int) pr->rim_id);
-#endif
   GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
                                     h->control_pending_tail, cm);
   pr->pcic = info;
@@ -287,10 +278,10 @@ GNUNET_ATS_peer_change_preference (struct GNUNET_ATS_Handle *h,
  */
 void
 GNUNET_ATS_peer_change_preference_cancel (struct
-                                           GNUNET_ATS_InformationRequestContext
-                                           *irc)
+                                          GNUNET_ATS_InformationRequestContext
+                                          *irc)
 {
-  struct GNUNET_ATS_Handle *h = irc->h;
+  struct GNUNET_ATS_SchedulingHandle *h = irc->h;
   struct PeerRecord *pr = irc->pr;
 
   GNUNET_assert (pr->pcic_ptr == irc);