parse preference change messages
[oweals/gnunet.git] / src / ats / gnunet-service-ats_performance.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_performance.c
23  * @brief ats service, interaction with 'performance' API
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet-service-ats_addresses.h"
29 #include "gnunet-service-ats_performance.h"
30 #include "gnunet-service-ats_reservations.h"
31 #include "ats.h"
32
33
34 /**
35  * We keep clients that are interested in performance in a linked list.
36  */
37 struct PerformanceClient
38 {
39   /**
40    * Next in doubly-linked list.
41    */
42   struct PerformanceClient * next;
43
44   /**
45    * Previous in doubly-linked list.
46    */
47   struct PerformanceClient * prev;
48   
49   /**
50    * Actual handle to the client.
51    */
52   struct GNUNET_SERVER_Client *client;
53
54   /**
55    * Options for the client.
56    */
57   enum StartFlag flag;
58
59 };
60
61
62 /**
63  * Head of linked list of all clients to this service.
64  */
65 static struct PerformanceClient *pc_head;
66
67 /**
68  * Tail of linked list of all clients to this service.
69  */
70 static struct PerformanceClient *pc_tail;
71  
72 /**
73  * Context for sending messages to performance clients.
74  */
75 static struct GNUNET_SERVER_NotificationContext *nc;
76
77
78 /**
79  * Find the performance client associated with the given handle.
80  *
81  * @param client server handle
82  * @return internal handle
83  */
84 static struct PerformanceClient * 
85 find_client (struct GNUNET_SERVER_Client *client)
86 {
87   struct PerformanceClient * pc;
88
89   for (pc = pc_head; pc != NULL; pc = pc->next)
90     if (pc->client == client)
91       return pc;
92   return NULL;
93 }
94
95
96 /**
97  * Register a new performance client.
98  *
99  * @param client handle of the new client
100  */
101 void
102 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
103                             enum StartFlag flag)
104 {
105   struct PerformanceClient * pc;
106
107   GNUNET_break (NULL == find_client (client));
108   pc = GNUNET_malloc (sizeof (struct PerformanceClient));
109   pc->client = client;
110   pc->flag = flag;
111   GNUNET_SERVER_notification_context_add (nc, client);
112   GNUNET_SERVER_client_keep (client);
113   GNUNET_CONTAINER_DLL_insert(pc_head, pc_tail, pc);
114 }
115
116
117 /**
118  * Unregister a client (which may have been a performance client,
119  * but this is not assured).
120  *
121  * @param client handle of the (now dead) client
122  */
123 void
124 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
125 {
126   struct PerformanceClient * pc;
127
128   pc = find_client (client);
129   if (NULL == pc)
130     return;
131   GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
132   GNUNET_SERVER_client_drop (client);
133   GNUNET_free (pc);
134 }
135
136
137 /**
138  * Transmit the given performance information to all performance
139  * clients.
140  *
141  * @param peer peer for which this is an address suggestion
142  * @param plugin_name 0-termintated string specifying the transport plugin
143  * @param plugin_addr binary address for the plugin to use
144  * @param plugin_addr_len number of bytes in plugin_addr
145  * @param atsi performance data for the address
146  * @param atsi_count number of performance records in 'ats'
147  * @param bandwidth_out assigned outbound bandwidth
148  * @param bandwidth_in assigned inbound bandwidth
149  */
150 void
151 GAS_performance_notify_clients (const struct GNUNET_PeerIdentity *peer,
152                                 const char *plugin_name,
153                                 const void *plugin_addr, size_t plugin_addr_len,
154                                 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
155                                 uint32_t atsi_count,                            
156                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
157                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
158 {
159   struct PerformanceClient *pc;
160   struct PeerInformationMessage *msg;
161   size_t plugin_name_length = strlen (plugin_name) + 1;
162   size_t msize = sizeof (struct PeerInformationMessage) + atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) 
163     + plugin_addr_len + plugin_name_length;
164   char buf[msize];
165   struct GNUNET_TRANSPORT_ATS_Information *atsp;
166   char *addrp;
167
168   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
169   GNUNET_assert (atsi_count < GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_TRANSPORT_ATS_Information));
170   msg = (struct PeerInformationMessage*) buf;
171   msg->header.size = htons (msize);
172   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
173   msg->ats_count = htonl (atsi_count);
174   msg->peer = *peer;
175   msg->address_length = htons (plugin_addr_len);
176   msg->plugin_name_length = htons (plugin_name_length);
177   msg->bandwidth_out = bandwidth_out;
178   msg->bandwidth_in = bandwidth_in;
179   atsp = (struct GNUNET_TRANSPORT_ATS_Information* ) &msg[1];
180   memcpy (atsp, atsi, sizeof (struct GNUNET_TRANSPORT_ATS_Information) * atsi_count);
181   addrp = (char*) &atsp[atsi_count];
182   memcpy (addrp, plugin_addr, plugin_addr_len);
183   strcpy (&addrp[plugin_addr_len], plugin_name);
184   for (pc = pc_head; pc != NULL; pc = pc->next)
185     GNUNET_SERVER_notification_context_unicast (nc,
186                                                 pc->client,
187                                                 &msg->header,
188                                                 GNUNET_YES);
189 }
190
191
192 /**
193  * Handle 'reservation request' messages from clients.
194  *
195  * @param cls unused, NULL
196  * @param client client that sent the request
197  * @param message the request message
198  */
199 void
200 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
201                                 const struct GNUNET_MessageHeader *message)
202 {
203   const struct ReservationRequestMessage * msg = (const struct ReservationRequestMessage *) message;
204   struct ReservationResultMessage result;
205   int32_t amount;
206   struct GNUNET_TIME_Relative res_delay;
207
208   if (NULL == find_client (client))
209   {
210     /* missing start message! */
211     GNUNET_break (0);
212     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
213     return;
214   }
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216               "Received `%s' message\n",
217               "RESERVATION_REQUEST");
218   amount = (int32_t) ntohl (msg->amount);
219   res_delay = GAS_reservations_reserve (&msg->peer,
220                                         amount);
221   if (res_delay.rel_value > 0)
222     amount = 0;
223   result.header.size = htons (sizeof (struct ReservationResultMessage));
224   result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
225   result.amount = htonl (amount);
226   result.res_delay = GNUNET_TIME_relative_hton (res_delay);
227   GNUNET_SERVER_notification_context_unicast (nc,
228                                               client,
229                                               &result.header,
230                                               GNUNET_NO);
231   GNUNET_SERVER_receive_done (client, GNUNET_OK);
232 }
233
234
235 /**
236  * Handle 'preference change' messages from clients.
237  *
238  * @param cls unused, NULL
239  * @param client client that sent the request
240  * @param message the request message
241  */
242 void
243 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
244                               const struct GNUNET_MessageHeader *message)
245 {
246   const struct ChangePreferenceMessage * msg;
247   const struct PreferenceInformation *pi;
248   uint16_t msize;
249   uint32_t nump;
250   uint32_t i;
251
252   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
253   msize = ntohs (message->size);
254   if (msize < sizeof (struct ChangePreferenceMessage))
255   {
256     GNUNET_break (0);
257     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
258   }
259   msg = (const struct ChangePreferenceMessage *) message;
260   nump = ntohl (msg->num_preferences);
261   if (msize != sizeof (struct ChangePreferenceMessage) * nump * sizeof (struct PreferenceInformation))
262   {
263     GNUNET_break (0);
264     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
265   }
266   pi = (const struct PreferenceInformation *) &msg[1];
267   for (i=0;i<nump;i++)
268     GAS_addresses_change_preference (&msg->peer,
269                                      (enum GNUNET_ATS_PreferenceKind) ntohl (pi[i].preference_kind),
270                                      pi[i].preference_value);
271   GNUNET_SERVER_receive_done (client, GNUNET_OK);
272 }
273
274
275 /**
276  * Initialize performance subsystem.
277  *
278  * @param server handle to our server
279  */
280 void
281 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
282 {
283   nc = GNUNET_SERVER_notification_context_create (server, 128);
284 }
285
286
287 /**
288  * Shutdown performance subsystem.
289  */
290 void
291 GAS_performance_done ()
292 {
293   GNUNET_SERVER_notification_context_destroy (nc);
294   nc = NULL;
295 }
296
297 /* end of gnunet-service-ats_performance.c */