only transmit if flag is set
[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_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_ATS_Information) 
163     + plugin_addr_len + plugin_name_length;
164   char buf[msize];
165   struct GNUNET_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_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_ATS_Information* ) &msg[1];
180   memcpy (atsp, atsi, sizeof (struct GNUNET_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     if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
186       GNUNET_SERVER_notification_context_unicast (nc,
187                                                   pc->client,
188                                                   &msg->header,
189                                                   GNUNET_YES);
190 }
191
192
193 /**
194  * Handle 'reservation request' messages from clients.
195  *
196  * @param cls unused, NULL
197  * @param client client that sent the request
198  * @param message the request message
199  */
200 void
201 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
202                                 const struct GNUNET_MessageHeader *message)
203 {
204   const struct ReservationRequestMessage * msg = (const struct ReservationRequestMessage *) message;
205   struct ReservationResultMessage result;
206   int32_t amount;
207   struct GNUNET_TIME_Relative res_delay;
208
209   if (NULL == find_client (client))
210   {
211     /* missing start message! */
212     GNUNET_break (0);
213     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
214     return;
215   }
216   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217               "Received `%s' message\n",
218               "RESERVATION_REQUEST");
219   amount = (int32_t) ntohl (msg->amount);
220   res_delay = GAS_reservations_reserve (&msg->peer,
221                                         amount);
222   if (res_delay.rel_value > 0)
223     amount = 0;
224   result.header.size = htons (sizeof (struct ReservationResultMessage));
225   result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
226   result.amount = htonl (amount);
227   result.res_delay = GNUNET_TIME_relative_hton (res_delay);
228   GNUNET_SERVER_notification_context_unicast (nc,
229                                               client,
230                                               &result.header,
231                                               GNUNET_NO);
232   GNUNET_SERVER_receive_done (client, GNUNET_OK);
233 }
234
235
236 /**
237  * Handle 'preference change' messages from clients.
238  *
239  * @param cls unused, NULL
240  * @param client client that sent the request
241  * @param message the request message
242  */
243 void
244 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
245                               const struct GNUNET_MessageHeader *message)
246 {
247   const struct ChangePreferenceMessage * msg;
248   const struct PreferenceInformation *pi;
249   uint16_t msize;
250   uint32_t nump;
251   uint32_t i;
252
253   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
254   msize = ntohs (message->size);
255   if (msize < sizeof (struct ChangePreferenceMessage))
256   {
257     GNUNET_break (0);
258     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
259   }
260   msg = (const struct ChangePreferenceMessage *) message;
261   nump = ntohl (msg->num_preferences);
262   if (msize != sizeof (struct ChangePreferenceMessage) * nump * sizeof (struct PreferenceInformation))
263   {
264     GNUNET_break (0);
265     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
266   }
267   pi = (const struct PreferenceInformation *) &msg[1];
268   for (i=0;i<nump;i++)
269     GAS_addresses_change_preference (&msg->peer,
270                                      (enum GNUNET_ATS_PreferenceKind) ntohl (pi[i].preference_kind),
271                                      pi[i].preference_value);
272   GNUNET_SERVER_receive_done (client, GNUNET_OK);
273 }
274
275
276 /**
277  * Initialize performance subsystem.
278  *
279  * @param server handle to our server
280  */
281 void
282 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
283 {
284   nc = GNUNET_SERVER_notification_context_create (server, 128);
285 }
286
287
288 /**
289  * Shutdown performance subsystem.
290  */
291 void
292 GAS_performance_done ()
293 {
294   GNUNET_SERVER_notification_context_destroy (nc);
295   nc = NULL;
296 }
297
298 /* end of gnunet-service-ats_performance.c */