2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file ats/gnunet-service-ats_reservations.c
23 * @brief ats service, inbound bandwidth reservation management
24 * @author Christian Grothoff
27 #include "gnunet-service-ats_reservations.h"
30 * Number of seconds that available bandwidth carries over
33 #define MAX_BANDWIDTH_CARRY_S 5
37 * Map of peer identities to 'struct GNUNET_BANDWIDTH_Tracker *'s
39 static struct GNUNET_CONTAINER_MultiHashMap *trackers;
43 * Reserve the given amount of incoming bandwidth (in bytes) from the
44 * given peer. If a reservation is not possible right now, return how
45 * long the client should wait before trying again.
47 * @param peer peer to reserve bandwidth from
48 * @param amount number of bytes to reserve
49 * @return 0 if the reservation was successful, FOREVER if the
50 * peer is not connected, otherwise the time to wait
51 * until the reservation might succeed
53 struct GNUNET_TIME_Relative
54 GAS_reservations_reserve (const struct GNUNET_PeerIdentity *peer,
57 struct GNUNET_BANDWIDTH_Tracker *tracker;
58 struct GNUNET_TIME_Relative ret;
60 tracker = GNUNET_CONTAINER_multihashmap_get (trackers,
63 return GNUNET_TIME_UNIT_ZERO; /* not connected, satisfy now */
66 ret = GNUNET_BANDWIDTH_tracker_get_delay (tracker,
68 if (ret.rel_value > 0)
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
71 "Delay to satisfy reservation for %d bytes is %llu ms\n",
73 (unsigned long long) ret.rel_value);
77 (void) GNUNET_BANDWIDTH_tracker_consume (tracker, amount);
78 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
79 "Reserved %d bytes\n",
81 return GNUNET_TIME_UNIT_ZERO;
86 * Set the amount of bandwidth the other peer could currently transmit
87 * to us (as far as we know) to the given value.
89 * @param peer identity of the peer
90 * @param bandwidth_in currently available bandwidth from that peer to
91 * this peer (estimate)
94 GAS_reservations_set_bandwidth (const struct GNUNET_PeerIdentity *peer,
95 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
97 struct GNUNET_BANDWIDTH_Tracker *tracker;
99 tracker = GNUNET_CONTAINER_multihashmap_get (trackers,
101 if (0 == ntohl (bandwidth_in.value__))
103 GNUNET_assert (GNUNET_YES ==
104 GNUNET_CONTAINER_multihashmap_remove (trackers,
107 GNUNET_free (tracker);
112 tracker = GNUNET_malloc (sizeof (struct GNUNET_BANDWIDTH_Tracker));
113 GNUNET_BANDWIDTH_tracker_init (tracker,
115 MAX_BANDWIDTH_CARRY_S);
116 GNUNET_CONTAINER_multihashmap_put (trackers,
119 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
122 GNUNET_BANDWIDTH_tracker_update_quota (tracker,
128 * Initialize reservations subsystem.
131 GAS_reservations_init ()
133 trackers = GNUNET_CONTAINER_multihashmap_create(128);
138 * Free memory of bandwidth tracker.
141 * @param key peer identity (unused)
142 * @param value the 'struct GNUNET_BANDWIDTH_Tracker' to free
143 * @return GNUNET_OK (continue to iterate)
146 free_tracker (void *cls,
147 const GNUNET_HashCode * key,
150 struct GNUNET_BANDWIDTH_Tracker *tracker = value;
152 GNUNET_free (tracker);
158 * Shutdown reservations subsystem.
161 GAS_reservations_done ()
163 GNUNET_CONTAINER_multihashmap_iterate (trackers, &free_tracker, NULL);
164 GNUNET_CONTAINER_multihashmap_destroy (trackers);
167 /* end of gnunet-service-ats_reservations.c */