use LOG macro in ats_api.c
[oweals/gnunet.git] / src / ats / ats_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file ats/ats_api.c
22  * @brief automatic transport selection API
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  *
26  * TODO:
27  * - write test case
28  * - extend API to get performance data
29  * - implement simplistic strategy based on say 'lowest latency' or strict ordering
30  * - extend API to get peer preferences, implement proportional bandwidth assignment
31  * - re-implement API against a real ATS service (!)
32  */
33 #include "platform.h"
34 #include "gnunet_ats_service.h"
35 #include "ats_api.h"
36
37 #define DEBUG_ATS GNUNET_EXTRA_LOGGING
38
39 #define LOG(kind,...) GNUNET_log_from (kind, "ats-api", __VA_ARGS__)
40
41 /**
42  * Receive and send buffer windows grow over time.  For
43  * how long can 'unused' bandwidth accumulate before we
44  * need to cap it?  (specified in seconds).
45  */
46 #define MAX_WINDOW_TIME_S (5 * 60)
47
48 // NOTE: this implementation is simply supposed
49 // to implement a simplistic strategy in-process;
50 // in the future, we plan to replace it with a real
51 // service implementation
52
53
54 /**
55  * Opaque handle to obtain address suggestions.
56  */
57 struct GNUNET_ATS_SuggestionContext
58 {
59
60   /**
61    * Function to call with our final suggestion.
62    */
63   GNUNET_ATS_AddressSuggestionCallback cb;
64
65   /**
66    * Closure for 'cb'.
67    */
68   void *cb_cls;
69
70   /**
71    * Global ATS handle.
72    */
73   struct GNUNET_ATS_Handle *atc;
74
75   /**
76    * Which peer are we monitoring?
77    */
78   struct GNUNET_PeerIdentity target;
79
80 };
81
82
83 /**
84  * Count number of connected records.
85  *
86  * @param cls pointer to counter
87  * @param key identity of the peer associated with the records
88  * @param value a 'struct AllocationRecord'
89  * @return GNUNET_YES (continue iteration)
90  */
91 static int
92 count_connections (void *cls, const GNUNET_HashCode * key, void *value)
93 {
94   unsigned int *ac = cls;
95   struct AllocationRecord *ar = value;
96
97   if (GNUNET_YES == ar->connected)
98     (*ac)++;
99   return GNUNET_YES;
100 }
101
102
103 /**
104  * Closure for 'set_bw_connections'.
105  */
106 struct SetBandwidthContext
107 {
108   /**
109    * ATS handle.
110    */
111   struct GNUNET_ATS_Handle *atc;
112
113   /**
114    * Inbound bandwidth to assign.
115    */
116   struct GNUNET_BANDWIDTH_Value32NBO bw_in;
117
118   /**
119    * Outbound bandwidth to assign.
120    */
121   struct GNUNET_BANDWIDTH_Value32NBO bw_out;
122 };
123
124
125 /**
126  * Set bandwidth based on record.
127  *
128  * @param cls 'struct SetBandwidthContext'
129  * @param key identity of the peer associated with the records
130  * @param value a 'struct AllocationRecord'
131  * @return GNUNET_YES (continue iteration)
132  */
133 static int
134 set_bw_connections (void *cls, const GNUNET_HashCode * key, void *value)
135 {
136   struct SetBandwidthContext *sbc = cls;
137   struct AllocationRecord *ar = value;
138
139   if (GNUNET_YES == ar->connected)
140   {
141     ar->bandwidth_in = sbc->bw_in;
142     ar->bandwidth_out = sbc->bw_out;
143     GNUNET_BANDWIDTH_tracker_update_quota (&ar->available_recv_window,
144                                            ar->bandwidth_in);
145     if (NULL != sbc->atc->alloc_cb)
146       sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
147                           (const struct GNUNET_PeerIdentity *) key,
148                           ar->plugin_name, ar->session, ar->plugin_addr,
149                           ar->plugin_addr_len, ar->bandwidth_out,
150                           ar->bandwidth_in);
151   }
152   else if (ntohl (ar->bandwidth_out.value__) > 0)
153   {
154     ar->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
155     ar->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
156     if (NULL != sbc->atc->alloc_cb)
157       sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
158                           (const struct GNUNET_PeerIdentity *) key,
159                           ar->plugin_name, ar->session, ar->plugin_addr,
160                           ar->plugin_addr_len, ar->bandwidth_out,
161                           ar->bandwidth_in);
162   }
163   return GNUNET_YES;
164 }
165
166
167 /**
168  * Task run to update bandwidth assignments.
169  *
170  * @param cls the 'struct GNUNET_ATS_Handle'
171  * @param tc scheduler context
172  */
173 static void
174 update_bandwidth_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
175 {
176   struct GNUNET_ATS_Handle *atc = cls;
177   unsigned int ac = 0;
178   struct SetBandwidthContext bwc;
179
180   atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
181   /* FIXME: update calculations NICELY; what follows is a naive version */
182   GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &count_connections, &ac);
183   bwc.atc = atc;
184   if (ac == 0)
185     ac++;
186   GNUNET_assert (ac > 0);
187   bwc.bw_in = GNUNET_BANDWIDTH_value_init (atc->total_bps_in / ac);
188   bwc.bw_out = GNUNET_BANDWIDTH_value_init (atc->total_bps_out / ac);
189   GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &set_bw_connections, &bwc);
190 }
191
192
193 /**
194  * Calculate an updated bandwidth assignment and notify.
195  *
196  * @param atc handle
197  * @param change which allocation record changed?
198  */
199 static void
200 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
201                              struct AllocationRecord *change)
202 {
203   /* FIXME: based on the 'change', update the LP-problem... */
204   if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
205     atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task, atc);
206 }
207
208
209 /**
210  * Function called with feasbile addresses we might want to suggest.
211  *
212  * @param cls the 'struct GNUNET_ATS_SuggestionContext'
213  * @param key identity of the peer
214  * @param value a 'struct AllocationRecord' for the peer
215  * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
216  */
217 static int
218 suggest_address (void *cls, const GNUNET_HashCode * key, void *value)
219 {
220   struct GNUNET_ATS_SuggestionContext *asc = cls;
221   struct AllocationRecord *ar = value;
222
223 #if DEBUG_ATS
224   LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting address for peer `%s'\n",
225        GNUNET_h2s (key));
226 #endif
227
228   /* trivial strategy: pick first available address... */
229   asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
230            ar->plugin_addr_len, ar->session,
231            GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
232            GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32), ar->ats,
233            ar->ats_count);
234   asc->cb = NULL;
235   return GNUNET_NO;
236 }
237
238 int
239 map_it (void *cls, const GNUNET_HashCode * key, void *value)
240 {
241   LOG (GNUNET_ERROR_TYPE_DEBUG, "Found entry for %s\n", GNUNET_h2s (key));
242   return GNUNET_YES;
243 }
244
245 /**
246  * We would like to establish a new connection with a peer.
247  * ATS should suggest a good address to begin with.
248  *
249  * @param atc handle
250  * @param peer identity of the new peer
251  * @param cb function to call with the address
252  * @param cb_cls closure for cb
253  */
254 struct GNUNET_ATS_SuggestionContext *
255 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
256                             const struct GNUNET_PeerIdentity *peer,
257                             GNUNET_ATS_AddressSuggestionCallback cb,
258                             void *cb_cls)
259 {
260   struct GNUNET_ATS_SuggestionContext *asc;
261
262 #if DEBUG_ATS
263   LOG (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
264        "Looking up suggested address for peer `%s'\n", GNUNET_i2s (peer));
265 #endif
266   asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
267   asc->cb = cb;
268   asc->cb_cls = cb_cls;
269   asc->atc = atc;
270   asc->target = *peer;
271   (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
272                                                      &peer->hashPubKey,
273                                                      &suggest_address, asc);
274
275   if (NULL == asc->cb)
276   {
277     GNUNET_free (asc);
278     return NULL;
279   }
280   GNUNET_CONTAINER_multihashmap_put (atc->notify_map, &peer->hashPubKey, asc,
281                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
282   return asc;
283 }
284
285
286 /**
287  * Cancel suggestion request.
288  *
289  * @param asc handle of the request to cancel
290  */
291 void
292 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
293 {
294   GNUNET_assert (GNUNET_OK ==
295                  GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
296                                                        &asc->target.hashPubKey,
297                                                        asc));
298   GNUNET_free (asc);
299 }
300
301
302 /**
303  * Initialize the ATS subsystem.
304  *
305  * @param cfg configuration to use
306  * @param alloc_cb notification to call whenever the allocation changed
307  * @param alloc_cb_cls closure for 'alloc_cb'
308  * @return ats context
309  */
310 struct GNUNET_ATS_Handle *
311 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
312                  GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
313                  void *alloc_cb_cls)
314 {
315   struct GNUNET_ATS_Handle *atc;
316
317 #if DEBUG_ATS
318   LOG (GNUNET_ERROR_TYPE_DEBUG, "ATS init\n");
319 #endif
320   atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
321   atc->cfg = cfg;
322   atc->alloc_cb = alloc_cb;
323   atc->alloc_cb_cls = alloc_cb_cls;
324   atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
325   atc->notify_map = GNUNET_CONTAINER_multihashmap_create (256);
326   GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_OUT",
327                                          &atc->total_bps_out);
328   GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_IN",
329                                          &atc->total_bps_in);
330   return atc;
331 }
332
333
334 /**
335  * Free an allocation record.
336  *
337  * @param cls unused
338  * @param key identity of the peer associated with the record
339  * @param value the 'struct AllocationRecord' to free
340  * @return GNUNET_OK (continue to iterate)
341  */
342 static int
343 destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value)
344 {
345   struct AllocationRecord *ar = value;
346
347   GNUNET_array_grow (ar->ats, ar->ats_count, 0);
348   GNUNET_free (ar->plugin_name);
349   GNUNET_free (ar);
350   return GNUNET_OK;
351 }
352
353
354 /**
355  * Shutdown the ATS subsystem.
356  *
357  * @param atc handle
358  */
359 void
360 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
361 {
362 #if DEBUG_ATS
363   LOG (GNUNET_ERROR_TYPE_DEBUG, "ATS shutdown\n");
364 #endif
365   if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
366   {
367     GNUNET_SCHEDULER_cancel (atc->ba_task);
368     atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
369   }
370   GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_allocation_record,
371                                          NULL);
372   GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
373   GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
374   GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
375   atc->notify_map = NULL;
376   GNUNET_free (atc);
377 }
378
379
380 /**
381  * Closure for 'update_session'
382  */
383 struct UpdateSessionContext
384 {
385   /**
386    * Ats handle.
387    */
388   struct GNUNET_ATS_Handle *atc;
389
390   /**
391    * Allocation record with new information.
392    */
393   struct AllocationRecord *arnew;
394 };
395
396
397 /**
398  * Update an allocation record, merging with the new information
399  *
400  * @param cls a new 'struct AllocationRecord'
401  * @param key identity of the peer associated with the records
402  * @param value the old 'struct AllocationRecord'
403  * @return GNUNET_YES if the records do not match,
404  *         GNUNET_NO if the record do match and 'old' was updated
405  */
406 static int
407 update_session (void *cls, const GNUNET_HashCode * key, void *value)
408 {
409   struct UpdateSessionContext *usc = cls;
410   struct AllocationRecord *arnew = usc->arnew;
411   struct AllocationRecord *arold = value;
412   int change;
413   int c_old;
414   int c_new;
415   int found;
416
417
418 #if DEBUG_ATS
419   LOG (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
420        "Updating session for peer `%s' plugin `%s'\n", GNUNET_h2s (key),
421        arold->plugin_name);
422 #endif
423
424   if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
425     return GNUNET_YES;
426   if (((arnew->session == arold->session) && (arnew->session != NULL)) ||
427       ((arold->session == NULL) &&
428        (arold->plugin_addr_len == arnew->plugin_addr_len) &&
429        (0 ==
430         memcmp (arold->plugin_addr, arnew->plugin_addr,
431                 arnew->plugin_addr_len))))
432   {
433     change = GNUNET_NO;
434     /* records match */
435     if (arnew->session != arold->session)
436     {
437       arold->session = arnew->session;
438       change = GNUNET_YES;
439     }
440     if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO))
441     {
442       arold->connected = GNUNET_YES;
443       change = GNUNET_YES;
444     }
445
446     /* Update existing value */
447     c_new = 0;
448     while (c_new < arnew->ats_count)
449     {
450       c_old = 0;
451       found = GNUNET_NO;
452       while (c_old < arold->ats_count)
453       {
454         if (arold->ats[c_old].type == arnew->ats[c_new].type)
455         {
456 #if DEBUG_ATS
457           LOG (GNUNET_ERROR_TYPE_DEBUG,
458                "Found type %i, old value=%i new value=%i\n",
459                ntohl (arold->ats[c_old].type), ntohl (arold->ats[c_old].value),
460                ntohl (arnew->ats[c_new].value));
461 #endif
462           arold->ats[c_old].value = arnew->ats[c_new].value;
463           found = GNUNET_YES;
464         }
465         c_old++;
466       }
467       /* Add new value */
468       if (found == GNUNET_NO)
469       {
470 #if DEBUG_ATS
471         LOG (GNUNET_ERROR_TYPE_DEBUG, "Added new type %i new value=%i\n",
472              ntohl (arnew->ats[c_new].type), ntohl (arnew->ats[c_new].value));
473         LOG (GNUNET_ERROR_TYPE_DEBUG, "Old array size: %u\n", arold->ats_count);
474 #endif
475         GNUNET_array_grow (arold->ats, arold->ats_count, arold->ats_count + 1);
476         GNUNET_assert (arold->ats_count >= 2);
477         arold->ats[arold->ats_count - 2].type = arnew->ats[c_new].type;
478         arold->ats[arold->ats_count - 2].value = arnew->ats[c_new].value;
479         arold->ats[arold->ats_count - 1].type = htonl (0);
480         arold->ats[arold->ats_count - 1].value = htonl (0);
481 #if DEBUG_ATS
482         LOG (GNUNET_ERROR_TYPE_DEBUG, "New array size: %i\n", arold->ats_count);
483 #endif
484       }
485       c_new++;
486     }
487
488     if (GNUNET_YES == change)
489       update_bandwidth_assignment (usc->atc, arold);
490     return GNUNET_NO;
491   }
492   return GNUNET_YES;
493 }
494
495
496 /**
497  * Create an allocation record with the given properties.
498  *
499  * @param plugin_name name of the currently used transport plugin
500  * @param session session in use (if available)
501  * @param plugin_addr address in use (if available)
502  * @param plugin_addr_len number of bytes in plugin_addr
503  * @param ats performance data for the connection
504  * @param ats_count number of performance records in 'ats'
505  */
506 static struct AllocationRecord *
507 create_allocation_record (const char *plugin_name, struct Session *session,
508                           const void *plugin_addr, size_t plugin_addr_len,
509                           const struct GNUNET_TRANSPORT_ATS_Information *ats,
510                           uint32_t ats_count)
511 {
512   struct AllocationRecord *ar;
513
514   ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
515   ar->plugin_name = GNUNET_strdup (plugin_name);
516   ar->plugin_addr = &ar[1];
517   memcpy (&ar[1], plugin_addr, plugin_addr_len);
518   ar->session = session;
519   ar->plugin_addr_len = plugin_addr_len;
520   GNUNET_BANDWIDTH_tracker_init (&ar->available_recv_window, ar->bandwidth_in,
521                                  MAX_WINDOW_TIME_S);
522   GNUNET_assert (ats_count > 0);
523   GNUNET_array_grow (ar->ats, ar->ats_count, ats_count);
524   memcpy (ar->ats, ats,
525           ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
526   return ar;
527 }
528
529
530 /**
531  * Mark all matching allocation records as not connected.
532  *
533  * @param cls 'struct GTS_AtsHandle'
534  * @param key identity of the peer associated with the record
535  * @param value the 'struct AllocationRecord' to clear the 'connected' flag
536  * @return GNUNET_OK (continue to iterate)
537  */
538 static int
539 disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value)
540 {
541   struct GNUNET_ATS_Handle *atc = cls;
542   struct AllocationRecord *ar = value;
543
544   if (GNUNET_YES == ar->connected)
545   {
546     ar->connected = GNUNET_NO;
547     update_bandwidth_assignment (atc, ar);
548   }
549   return GNUNET_OK;
550 }
551
552
553 /**
554  * We established a new connection with a peer (for example, because
555  * core asked for it or because the other peer connected to us).
556  * Calculate bandwidth assignments including the new peer.
557  *
558  * @param atc handle
559  * @param peer identity of the new peer
560  * @param plugin_name name of the currently used transport plugin
561  * @param session session in use (if available)
562  * @param plugin_addr address in use (if available)
563  * @param plugin_addr_len number of bytes in plugin_addr
564  * @param ats performance data for the connection
565  * @param ats_count number of performance records in 'ats'
566  */
567 void
568 GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc,
569                          const struct GNUNET_PeerIdentity *peer,
570                          const char *plugin_name, struct Session *session,
571                          const void *plugin_addr, size_t plugin_addr_len,
572                          const struct GNUNET_TRANSPORT_ATS_Information *ats,
573                          uint32_t ats_count)
574 {
575   struct AllocationRecord *ar;
576   struct UpdateSessionContext usc;
577
578   (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &disconnect_peer,
579                                                 atc);
580   ar = create_allocation_record (plugin_name, session, plugin_addr,
581                                  plugin_addr_len, ats, ats_count);
582   ar->connected = GNUNET_YES;
583   usc.atc = atc;
584   usc.arnew = ar;
585   if (GNUNET_SYSERR ==
586       GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
587   {
588     destroy_allocation_record (NULL, &peer->hashPubKey, ar);
589     return;
590   }
591   GNUNET_assert (GNUNET_OK ==
592                  GNUNET_CONTAINER_multihashmap_put (atc->peers,
593                                                     &peer->hashPubKey, ar,
594                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
595 }
596
597
598 /**
599  * We disconnected from the given peer (for example, because ats, core
600  * or blacklist asked for it or because the other peer disconnected).
601  * Calculate bandwidth assignments without the peer.
602  *
603  * @param atc handle
604  * @param peer identity of the new peer
605  */
606 void
607 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
608                             const struct GNUNET_PeerIdentity *peer)
609 {
610   (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
611                                                      &peer->hashPubKey,
612                                                      &disconnect_peer, atc);
613 }
614
615
616 /**
617  * Closure for 'destroy_allocation_record'
618  */
619 struct SessionDestroyContext
620 {
621   /**
622    * Ats handle.
623    */
624   struct GNUNET_ATS_Handle *atc;
625
626   /**
627    * Session being destroyed.
628    */
629   const struct Session *session;
630 };
631
632
633 /**
634  * Free an allocation record matching the given session.
635  *
636  * @param cls the 'struct SessionDestroyContext'
637  * @param key identity of the peer associated with the record
638  * @param value the 'struct AllocationRecord' to free
639  * @return GNUNET_OK (continue to iterate)
640  */
641 static int
642 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
643 {
644   struct SessionDestroyContext *sdc = cls;
645   struct AllocationRecord *ar = value;
646
647   if (ar->session != sdc->session)
648     return GNUNET_OK;
649   ar->session = NULL;
650   if (ar->plugin_addr != NULL)
651     return GNUNET_OK;
652   GNUNET_assert (GNUNET_OK ==
653                  GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, key,
654                                                        ar));
655   if (GNUNET_YES == ar->connected) ;
656   {
657     /* FIXME: is this supposed to be allowed? What to do then? */
658     GNUNET_break (0);
659   }
660   destroy_allocation_record (NULL, key, ar);
661   return GNUNET_OK;
662 }
663
664
665 /**
666  * A session got destroyed, stop including it as a valid address.
667  *
668  * @param atc handle
669  * @param peer identity of the peer
670  * @param session session handle that is no longer valid
671  */
672 void
673 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
674                               const struct GNUNET_PeerIdentity *peer,
675                               const struct Session *session)
676 {
677   struct SessionDestroyContext sdc;
678
679   sdc.atc = atc;
680   sdc.session = session;
681   (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_session,
682                                                 &sdc);
683 }
684
685
686 /**
687  * Notify validation watcher that an entry is now valid
688  *
689  * @param cls 'struct ValidationEntry' that is now valid
690  * @param key peer identity (unused)
691  * @param value a 'GST_ValidationIteratorContext' to notify
692  * @return GNUNET_YES (continue to iterate)
693  */
694 static int
695 notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
696 {
697   struct AllocationRecord *ar = cls;
698   struct GNUNET_ATS_SuggestionContext *asc = value;
699
700   asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
701            ar->plugin_addr_len, ar->session,
702            GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
703            GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32), ar->ats,
704            ar->ats_count);
705   GNUNET_ATS_suggest_address_cancel (asc);
706   asc = NULL;
707   return GNUNET_OK;
708 }
709
710
711 /**
712  * We have updated performance statistics for a given address.  Note
713  * that this function can be called for addresses that are currently
714  * in use as well as addresses that are valid but not actively in use.
715  * Furthermore, the peer may not even be connected to us right now (in
716  * which case the call may be ignored or the information may be stored
717  * for later use).  Update bandwidth assignments.
718  *
719  * @param atc handle
720  * @param peer identity of the peer
721  * @param valid_until how long is the address valid?
722  * @param plugin_name name of the transport plugin
723  * @param session session handle (if available)
724  * @param plugin_addr address  (if available)
725  * @param plugin_addr_len number of bytes in plugin_addr
726  * @param ats performance data for the address
727  * @param ats_count number of performance records in 'ats'
728  */
729 void
730 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
731                            const struct GNUNET_PeerIdentity *peer,
732                            struct GNUNET_TIME_Absolute valid_until,
733                            const char *plugin_name, struct Session *session,
734                            const void *plugin_addr, size_t plugin_addr_len,
735                            const struct GNUNET_TRANSPORT_ATS_Information *ats,
736                            uint32_t ats_count)
737 {
738   struct AllocationRecord *ar;
739   struct UpdateSessionContext usc;
740
741 #if DEBUG_ATS
742   LOG (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
743        "Updating address for peer `%s', plugin `%s'\n", GNUNET_i2s (peer),
744        plugin_name);
745 #endif
746   ar = create_allocation_record (plugin_name, session, plugin_addr,
747                                  plugin_addr_len, ats, ats_count);
748   usc.atc = atc;
749   usc.arnew = ar;
750   if (GNUNET_SYSERR ==
751       GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
752   {
753     destroy_allocation_record (NULL, &peer->hashPubKey, ar);
754     return;
755   }
756 #if DEBUG_ATS
757   LOG (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
758        "Adding new address for peer `%s', plugin `%s'\n", GNUNET_i2s (peer),
759        plugin_name);
760 #endif
761   GNUNET_assert (GNUNET_OK ==
762                  GNUNET_CONTAINER_multihashmap_put (atc->peers,
763                                                     &peer->hashPubKey, ar,
764                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
765   GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
766                                               &peer->hashPubKey, &notify_valid,
767                                               ar);
768 }
769
770 /* end of file gnunet-service-transport_ats.c */