- refactor channel nack
[oweals/gnunet.git] / src / ats / gnunet-service-ats_performance.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file ats/gnunet-service-ats_performance.c
23  * @brief ats service, interaction with 'performance' API
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet-service-ats.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_performance.h"
31 #include "gnunet-service-ats_reservations.h"
32 #include "ats.h"
33
34 /**
35  * We keep clients that are interested in performance in a linked list.
36  */
37 struct PerformanceClient
38 {
39   /**
40    * Next in doubly-linked list.
41    */
42   struct PerformanceClient *next;
43
44   /**
45    * Previous in doubly-linked list.
46    */
47   struct PerformanceClient *prev;
48
49   /**
50    * Actual handle to the client.
51    */
52   struct GNUNET_SERVER_Client *client;
53
54   /**
55    * Options for the client.
56    */
57   enum StartFlag flag;
58
59 };
60
61
62 /**
63  * We keep clients that are interested in performance in a linked list.
64  */
65 struct AddressIteration
66 {
67   /**
68    * Actual handle to the client.
69    */
70   struct PerformanceClient *pc;
71
72   int all;
73
74   uint32_t id;
75
76   unsigned int msg_type;
77 };
78
79 /**
80  * Address handle
81  */
82 static struct GAS_Addresses_Handle *GSA_addresses;
83
84 /**
85  * Head of linked list of all clients to this service.
86  */
87 static struct PerformanceClient *pc_head;
88
89 /**
90  * Tail of linked list of all clients to this service.
91  */
92 static struct PerformanceClient *pc_tail;
93
94 /**
95  * Context for sending messages to performance clients.
96  */
97 static struct GNUNET_SERVER_NotificationContext *nc;
98
99
100 /**
101  * Find the performance client associated with the given handle.
102  *
103  * @param client server handle
104  * @return internal handle
105  */
106 static struct PerformanceClient *
107 find_client (struct GNUNET_SERVER_Client *client)
108 {
109   struct PerformanceClient *pc;
110
111   for (pc = pc_head; pc != NULL; pc = pc->next)
112     if (pc->client == client)
113       return pc;
114   return NULL;
115 }
116
117 /**
118  * Unregister a client (which may have been a performance client,
119  * but this is not assured).
120  *
121  * @param client handle of the (now dead) client
122  */
123 void
124 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
125 {
126   struct PerformanceClient *pc;
127
128   pc = find_client (client);
129   if (NULL == pc)
130     return;
131   GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
132   GAS_addresses_preference_client_disconnect (GSA_addresses, client);
133   GNUNET_free (pc);
134 }
135
136 /**
137  * Transmit the given performance information to all performance
138  * clients.
139  *
140  * @param pc performance client to send to
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 active is this address active
146  * @param atsi performance data for the address
147  * @param atsi_count number of performance records in 'ats'
148  * @param bandwidth_out assigned outbound bandwidth
149  * @param bandwidth_in assigned inbound bandwidth
150  */
151 void
152 GAS_performance_notify_client (struct PerformanceClient *pc,
153                                const struct GNUNET_PeerIdentity *peer,
154                                const char *plugin_name,
155                                const void *plugin_addr, size_t plugin_addr_len,
156                                int active,
157                                const struct GNUNET_ATS_Information *atsi,
158                                uint32_t atsi_count,
159                                struct GNUNET_BANDWIDTH_Value32NBO
160                                bandwidth_out,
161                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
162 {
163
164   struct PeerInformationMessage *msg;
165   size_t plugin_name_length = strlen (plugin_name) + 1;
166   size_t msize =
167       sizeof (struct PeerInformationMessage) +
168       atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
169       plugin_name_length;
170   char buf[msize] GNUNET_ALIGN;
171   struct GNUNET_ATS_Information *atsp;
172   char *addrp;
173
174   GNUNET_assert (NULL != pc);
175   if (NULL == find_client (pc->client))
176     return; /* Client disconnected */
177
178   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
179   GNUNET_assert (atsi_count <
180                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
181                  sizeof (struct GNUNET_ATS_Information));
182   msg = (struct PeerInformationMessage *) buf;
183   msg->header.size = htons (msize);
184   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
185   msg->id = htonl (0);
186   msg->ats_count = htonl (atsi_count);
187   msg->peer = *peer;
188   msg->address_length = htons (plugin_addr_len);
189   msg->address_active = ntohl (active);
190   msg->plugin_name_length = htons (plugin_name_length);
191   msg->bandwidth_out = bandwidth_out;
192   msg->bandwidth_in = bandwidth_in;
193   atsp = (struct GNUNET_ATS_Information *) &msg[1];
194   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
195   addrp = (char *) &atsp[atsi_count];
196   memcpy (addrp, plugin_addr, plugin_addr_len);
197   strcpy (&addrp[plugin_addr_len], plugin_name);
198   GNUNET_SERVER_notification_context_unicast (nc, pc->client, &msg->header,
199                                               GNUNET_YES);
200 }
201
202
203 /**
204  * Transmit the given performance information to all performance
205  * clients.
206  *
207  * @param peer peer for which this is an address suggestion
208  * @param plugin_name 0-termintated string specifying the transport plugin
209  * @param plugin_addr binary address for the plugin to use
210  * @param plugin_addr_len number of bytes in plugin_addr
211  * @param active is this address active
212  * @param atsi performance data for the address
213  * @param atsi_count number of performance records in 'ats'
214  * @param bandwidth_out assigned outbound bandwidth
215  * @param bandwidth_in assigned inbound bandwidth
216  */
217 void
218 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
219                                     const char *plugin_name,
220                                     const void *plugin_addr, size_t plugin_addr_len,
221                                     int active,
222                                 const struct GNUNET_ATS_Information *atsi,
223                                 uint32_t atsi_count,
224                                 struct GNUNET_BANDWIDTH_Value32NBO
225                                 bandwidth_out,
226                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
227 {
228   struct PerformanceClient *pc;
229
230   for (pc = pc_head; pc != NULL; pc = pc->next)
231     if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
232     {
233         GAS_performance_notify_client (pc,
234                                        peer,
235                                        plugin_name, plugin_addr, plugin_addr_len,
236                                        active,
237                                        atsi, atsi_count,
238                                        bandwidth_out, bandwidth_in);
239     }
240   GNUNET_STATISTICS_update (GSA_stats,
241                             "# performance updates given to clients", 1,
242                             GNUNET_NO);
243 }
244
245
246 static void
247 peerinfo_it (void *cls,
248              const struct GNUNET_PeerIdentity *id,
249              const char *plugin_name,
250              const void *plugin_addr, size_t plugin_addr_len,
251              const int active,
252              const struct GNUNET_ATS_Information *atsi,
253              uint32_t atsi_count,
254              struct GNUNET_BANDWIDTH_Value32NBO
255              bandwidth_out,
256              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
257 {
258   struct PerformanceClient *pc = cls;
259   GNUNET_assert (NULL != pc);
260   if (NULL == id)
261     return;
262
263   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264               "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
265               GNUNET_i2s (id),
266               plugin_name,
267               (unsigned int) ntohl (bandwidth_out.value__),
268               (unsigned int) ntohl (bandwidth_in.value__));
269   GAS_performance_notify_client(pc,
270                                 id,
271                                 plugin_name,
272                                 plugin_addr,
273                                 plugin_addr_len,
274                                 active,
275                                 atsi, atsi_count,
276                                 bandwidth_out, bandwidth_in);
277 }
278
279
280 /**
281  * Iterator for GAS_performance_add_client
282  *
283  * @param cls the client requesting information
284  * @param id result
285  */
286 static void
287 peer_it (void *cls,
288          const struct GNUNET_PeerIdentity *id)
289 {
290   struct PerformanceClient *pc = cls;
291   if (NULL != id)
292   {
293     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
294     GAS_addresses_get_peer_info (GSA_addresses, id, &peerinfo_it, pc);
295   }
296 }
297
298 /**
299  * Register a new performance client.
300  *
301  * @param client handle of the new client
302  * @param flag flag specifying the type of the client
303  */
304 void
305 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
306                             enum StartFlag flag)
307 {
308   struct PerformanceClient *pc;
309   GNUNET_break (NULL == find_client (client));
310
311   pc = GNUNET_new (struct PerformanceClient);
312   pc->client = client;
313   pc->flag = flag;
314
315   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding performance client %s PIC\n",
316       (flag == START_FLAG_PERFORMANCE_WITH_PIC) ? "with" : "without");
317
318   GNUNET_SERVER_notification_context_add (nc, client);
319   GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
320
321   /* Send information about clients */
322   GAS_addresses_iterate_peers (GSA_addresses, &peer_it, pc);
323 }
324
325
326 static void transmit_req_addr (struct AddressIteration *ai,
327     const struct GNUNET_PeerIdentity *id,
328     const char *plugin_name,
329     const void *plugin_addr, size_t plugin_addr_len,
330     const int active,
331     const struct GNUNET_ATS_Information *atsi,
332     uint32_t atsi_count,
333     struct GNUNET_BANDWIDTH_Value32NBO
334     bandwidth_out,
335     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
336
337 {
338   struct GNUNET_ATS_Information *atsp;
339   struct PeerInformationMessage *msg;
340   char *addrp;
341   size_t plugin_name_length;
342   size_t msize;
343
344   if (NULL != plugin_name)
345     plugin_name_length = strlen (plugin_name) + 1;
346   else
347     plugin_name_length = 0;
348   msize = sizeof (struct PeerInformationMessage) +
349           atsi_count * sizeof (struct GNUNET_ATS_Information) +
350           plugin_addr_len + plugin_name_length;
351   char buf[msize] GNUNET_ALIGN;
352
353   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
354   GNUNET_assert (atsi_count <
355                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
356                  sizeof (struct GNUNET_ATS_Information));
357   msg = (struct PeerInformationMessage *) buf;
358   msg->header.size = htons (msize);
359   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
360   msg->ats_count = htonl (atsi_count);
361   msg->id = htonl (ai->id);
362   if (NULL != id)
363     msg->peer = *id;
364   else
365     memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
366   msg->address_length = htons (plugin_addr_len);
367   msg->address_active = ntohl (active);
368   msg->plugin_name_length = htons (plugin_name_length);
369   msg->bandwidth_out = bandwidth_out;
370   msg->bandwidth_in = bandwidth_in;
371   atsp = (struct GNUNET_ATS_Information *) &msg[1];
372   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
373   addrp = (char *) &atsp[atsi_count];
374   if (NULL != plugin_addr)
375     memcpy (addrp, plugin_addr, plugin_addr_len);
376   if (NULL != plugin_name)
377     strcpy (&addrp[plugin_addr_len], plugin_name);
378   GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
379                                               GNUNET_NO);
380 }
381
382 static void
383 req_addr_peerinfo_it (void *cls,
384              const struct GNUNET_PeerIdentity *id,
385              const char *plugin_name,
386              const void *plugin_addr, size_t plugin_addr_len,
387              const int active,
388              const struct GNUNET_ATS_Information *atsi,
389              uint32_t atsi_count,
390              struct GNUNET_BANDWIDTH_Value32NBO
391              bandwidth_out,
392              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
393 {
394   struct AddressIteration *ai = cls;
395
396   GNUNET_assert (NULL != ai);
397   GNUNET_assert (NULL != ai->pc);
398   if (NULL == find_client (ai->pc->client))
399     return; /* Client disconnected */
400
401   if ((NULL == id) && (NULL == plugin_name) && (NULL == plugin_addr))
402   {
403       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404                   "Address iteration done\n");
405       return;
406   }
407   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408               "Callback for  %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
409               (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
410               GNUNET_i2s (id),
411               plugin_name,
412               (unsigned int) ntohl (bandwidth_out.value__),
413               (unsigned int) ntohl (bandwidth_in.value__));
414
415   /* Transmit result */
416   if ((GNUNET_YES == ai->all) || (GNUNET_YES == active))
417   {
418       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419                   "Sending result for  %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
420                   (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
421                   GNUNET_i2s (id),
422                   plugin_name,
423                   (unsigned int) ntohl (bandwidth_out.value__),
424                   (unsigned int) ntohl (bandwidth_in.value__));
425     transmit_req_addr (cls,
426                        id,
427                        plugin_name,
428                        plugin_addr, plugin_addr_len,
429                        active,
430                        atsi,
431                        atsi_count,
432                        bandwidth_out, bandwidth_in);
433   }
434 }
435
436
437 /**
438  * Iterator for GAS_handle_request_address_list
439  *
440  * @param cls the client requesting information
441  * @param id result
442  */
443 static void
444 req_addr_peer_it (void *cls,
445          const struct GNUNET_PeerIdentity *id)
446 {
447   struct AddressIteration *ai = cls;
448   if (NULL != id)
449   {
450     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
451     GAS_addresses_get_peer_info (GSA_addresses, id, &req_addr_peerinfo_it, ai);
452   }
453   else
454   {
455       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer iteration done\n");
456   }
457 }
458
459
460 /**
461  * Handle 'address list request' messages from clients.
462  *
463  * @param cls unused, NULL
464  * @param client client that sent the request
465  * @param message the request message
466  */
467 void
468 GAS_handle_request_address_list (void *cls, struct GNUNET_SERVER_Client *client,
469                                  const struct GNUNET_MessageHeader *message)
470 {
471   struct PerformanceClient *pc;
472   struct AddressIteration ai;
473   struct AddressListRequestMessage * alrm = (struct AddressListRequestMessage *) message;
474   struct GNUNET_PeerIdentity allzeros;
475   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
476
477   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
478               "ADDRESSLIST_REQUEST");
479
480   if (NULL == (pc = find_client(client)))
481   {
482       GNUNET_break (0);
483       return;
484   }
485
486   ai.all = ntohl (alrm->all);
487   ai.id = ntohl (alrm->id);
488   ai.pc = pc;
489
490   memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
491   bandwidth_zero.value__ = htonl (0);
492   if (0 == memcmp (&alrm->peer, &allzeros, sizeof (struct GNUNET_PeerIdentity)))
493   {
494       /* Return addresses for all peers */
495       GAS_addresses_iterate_peers (GSA_addresses, &req_addr_peer_it, &ai);
496       transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
497   }
498   else
499   {
500       /* Return addresses for a specific peer */
501       GAS_addresses_get_peer_info (GSA_addresses, &alrm->peer, &req_addr_peerinfo_it, &ai);
502       transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
503   }
504   GNUNET_SERVER_receive_done (client, GNUNET_OK);
505 }
506
507
508 void
509 GAS_handle_performance_update (struct GNUNET_PeerIdentity *peer,
510                                                                                                                          const char *plugin_name,
511                                                                                                                          const void *plugin_addr,
512                                                                                                                          size_t plugin_addr_len,
513                                                                                                                          const int active,
514                                                                                                                          struct GNUNET_ATS_Information *ats,
515                                                                                                                          uint32_t ats_count,
516                                                                                                                          struct GNUNET_BANDWIDTH_Value32NBO
517                                                                                                                          bandwidth_out,
518                                                                                                                          struct GNUNET_BANDWIDTH_Value32NBO
519                                                                                                                          bandwidth_in)
520 {
521 /* Notify here */
522         GAS_performance_notify_all_clients (peer,
523                                                                                                                                                         plugin_name,
524                                                                                                                                                         plugin_addr, plugin_addr_len,
525                                                                                                                                                         active,
526                                                                                                                                                         ats, ats_count,
527                                                                                                                                                         bandwidth_out, bandwidth_in);
528
529 #if 0
530         struct PerformanceClient *cur;
531         struct PerformanceMonitorClient *curm;
532         struct MonitorResponseMessage *mrm;
533         size_t msglen;
534
535         msglen = sizeof (struct MonitorResponseMessage) +
536                                          ats_count * sizeof (struct GNUNET_ATS_Information);
537         mrm = GNUNET_malloc (msglen);
538
539         mrm->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR_RESPONSE);
540         mrm->header.size = htons (msglen);
541         mrm->ats_count = htonl (ats_count);
542         mrm->peer = *peer;
543         memcpy (&mrm[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
544
545         for (cur = pc_head; NULL != cur; cur = cur->next)
546                 for (curm = cur->pm_head; NULL != curm; curm = curm->next)
547                 {
548                                 /* Notify client about update */
549                                 mrm->id = htonl (curm->id);
550                           GNUNET_SERVER_notification_context_unicast (nc,
551                                         cur->client,
552                                         (struct GNUNET_MessageHeader *) mrm,
553                                         GNUNET_YES);
554                 }
555         GNUNET_free (mrm);
556 #endif
557 }
558
559
560 /**
561  * Handle 'reservation request' messages from clients.
562  *
563  * @param cls unused, NULL
564  * @param client client that sent the request
565  * @param message the request message
566  */
567 void
568 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
569                                 const struct GNUNET_MessageHeader *message)
570 {
571   const struct ReservationRequestMessage *msg =
572       (const struct ReservationRequestMessage *) message;
573   struct ReservationResultMessage result;
574   int32_t amount;
575   struct GNUNET_TIME_Relative res_delay;
576
577   if (NULL == find_client (client))
578   {
579     /* missing start message! */
580     GNUNET_break (0);
581     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
582     return;
583   }
584   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
585               "RESERVATION_REQUEST");
586   amount = (int32_t) ntohl (msg->amount);
587   res_delay = GAS_reservations_reserve (&msg->peer, amount);
588   if (res_delay.rel_value_us > 0)
589     amount = 0;
590   result.header.size = htons (sizeof (struct ReservationResultMessage));
591   result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
592   result.amount = htonl (amount);
593   result.peer = msg->peer;
594   result.res_delay = GNUNET_TIME_relative_hton (res_delay);
595   GNUNET_STATISTICS_update (GSA_stats, "# reservation requests processed", 1,
596                             GNUNET_NO);
597   GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
598                                               GNUNET_NO);
599   GNUNET_SERVER_receive_done (client, GNUNET_OK);
600 }
601
602
603 /**
604  * Handle 'preference change' messages from clients.
605  *
606  * @param cls unused, NULL
607  * @param client client that sent the request
608  * @param message the request message
609  */
610 void
611 GAS_handle_preference_change (void *cls,
612                               struct GNUNET_SERVER_Client *client,
613                               const struct GNUNET_MessageHeader *message)
614 {
615   const struct ChangePreferenceMessage *msg;
616   const struct PreferenceInformation *pi;
617   uint16_t msize;
618   uint32_t nump;
619   uint32_t i;
620
621   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
622               "PREFERENCE_CHANGE");
623   msize = ntohs (message->size);
624   if (msize < sizeof (struct ChangePreferenceMessage))
625   {
626     GNUNET_break (0);
627     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
628     return;
629   }
630   msg = (const struct ChangePreferenceMessage *) message;
631   nump = ntohl (msg->num_preferences);
632   if (msize !=
633       sizeof (struct ChangePreferenceMessage) +
634       nump * sizeof (struct PreferenceInformation))
635   {
636     GNUNET_break (0);
637     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
638     return;
639   }
640   GNUNET_STATISTICS_update (GSA_stats, "# preference change requests processed",
641                             1, GNUNET_NO);
642   pi = (const struct PreferenceInformation *) &msg[1];
643   for (i = 0; i < nump; i++)
644     GAS_addresses_preference_change (GSA_addresses,
645                                      client,
646                                      &msg->peer,
647                                      (enum GNUNET_ATS_PreferenceKind)
648                                      ntohl (pi[i].preference_kind),
649                                      pi[i].preference_value);
650   GNUNET_SERVER_receive_done (client, GNUNET_OK);
651 }
652
653
654 /**
655  * Handle 'preference feedback' messages from clients.
656  *
657  * @param cls unused, NULL
658  * @param client client that sent the request
659  * @param message the request message
660  */
661 void
662 GAS_handle_preference_feedback (void *cls,
663                               struct GNUNET_SERVER_Client *client,
664                               const struct GNUNET_MessageHeader *message)
665 {
666   const struct FeedbackPreferenceMessage *msg;
667   const struct PreferenceInformation *pi;
668   uint16_t msize;
669   uint32_t nump;
670   uint32_t i;
671
672   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
673               "PREFERENCE_FEEDBACK");
674   msize = ntohs (message->size);
675   if (msize < sizeof (struct FeedbackPreferenceMessage))
676   {
677     GNUNET_break (0);
678     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
679     return;
680   }
681   msg = (const struct FeedbackPreferenceMessage *) message;
682   nump = ntohl (msg->num_feedback);
683   if (msize !=
684       sizeof (struct FeedbackPreferenceMessage) +
685       nump * sizeof (struct PreferenceInformation))
686   {
687     GNUNET_break (0);
688     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
689     return;
690   }
691   GNUNET_STATISTICS_update (GSA_stats, "# preference feedbacks requests processed",
692                             1, GNUNET_NO);
693   pi = (const struct PreferenceInformation *) &msg[1];
694   for (i = 0; i < nump; i++)
695     GAS_addresses_preference_feedback (GSA_addresses,
696                                      client,
697                                      &msg->peer,
698                                      GNUNET_TIME_relative_ntoh(msg->scope),
699                                      (enum GNUNET_ATS_PreferenceKind)
700                                      ntohl (pi[i].preference_kind),
701                                      pi[i].preference_value);
702   GNUNET_SERVER_receive_done (client, GNUNET_OK);
703 }
704
705
706
707 /**
708  * Initialize performance subsystem.
709  *
710  * @param server handle to our server
711  * @param addresses the address handle to use
712  */
713 void
714 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
715                       struct GAS_Addresses_Handle *addresses)
716 {
717   GSA_addresses = addresses;
718   nc = GNUNET_SERVER_notification_context_create (server, 128);
719 }
720
721
722 /**
723  * Shutdown performance subsystem.
724  */
725 void
726 GAS_performance_done ()
727 {
728   GNUNET_SERVER_notification_context_destroy (nc);
729   nc = NULL;
730 }
731
732 /* end of gnunet-service-ats_performance.c */