aboutsummaryrefslogtreecommitdiff
path: root/src/ats/gnunet-service-ats.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-09-21 10:56:28 +0000
committerChristian Grothoff <christian@grothoff.org>2016-09-21 10:56:28 +0000
commitd4afc6e37a14fe3257263c377a243c1a22ed9ee5 (patch)
treeccfce6d4f92808372d3e7ebfe9f5372e9f21f50c /src/ats/gnunet-service-ats.c
parent60d02b5b0899f454cb65408bd2ed4c453fa75a3d (diff)
downloadgnunet-d4afc6e37a14fe3257263c377a243c1a22ed9ee5.tar.gz
gnunet-d4afc6e37a14fe3257263c377a243c1a22ed9ee5.zip
migrating more services to new service API
Diffstat (limited to 'src/ats/gnunet-service-ats.c')
-rw-r--r--src/ats/gnunet-service-ats.c456
1 files changed, 374 insertions, 82 deletions
diff --git a/src/ats/gnunet-service-ats.c b/src/ats/gnunet-service-ats.c
index 045a5bb67..9762faa51 100644
--- a/src/ats/gnunet-service-ats.c
+++ b/src/ats/gnunet-service-ats.c
@@ -28,7 +28,6 @@
28#include "gnunet-service-ats.h" 28#include "gnunet-service-ats.h"
29#include "gnunet-service-ats_addresses.h" 29#include "gnunet-service-ats_addresses.h"
30#include "gnunet-service-ats_connectivity.h" 30#include "gnunet-service-ats_connectivity.h"
31#include "gnunet-service-ats_feedback.h"
32#include "gnunet-service-ats_normalization.h" 31#include "gnunet-service-ats_normalization.h"
33#include "gnunet-service-ats_performance.h" 32#include "gnunet-service-ats_performance.h"
34#include "gnunet-service-ats_preferences.h" 33#include "gnunet-service-ats_preferences.h"
@@ -42,27 +41,19 @@
42 */ 41 */
43struct GNUNET_STATISTICS_Handle *GSA_stats; 42struct GNUNET_STATISTICS_Handle *GSA_stats;
44 43
45/**
46 * Handle to the ATS server.
47 */
48static struct GNUNET_SERVER_Handle *GSA_server;
49
50 44
51/** 45/**
52 * We have received a `struct ClientStartMessage` from a client. Find 46 * We have received a `struct ClientStartMessage` from a client. Find
53 * out which type of client it is and notify the respective subsystem. 47 * out which type of client it is and notify the respective subsystem.
54 * 48 *
55 * @param cls closure, unused 49 * @param cls handle to the client
56 * @param client handle to the client 50 * @param msg the start message
57 * @param message the start message
58 */ 51 */
59static void 52static void
60handle_ats_start (void *cls, 53handle_ats_start (void *cls,
61 struct GNUNET_SERVER_Client *client, 54 const struct ClientStartMessage *msg)
62 const struct GNUNET_MessageHeader *message)
63{ 55{
64 const struct ClientStartMessage *msg = 56 struct GNUNET_SERVICE_Client *client = cls;
65 (const struct ClientStartMessage *) message;
66 enum StartFlag flag; 57 enum StartFlag flag;
67 58
68 flag = ntohl (msg->start_flag); 59 flag = ntohl (msg->start_flag);
@@ -72,10 +63,10 @@ handle_ats_start (void *cls,
72 switch (flag) 63 switch (flag)
73 { 64 {
74 case START_FLAG_SCHEDULING: 65 case START_FLAG_SCHEDULING:
75 if (GNUNET_OK != GAS_scheduling_add_client (client)) 66 if (GNUNET_OK !=
67 GAS_scheduling_add_client (client))
76 { 68 {
77 GNUNET_SERVER_receive_done (client, 69 GNUNET_SERVICE_client_drop (client);
78 GNUNET_SYSERR);
79 return; 70 return;
80 } 71 }
81 break; 72 break;
@@ -92,12 +83,310 @@ handle_ats_start (void *cls,
92 break; 83 break;
93 default: 84 default:
94 GNUNET_break (0); 85 GNUNET_break (0);
95 GNUNET_SERVER_receive_done (client, 86 GNUNET_SERVICE_client_drop (client);
96 GNUNET_SYSERR);
97 return; 87 return;
98 } 88 }
99 GNUNET_SERVER_receive_done (client, 89 GNUNET_SERVICE_client_continue (client);
100 GNUNET_OK); 90}
91
92
93
94/**
95 * Handle 'reservation request' messages from clients.
96 *
97 * @param cls client that sent the request
98 * @param message the request message
99 */
100static void
101handle_reservation_request (void *cls,
102 const struct ReservationRequestMessage *message)
103{
104 struct GNUNET_SERVICE_Client *client = cls;
105
106 GAS_handle_reservation_request (client,
107 message);
108 GNUNET_SERVICE_client_continue (client);
109}
110
111
112/**
113 * Check 'preference feedback' message is well-formed
114 *
115 * @param cls client that sent the request
116 * @param message the request message
117 * @return #GNUNET_OK if @a message is well-formed
118 */
119static int
120check_feedback (void *cls,
121 const struct FeedbackPreferenceMessage *message)
122{
123 uint16_t msize;
124 uint32_t nump;
125
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Received PREFERENCE_FEEDBACK message\n");
128 msize = ntohs (message->header.size);
129 nump = ntohl (message->num_feedback);
130 if (msize !=
131 sizeof (struct FeedbackPreferenceMessage) +
132 nump * sizeof (struct PreferenceInformation))
133 {
134 GNUNET_break (0);
135 return GNUNET_SYSERR;
136 }
137 return GNUNET_OK;
138}
139
140
141/**
142 * Handle 'preference feedback' messages from clients.
143 *
144 * @param cls client that sent the request
145 * @param msg the request message
146 */
147static void
148handle_feedback (void *cls,
149 const struct FeedbackPreferenceMessage *msg)
150{
151 struct GNUNET_SERVICE_Client *client = cls;
152 const struct PreferenceInformation *pi;
153 uint32_t nump;
154
155 nump = ntohl (msg->num_feedback);
156 if (GNUNET_NO ==
157 GNUNET_CONTAINER_multipeermap_contains (GSA_addresses,
158 &msg->peer))
159 {
160 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
161 "Received PREFERENCE FEEDBACK for unknown peer `%s'\n",
162 GNUNET_i2s (&msg->peer));
163 GNUNET_SERVICE_client_continue (client);
164 return;
165 }
166
167 GNUNET_STATISTICS_update (GSA_stats,
168 "# preference feedbacks requests processed",
169 1,
170 GNUNET_NO);
171 pi = (const struct PreferenceInformation *) &msg[1];
172 for (uint32_t i = 0; i < nump; i++)
173 {
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Received PREFERENCE FEEDBACK for peer `%s'\n",
176 GNUNET_i2s (&msg->peer));
177 GAS_plugin_notify_feedback (client,
178 &msg->peer,
179 GNUNET_TIME_relative_ntoh (msg->scope),
180 (enum GNUNET_ATS_PreferenceKind) ntohl (pi[i].preference_kind),
181 pi[i].preference_value);
182 }
183 GNUNET_SERVICE_client_continue (client);
184}
185
186
187/**
188 * Handle 'request address list' messages from clients.
189 *
190 * @param cls client that sent the request
191 * @param message the request message
192 */
193static void
194handle_request_address_list (void *cls,
195 const struct AddressListRequestMessage *message)
196{
197 struct GNUNET_SERVICE_Client *client = cls;
198
199 GAS_handle_request_address_list (client,
200 message);
201 GNUNET_SERVICE_client_continue (client);
202}
203
204
205/**
206 * Handle 'request address' messages from clients.
207 *
208 * @param cls client that sent the request
209 * @param message the request message
210 */
211static void
212handle_request_address (void *cls,
213 const struct RequestAddressMessage * message)
214{
215 struct GNUNET_SERVICE_Client *client = cls;
216
217 GAS_handle_request_address (client,
218 message);
219 GNUNET_SERVICE_client_continue (client);
220}
221
222
223/**
224 * Cancel 'request address' messages from clients.
225 *
226 * @param cls client that sent the request
227 * @param message the request message
228 */
229static void
230handle_request_address_cancel (void *cls,
231 const struct RequestAddressMessage *message)
232{
233 struct GNUNET_SERVICE_Client *client = cls;
234
235 GAS_handle_request_address_cancel (client,
236 message);
237 GNUNET_SERVICE_client_continue (client);
238}
239
240
241/**
242 * Handle 'address add' messages from clients.
243 *
244 * @param cls client that sent the request
245 * @param m the request message
246 */
247static int
248check_address_add (void *cls,
249 const struct AddressAddMessage *m)
250{
251 const char *address;
252 const char *plugin_name;
253 uint16_t address_length;
254 uint16_t plugin_name_length;
255 uint16_t size;
256
257 size = ntohs (m->header.size);
258 address_length = ntohs (m->address_length);
259 plugin_name_length = ntohs (m->plugin_name_length);
260 address = (const char *) &m[1];
261 if (plugin_name_length != 0)
262 plugin_name = &address[address_length];
263 else
264 plugin_name = "";
265
266 if ( (address_length + plugin_name_length +
267 sizeof (struct AddressAddMessage) != size) ||
268 ( (plugin_name_length > 0) &&
269 (plugin_name[plugin_name_length - 1] != '\0') ) )
270 {
271 GNUNET_break (0);
272 return GNUNET_SYSERR;
273 }
274 return GNUNET_OK;
275}
276
277
278/**
279 * Handle 'address add' messages from clients.
280 *
281 * @param cls client that sent the request
282 * @param message the request message
283 */
284static void
285handle_address_add (void *cls,
286 const struct AddressAddMessage *message)
287{
288 struct GNUNET_SERVICE_Client *client = cls;
289
290 GAS_handle_address_add (message);
291 GNUNET_SERVICE_client_continue (client);
292}
293
294
295/**
296 * Handle 'address update' messages from clients.
297 *
298 * @param cls client that sent the request
299 * @param message the request message
300 */
301static void
302handle_address_update (void *cls,
303 const struct AddressUpdateMessage *message)
304{
305 struct GNUNET_SERVICE_Client *client = cls;
306
307 GAS_handle_address_update (message);
308 GNUNET_SERVICE_client_continue (client);
309}
310
311
312/**
313 * Handle 'address destroyed' messages from clients.
314 *
315 * @param cls client that sent the request
316 * @param message the request message
317 */
318static void
319handle_address_destroyed (void *cls,
320 const struct AddressDestroyedMessage *message)
321{
322 struct GNUNET_SERVICE_Client *client = cls;
323
324 GAS_handle_address_destroyed (message);
325 GNUNET_SERVICE_client_continue (client);
326}
327
328
329/**
330 * Check that 'change preference' message is well-formed.
331 *
332 * @param cls client that sent the request
333 * @param message the request message
334 * @return #GNUNET_OK if @a message is well-formed
335 */
336static int
337check_preference_change (void *cls,
338 const struct ChangePreferenceMessage *message)
339{
340 uint16_t msize;
341 uint32_t nump;
342
343 msize = ntohs (message->header.size);
344 nump = ntohl (message->num_preferences);
345 if ( (msize !=
346 sizeof (struct ChangePreferenceMessage) +
347 nump * sizeof (struct PreferenceInformation)) ||
348 (UINT16_MAX / sizeof (struct PreferenceInformation) < nump) )
349 {
350 GNUNET_break (0);
351 return GNUNET_SYSERR;
352 }
353 return GNUNET_OK;
354}
355
356
357/**
358 * Handle 'change preference' messages from clients.
359 *
360 * @param cls client that sent the request
361 * @param message the request message
362 */
363static void
364handle_preference_change (void *cls,
365 const struct ChangePreferenceMessage *message)
366{
367 struct GNUNET_SERVICE_Client *client = cls;
368
369 GAS_handle_preference_change (client,
370 message);
371 GNUNET_SERVICE_client_continue (client);
372}
373
374
375/**
376 * A client connected to us. Setup the local client
377 * record.
378 *
379 * @param cls unused
380 * @param client handle of the client
381 * @param mq message queue to talk to @a client
382 * @return @a client
383 */
384static void *
385client_connect_cb (void *cls,
386 struct GNUNET_SERVICE_Client *client,
387 struct GNUNET_MQ_Handle *mq)
388{
389 return client;
101} 390}
102 391
103 392
@@ -107,10 +396,12 @@ handle_ats_start (void *cls,
107 * 396 *
108 * @param cls unused 397 * @param cls unused
109 * @param client handle of the client 398 * @param client handle of the client
399 * @param app_ctx
110 */ 400 */
111static void 401static void
112client_disconnect_handler (void *cls, 402client_disconnect_cb (void *cls,
113 struct GNUNET_SERVER_Client *client) 403 struct GNUNET_SERVICE_Client *client,
404 void *app_ctx)
114{ 405{
115 if (NULL == client) 406 if (NULL == client)
116 return; 407 return;
@@ -134,7 +425,6 @@ cleanup_task (void *cls)
134 GAS_addresses_done (); 425 GAS_addresses_done ();
135 GAS_plugin_done (); 426 GAS_plugin_done ();
136 GAS_normalization_stop (); 427 GAS_normalization_stop ();
137 GAS_scheduling_done ();
138 GAS_performance_done (); 428 GAS_performance_done ();
139 GAS_preference_done (); 429 GAS_preference_done ();
140 GAS_reservations_done (); 430 GAS_reservations_done ();
@@ -150,51 +440,21 @@ cleanup_task (void *cls)
150 * Process template requests. 440 * Process template requests.
151 * 441 *
152 * @param cls closure 442 * @param cls closure
153 * @param server the initialized server
154 * @param cfg configuration to use 443 * @param cfg configuration to use
444 * @param service the initialized service
155 */ 445 */
156static void 446static void
157run (void *cls, 447run (void *cls,
158 struct GNUNET_SERVER_Handle *server, 448 const struct GNUNET_CONFIGURATION_Handle *cfg,
159 const struct GNUNET_CONFIGURATION_Handle *cfg) 449 struct GNUNET_SERVICE_Handle *service)
160{ 450{
161 static const struct GNUNET_SERVER_MessageHandler handlers[] = { 451 GSA_stats = GNUNET_STATISTICS_create ("ats",
162 {&handle_ats_start, NULL, 452 cfg);
163 GNUNET_MESSAGE_TYPE_ATS_START, 453 GAS_reservations_init ();
164 sizeof (struct ClientStartMessage)},
165 {&GAS_handle_request_address, NULL,
166 GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS,
167 sizeof (struct RequestAddressMessage)},
168 {&GAS_handle_request_address_cancel, NULL,
169 GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL,
170 sizeof (struct RequestAddressMessage)},
171 {&GAS_handle_request_address_list, NULL,
172 GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST,
173 sizeof (struct AddressListRequestMessage)},
174 {&GAS_handle_address_add, NULL,
175 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD, 0},
176 {&GAS_handle_address_update, NULL,
177 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE,
178 sizeof (struct AddressUpdateMessage) },
179 {&GAS_handle_address_destroyed, NULL,
180 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED,
181 sizeof (struct AddressDestroyedMessage) },
182 {&GAS_handle_reservation_request, NULL,
183 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST,
184 sizeof (struct ReservationRequestMessage)},
185 {&GAS_handle_preference_change, NULL,
186 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
187 {&GAS_handle_feedback, NULL,
188 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK, 0},
189 {NULL, NULL, 0, 0}
190 };
191 GSA_server = server;
192 GSA_stats = GNUNET_STATISTICS_create ("ats", cfg);
193 GAS_reservations_init (server);
194 GAS_connectivity_init (); 454 GAS_connectivity_init ();
195 GAS_preference_init (); 455 GAS_preference_init ();
196 GAS_normalization_start (); 456 GAS_normalization_start ();
197 GAS_addresses_init (server); 457 GAS_addresses_init ();
198 if (GNUNET_OK != 458 if (GNUNET_OK !=
199 GAS_plugin_init (cfg)) 459 GAS_plugin_init (cfg))
200 { 460 {
@@ -206,37 +466,69 @@ run (void *cls,
206 GAS_preference_done (); 466 GAS_preference_done ();
207 if (NULL != GSA_stats) 467 if (NULL != GSA_stats)
208 { 468 {
209 GNUNET_STATISTICS_destroy (GSA_stats, GNUNET_NO); 469 GNUNET_STATISTICS_destroy (GSA_stats,
470 GNUNET_NO);
210 GSA_stats = NULL; 471 GSA_stats = NULL;
211 } 472 }
212 return; 473 return;
213 } 474 }
214 GAS_performance_init (server); 475 GAS_performance_init ();
215 GAS_scheduling_init (server);
216
217 GNUNET_SERVER_disconnect_notify (server,
218 &client_disconnect_handler,
219 NULL);
220 GNUNET_SERVER_add_handlers (server, handlers);
221 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, 476 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
222 NULL); 477 NULL);
223} 478}
224 479
225 480
226/** 481/**
227 * The main function for the ats service. 482 * Define "main" method using service macro.
228 *
229 * @param argc number of arguments from the command line
230 * @param argv command line arguments
231 * @return 0 ok, 1 on error
232 */ 483 */
233int 484GNUNET_SERVICE_MAIN
234main (int argc, char *const *argv) 485("ats",
235{ 486 GNUNET_SERVICE_OPTION_NONE,
236 return (GNUNET_OK == 487 &run,
237 GNUNET_SERVICE_run (argc, argv, "ats", 488 &client_connect_cb,
238 GNUNET_SERVICE_OPTION_NONE, 489 &client_disconnect_cb,
239 &run, NULL)) ? 0 : 1; 490 NULL,
240} 491 GNUNET_MQ_hd_fixed_size (ats_start,
492 GNUNET_MESSAGE_TYPE_ATS_START,
493 struct ClientStartMessage,
494 NULL),
495 GNUNET_MQ_hd_fixed_size (request_address,
496 GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS,
497 struct RequestAddressMessage,
498 NULL),
499 GNUNET_MQ_hd_fixed_size (request_address_cancel,
500 GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL,
501 struct RequestAddressMessage,
502 NULL),
503 GNUNET_MQ_hd_fixed_size (request_address_list,
504 GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST,
505 struct AddressListRequestMessage,
506 NULL),
507 GNUNET_MQ_hd_var_size (address_add,
508 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD,
509 struct AddressAddMessage,
510 NULL),
511 GNUNET_MQ_hd_fixed_size (address_update,
512 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE,
513 struct AddressUpdateMessage,
514 NULL),
515 GNUNET_MQ_hd_fixed_size (address_destroyed,
516 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED,
517 struct AddressDestroyedMessage,
518 NULL),
519 GNUNET_MQ_hd_fixed_size (reservation_request,
520 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST,
521 struct ReservationRequestMessage,
522 NULL),
523 GNUNET_MQ_hd_var_size (preference_change,
524 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE,
525 struct ChangePreferenceMessage,
526 NULL),
527 GNUNET_MQ_hd_var_size (feedback,
528 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK,
529 struct FeedbackPreferenceMessage,
530 NULL),
531 GNUNET_MQ_handler_end ());
532
241 533
242/* end of gnunet-service-ats.c */ 534/* end of gnunet-service-ats.c */