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