08764f4d1e36323d7d5dc47cbc720a5d6ef3361a
[oweals/gnunet.git] / src / ats / gnunet-service-ats_reservations.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 /**
22  * @file ats/gnunet-service-ats_reservations.c
23  * @brief ats service, inbound bandwidth reservation management
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet-service-ats_reservations.h"
28
29 /**
30  * Number of seconds that available bandwidth carries over 
31  * (can accumulate).
32  */
33 #define MAX_BANDWIDTH_CARRY_S 5
34
35
36 /**
37  * Map of peer identities to 'struct GNUNET_BANDWIDTH_Tracker *'s
38  */
39 static struct GNUNET_CONTAINER_MultiHashMap *trackers;
40
41
42 /**
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.
46  *
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
52  */
53 struct GNUNET_TIME_Relative
54 GAS_reservations_reserve (const struct GNUNET_PeerIdentity *peer,
55                           int32_t amount)
56 {
57   struct GNUNET_BANDWIDTH_Tracker *tracker;
58   struct GNUNET_TIME_Relative ret;
59
60   tracker = GNUNET_CONTAINER_multihashmap_get (trackers,
61                                                &peer->hashPubKey);
62   if (NULL == tracker)
63     return GNUNET_TIME_UNIT_FOREVER_REL;
64   if (amount >= 0)
65   {
66     ret = GNUNET_BANDWIDTH_tracker_get_delay (tracker,
67                                               amount);
68     if (ret.rel_value > 0)
69       return ret;
70   }
71   GNUNET_break (GNUNET_NO == /* no == not above limit */
72                 GNUNET_BANDWIDTH_tracker_consume (tracker, amount));
73   return GNUNET_TIME_UNIT_ZERO;
74 }
75
76
77 /**
78  * Set the amount of bandwidth the other peer could currently transmit
79  * to us (as far as we know) to the given value.
80  * 
81  * @param peer identity of the peer
82  * @param bandwidth_in currently available bandwidth from that peer to
83  *        this peer (estimate)
84  */
85 void
86 GAS_reservations_set_bandwidth (const struct GNUNET_PeerIdentity *peer,
87                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
88 {
89   struct GNUNET_BANDWIDTH_Tracker *tracker;
90
91   tracker = GNUNET_CONTAINER_multihashmap_get (trackers,
92                                                &peer->hashPubKey);
93   if (0 == ntohl (bandwidth_in.value__))
94   {
95     GNUNET_assert (GNUNET_YES ==
96                    GNUNET_CONTAINER_multihashmap_remove (trackers,
97                                                          &peer->hashPubKey,
98                                                          tracker));
99     GNUNET_free (tracker);
100     return;
101   }
102   if (NULL == tracker)
103   {
104     tracker = GNUNET_malloc (sizeof (struct GNUNET_BANDWIDTH_Tracker));
105     GNUNET_BANDWIDTH_tracker_init (tracker,
106                                    bandwidth_in,
107                                    MAX_BANDWIDTH_CARRY_S);
108     GNUNET_CONTAINER_multihashmap_put (trackers,
109                                        &peer->hashPubKey,
110                                        tracker,
111                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
112     return;
113   }
114   GNUNET_BANDWIDTH_tracker_update_quota (tracker,
115                                          bandwidth_in);
116 }
117
118
119
120 /**
121  * Initialize reservations subsystem.
122  */
123 void
124 GAS_reservations_init ()
125 {
126   trackers = GNUNET_CONTAINER_multihashmap_create(128);
127 }
128
129
130 /**
131  * Free memory of bandwidth tracker.
132  *
133  * @param cls NULL
134  * @param key peer identity (unused)
135  * @param value the 'struct GNUNET_BANDWIDTH_Tracker' to free
136  * @return GNUNET_OK (continue to iterate)
137  */
138 static int 
139 free_tracker (void *cls,
140               const GNUNET_HashCode * key,
141               void *value)
142 {
143   struct GNUNET_BANDWIDTH_Tracker *tracker = cls;
144   GNUNET_free (tracker);
145   return GNUNET_OK;
146 }
147
148
149 /**
150  * Shutdown reservations subsystem.
151  */
152 void
153 GAS_reservations_done ()
154 {
155   GNUNET_CONTAINER_multihashmap_iterate (trackers, &free_tracker, NULL);
156   GNUNET_CONTAINER_multihashmap_destroy (trackers);
157 }
158
159
160
161 /* end of gnunet-service-ats_reservations.c */