2 This file is part of GNUnet.
3 Copyright (C) 2011-2014 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file ats/gnunet-service-ats_scheduling.c
23 * @brief ats service, interaction with 'scheduling' API
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet-service-ats_addresses.h"
29 #include "gnunet-service-ats_scheduling.h"
34 * Context for sending messages to clients.
36 static struct GNUNET_SERVER_NotificationContext *nc;
39 * Actual handle to the client.
41 static struct GNUNET_SERVER_Client *my_client;
45 * Register a new scheduling client.
47 * @param client handle of the new client
48 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
51 GAS_scheduling_add_client (struct GNUNET_SERVER_Client *client)
53 if (NULL != my_client)
55 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
56 "This ATS already has a scheduling client, refusing new scheduling client for now.\n");
60 GNUNET_SERVER_notification_context_add (nc,
62 GNUNET_SERVER_client_set_user_context (client,
69 * Unregister a client (which may have been a scheduling client,
70 * but this is not assured).
72 * @param client handle of the (now dead) client
75 GAS_scheduling_remove_client (struct GNUNET_SERVER_Client *client)
77 if (my_client != client)
79 GAS_addresses_destroy_all ();
85 * Transmit the given address suggestion and bandwidth update to all scheduling
88 * @param peer peer for which this is an address suggestion
89 * @param session_id session ID to use for the given client
90 * @param bandwidth_out assigned outbound bandwidth
91 * @param bandwidth_in assigned inbound bandwidth
94 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity *peer,
96 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
97 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
99 struct AddressSuggestionMessage msg;
101 if (NULL == my_client)
103 GNUNET_STATISTICS_update (GSA_stats,
104 "# address suggestions made",
107 msg.header.size = htons (sizeof (struct AddressSuggestionMessage));
108 msg.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
110 msg.session_id = htonl (session_id);
111 msg.bandwidth_out = bandwidth_out;
112 msg.bandwidth_in = bandwidth_in;
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114 "ATS sends quota for peer `%s': (in/out) %u/%u\n",
116 (unsigned int) ntohl (bandwidth_in.value__),
117 (unsigned int) ntohl (bandwidth_out.value__));
118 GNUNET_SERVER_notification_context_unicast (nc,
126 * Handle 'address add' messages from clients.
128 * @param cls unused, NULL
129 * @param client client that sent the request
130 * @param message the request message
133 GAS_handle_address_add (void *cls,
134 struct GNUNET_SERVER_Client *client,
135 const struct GNUNET_MessageHeader *message)
137 const struct AddressAddMessage *m;
139 const char *plugin_name;
140 uint16_t address_length;
141 uint16_t plugin_name_length;
143 struct GNUNET_ATS_Properties prop;
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Received `%s' message\n",
148 size = ntohs (message->size);
149 if (size < sizeof (struct AddressAddMessage))
152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
155 m = (const struct AddressAddMessage *) message;
156 address_length = ntohs (m->address_length);
157 plugin_name_length = ntohs (m->plugin_name_length);
158 address = (const char *) &m[1];
159 if (plugin_name_length != 0)
160 plugin_name = &address[address_length];
164 if ((address_length + plugin_name_length +
165 sizeof (struct AddressAddMessage) != ntohs (message->size)) ||
166 ( (plugin_name_length > 0) &&
167 (plugin_name[plugin_name_length - 1] != '\0') ) )
170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
173 GNUNET_STATISTICS_update (GSA_stats,
174 "# addresses created",
177 GNUNET_ATS_properties_ntoh (&prop,
179 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop.scope);
180 GAS_addresses_add (&m->peer,
184 ntohl (m->address_local_info),
185 ntohl (m->session_id),
187 GNUNET_SERVER_receive_done (client,
193 * Handle 'address update' 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_address_update (void *cls,
201 struct GNUNET_SERVER_Client *client,
202 const struct GNUNET_MessageHeader *message)
204 const struct AddressUpdateMessage *m;
205 struct GNUNET_ATS_Properties prop;
207 m = (const struct AddressUpdateMessage *) message;
208 GNUNET_STATISTICS_update (GSA_stats,
209 "# address updates received",
212 GNUNET_ATS_properties_ntoh (&prop,
214 GAS_addresses_update (&m->peer,
215 ntohl (m->session_id),
217 GNUNET_SERVER_receive_done (client,
223 * Handle 'address destroyed' messages from clients.
225 * @param cls unused, NULL
226 * @param client client that sent the request
227 * @param message the request message
230 GAS_handle_address_destroyed (void *cls,
231 struct GNUNET_SERVER_Client *client,
232 const struct GNUNET_MessageHeader *message)
234 const struct AddressDestroyedMessage *m;
235 struct GNUNET_ATS_SessionReleaseMessage srm;
237 m = (const struct AddressDestroyedMessage *) message;
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "Received `%s' message\n",
240 "ADDRESS_DESTROYED");
241 GNUNET_STATISTICS_update (GSA_stats,
242 "# addresses destroyed",
245 GAS_addresses_destroy (&m->peer,
246 ntohl (m->session_id));
247 srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
248 srm.header.size = ntohs (sizeof (struct GNUNET_ATS_SessionReleaseMessage));
249 srm.session_id = m->session_id;
251 GNUNET_SERVER_notification_context_unicast (nc,
255 GNUNET_SERVER_receive_done (client, GNUNET_OK);
260 * Initialize scheduling subsystem.
262 * @param server handle to our server
263 * @param ah the address handle to use
266 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server)
268 nc = GNUNET_SERVER_notification_context_create (server, 128);
273 * Shutdown scheduling subsystem.
276 GAS_scheduling_done ()
278 if (NULL != my_client)
282 GNUNET_SERVER_notification_context_destroy (nc);
287 /* end of gnunet-service-ats_scheduling.c */