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