2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file ats/gnunet-service-ats_performance.c
20 * @brief ats service, interaction with 'performance' API
21 * @author Matthias Wachs
22 * @author Christian Grothoff
25 * - simplify functions by passing a `struct GNUNET_HELLO_Address`
28 #include "gnunet-service-ats.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_performance.h"
35 * Context for sending messages to performance clients without PIC.
37 static struct GNUNET_NotificationContext *nc_no_pic;
40 * Context for sending messages to performance clients with PIC.
42 static struct GNUNET_NotificationContext *nc_pic;
46 * Transmit the given performance information to all performance
49 * @param client 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 prop performance data for the address
59 * @param local_address_info information about the local flags for the address
60 * @param bandwidth_out assigned outbound bandwidth
61 * @param bandwidth_in assigned inbound bandwidth
64 notify_client (struct GNUNET_SERVICE_Client *client,
65 const struct GNUNET_PeerIdentity *peer,
66 const char *plugin_name,
67 const void *plugin_addr,
68 size_t plugin_addr_len,
70 const struct GNUNET_ATS_Properties *prop,
71 enum GNUNET_HELLO_AddressInfo local_address_info,
72 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
73 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
75 struct PeerInformationMessage *msg;
76 size_t plugin_name_length = strlen (plugin_name) + 1;
78 sizeof (struct PeerInformationMessage) +
81 char buf[msize] GNUNET_ALIGN;
85 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
86 GNUNET_assert (msize < GNUNET_MAX_MESSAGE_SIZE);
87 msg = (struct PeerInformationMessage *) buf;
88 msg->header.size = htons (msize);
89 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
92 msg->address_length = htons (plugin_addr_len);
93 msg->address_active = ntohl ((uint32_t) active);
94 msg->plugin_name_length = htons (plugin_name_length);
95 msg->bandwidth_out = bandwidth_out;
96 msg->bandwidth_in = bandwidth_in;
98 GNUNET_ATS_properties_hton (&msg->properties,
101 memset (&msg->properties,
103 sizeof (struct GNUNET_ATS_Properties));
104 msg->address_local_info = htonl (local_address_info);
105 addrp = (char *) &msg[1];
106 GNUNET_memcpy (addrp, plugin_addr, plugin_addr_len);
107 strcpy (&addrp[plugin_addr_len], plugin_name);
110 GNUNET_notification_context_broadcast (nc_pic,
116 struct GNUNET_MQ_Envelope *env;
118 env = GNUNET_MQ_msg_copy (&msg->header);
119 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
126 * Transmit the given performance information to all performance
129 * @param peer peer for which this is an address suggestion
130 * @param plugin_name 0-termintated string specifying the transport plugin
131 * @param plugin_addr binary address for the plugin to use
132 * @param plugin_addr_len number of bytes in @a plugin_addr
133 * @param active #GNUNET_YES if this address is actively used
134 * to maintain a connection to a peer;
135 * #GNUNET_NO if the address is not actively used;
136 * #GNUNET_SYSERR if this address is no longer available for ATS
137 * @param prop performance data for the address
138 * @param local_address_info information about the local flags for the address
139 * @param bandwidth_out assigned outbound bandwidth
140 * @param bandwidth_in assigned inbound bandwidth
143 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
144 const char *plugin_name,
145 const void *plugin_addr,
146 size_t plugin_addr_len,
148 const struct GNUNET_ATS_Properties *prop,
149 enum GNUNET_HELLO_AddressInfo local_address_info,
150 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
151 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
153 GNUNET_break ( (NULL == prop) ||
154 (GNUNET_ATS_NET_UNSPECIFIED != prop->scope) );
165 GNUNET_STATISTICS_update (GSA_stats,
166 "# performance updates given to clients",
173 * Iterator for called from #GAS_addresses_get_peer_info()
175 * @param cls closure with the `struct GNUNET_SERVICE_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 prop performance information
182 * @param local_address_info information about the local flags for the address
183 * @param bandwidth_out current outbound bandwidth assigned to address
184 * @param bandwidth_in current inbound bandwidth assigned to address
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,
193 const struct GNUNET_ATS_Properties *prop,
194 enum GNUNET_HELLO_AddressInfo local_address_info,
195 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
196 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
198 struct GNUNET_SERVICE_Client *client = cls;
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
206 (unsigned int) ntohl (bandwidth_out.value__),
207 (unsigned int) ntohl (bandwidth_in.value__));
208 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
209 notify_client (client,
223 * Register a new performance client.
225 * @param client handle of the new client
226 * @param flag flag specifying the type of the client
229 GAS_performance_add_client (struct GNUNET_SERVICE_Client *client,
232 struct GNUNET_MQ_Handle *mq;
234 mq = GNUNET_SERVICE_client_get_mq (client);
235 if (START_FLAG_PERFORMANCE_WITH_PIC == flag)
237 GNUNET_notification_context_add (nc_pic,
239 GAS_addresses_get_peer_info (NULL,
245 GNUNET_notification_context_add (nc_no_pic,
252 * Initialize performance subsystem.
254 * @param server handle to our server
257 GAS_performance_init ()
259 nc_no_pic = GNUNET_notification_context_create (32);
260 nc_pic = GNUNET_notification_context_create (32);
265 * Shutdown performance subsystem.
268 GAS_performance_done ()
270 GNUNET_notification_context_destroy (nc_no_pic);
272 GNUNET_notification_context_destroy (nc_pic);
276 /* end of gnunet-service-ats_performance.c */