2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file ats/gnunet-service-ats_performance.c
23 * @brief ats service, interaction with 'performance' API
24 * @author Matthias Wachs
25 * @author Christian Grothoff
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"
36 * We keep clients that are interested in performance in a linked list.
38 struct PerformanceClient
41 * Next in doubly-linked list.
43 struct PerformanceClient *next;
46 * Previous in doubly-linked list.
48 struct PerformanceClient *prev;
51 * Actual handle to the client.
53 struct GNUNET_SERVER_Client *client;
56 * Options for the client.
64 * Head of linked list of all clients to this service.
66 static struct PerformanceClient *pc_head;
69 * Tail of linked list of all clients to this service.
71 static struct PerformanceClient *pc_tail;
74 * Context for sending messages to performance clients.
76 static struct GNUNET_SERVER_NotificationContext *nc;
80 * Find the performance client associated with the given handle.
82 * @param client server handle
83 * @return internal handle
85 static struct PerformanceClient *
86 find_client (struct GNUNET_SERVER_Client *client)
88 struct PerformanceClient *pc;
90 for (pc = pc_head; pc != NULL; pc = pc->next)
91 if (pc->client == client)
98 * Register a new performance client.
100 * @param client handle of the new client
103 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
106 struct PerformanceClient *pc;
108 GNUNET_break (NULL == find_client (client));
109 pc = GNUNET_malloc (sizeof (struct PerformanceClient));
112 GNUNET_SERVER_notification_context_add (nc, client);
113 GNUNET_SERVER_client_keep (client);
114 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
119 * Unregister a client (which may have been a performance client,
120 * but this is not assured).
122 * @param client handle of the (now dead) client
125 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
127 struct PerformanceClient *pc;
129 pc = find_client (client);
132 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
133 GNUNET_SERVER_client_drop (client);
139 * Transmit the given performance information to all performance
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
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,
157 struct GNUNET_BANDWIDTH_Value32NBO
159 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
161 struct PerformanceClient *pc;
162 struct PeerInformationMessage *msg;
163 size_t plugin_name_length = strlen (plugin_name) + 1;
165 sizeof (struct PeerInformationMessage) +
166 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
169 struct GNUNET_ATS_Information *atsp;
172 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
173 GNUNET_assert (atsi_count <
174 GNUNET_SERVER_MAX_MESSAGE_SIZE /
175 sizeof (struct GNUNET_ATS_Information));
176 msg = (struct PeerInformationMessage *) buf;
177 msg->header.size = htons (msize);
178 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
179 msg->ats_count = htonl (atsi_count);
181 msg->address_length = htons (plugin_addr_len);
182 msg->plugin_name_length = htons (plugin_name_length);
183 msg->bandwidth_out = bandwidth_out;
184 msg->bandwidth_in = bandwidth_in;
185 atsp = (struct GNUNET_ATS_Information *) &msg[1];
186 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
187 addrp = (char *) &atsp[atsi_count];
188 memcpy (addrp, plugin_addr, plugin_addr_len);
189 strcpy (&addrp[plugin_addr_len], plugin_name);
190 for (pc = pc_head; pc != NULL; pc = pc->next)
191 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
193 GNUNET_SERVER_notification_context_unicast (nc, pc->client, &msg->header,
195 GNUNET_STATISTICS_update (GSA_stats,
196 "# performance updates given to clients", 1,
203 * Handle 'reservation request' messages from clients.
205 * @param cls unused, NULL
206 * @param client client that sent the request
207 * @param message the request message
210 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
211 const struct GNUNET_MessageHeader *message)
213 const struct ReservationRequestMessage *msg =
214 (const struct ReservationRequestMessage *) message;
215 struct ReservationResultMessage result;
217 struct GNUNET_TIME_Relative res_delay;
219 if (NULL == find_client (client))
221 /* missing start message! */
223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
227 "RESERVATION_REQUEST");
228 amount = (int32_t) ntohl (msg->amount);
229 res_delay = GAS_reservations_reserve (&msg->peer, amount);
230 if (res_delay.rel_value > 0)
232 result.header.size = htons (sizeof (struct ReservationResultMessage));
233 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
234 result.amount = htonl (amount);
235 result.peer = msg->peer;
236 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
237 GNUNET_STATISTICS_update (GSA_stats, "# reservation requests processed", 1,
239 GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
241 GNUNET_SERVER_receive_done (client, GNUNET_OK);
246 * Handle 'preference change' messages from clients.
248 * @param cls unused, NULL
249 * @param client client that sent the request
250 * @param message the request message
253 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
254 const struct GNUNET_MessageHeader *message)
256 const struct ChangePreferenceMessage *msg;
257 const struct PreferenceInformation *pi;
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
263 "PREFERENCE_CHANGE");
264 msize = ntohs (message->size);
265 if (msize < sizeof (struct ChangePreferenceMessage))
268 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
271 msg = (const struct ChangePreferenceMessage *) message;
272 nump = ntohl (msg->num_preferences);
274 sizeof (struct ChangePreferenceMessage) +
275 nump * sizeof (struct PreferenceInformation))
278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
281 GNUNET_STATISTICS_update (GSA_stats, "# preference change requests processed",
283 pi = (const struct PreferenceInformation *) &msg[1];
284 for (i = 0; i < nump; i++)
285 GAS_addresses_change_preference (&msg->peer,
286 (enum GNUNET_ATS_PreferenceKind)
287 ntohl (pi[i].preference_kind),
288 pi[i].preference_value);
289 GNUNET_SERVER_receive_done (client, GNUNET_OK);
294 * Initialize performance subsystem.
296 * @param server handle to our server
299 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
301 nc = GNUNET_SERVER_notification_context_create (server, 128);
306 * Shutdown performance subsystem.
309 GAS_performance_done ()
311 GNUNET_SERVER_notification_context_destroy (nc);
315 /* end of gnunet-service-ats_performance.c */