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.
21 * @file ats/gnunet-service-ats_reservations.c
22 * @brief ats service, inbound bandwidth reservation management
23 * @author Christian Grothoff
26 #include "gnunet-service-ats_reservations.h"
27 #include "gnunet-service-ats.h"
31 * Number of seconds that available bandwidth carries over
34 #define MAX_BANDWIDTH_CARRY_S 5
38 * Map of peer identities to `struct GNUNET_BANDWIDTH_Tracker *`s
40 static struct GNUNET_CONTAINER_MultiPeerMap *trackers;
44 * Reserve the given amount of incoming bandwidth (in bytes) from the
45 * given peer. If a reservation is not possible right now, return how
46 * long the client should wait before trying again.
48 * @param peer peer to reserve bandwidth from
49 * @param amount number of bytes to reserve
50 * @return 0 if the reservation was successful, FOREVER if the
51 * peer is not connected, otherwise the time to wait
52 * until the reservation might succeed
54 struct GNUNET_TIME_Relative
55 GAS_reservations_reserve (const struct GNUNET_PeerIdentity *peer,
58 struct GNUNET_BANDWIDTH_Tracker *tracker;
59 struct GNUNET_TIME_Relative ret;
61 tracker = GNUNET_CONTAINER_multipeermap_get (trackers, peer);
63 return GNUNET_TIME_UNIT_ZERO; /* not connected, satisfy now */
66 ret = GNUNET_BANDWIDTH_tracker_get_delay (tracker, amount);
67 if (ret.rel_value_us > 0)
69 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70 "Delay to satisfy reservation for %d bytes is %s\n",
72 GNUNET_STRINGS_relative_time_to_string (ret,
77 (void) GNUNET_BANDWIDTH_tracker_consume (tracker, amount);
78 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reserved %d bytes\n", (int) amount);
79 return GNUNET_TIME_UNIT_ZERO;
84 * Set the amount of bandwidth the other peer could currently transmit
85 * to us (as far as we know) to the given value.
87 * @param peer identity of the peer
88 * @param bandwidth_in currently available bandwidth from that peer to
89 * this peer (estimate)
92 GAS_reservations_set_bandwidth (const struct GNUNET_PeerIdentity *peer,
93 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
95 struct GNUNET_BANDWIDTH_Tracker *tracker;
97 tracker = GNUNET_CONTAINER_multipeermap_get (trackers, peer);
98 if (0 == ntohl (bandwidth_in.value__))
102 GNUNET_assert (GNUNET_YES ==
103 GNUNET_CONTAINER_multipeermap_remove (trackers,
106 GNUNET_free (tracker);
111 tracker = GNUNET_new (struct GNUNET_BANDWIDTH_Tracker);
112 GNUNET_BANDWIDTH_tracker_init (tracker, NULL, NULL, bandwidth_in,
113 MAX_BANDWIDTH_CARRY_S);
114 GNUNET_assert (GNUNET_OK ==
115 GNUNET_CONTAINER_multipeermap_put (trackers, peer, tracker,
116 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
119 GNUNET_BANDWIDTH_tracker_update_quota (tracker, bandwidth_in);
124 * Handle 'reservation request' messages from clients.
126 * @param cls unused, NULL
127 * @param client client that sent the request
128 * @param message the request message
131 GAS_handle_reservation_request (void *cls,
132 struct GNUNET_SERVER_Client *client,
133 const struct GNUNET_MessageHeader *message)
135 const struct ReservationRequestMessage *msg =
136 (const struct ReservationRequestMessage *) message;
137 struct ReservationResultMessage result;
139 struct GNUNET_TIME_Relative res_delay;
140 struct GNUNET_SERVER_NotificationContext *nc;
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143 "Received RESERVATION_REQUEST message\n");
144 nc = *GNUNET_SERVER_client_get_user_context (client,
145 struct GNUNET_SERVER_NotificationContext *);
151 amount = (int32_t) ntohl (msg->amount);
152 res_delay = GAS_reservations_reserve (&msg->peer, amount);
153 if (res_delay.rel_value_us > 0)
155 result.header.size = htons (sizeof (struct ReservationResultMessage));
156 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
157 result.amount = htonl (amount);
158 result.peer = msg->peer;
159 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
160 GNUNET_STATISTICS_update (GSA_stats,
161 "# reservation requests processed",
165 GNUNET_SERVER_notification_context_unicast (nc,
169 GNUNET_SERVER_receive_done (client,
175 * Initialize reservations subsystem.
177 * @param server handle to our server
180 GAS_reservations_init (struct GNUNET_SERVER_Handle *server)
182 trackers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
187 * Free memory of bandwidth tracker.
190 * @param key peer identity (unused)
191 * @param value the `struct GNUNET_BANDWIDTH_Tracker` to free
192 * @return #GNUNET_OK (continue to iterate)
195 free_tracker (void *cls,
196 const struct GNUNET_PeerIdentity *key, void *value)
198 struct GNUNET_BANDWIDTH_Tracker *tracker = value;
200 GNUNET_free (tracker);
206 * Shutdown reservations subsystem.
209 GAS_reservations_done ()
211 GNUNET_CONTAINER_multipeermap_iterate (trackers,
214 GNUNET_CONTAINER_multipeermap_destroy (trackers);
217 /* end of gnunet-service-ats_reservations.c */