log
[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     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
146                      "ats-api",
147                      "Bandwidth assigned to peer %s is i:%u/o:%u bytes/s\n",
148                      GNUNET_i2s ((const struct GNUNET_PeerIdentity *) key),
149                      ntohl (ar->bandwidth_in.value__),
150                      ntohl (ar->bandwidth_out.value__));
151     if (NULL != sbc->atc->alloc_cb)
152       sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
153                           (const struct GNUNET_PeerIdentity *) key,
154                           ar->plugin_name, ar->session, ar->plugin_addr,
155                           ar->plugin_addr_len, ar->bandwidth_out,
156                           ar->bandwidth_in);
157   }
158   else if (ntohl (ar->bandwidth_out.value__) > 0)
159   {
160     ar->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
161     ar->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
162     if (NULL != sbc->atc->alloc_cb)
163       sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
164                           (const struct GNUNET_PeerIdentity *) key,
165                           ar->plugin_name, ar->session, ar->plugin_addr,
166                           ar->plugin_addr_len, ar->bandwidth_out,
167                           ar->bandwidth_in);
168   }
169   return GNUNET_YES;
170 }
171
172
173 /**
174  * Task run to update bandwidth assignments.
175  *
176  * @param cls the 'struct GNUNET_ATS_Handle'
177  * @param tc scheduler context
178  */
179 static void
180 update_bandwidth_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 {
182   struct GNUNET_ATS_Handle *atc = cls;
183   unsigned int ac = 0;
184   struct SetBandwidthContext bwc;
185
186   atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
187   /* FIXME: update calculations NICELY; what follows is a naive version */
188   GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &count_connections, &ac);
189   bwc.atc = atc;
190   if (ac == 0)
191     ac++;
192   GNUNET_assert (ac > 0);
193   bwc.bw_in = GNUNET_BANDWIDTH_value_init (atc->total_bps_in / ac);
194   bwc.bw_out = GNUNET_BANDWIDTH_value_init (atc->total_bps_out / ac);
195   GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &set_bw_connections, &bwc);
196 }
197
198
199 /**
200  * Calculate an updated bandwidth assignment and notify.
201  *
202  * @param atc handle
203  * @param change which allocation record changed?
204  */
205 static void
206 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
207                              struct AllocationRecord *change)
208 {
209   /* FIXME: based on the 'change', update the LP-problem... */
210   if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
211     atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task, atc);
212 }
213
214
215 /**
216  * Function called with feasbile addresses we might want to suggest.
217  *
218  * @param cls the 'struct GNUNET_ATS_SuggestionContext'
219  * @param key identity of the peer
220  * @param value a 'struct AllocationRecord' for the peer
221  * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
222  */
223 static int
224 suggest_address (void *cls, const GNUNET_HashCode * key, void *value)
225 {
226   struct GNUNET_ATS_SuggestionContext *asc = cls;
227   struct AllocationRecord *ar = value;
228
229 #if DEBUG_ATS
230   LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting address for peer `%s'\n",
231        GNUNET_h2s (key));
232 #endif
233
234   /* trivial strategy: pick first available address... */
235   asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
236            ar->plugin_addr_len, ar->session,
237            GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
238            GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32), ar->ats,
239            ar->ats_count);
240   asc->cb = NULL;
241   return GNUNET_NO;
242 }
243
244
245 int
246 map_it (void *cls, const GNUNET_HashCode * key, void *value)
247 {
248   LOG (GNUNET_ERROR_TYPE_DEBUG, "Found entry for %s\n", GNUNET_h2s (key));
249   return GNUNET_YES;
250 }
251
252 /**
253  * We would like to establish a new connection with a peer.
254  * ATS should suggest a good address to begin with.
255  *
256  * @param atc handle
257  * @param peer identity of the new peer
258  * @param cb function to call with the address
259  * @param cb_cls closure for cb
260  */
261 struct GNUNET_ATS_SuggestionContext *
262 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
263                             const struct GNUNET_PeerIdentity *peer,
264                             GNUNET_ATS_AddressSuggestionCallback cb,
265                             void *cb_cls)
266 {
267   struct GNUNET_ATS_SuggestionContext *asc;
268
269 #if DEBUG_ATS
270   LOG (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
271        "Looking up suggested address for peer `%s'\n", GNUNET_i2s (peer));
272 #endif
273   asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
274   asc->cb = cb;
275   asc->cb_cls = cb_cls;
276   asc->atc = atc;
277   asc->target = *peer;
278   (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
279                                                      &peer->hashPubKey,
280                                                      &suggest_address, asc);
281
282   if (NULL == asc->cb)
283   {
284     GNUNET_free (asc);
285     return NULL;
286   }
287   GNUNET_CONTAINER_multihashmap_put (atc->notify_map, &peer->hashPubKey, asc,
288                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
289   return asc;
290 }
291
292
293 /**
294  * Cancel suggestion request.
295  *
296  * @param asc handle of the request to cancel
297  */
298 void
299 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
300 {
301   GNUNET_assert (GNUNET_OK ==
302                  GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
303                                                        &asc->target.hashPubKey,
304                                                        asc));
305   GNUNET_free (asc);
306 }
307
308
309 /**
310  * Initialize the ATS subsystem.
311  *
312  * @param cfg configuration to use
313  * @param alloc_cb notification to call whenever the allocation changed
314  * @param alloc_cb_cls closure for 'alloc_cb'
315  * @return ats context
316  */
317 struct GNUNET_ATS_Handle *
318 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
319                  GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
320                  void *alloc_cb_cls)
321 {
322   struct GNUNET_ATS_Handle *atc;
323
324 #if DEBUG_ATS
325   LOG (GNUNET_ERROR_TYPE_DEBUG, "ATS init\n");
326 #endif
327   atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
328   atc->cfg = cfg;
329   atc->alloc_cb = alloc_cb;
330   atc->alloc_cb_cls = alloc_cb_cls;
331   atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
332   atc->notify_map = GNUNET_CONTAINER_multihashmap_create (256);
333   GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_OUT",
334                                          &atc->total_bps_out);
335   GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_IN",
336                                          &atc->total_bps_in);
337   return atc;
338 }
339
340
341 /**
342  * Free an allocation record.
343  *
344  * @param cls unused
345  * @param key identity of the peer associated with the record
346  * @param value the 'struct AllocationRecord' to free
347  * @return GNUNET_OK (continue to iterate)
348  */
349 static int
350 destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value)
351 {
352   struct AllocationRecord *ar = value;
353
354   GNUNET_array_grow (ar->ats, ar->ats_count, 0);
355   GNUNET_free (ar->plugin_name);
356   GNUNET_free (ar);
357   return GNUNET_OK;
358 }
359
360
361 /**
362  * Shutdown the ATS subsystem.
363  *
364  * @param atc handle
365  */
366 void
367 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
368 {
369 #if DEBUG_ATS
370   LOG (GNUNET_ERROR_TYPE_DEBUG, "ATS shutdown\n");
371 #endif
372   if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
373   {
374     GNUNET_SCHEDULER_cancel (atc->ba_task);
375     atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
376   }
377   GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_allocation_record,
378                                          NULL);
379   GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
380   GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
381   GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
382   atc->notify_map = NULL;
383   GNUNET_free (atc);
384 }
385
386
387 /**
388  * Closure for 'update_session'
389  */
390 struct UpdateSessionContext
391 {
392   /**
393    * Ats handle.
394    */
395   struct GNUNET_ATS_Handle *atc;
396
397   /**
398    * Allocation record with new information.
399    */
400   struct AllocationRecord *arnew;
401 };
402
403
404 /**
405  * Update an allocation record, merging with the new information
406  *
407  * @param cls a new 'struct AllocationRecord'
408  * @param key identity of the peer associated with the records
409  * @param value the old 'struct AllocationRecord'
410  * @return GNUNET_YES if the records do not match,
411  *         GNUNET_NO if the record do match and 'old' was updated
412  */
413 static int
414 update_session (void *cls, const GNUNET_HashCode * key, void *value)
415 {
416   struct UpdateSessionContext *usc = cls;
417   struct AllocationRecord *arnew = usc->arnew;
418   struct AllocationRecord *arold = value;
419   int change;
420   int c_old;
421   int c_new;
422   int found;
423
424
425 #if DEBUG_ATS
426   LOG (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
427        "Updating session for peer `%s' plugin `%s'\n", GNUNET_h2s (key),
428        arold->plugin_name);
429 #endif
430
431   if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
432     return GNUNET_YES;
433   if (((arnew->session == arold->session) && (arnew->session != NULL)) ||
434       ((arold->session == NULL) &&
435        (arold->plugin_addr_len == arnew->plugin_addr_len) &&
436        (0 ==
437         memcmp (arold->plugin_addr, arnew->plugin_addr,
438                 arnew->plugin_addr_len))))
439   {
440     change = GNUNET_NO;
441     /* records match */
442     if (arnew->session != arold->session)
443     {
444       arold->session = arnew->session;
445       change = GNUNET_YES;
446     }
447     if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO))
448     {
449       arold->connected = GNUNET_YES;
450       change = GNUNET_YES;
451     }
452
453     /* Update existing value */
454     c_new = 0;
455     while (c_new < arnew->ats_count)
456     {
457       c_old = 0;
458       found = GNUNET_NO;
459       while (c_old < arold->ats_count)
460       {
461         if (arold->ats[c_old].type == arnew->ats[c_new].type)
462         {
463 #if DEBUG_ATS
464           LOG (GNUNET_ERROR_TYPE_DEBUG,
465                "Found type %i, old value=%i new value=%i\n",
466                ntohl (arold->ats[c_old].type), ntohl (arold->ats[c_old].value),
467                ntohl (arnew->ats[c_new].value));
468 #endif
469           arold->ats[c_old].value = arnew->ats[c_new].value;
470           found = GNUNET_YES;
471         }
472         c_old++;
473       }
474       /* Add new value */
475       if (found == GNUNET_NO)
476       {
477 #if DEBUG_ATS
478         LOG (GNUNET_ERROR_TYPE_DEBUG, "Added new type %i new value=%i\n",
479              ntohl (arnew->ats[c_new].type), ntohl (arnew->ats[c_new].value));
480         LOG (GNUNET_ERROR_TYPE_DEBUG, "Old array size: %u\n", arold->ats_count);
481 #endif
482         GNUNET_array_grow (arold->ats, arold->ats_count, arold->ats_count + 1);
483         GNUNET_assert (arold->ats_count >= 2);
484         arold->ats[arold->ats_count - 2].type = arnew->ats[c_new].type;
485         arold->ats[arold->ats_count - 2].value = arnew->ats[c_new].value;
486         arold->ats[arold->ats_count - 1].type = htonl (0);
487         arold->ats[arold->ats_count - 1].value = htonl (0);
488 #if DEBUG_ATS
489         LOG (GNUNET_ERROR_TYPE_DEBUG, "New array size: %i\n", arold->ats_count);
490 #endif
491       }
492       c_new++;
493     }
494
495     if (GNUNET_YES == change)
496       update_bandwidth_assignment (usc->atc, arold);
497     return GNUNET_NO;
498   }
499   return GNUNET_YES;
500 }
501
502
503 /**
504  * Create an allocation record with the given properties.
505  *
506  * @param plugin_name name of the currently used transport plugin
507  * @param session session in use (if available)
508  * @param plugin_addr address in use (if available)
509  * @param plugin_addr_len number of bytes in plugin_addr
510  * @param ats performance data for the connection
511  * @param ats_count number of performance records in 'ats'
512  */
513 static struct AllocationRecord *
514 create_allocation_record (const char *plugin_name, struct Session *session,
515                           const void *plugin_addr, size_t plugin_addr_len,
516                           const struct GNUNET_TRANSPORT_ATS_Information *ats,
517                           uint32_t ats_count)
518 {
519   struct AllocationRecord *ar;
520
521   ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
522   ar->plugin_name = GNUNET_strdup (plugin_name);
523   ar->plugin_addr = &ar[1];
524   memcpy (&ar[1], plugin_addr, plugin_addr_len);
525   ar->session = session;
526   ar->plugin_addr_len = plugin_addr_len;
527   GNUNET_BANDWIDTH_tracker_init (&ar->available_recv_window, ar->bandwidth_in,
528                                  MAX_WINDOW_TIME_S);
529   GNUNET_assert (ats_count > 0);
530   GNUNET_array_grow (ar->ats, ar->ats_count, ats_count);
531   memcpy (ar->ats, ats,
532           ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
533   return ar;
534 }
535
536
537 /**
538  * Mark all matching allocation records as not connected.
539  *
540  * @param cls 'struct GTS_AtsHandle'
541  * @param key identity of the peer associated with the record
542  * @param value the 'struct AllocationRecord' to clear the 'connected' flag
543  * @return GNUNET_OK (continue to iterate)
544  */
545 static int
546 disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value)
547 {
548   struct GNUNET_ATS_Handle *atc = cls;
549   struct AllocationRecord *ar = value;
550
551   if (GNUNET_YES == ar->connected)
552   {
553     ar->connected = GNUNET_NO;
554     update_bandwidth_assignment (atc, ar);
555   }
556   return GNUNET_OK;
557 }
558
559
560 /**
561  * We established a new connection with a peer (for example, because
562  * core asked for it or because the other peer connected to us).
563  * Calculate bandwidth assignments including the new peer.
564  *
565  * @param atc handle
566  * @param peer identity of the new peer
567  * @param plugin_name name of the currently used transport plugin
568  * @param session session in use (if available)
569  * @param plugin_addr address in use (if available)
570  * @param plugin_addr_len number of bytes in plugin_addr
571  * @param ats performance data for the connection
572  * @param ats_count number of performance records in 'ats'
573  */
574 void
575 GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc,
576                          const struct GNUNET_PeerIdentity *peer,
577                          const char *plugin_name, struct Session *session,
578                          const void *plugin_addr, size_t plugin_addr_len,
579                          const struct GNUNET_TRANSPORT_ATS_Information *ats,
580                          uint32_t ats_count)
581 {
582   struct AllocationRecord *ar;
583   struct UpdateSessionContext usc;
584
585   (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &disconnect_peer,
586                                                 atc);
587   ar = create_allocation_record (plugin_name, session, plugin_addr,
588                                  plugin_addr_len, ats, ats_count);
589   ar->connected = GNUNET_YES;
590   usc.atc = atc;
591   usc.arnew = ar;
592   if (GNUNET_SYSERR ==
593       GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
594   {
595     destroy_allocation_record (NULL, &peer->hashPubKey, ar);
596     return;
597   }
598   GNUNET_assert (GNUNET_OK ==
599                  GNUNET_CONTAINER_multihashmap_put (atc->peers,
600                                                     &peer->hashPubKey, ar,
601                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
602 }
603
604
605 /**
606  * We disconnected from the given peer (for example, because ats, core
607  * or blacklist asked for it or because the other peer disconnected).
608  * Calculate bandwidth assignments without the peer.
609  *
610  * @param atc handle
611  * @param peer identity of the new peer
612  */
613 void
614 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
615                             const struct GNUNET_PeerIdentity *peer)
616 {
617   (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
618                                                      &peer->hashPubKey,
619                                                      &disconnect_peer, atc);
620 }
621
622
623 /**
624  * Closure for 'destroy_allocation_record'
625  */
626 struct SessionDestroyContext
627 {
628   /**
629    * Ats handle.
630    */
631   struct GNUNET_ATS_Handle *atc;
632
633   /**
634    * Session being destroyed.
635    */
636   const struct Session *session;
637 };
638
639
640 /**
641  * Free an allocation record matching the given session.
642  *
643  * @param cls the 'struct SessionDestroyContext'
644  * @param key identity of the peer associated with the record
645  * @param value the 'struct AllocationRecord' to free
646  * @return GNUNET_OK (continue to iterate)
647  */
648 static int
649 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
650 {
651   struct SessionDestroyContext *sdc = cls;
652   struct AllocationRecord *ar = value;
653
654   if (ar->session != sdc->session)
655     return GNUNET_OK;
656   ar->session = NULL;
657   if (ar->plugin_addr != NULL)
658     return GNUNET_OK;
659   GNUNET_assert (GNUNET_OK ==
660                  GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, key,
661                                                        ar));
662   if (GNUNET_YES == ar->connected) ;
663   {
664     /* FIXME: is this supposed to be allowed? What to do then? */
665     GNUNET_break (0);
666   }
667   destroy_allocation_record (NULL, key, ar);
668   return GNUNET_OK;
669 }
670
671
672 /**
673  * A session got destroyed, stop including it as a valid address.
674  *
675  * @param atc handle
676  * @param peer identity of the peer
677  * @param session session handle that is no longer valid
678  */
679 void
680 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
681                               const struct GNUNET_PeerIdentity *peer,
682                               const struct Session *session)
683 {
684   struct SessionDestroyContext sdc;
685
686   sdc.atc = atc;
687   sdc.session = session;
688   (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_session,
689                                                 &sdc);
690 }
691
692
693 /**
694  * Notify validation watcher that an entry is now valid
695  *
696  * @param cls 'struct ValidationEntry' that is now valid
697  * @param key peer identity (unused)
698  * @param value a 'GST_ValidationIteratorContext' to notify
699  * @return GNUNET_YES (continue to iterate)
700  */
701 static int
702 notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
703 {
704   struct AllocationRecord *ar = cls;
705   struct GNUNET_ATS_SuggestionContext *asc = value;
706
707   asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
708            ar->plugin_addr_len, ar->session,
709            GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
710            GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32), ar->ats,
711            ar->ats_count);
712   GNUNET_ATS_suggest_address_cancel (asc);
713   asc = NULL;
714   return GNUNET_OK;
715 }
716
717
718 /**
719  * We have updated performance statistics for a given address.  Note
720  * that this function can be called for addresses that are currently
721  * in use as well as addresses that are valid but not actively in use.
722  * Furthermore, the peer may not even be connected to us right now (in
723  * which case the call may be ignored or the information may be stored
724  * for later use).  Update bandwidth assignments.
725  *
726  * @param atc handle
727  * @param peer identity of the peer
728  * @param valid_until how long is the address valid?
729  * @param plugin_name name of the transport plugin
730  * @param session session handle (if available)
731  * @param plugin_addr address  (if available)
732  * @param plugin_addr_len number of bytes in plugin_addr
733  * @param ats performance data for the address
734  * @param ats_count number of performance records in 'ats'
735  */
736 void
737 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
738                            const struct GNUNET_PeerIdentity *peer,
739                            struct GNUNET_TIME_Absolute valid_until,
740                            const char *plugin_name, struct Session *session,
741                            const void *plugin_addr, size_t plugin_addr_len,
742                            const struct GNUNET_TRANSPORT_ATS_Information *ats,
743                            uint32_t ats_count)
744 {
745   struct AllocationRecord *ar;
746   struct UpdateSessionContext usc;
747
748 #if DEBUG_ATS
749   LOG (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
750        "Updating address for peer `%s', plugin `%s'\n", GNUNET_i2s (peer),
751        plugin_name);
752 #endif
753   ar = create_allocation_record (plugin_name, session, plugin_addr,
754                                  plugin_addr_len, ats, ats_count);
755   usc.atc = atc;
756   usc.arnew = ar;
757   if (GNUNET_SYSERR ==
758       GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
759   {
760     destroy_allocation_record (NULL, &peer->hashPubKey, ar);
761     return;
762   }
763 #if DEBUG_ATS
764   LOG (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
765        "Adding new address for peer `%s', plugin `%s'\n", GNUNET_i2s (peer),
766        plugin_name);
767 #endif
768   GNUNET_assert (GNUNET_OK ==
769                  GNUNET_CONTAINER_multihashmap_put (atc->peers,
770                                                     &peer->hashPubKey, ar,
771                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
772   GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
773                                               &peer->hashPubKey, &notify_valid,
774                                               ar);
775 }
776
777 /* end of file gnunet-service-transport_ats.c */