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