adding stats
[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
97 /**
98  * Register a new performance client.
99  *
100  * @param client handle of the new client
101  */
102 void
103 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
104                             enum StartFlag flag)
105 {
106   struct PerformanceClient * pc;
107
108   GNUNET_break (NULL == find_client (client));
109   pc = GNUNET_malloc (sizeof (struct PerformanceClient));
110   pc->client = client;
111   pc->flag = flag;
112   GNUNET_SERVER_notification_context_add (nc, client);
113   GNUNET_SERVER_client_keep (client);
114   GNUNET_CONTAINER_DLL_insert(pc_head, pc_tail, pc);
115 }
116
117
118 /**
119  * Unregister a client (which may have been a performance client,
120  * but this is not assured).
121  *
122  * @param client handle of the (now dead) client
123  */
124 void
125 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
126 {
127   struct PerformanceClient * pc;
128
129   pc = find_client (client);
130   if (NULL == pc)
131     return;
132   GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
133   GNUNET_SERVER_client_drop (client);
134   GNUNET_free (pc);
135 }
136
137
138 /**
139  * Transmit the given performance information to all performance
140  * clients.
141  *
142  * @param peer peer for which this is an address suggestion
143  * @param plugin_name 0-termintated string specifying the transport plugin
144  * @param plugin_addr binary address for the plugin to use
145  * @param plugin_addr_len number of bytes in plugin_addr
146  * @param atsi performance data for the address
147  * @param atsi_count number of performance records in 'ats'
148  * @param bandwidth_out assigned outbound bandwidth
149  * @param bandwidth_in assigned inbound bandwidth
150  */
151 void
152 GAS_performance_notify_clients (const struct GNUNET_PeerIdentity *peer,
153                                 const char *plugin_name,
154                                 const void *plugin_addr, size_t plugin_addr_len,
155                                 const struct GNUNET_ATS_Information *atsi,
156                                 uint32_t atsi_count,                            
157                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
158                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
159 {
160   struct PerformanceClient *pc;
161   struct PeerInformationMessage *msg;
162   size_t plugin_name_length = strlen (plugin_name) + 1;
163   size_t msize = sizeof (struct PeerInformationMessage) + atsi_count * sizeof (struct GNUNET_ATS_Information) 
164     + plugin_addr_len + plugin_name_length;
165   char buf[msize];
166   struct GNUNET_ATS_Information *atsp;
167   char *addrp;
168
169   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
170   GNUNET_assert (atsi_count < GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information));
171   msg = (struct PeerInformationMessage*) buf;
172   msg->header.size = htons (msize);
173   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
174   msg->ats_count = htonl (atsi_count);
175   msg->peer = *peer;
176   msg->address_length = htons (plugin_addr_len);
177   msg->plugin_name_length = htons (plugin_name_length);
178   msg->bandwidth_out = bandwidth_out;
179   msg->bandwidth_in = bandwidth_in;
180   atsp = (struct GNUNET_ATS_Information* ) &msg[1];
181   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
182   addrp = (char*) &atsp[atsi_count];
183   memcpy (addrp, plugin_addr, plugin_addr_len);
184   strcpy (&addrp[plugin_addr_len], plugin_name);
185   for (pc = pc_head; pc != NULL; pc = pc->next)
186     if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
187     {
188       GNUNET_SERVER_notification_context_unicast (nc,
189                                                   pc->client,
190                                                   &msg->header,
191                                                   GNUNET_YES);
192       GNUNET_STATISTICS_update (GSA_stats,
193                                 "# performance updates given to clients",
194                                 1,
195                                 GNUNET_NO);
196     }
197 }
198
199
200 /**
201  * Handle 'reservation request' messages from clients.
202  *
203  * @param cls unused, NULL
204  * @param client client that sent the request
205  * @param message the request message
206  */
207 void
208 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
209                                 const struct GNUNET_MessageHeader *message)
210 {
211   const struct ReservationRequestMessage * msg = (const struct ReservationRequestMessage *) message;
212   struct ReservationResultMessage result;
213   int32_t amount;
214   struct GNUNET_TIME_Relative res_delay;
215
216   if (NULL == find_client (client))
217   {
218     /* missing start message! */
219     GNUNET_break (0);
220     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
221     return;
222   }
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224               "Received `%s' message\n",
225               "RESERVATION_REQUEST");
226   amount = (int32_t) ntohl (msg->amount);
227   res_delay = GAS_reservations_reserve (&msg->peer,
228                                         amount);
229   if (res_delay.rel_value > 0)
230     amount = 0;
231   result.header.size = htons (sizeof (struct ReservationResultMessage));
232   result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
233   result.amount = htonl (amount);
234   result.res_delay = GNUNET_TIME_relative_hton (res_delay);
235   GNUNET_STATISTICS_update (GSA_stats,
236                             "# reservatin requests processed",
237                             1,
238                             GNUNET_NO);
239   GNUNET_SERVER_notification_context_unicast (nc,
240                                               client,
241                                               &result.header,
242                                               GNUNET_NO);
243   GNUNET_SERVER_receive_done (client, GNUNET_OK);
244 }
245
246
247 /**
248  * Handle 'preference change' messages from clients.
249  *
250  * @param cls unused, NULL
251  * @param client client that sent the request
252  * @param message the request message
253  */
254 void
255 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
256                               const struct GNUNET_MessageHeader *message)
257 {
258   const struct ChangePreferenceMessage * msg;
259   const struct PreferenceInformation *pi;
260   uint16_t msize;
261   uint32_t nump;
262   uint32_t i;
263
264   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
265   msize = ntohs (message->size);
266   if (msize < sizeof (struct ChangePreferenceMessage))
267   {
268     GNUNET_break (0);
269     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
270     return;
271   }
272   msg = (const struct ChangePreferenceMessage *) message;
273   nump = ntohl (msg->num_preferences);
274   if (msize != sizeof (struct ChangePreferenceMessage) * nump * sizeof (struct PreferenceInformation))
275   {
276     GNUNET_break (0);
277     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
278     return;
279   }
280   GNUNET_STATISTICS_update (GSA_stats,
281                             "# preference change requests processed",
282                             1,
283                             GNUNET_NO);
284   pi = (const struct PreferenceInformation *) &msg[1];
285   for (i=0;i<nump;i++)
286     GAS_addresses_change_preference (&msg->peer,
287                                      (enum GNUNET_ATS_PreferenceKind) ntohl (pi[i].preference_kind),
288                                      pi[i].preference_value);
289   GNUNET_SERVER_receive_done (client, GNUNET_OK);
290 }
291
292
293 /**
294  * Initialize performance subsystem.
295  *
296  * @param server handle to our server
297  */
298 void
299 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
300 {
301   nc = GNUNET_SERVER_notification_context_create (server, 128);
302 }
303
304
305 /**
306  * Shutdown performance subsystem.
307  */
308 void
309 GAS_performance_done ()
310 {
311   GNUNET_SERVER_notification_context_destroy (nc);
312   nc = NULL;
313 }
314
315 /* end of gnunet-service-ats_performance.c */