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_addresses.h"
29 #include "gnunet-service-ats_performance.h"
30 #include "gnunet-service-ats_reservations.h"
35 * We keep clients that are interested in performance in a linked list.
37 struct PerformanceClient
40 * Next in doubly-linked list.
42 struct PerformanceClient * next;
45 * Previous in doubly-linked list.
47 struct PerformanceClient * prev;
50 * Actual handle to the client.
52 struct GNUNET_SERVER_Client *client;
55 * Options for the client.
63 * Head of linked list of all clients to this service.
65 static struct PerformanceClient *pc_head;
68 * Tail of linked list of all clients to this service.
70 static struct PerformanceClient *pc_tail;
73 * Context for sending messages to performance clients.
75 static struct GNUNET_SERVER_NotificationContext *nc;
79 * Find the performance client associated with the given handle.
81 * @param client server handle
82 * @return internal handle
84 static struct PerformanceClient *
85 find_client (struct GNUNET_SERVER_Client *client)
87 struct PerformanceClient * pc;
89 for (pc = pc_head; pc != NULL; pc = pc->next)
90 if (pc->client == client)
97 * Register a new performance client.
99 * @param client handle of the new client
102 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
105 struct PerformanceClient * pc;
107 GNUNET_break (NULL == find_client (client));
108 pc = GNUNET_malloc (sizeof (struct PerformanceClient));
111 GNUNET_SERVER_notification_context_add (nc, client);
112 GNUNET_SERVER_client_keep (client);
113 GNUNET_CONTAINER_DLL_insert(pc_head, pc_tail, pc);
118 * Unregister a client (which may have been a performance client,
119 * but this is not assured).
121 * @param client handle of the (now dead) client
124 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
126 struct PerformanceClient * pc;
128 pc = find_client (client);
131 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
132 GNUNET_SERVER_client_drop (client);
138 * Transmit the given performance information to all performance
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
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,
156 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
157 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
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;
165 struct GNUNET_ATS_Information *atsp;
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);
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 GNUNET_SERVER_notification_context_unicast (nc,
193 * Handle 'reservation request' messages from clients.
195 * @param cls unused, NULL
196 * @param client client that sent the request
197 * @param message the request message
200 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
201 const struct GNUNET_MessageHeader *message)
203 const struct ReservationRequestMessage * msg = (const struct ReservationRequestMessage *) message;
204 struct ReservationResultMessage result;
206 struct GNUNET_TIME_Relative res_delay;
208 if (NULL == find_client (client))
210 /* missing start message! */
212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Received `%s' message\n",
217 "RESERVATION_REQUEST");
218 amount = (int32_t) ntohl (msg->amount);
219 res_delay = GAS_reservations_reserve (&msg->peer,
221 if (res_delay.rel_value > 0)
223 result.header.size = htons (sizeof (struct ReservationResultMessage));
224 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
225 result.amount = htonl (amount);
226 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
227 GNUNET_SERVER_notification_context_unicast (nc,
231 GNUNET_SERVER_receive_done (client, GNUNET_OK);
236 * Handle 'preference change' messages from clients.
238 * @param cls unused, NULL
239 * @param client client that sent the request
240 * @param message the request message
243 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
244 const struct GNUNET_MessageHeader *message)
246 const struct ChangePreferenceMessage * msg;
247 const struct PreferenceInformation *pi;
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
253 msize = ntohs (message->size);
254 if (msize < sizeof (struct ChangePreferenceMessage))
257 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
259 msg = (const struct ChangePreferenceMessage *) message;
260 nump = ntohl (msg->num_preferences);
261 if (msize != sizeof (struct ChangePreferenceMessage) * nump * sizeof (struct PreferenceInformation))
264 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
266 pi = (const struct PreferenceInformation *) &msg[1];
268 GAS_addresses_change_preference (&msg->peer,
269 (enum GNUNET_ATS_PreferenceKind) ntohl (pi[i].preference_kind),
270 pi[i].preference_value);
271 GNUNET_SERVER_receive_done (client, GNUNET_OK);
276 * Initialize performance subsystem.
278 * @param server handle to our server
281 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
283 nc = GNUNET_SERVER_notification_context_create (server, 128);
288 * Shutdown performance subsystem.
291 GAS_performance_done ()
293 GNUNET_SERVER_notification_context_destroy (nc);
297 /* end of gnunet-service-ats_performance.c */