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