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_performance.h"
29 #include "gnunet-service-ats_reservations.h"
34 * We keep clients that are interested in performance in a linked list.
36 struct PerformanceClient
39 * Next in doubly-linked list.
41 struct PerformanceClient * next;
44 * Previous in doubly-linked list.
46 struct PerformanceClient * prev;
49 * Actual handle to the client.
51 struct GNUNET_SERVER_Client *client;
54 * Options for the client.
62 * Head of linked list of all clients to this service.
64 static struct PerformanceClient *pc_head;
67 * Tail of linked list of all clients to this service.
69 static struct PerformanceClient *pc_tail;
72 * Context for sending messages to performance clients.
74 static struct GNUNET_SERVER_NotificationContext *nc;
78 * Find the performance client associated with the given handle.
80 * @param client server handle
81 * @return internal handle
83 static struct PerformanceClient *
84 find_client (struct GNUNET_SERVER_Client *client)
86 struct PerformanceClient * pc;
88 for (pc = pc_head; pc != NULL; pc = pc->next)
89 if (pc->client == client)
96 * Register a new performance client.
98 * @param client handle of the new client
101 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
104 struct PerformanceClient * pc;
106 GNUNET_break (NULL == find_client (client));
107 pc = GNUNET_malloc (sizeof (struct PerformanceClient));
110 GNUNET_SERVER_notification_context_add (nc, client);
111 GNUNET_SERVER_client_keep (client);
112 GNUNET_CONTAINER_DLL_insert(pc_head, pc_tail, pc);
117 * Unregister a client (which may have been a performance client,
118 * but this is not assured).
120 * @param client handle of the (now dead) client
123 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
125 struct PerformanceClient * pc;
127 pc = find_client (client);
130 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
131 GNUNET_SERVER_client_drop (client);
137 * Transmit the given performance information to all performance
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
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,
155 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
156 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
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;
164 struct GNUNET_TRANSPORT_ATS_Information *atsp;
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);
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,
192 * Handle 'reservation request' messages from clients.
194 * @param cls unused, NULL
195 * @param client client that sent the request
196 * @param message the request message
199 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
200 const struct GNUNET_MessageHeader *message)
202 const struct ReservationRequestMessage * msg = (const struct ReservationRequestMessage *) message;
203 struct ReservationResultMessage result;
205 struct GNUNET_TIME_Relative res_delay;
207 if (NULL == find_client (client))
209 /* missing start message! */
211 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
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,
220 if (res_delay.rel_value > 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,
230 GNUNET_SERVER_receive_done (client, GNUNET_OK);
235 * Handle 'preference change' messages from clients.
237 * @param cls unused, NULL
238 * @param client client that sent the request
239 * @param message the request message
242 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
243 const struct GNUNET_MessageHeader *message)
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);
253 * Initialize performance subsystem.
255 * @param server handle to our server
258 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
260 nc = GNUNET_SERVER_notification_context_create (server, 128);
265 * Shutdown performance subsystem.
268 GAS_performance_done ()
270 GNUNET_SERVER_notification_context_destroy (nc);
274 /* end of gnunet-service-ats_performance.c */