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