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 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
186 GNUNET_SERVER_notification_context_unicast (nc,
194 * Handle 'reservation request' messages from clients.
196 * @param cls unused, NULL
197 * @param client client that sent the request
198 * @param message the request message
201 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
202 const struct GNUNET_MessageHeader *message)
204 const struct ReservationRequestMessage * msg = (const struct ReservationRequestMessage *) message;
205 struct ReservationResultMessage result;
207 struct GNUNET_TIME_Relative res_delay;
209 if (NULL == find_client (client))
211 /* missing start message! */
213 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
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,
222 if (res_delay.rel_value > 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,
232 GNUNET_SERVER_receive_done (client, GNUNET_OK);
237 * Handle 'preference change' messages from clients.
239 * @param cls unused, NULL
240 * @param client client that sent the request
241 * @param message the request message
244 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
245 const struct GNUNET_MessageHeader *message)
247 const struct ChangePreferenceMessage * msg;
248 const struct PreferenceInformation *pi;
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
254 msize = ntohs (message->size);
255 if (msize < sizeof (struct ChangePreferenceMessage))
258 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
260 msg = (const struct ChangePreferenceMessage *) message;
261 nump = ntohl (msg->num_preferences);
262 if (msize != sizeof (struct ChangePreferenceMessage) * nump * sizeof (struct PreferenceInformation))
265 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
267 pi = (const struct PreferenceInformation *) &msg[1];
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);
277 * Initialize performance subsystem.
279 * @param server handle to our server
282 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
284 nc = GNUNET_SERVER_notification_context_create (server, 128);
289 * Shutdown performance subsystem.
292 GAS_performance_done ()
294 GNUNET_SERVER_notification_context_destroy (nc);
298 /* end of gnunet-service-ats_performance.c */