notify performance monitors about destroyed addresses (for #3406)
[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 /**
119  * Unregister a client (which may have been a performance client,
120  * but this is not assured).
121  *
122  * @param client handle of the (now dead) client
123  */
124 void
125 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
126 {
127   struct PerformanceClient *pc;
128
129   pc = find_client (client);
130   if (NULL == pc)
131     return;
132   GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
133   GAS_addresses_preference_client_disconnect (GSA_addresses, client);
134   GNUNET_free (pc);
135 }
136
137
138 /**
139  * Transmit the given performance information to all performance
140  * clients.
141  *
142  * @param pc performance client to send to
143  * @param peer peer for which this is an address suggestion
144  * @param plugin_name 0-termintated string specifying the transport plugin
145  * @param plugin_addr binary address for the plugin to use
146  * @param plugin_addr_len number of bytes in plugin_addr
147  * @param active #GNUNET_YES if this address is actively used
148  *        to maintain a connection to a peer;
149  *        #GNUNET_NO if the address is not actively used;
150  *        #GNUNET_SYSERR if this address is no longer available for ATS
151  * @param atsi performance data for the address
152  * @param atsi_count number of performance records in @a atsi
153  * @param bandwidth_out assigned outbound bandwidth
154  * @param bandwidth_in assigned inbound bandwidth
155  */
156 void
157 GAS_performance_notify_client (struct PerformanceClient *pc,
158                                const struct GNUNET_PeerIdentity *peer,
159                                const char *plugin_name,
160                                const void *plugin_addr,
161                                size_t plugin_addr_len,
162                                int active,
163                                const struct GNUNET_ATS_Information *atsi,
164                                uint32_t atsi_count,
165                                struct GNUNET_BANDWIDTH_Value32NBO
166                                bandwidth_out,
167                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
168 {
169
170   struct PeerInformationMessage *msg;
171   size_t plugin_name_length = strlen (plugin_name) + 1;
172   size_t msize =
173       sizeof (struct PeerInformationMessage) +
174       atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
175       plugin_name_length;
176   char buf[msize] GNUNET_ALIGN;
177   struct GNUNET_ATS_Information *atsp;
178   char *addrp;
179
180   GNUNET_assert (NULL != pc);
181   if (NULL == find_client (pc->client))
182     return; /* Client disconnected */
183
184   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
185   GNUNET_assert (atsi_count <
186                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
187                  sizeof (struct GNUNET_ATS_Information));
188   msg = (struct PeerInformationMessage *) buf;
189   msg->header.size = htons (msize);
190   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
191   msg->id = htonl (0);
192   msg->ats_count = htonl (atsi_count);
193   msg->peer = *peer;
194   msg->address_length = htons (plugin_addr_len);
195   msg->address_active = ntohl ((uint32_t) active);
196   msg->plugin_name_length = htons (plugin_name_length);
197   msg->bandwidth_out = bandwidth_out;
198   msg->bandwidth_in = bandwidth_in;
199   atsp = (struct GNUNET_ATS_Information *) &msg[1];
200   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
201   addrp = (char *) &atsp[atsi_count];
202   memcpy (addrp, plugin_addr, plugin_addr_len);
203   strcpy (&addrp[plugin_addr_len], plugin_name);
204   GNUNET_SERVER_notification_context_unicast (nc,
205                                               pc->client,
206                                               &msg->header,
207                                               GNUNET_YES);
208 }
209
210
211 /**
212  * Transmit the given performance information to all performance
213  * clients.
214  *
215  * @param peer peer for which this is an address suggestion
216  * @param plugin_name 0-termintated string specifying the transport plugin
217  * @param plugin_addr binary address for the plugin to use
218  * @param plugin_addr_len number of bytes in @a plugin_addr
219  * @param active #GNUNET_YES if this address is actively used
220  *        to maintain a connection to a peer;
221  *        #GNUNET_NO if the address is not actively used;
222  *        #GNUNET_SYSERR if this address is no longer available for ATS
223  * @param atsi performance data for the address
224  * @param atsi_count number of performance records in @a atsi
225  * @param bandwidth_out assigned outbound bandwidth
226  * @param bandwidth_in assigned inbound bandwidth
227  */
228 void
229 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
230                                     const char *plugin_name,
231                                     const void *plugin_addr,
232                                     size_t plugin_addr_len,
233                                     int active,
234                                     const struct GNUNET_ATS_Information *atsi,
235                                     uint32_t atsi_count,
236                                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
237                                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
238 {
239   struct PerformanceClient *pc;
240
241   for (pc = pc_head; pc != NULL; pc = pc->next)
242     if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
243     {
244         GAS_performance_notify_client (pc,
245                                        peer,
246                                        plugin_name,
247                                        plugin_addr,
248                                        plugin_addr_len,
249                                        active,
250                                        atsi, atsi_count,
251                                        bandwidth_out, bandwidth_in);
252     }
253   GNUNET_STATISTICS_update (GSA_stats,
254                             "# performance updates given to clients", 1,
255                             GNUNET_NO);
256 }
257
258
259
260 /**
261  * Iterator for called from #GAS_addresses_get_peer_info()
262  *
263  * @param p_it_cls closure with the `struct PerformanceClient *`
264  * @param id the peer id
265  * @param plugin_name plugin name
266  * @param plugin_addr address
267  * @param plugin_addr_len length of @a plugin_addr
268  * @param active is address actively used
269  * @param atsi ats performance information
270  * @param atsi_count number of ats performance elements in @a atsi
271  * @param bandwidth_out current outbound bandwidth assigned to address
272  * @param bandwidth_in current inbound bandwidth assigned to address
273  */
274 static void
275 peerinfo_it (void *cls,
276              const struct GNUNET_PeerIdentity *id,
277              const char *plugin_name,
278              const void *plugin_addr,
279              size_t plugin_addr_len,
280              int active,
281              const struct GNUNET_ATS_Information *atsi,
282              uint32_t atsi_count,
283              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
284              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
285 {
286   struct PerformanceClient *pc = cls;
287
288   GNUNET_assert (NULL != pc);
289   if (NULL == id)
290     return;
291   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292               "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
293               GNUNET_i2s (id),
294               plugin_name,
295               (unsigned int) ntohl (bandwidth_out.value__),
296               (unsigned int) ntohl (bandwidth_in.value__));
297   GAS_performance_notify_client(pc,
298                                 id,
299                                 plugin_name,
300                                 plugin_addr,
301                                 plugin_addr_len,
302                                 active,
303                                 atsi, atsi_count,
304                                 bandwidth_out, bandwidth_in);
305 }
306
307
308 /**
309  * Iterator for #GAS_performance_add_client()
310  *
311  * @param cls the client requesting information
312  * @param id result
313  */
314 static void
315 peer_it (void *cls,
316          const struct GNUNET_PeerIdentity *id)
317 {
318   struct PerformanceClient *pc = cls;
319
320   if (NULL != id)
321   {
322     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323                 "Callback for peer `%s'\n",
324                 GNUNET_i2s (id));
325     GAS_addresses_get_peer_info (GSA_addresses, id,
326                                  &peerinfo_it, pc);
327   }
328 }
329
330
331 /**
332  * Register a new performance client.
333  *
334  * @param client handle of the new client
335  * @param flag flag specifying the type of the client
336  */
337 void
338 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
339                             enum StartFlag flag)
340 {
341   struct PerformanceClient *pc;
342   GNUNET_break (NULL == find_client (client));
343
344   pc = GNUNET_new (struct PerformanceClient);
345   pc->client = client;
346   pc->flag = flag;
347
348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349               "Adding performance client %s PIC\n",
350               (flag == START_FLAG_PERFORMANCE_WITH_PIC) ? "with" : "without");
351
352   GNUNET_SERVER_notification_context_add (nc, client);
353   GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
354
355   /* Send information about clients */
356   GAS_addresses_iterate_peers (GSA_addresses,
357                                &peer_it,
358                                pc);
359 }
360
361
362 /**
363  * FIXME.
364  */
365 static void
366 transmit_req_addr (struct AddressIteration *ai,
367                    const struct GNUNET_PeerIdentity *id,
368                    const char *plugin_name,
369                    const void *plugin_addr, size_t plugin_addr_len,
370                    int active,
371                    const struct GNUNET_ATS_Information *atsi,
372                    uint32_t atsi_count,
373                    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
374                    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
375
376 {
377   struct GNUNET_ATS_Information *atsp;
378   struct PeerInformationMessage *msg;
379   char *addrp;
380   size_t plugin_name_length;
381   size_t msize;
382
383   if (NULL != plugin_name)
384     plugin_name_length = strlen (plugin_name) + 1;
385   else
386     plugin_name_length = 0;
387   msize = sizeof (struct PeerInformationMessage) +
388           atsi_count * sizeof (struct GNUNET_ATS_Information) +
389           plugin_addr_len + plugin_name_length;
390   char buf[msize] GNUNET_ALIGN;
391
392   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
393   GNUNET_assert (atsi_count <
394                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
395                  sizeof (struct GNUNET_ATS_Information));
396   msg = (struct PeerInformationMessage *) buf;
397   msg->header.size = htons (msize);
398   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
399   msg->ats_count = htonl (atsi_count);
400   msg->id = htonl (ai->id);
401   if (NULL != id)
402     msg->peer = *id;
403   else
404     memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
405   msg->address_length = htons (plugin_addr_len);
406   msg->address_active = ntohl (active);
407   msg->plugin_name_length = htons (plugin_name_length);
408   msg->bandwidth_out = bandwidth_out;
409   msg->bandwidth_in = bandwidth_in;
410   atsp = (struct GNUNET_ATS_Information *) &msg[1];
411   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
412   addrp = (char *) &atsp[atsi_count];
413   if (NULL != plugin_addr)
414     memcpy (addrp, plugin_addr, plugin_addr_len);
415   if (NULL != plugin_name)
416     strcpy (&addrp[plugin_addr_len], plugin_name);
417   GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
418                                               GNUNET_NO);
419 }
420
421
422 /**
423  * Iterator for #GAS_addresses_get_peer_info()
424  *
425  * @param p_it_cls closure with our `struct AddressIteration *`
426  * @param id the peer id
427  * @param plugin_name plugin name
428  * @param plugin_addr address
429  * @param plugin_addr_len length of @a plugin_addr
430  * @param address_active is address actively used
431  * @param atsi ats performance information
432  * @param atsi_count number of ats performance elements in @a atsi
433  * @param bandwidth_out current outbound bandwidth assigned to address
434  * @param bandwidth_in current inbound bandwidth assigned to address
435  */
436 static void
437 req_addr_peerinfo_it (void *cls,
438                       const struct GNUNET_PeerIdentity *id,
439                       const char *plugin_name,
440                       const void *plugin_addr, size_t plugin_addr_len,
441                       int active,
442                       const struct GNUNET_ATS_Information *atsi,
443                       uint32_t atsi_count,
444                       struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
445                       struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
446 {
447   struct AddressIteration *ai = cls;
448
449   GNUNET_assert (NULL != ai);
450   GNUNET_assert (NULL != ai->pc);
451   if (NULL == find_client (ai->pc->client))
452     return; /* Client disconnected */
453
454   if ((NULL == id) && (NULL == plugin_name) && (NULL == plugin_addr))
455   {
456     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457                 "Address iteration done\n");
458     return;
459   }
460   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461               "Callback for  %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
462               (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
463               GNUNET_i2s (id),
464               plugin_name,
465               (unsigned int) ntohl (bandwidth_out.value__),
466               (unsigned int) ntohl (bandwidth_in.value__));
467
468   /* Transmit result */
469   if ((GNUNET_YES == ai->all) || (GNUNET_YES == active))
470   {
471       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472                   "Sending result for  %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
473                   (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
474                   GNUNET_i2s (id),
475                   plugin_name,
476                   (unsigned int) ntohl (bandwidth_out.value__),
477                   (unsigned int) ntohl (bandwidth_in.value__));
478     transmit_req_addr (cls,
479                        id,
480                        plugin_name,
481                        plugin_addr, plugin_addr_len,
482                        active,
483                        atsi,
484                        atsi_count,
485                        bandwidth_out, bandwidth_in);
486   }
487 }
488
489
490 /**
491  * Iterator for GAS_handle_request_address_list
492  *
493  * @param cls the client requesting information, a `struct AddressIteration *`
494  * @param id result
495  */
496 static void
497 req_addr_peer_it (void *cls,
498                   const struct GNUNET_PeerIdentity *id)
499 {
500   struct AddressIteration *ai = cls;
501
502   if (NULL == id)
503   {
504     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505                 "Peer iteration done\n");
506     return;
507   }
508   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509               "Callback for peer `%s'\n",
510               GNUNET_i2s (id));
511   GAS_addresses_get_peer_info (GSA_addresses, id,
512                                &req_addr_peerinfo_it, ai);
513 }
514
515
516 /**
517  * Handle 'address list request' messages from clients.
518  *
519  * @param cls unused, NULL
520  * @param client client that sent the request
521  * @param message the request message
522  */
523 void
524 GAS_handle_request_address_list (void *cls,
525                                  struct GNUNET_SERVER_Client *client,
526                                  const struct GNUNET_MessageHeader *message)
527 {
528   struct PerformanceClient *pc;
529   struct AddressIteration ai;
530   struct AddressListRequestMessage * alrm = (struct AddressListRequestMessage *) message;
531   struct GNUNET_PeerIdentity allzeros;
532   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
533
534   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535               "Received `%s' message\n",
536               "ADDRESSLIST_REQUEST");
537   if (NULL == (pc = find_client(client)))
538   {
539     GNUNET_break (0);
540     return;
541   }
542
543   ai.all = ntohl (alrm->all);
544   ai.id = ntohl (alrm->id);
545   ai.pc = pc;
546
547   memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
548   bandwidth_zero.value__ = htonl (0);
549   if (0 == memcmp (&alrm->peer,
550                    &allzeros,
551                    sizeof (struct GNUNET_PeerIdentity)))
552   {
553     /* Return addresses for all peers */
554     GAS_addresses_iterate_peers (GSA_addresses, &req_addr_peer_it, &ai);
555     transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0,
556                        bandwidth_zero, bandwidth_zero);
557   }
558   else
559   {
560     /* Return addresses for a specific peer */
561     GAS_addresses_get_peer_info (GSA_addresses,
562                                  &alrm->peer,
563                                  &req_addr_peerinfo_it, &ai);
564     transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0,
565                        bandwidth_zero, bandwidth_zero);
566   }
567   GNUNET_SERVER_receive_done (client, GNUNET_OK);
568 }
569
570
571 /**
572  * FIXME.
573  */
574 void
575 GAS_handle_performance_update (struct GNUNET_PeerIdentity *peer,
576                                const char *plugin_name,
577                                const void *plugin_addr, size_t plugin_addr_len,
578                                int active,
579                                struct GNUNET_ATS_Information *ats,
580                                uint32_t ats_count,
581                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
582                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
583 {
584   /* Notify here */
585   GAS_performance_notify_all_clients (peer,
586                                       plugin_name,
587                                       plugin_addr,
588                                       plugin_addr_len,
589                                       active,
590                                       ats, ats_count,
591                                       bandwidth_out,
592                                       bandwidth_in);
593
594 #if 0
595   struct PerformanceClient *cur;
596   struct PerformanceMonitorClient *curm;
597   struct MonitorResponseMessage *mrm;
598   size_t msglen;
599
600   msglen = sizeof (struct MonitorResponseMessage) +
601   ats_count * sizeof (struct GNUNET_ATS_Information);
602   mrm = GNUNET_malloc (msglen);
603
604   mrm->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR_RESPONSE);
605   mrm->header.size = htons (msglen);
606   mrm->ats_count = htonl (ats_count);
607   mrm->peer = *peer;
608   memcpy (&mrm[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
609
610   for (cur = pc_head; NULL != cur; cur = cur->next)
611   for (curm = cur->pm_head; NULL != curm; curm = curm->next)
612   {
613     /* Notify client about update */
614     mrm->id = htonl (curm->id);
615     GNUNET_SERVER_notification_context_unicast (nc,
616         cur->client,
617         (struct GNUNET_MessageHeader *) mrm,
618         GNUNET_YES);
619   }
620   GNUNET_free (mrm);
621 #endif
622 }
623
624
625 /**
626  * Handle 'reservation request' messages from clients.
627  *
628  * @param cls unused, NULL
629  * @param client client that sent the request
630  * @param message the request message
631  */
632 void
633 GAS_handle_reservation_request (void *cls,
634                                 struct GNUNET_SERVER_Client *client,
635                                 const struct GNUNET_MessageHeader *message)
636 {
637   const struct ReservationRequestMessage *msg =
638       (const struct ReservationRequestMessage *) message;
639   struct ReservationResultMessage result;
640   int32_t amount;
641   struct GNUNET_TIME_Relative res_delay;
642
643   if (NULL == find_client (client))
644   {
645     /* missing start message! */
646     GNUNET_break (0);
647     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
648     return;
649   }
650   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651               "Received `%s' message\n",
652               "RESERVATION_REQUEST");
653   amount = (int32_t) ntohl (msg->amount);
654   res_delay = GAS_reservations_reserve (&msg->peer, amount);
655   if (res_delay.rel_value_us > 0)
656     amount = 0;
657   result.header.size = htons (sizeof (struct ReservationResultMessage));
658   result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
659   result.amount = htonl (amount);
660   result.peer = msg->peer;
661   result.res_delay = GNUNET_TIME_relative_hton (res_delay);
662   GNUNET_STATISTICS_update (GSA_stats,
663                             "# reservation requests processed", 1,
664                             GNUNET_NO);
665   GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
666                                               GNUNET_NO);
667   GNUNET_SERVER_receive_done (client, GNUNET_OK);
668 }
669
670
671 /**
672  * Handle 'preference change' messages from clients.
673  *
674  * @param cls unused, NULL
675  * @param client client that sent the request
676  * @param message the request message
677  */
678 void
679 GAS_handle_preference_change (void *cls,
680                               struct GNUNET_SERVER_Client *client,
681                               const struct GNUNET_MessageHeader *message)
682 {
683   const struct ChangePreferenceMessage *msg;
684   const struct PreferenceInformation *pi;
685   uint16_t msize;
686   uint32_t nump;
687   uint32_t i;
688
689   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
690               "PREFERENCE_CHANGE");
691   msize = ntohs (message->size);
692   if (msize < sizeof (struct ChangePreferenceMessage))
693   {
694     GNUNET_break (0);
695     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
696     return;
697   }
698   msg = (const struct ChangePreferenceMessage *) message;
699   nump = ntohl (msg->num_preferences);
700   if (msize !=
701       sizeof (struct ChangePreferenceMessage) +
702       nump * sizeof (struct PreferenceInformation))
703   {
704     GNUNET_break (0);
705     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
706     return;
707   }
708   GNUNET_STATISTICS_update (GSA_stats,
709                             "# preference change requests processed",
710                             1, GNUNET_NO);
711   pi = (const struct PreferenceInformation *) &msg[1];
712   for (i = 0; i < nump; i++)
713     GAS_addresses_preference_change (GSA_addresses,
714                                      client,
715                                      &msg->peer,
716                                      (enum GNUNET_ATS_PreferenceKind)
717                                      ntohl (pi[i].preference_kind),
718                                      pi[i].preference_value);
719   GNUNET_SERVER_receive_done (client, GNUNET_OK);
720 }
721
722
723 /**
724  * Handle 'preference feedback' messages from clients.
725  *
726  * @param cls unused, NULL
727  * @param client client that sent the request
728  * @param message the request message
729  */
730 void
731 GAS_handle_preference_feedback (void *cls,
732                               struct GNUNET_SERVER_Client *client,
733                               const struct GNUNET_MessageHeader *message)
734 {
735   const struct FeedbackPreferenceMessage *msg;
736   const struct PreferenceInformation *pi;
737   uint16_t msize;
738   uint32_t nump;
739   uint32_t i;
740
741   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
742               "Received `%s' message\n",
743               "PREFERENCE_FEEDBACK");
744   msize = ntohs (message->size);
745   if (msize < sizeof (struct FeedbackPreferenceMessage))
746   {
747     GNUNET_break (0);
748     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
749     return;
750   }
751   msg = (const struct FeedbackPreferenceMessage *) message;
752   nump = ntohl (msg->num_feedback);
753   if (msize !=
754       sizeof (struct FeedbackPreferenceMessage) +
755       nump * sizeof (struct PreferenceInformation))
756   {
757     GNUNET_break (0);
758     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
759     return;
760   }
761   GNUNET_STATISTICS_update (GSA_stats,
762                             "# preference feedbacks requests processed",
763                             1, GNUNET_NO);
764   pi = (const struct PreferenceInformation *) &msg[1];
765   for (i = 0; i < nump; i++)
766     GAS_addresses_preference_feedback (GSA_addresses,
767                                      client,
768                                      &msg->peer,
769                                      GNUNET_TIME_relative_ntoh(msg->scope),
770                                      (enum GNUNET_ATS_PreferenceKind)
771                                      ntohl (pi[i].preference_kind),
772                                      pi[i].preference_value);
773   GNUNET_SERVER_receive_done (client, GNUNET_OK);
774 }
775
776
777
778 /**
779  * Initialize performance subsystem.
780  *
781  * @param server handle to our server
782  * @param addresses the address handle to use
783  */
784 void
785 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
786                       struct GAS_Addresses_Handle *addresses)
787 {
788   GSA_addresses = addresses;
789   nc = GNUNET_SERVER_notification_context_create (server, 128);
790 }
791
792
793 /**
794  * Shutdown performance subsystem.
795  */
796 void
797 GAS_performance_done ()
798 {
799   GNUNET_SERVER_notification_context_destroy (nc);
800   nc = NULL;
801 }
802
803 /* end of gnunet-service-ats_performance.c */