-cleaning up gnunet-service-ats_performance-*
[oweals/gnunet.git] / src / ats / gnunet-service-ats_performance.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011-2015 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  * @file ats/gnunet-service-ats_performance.c
22  * @brief ats service, interaction with 'performance' API
23  * @author Matthias Wachs
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet-service-ats.h"
28 #include "gnunet-service-ats_addresses.h"
29 #include "gnunet-service-ats_performance.h"
30 #include "gnunet-service-ats_reservations.h"
31 #include "ats.h"
32
33
34 /**
35  * Context for sending messages to performance clients without PIC.
36  */
37 static struct GNUNET_SERVER_NotificationContext *nc;
38
39 /**
40  * Context for sending messages to performance clients with PIC.
41  */
42 static struct GNUNET_SERVER_NotificationContext *nc_pic;
43
44
45 /**
46  * Transmit the given performance information to all performance
47  * clients.
48  *
49  * @param pc client to send to, NULL for all
50  * @param peer peer for which this is an address suggestion
51  * @param plugin_name 0-termintated string specifying the transport plugin
52  * @param plugin_addr binary address for the plugin to use
53  * @param plugin_addr_len number of bytes in plugin_addr
54  * @param active #GNUNET_YES if this address is actively used
55  *        to maintain a connection to a peer;
56  *        #GNUNET_NO if the address is not actively used;
57  *        #GNUNET_SYSERR if this address is no longer available for ATS
58  * @param atsi performance data for the address
59  * @param atsi_count number of performance records in @a atsi
60  * @param bandwidth_out assigned outbound bandwidth
61  * @param bandwidth_in assigned inbound bandwidth
62  */
63 void
64 GAS_performance_notify_client (struct GNUNET_SERVER_Client *client,
65                                const struct GNUNET_PeerIdentity *peer,
66                                const char *plugin_name,
67                                const void *plugin_addr,
68                                size_t plugin_addr_len,
69                                int active,
70                                const struct GNUNET_ATS_Information *atsi,
71                                uint32_t atsi_count,
72                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
73                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
74 {
75   struct PeerInformationMessage *msg;
76   size_t plugin_name_length = strlen (plugin_name) + 1;
77   size_t msize =
78       sizeof (struct PeerInformationMessage) +
79       atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
80       plugin_name_length;
81   char buf[msize] GNUNET_ALIGN;
82   struct GNUNET_ATS_Information *atsp;
83   char *addrp;
84
85   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
86   GNUNET_assert (atsi_count <
87                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
88                  sizeof (struct GNUNET_ATS_Information));
89   msg = (struct PeerInformationMessage *) buf;
90   msg->header.size = htons (msize);
91   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
92   msg->id = htonl (0);
93   msg->ats_count = htonl (atsi_count);
94   msg->peer = *peer;
95   msg->address_length = htons (plugin_addr_len);
96   msg->address_active = ntohl ((uint32_t) active);
97   msg->plugin_name_length = htons (plugin_name_length);
98   msg->bandwidth_out = bandwidth_out;
99   msg->bandwidth_in = bandwidth_in;
100   atsp = (struct GNUNET_ATS_Information *) &msg[1];
101   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
102   addrp = (char *) &atsp[atsi_count];
103   memcpy (addrp, plugin_addr, plugin_addr_len);
104   strcpy (&addrp[plugin_addr_len], plugin_name);
105   if (NULL == client)
106   {
107     GNUNET_SERVER_notification_context_broadcast (nc_pic,
108                                                   &msg->header,
109                                                   GNUNET_YES);
110   }
111   else
112   {
113     GNUNET_SERVER_notification_context_unicast (nc,
114                                                 client,
115                                                 &msg->header,
116                                                 GNUNET_YES);
117   }
118 }
119
120
121 /**
122  * Transmit the given performance information to all performance
123  * clients.
124  *
125  * @param peer peer for which this is an address suggestion
126  * @param plugin_name 0-termintated string specifying the transport plugin
127  * @param plugin_addr binary address for the plugin to use
128  * @param plugin_addr_len number of bytes in @a plugin_addr
129  * @param active #GNUNET_YES if this address is actively used
130  *        to maintain a connection to a peer;
131  *        #GNUNET_NO if the address is not actively used;
132  *        #GNUNET_SYSERR if this address is no longer available for ATS
133  * @param atsi performance data for the address
134  * @param atsi_count number of performance records in @a atsi
135  * @param bandwidth_out assigned outbound bandwidth
136  * @param bandwidth_in assigned inbound bandwidth
137  */
138 void
139 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
140                                     const char *plugin_name,
141                                     const void *plugin_addr,
142                                     size_t plugin_addr_len,
143                                     int active,
144                                     const struct GNUNET_ATS_Information *atsi,
145                                     uint32_t atsi_count,
146                                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
147                                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
148 {
149   GAS_performance_notify_client (NULL,
150                                  peer,
151                                  plugin_name,
152                                  plugin_addr,
153                                  plugin_addr_len,
154                                  active,
155                                  atsi, atsi_count,
156                                  bandwidth_out,
157                                  bandwidth_in);
158   GNUNET_STATISTICS_update (GSA_stats,
159                             "# performance updates given to clients",
160                             1,
161                             GNUNET_NO);
162 }
163
164
165 /**
166  * Iterator for called from #GAS_addresses_get_peer_info()
167  *
168  * @param cls closure with the `struct GNUNET_SERVER_Client *` to inform.
169  * @param id the peer id
170  * @param plugin_name plugin name
171  * @param plugin_addr address
172  * @param plugin_addr_len length of @a plugin_addr
173  * @param active is address actively used
174  * @param atsi ats performance information
175  * @param atsi_count number of ats performance elements in @a atsi
176  * @param bandwidth_out current outbound bandwidth assigned to address
177  * @param bandwidth_in current inbound bandwidth assigned to address
178  */
179 static void
180 peerinfo_it (void *cls,
181              const struct GNUNET_PeerIdentity *id,
182              const char *plugin_name,
183              const void *plugin_addr,
184              size_t plugin_addr_len,
185              int active,
186              const struct GNUNET_ATS_Information *atsi,
187              uint32_t atsi_count,
188              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
189              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
190 {
191   struct GNUNET_SERVER_Client *client = cls;
192
193   if (NULL == id)
194     return;
195   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196               "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
197               GNUNET_i2s (id),
198               plugin_name,
199               (unsigned int) ntohl (bandwidth_out.value__),
200               (unsigned int) ntohl (bandwidth_in.value__));
201   GAS_performance_notify_client (client,
202                                  id,
203                                  plugin_name,
204                                  plugin_addr,
205                                  plugin_addr_len,
206                                  active,
207                                  atsi, atsi_count,
208                                  bandwidth_out,
209                                  bandwidth_in);
210 }
211
212
213 /**
214  * Register a new performance client.
215  *
216  * @param client handle of the new client
217  * @param flag flag specifying the type of the client
218  */
219 void
220 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
221                             enum StartFlag flag)
222 {
223   if (START_FLAG_PERFORMANCE_WITH_PIC == flag)
224   {
225     GNUNET_SERVER_notification_context_add (nc_pic,
226                                             client);
227     GNUNET_SERVER_notification_context_add (nc,
228                                             client);
229   }
230   else
231     GNUNET_SERVER_notification_context_add (nc,
232                                             client);
233   GAS_addresses_get_peer_info (NULL,
234                                &peerinfo_it,
235                                client);
236 }
237
238
239 /**
240  * Initialize performance subsystem.
241  *
242  * @param server handle to our server
243  */
244 void
245 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
246 {
247   nc = GNUNET_SERVER_notification_context_create (server, 32);
248   nc_pic = GNUNET_SERVER_notification_context_create (server, 32);
249 }
250
251
252 /**
253  * Shutdown performance subsystem.
254  */
255 void
256 GAS_performance_done ()
257 {
258   GNUNET_SERVER_notification_context_destroy (nc);
259   nc = NULL;
260   GNUNET_SERVER_notification_context_destroy (nc_pic);
261   nc_pic = NULL;
262 }
263
264 /* end of gnunet-service-ats_performance.c */