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