- refactor to check messages from both enc systems
[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 prop performance data for the address
58  * @param bandwidth_out assigned outbound bandwidth
59  * @param bandwidth_in assigned inbound bandwidth
60  */
61 static void
62 notify_client (struct GNUNET_SERVER_Client *client,
63                const struct GNUNET_PeerIdentity *peer,
64                const char *plugin_name,
65                const void *plugin_addr,
66                size_t plugin_addr_len,
67                int active,
68                const struct GNUNET_ATS_Properties *prop,
69                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
70                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
71 {
72   struct PeerInformationMessage *msg;
73   size_t plugin_name_length = strlen (plugin_name) + 1;
74   size_t msize =
75     sizeof (struct PeerInformationMessage) +
76     plugin_addr_len +
77     plugin_name_length;
78   char buf[msize] GNUNET_ALIGN;
79   struct GNUNET_SERVER_NotificationContext **uc;
80   struct GNUNET_SERVER_NotificationContext *nc;
81   char *addrp;
82
83   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
84   msg = (struct PeerInformationMessage *) buf;
85   msg->header.size = htons (msize);
86   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
87   msg->id = htonl (0);
88   msg->peer = *peer;
89   msg->address_length = htons (plugin_addr_len);
90   msg->address_active = ntohl ((uint32_t) active);
91   msg->plugin_name_length = htons (plugin_name_length);
92   msg->bandwidth_out = bandwidth_out;
93   msg->bandwidth_in = bandwidth_in;
94   if (NULL != prop)
95     GNUNET_ATS_properties_hton (&msg->properties,
96                                 prop);
97   else
98     memset (&msg->properties,
99             0,
100             sizeof (struct GNUNET_ATS_Properties));
101   addrp = (char *) &msg[1];
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     uc = GNUNET_SERVER_client_get_user_context (client,
113                                                  struct GNUNET_SERVER_NotificationContext *);
114     if (NULL == uc)
115     {
116       GNUNET_break (0);
117       return;
118     }
119     nc = *uc;
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 prop performance data for the address
141  * @param bandwidth_out assigned outbound bandwidth
142  * @param bandwidth_in assigned inbound bandwidth
143  */
144 void
145 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
146                                     const char *plugin_name,
147                                     const void *plugin_addr,
148                                     size_t plugin_addr_len,
149                                     int active,
150                                     const struct GNUNET_ATS_Properties *prop,
151                                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
152                                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
153 {
154   notify_client (NULL,
155                  peer,
156                  plugin_name,
157                  plugin_addr,
158                  plugin_addr_len,
159                  active,
160                  prop,
161                  bandwidth_out,
162                  bandwidth_in);
163   GNUNET_STATISTICS_update (GSA_stats,
164                             "# performance updates given to clients",
165                             1,
166                             GNUNET_NO);
167 }
168
169
170 /**
171  * Iterator for called from #GAS_addresses_get_peer_info()
172  *
173  * @param cls closure with the `struct GNUNET_SERVER_Client *` to inform.
174  * @param id the peer id
175  * @param plugin_name plugin name
176  * @param plugin_addr address
177  * @param plugin_addr_len length of @a plugin_addr
178  * @param active is address actively used
179  * @param prop performance information
180  * @param bandwidth_out current outbound bandwidth assigned to address
181  * @param bandwidth_in current inbound bandwidth assigned to address
182  */
183 static void
184 peerinfo_it (void *cls,
185              const struct GNUNET_PeerIdentity *id,
186              const char *plugin_name,
187              const void *plugin_addr,
188              size_t plugin_addr_len,
189              int active,
190              const struct GNUNET_ATS_Properties *prop,
191              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
192              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
193 {
194   struct GNUNET_SERVER_Client *client = cls;
195
196   if (NULL == id)
197     return;
198   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199               "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
200               GNUNET_i2s (id),
201               plugin_name,
202               (unsigned int) ntohl (bandwidth_out.value__),
203               (unsigned int) ntohl (bandwidth_in.value__));
204   notify_client (client,
205                  id,
206                  plugin_name,
207                  plugin_addr,
208                  plugin_addr_len,
209                  active,
210                  prop,
211                  bandwidth_out,
212                  bandwidth_in);
213 }
214
215
216 /**
217  * Register a new performance client.
218  *
219  * @param client handle of the new client
220  * @param flag flag specifying the type of the client
221  */
222 void
223 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
224                             enum StartFlag flag)
225 {
226   if (START_FLAG_PERFORMANCE_WITH_PIC == flag)
227   {
228     GNUNET_SERVER_notification_context_add (nc_pic,
229                                             client);
230     GNUNET_SERVER_client_set_user_context (client,
231                                            &nc_pic);
232     GAS_addresses_get_peer_info (NULL,
233                                  &peerinfo_it,
234                                  client);
235   }
236   else
237   {
238     GNUNET_SERVER_notification_context_add (nc_no_pic,
239                                             client);
240     GNUNET_SERVER_client_set_user_context (client,
241                                            &nc_no_pic);
242   }
243 }
244
245
246 /**
247  * Initialize performance subsystem.
248  *
249  * @param server handle to our server
250  */
251 void
252 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
253 {
254   nc_no_pic = GNUNET_SERVER_notification_context_create (server, 32);
255   nc_pic = GNUNET_SERVER_notification_context_create (server, 32);
256 }
257
258
259 /**
260  * Shutdown performance subsystem.
261  */
262 void
263 GAS_performance_done ()
264 {
265   GNUNET_SERVER_notification_context_destroy (nc_no_pic);
266   nc_no_pic = NULL;
267   GNUNET_SERVER_notification_context_destroy (nc_pic);
268   nc_pic = NULL;
269 }
270
271 /* end of gnunet-service-ats_performance.c */