-fix config, shutdown issue
[oweals/gnunet.git] / src / ats / gnunet-service-ats_performance.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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_no_pic;
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   struct GNUNET_SERVER_NotificationContext *nc;
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     nc = *GNUNET_SERVER_client_get_user_context (client,
114                                                  struct GNUNET_SERVER_NotificationContext *);
115     if (NULL == nc)
116     {
117       GNUNET_break (0);
118       return;
119     }
120     GNUNET_SERVER_notification_context_unicast (nc,
121                                                 client,
122                                                 &msg->header,
123                                                 GNUNET_YES);
124   }
125 }
126
127
128 /**
129  * Transmit the given performance information to all performance
130  * clients.
131  *
132  * @param peer peer for which this is an address suggestion
133  * @param plugin_name 0-termintated string specifying the transport plugin
134  * @param plugin_addr binary address for the plugin to use
135  * @param plugin_addr_len number of bytes in @a plugin_addr
136  * @param active #GNUNET_YES if this address is actively used
137  *        to maintain a connection to a peer;
138  *        #GNUNET_NO if the address is not actively used;
139  *        #GNUNET_SYSERR if this address is no longer available for ATS
140  * @param atsi performance data for the address
141  * @param atsi_count number of performance records in @a atsi
142  * @param bandwidth_out assigned outbound bandwidth
143  * @param bandwidth_in assigned inbound bandwidth
144  */
145 void
146 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
147                                     const char *plugin_name,
148                                     const void *plugin_addr,
149                                     size_t plugin_addr_len,
150                                     int active,
151                                     const struct GNUNET_ATS_Information *atsi,
152                                     uint32_t atsi_count,
153                                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
154                                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
155 {
156   notify_client (NULL,
157                  peer,
158                  plugin_name,
159                  plugin_addr,
160                  plugin_addr_len,
161                  active,
162                  atsi, atsi_count,
163                  bandwidth_out,
164                  bandwidth_in);
165   GNUNET_STATISTICS_update (GSA_stats,
166                             "# performance updates given to clients",
167                             1,
168                             GNUNET_NO);
169 }
170
171
172 /**
173  * Iterator for called from #GAS_addresses_get_peer_info()
174  *
175  * @param cls closure with the `struct GNUNET_SERVER_Client *` to inform.
176  * @param id the peer id
177  * @param plugin_name plugin name
178  * @param plugin_addr address
179  * @param plugin_addr_len length of @a plugin_addr
180  * @param active is address actively used
181  * @param atsi ats performance information
182  * @param atsi_count number of ats performance elements in @a atsi
183  * @param bandwidth_out current outbound bandwidth assigned to address
184  * @param bandwidth_in current inbound bandwidth assigned to address
185  */
186 static void
187 peerinfo_it (void *cls,
188              const struct GNUNET_PeerIdentity *id,
189              const char *plugin_name,
190              const void *plugin_addr,
191              size_t plugin_addr_len,
192              int active,
193              const struct GNUNET_ATS_Information *atsi,
194              uint32_t atsi_count,
195              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
196              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
197 {
198   struct GNUNET_SERVER_Client *client = cls;
199
200   if (NULL == id)
201     return;
202   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203               "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
204               GNUNET_i2s (id),
205               plugin_name,
206               (unsigned int) ntohl (bandwidth_out.value__),
207               (unsigned int) ntohl (bandwidth_in.value__));
208   notify_client (client,
209                  id,
210                  plugin_name,
211                  plugin_addr,
212                  plugin_addr_len,
213                  active,
214                  atsi, atsi_count,
215                  bandwidth_out,
216                  bandwidth_in);
217 }
218
219
220 /**
221  * Register a new performance client.
222  *
223  * @param client handle of the new client
224  * @param flag flag specifying the type of the client
225  */
226 void
227 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
228                             enum StartFlag flag)
229 {
230   if (START_FLAG_PERFORMANCE_WITH_PIC == flag)
231   {
232     GNUNET_SERVER_notification_context_add (nc_pic,
233                                             client);
234     GNUNET_SERVER_client_set_user_context (client,
235                                            &nc_pic);
236     GAS_addresses_get_peer_info (NULL,
237                                  &peerinfo_it,
238                                  client);
239   }
240   else
241   {
242     GNUNET_SERVER_notification_context_add (nc_no_pic,
243                                             client);
244     GNUNET_SERVER_client_set_user_context (client,
245                                            &nc_no_pic);
246   }
247 }
248
249
250 /**
251  * Initialize performance subsystem.
252  *
253  * @param server handle to our server
254  */
255 void
256 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
257 {
258   nc_no_pic = GNUNET_SERVER_notification_context_create (server, 32);
259   nc_pic = GNUNET_SERVER_notification_context_create (server, 32);
260 }
261
262
263 /**
264  * Shutdown performance subsystem.
265  */
266 void
267 GAS_performance_done ()
268 {
269   GNUNET_SERVER_notification_context_destroy (nc_no_pic);
270   nc_no_pic = NULL;
271   GNUNET_SERVER_notification_context_destroy (nc_pic);
272   nc_pic = NULL;
273 }
274
275 /* end of gnunet-service-ats_performance.c */