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 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.
16 * @file ats/gnunet-service-ats_performance.c
17 * @brief ats service, interaction with 'performance' API
18 * @author Matthias Wachs
19 * @author Christian Grothoff
22 * - simplify functions by passing a `struct GNUNET_HELLO_Address`
25 #include "gnunet-service-ats.h"
26 #include "gnunet-service-ats_addresses.h"
27 #include "gnunet-service-ats_performance.h"
32 * Context for sending messages to performance clients without PIC.
34 static struct GNUNET_NotificationContext *nc_no_pic;
37 * Context for sending messages to performance clients with PIC.
39 static struct GNUNET_NotificationContext *nc_pic;
43 * Transmit the given performance information to all performance
46 * @param client client to send to, NULL for all
47 * @param peer peer for which this is an address suggestion
48 * @param plugin_name 0-termintated string specifying the transport plugin
49 * @param plugin_addr binary address for the plugin to use
50 * @param plugin_addr_len number of bytes in plugin_addr
51 * @param active #GNUNET_YES if this address is actively used
52 * to maintain a connection to a peer;
53 * #GNUNET_NO if the address is not actively used;
54 * #GNUNET_SYSERR if this address is no longer available for ATS
55 * @param prop performance data for the address
56 * @param local_address_info information about the local flags for the address
57 * @param bandwidth_out assigned outbound bandwidth
58 * @param bandwidth_in assigned inbound bandwidth
61 notify_client (struct GNUNET_SERVICE_Client *client,
62 const struct GNUNET_PeerIdentity *peer,
63 const char *plugin_name,
64 const void *plugin_addr,
65 size_t plugin_addr_len,
67 const struct GNUNET_ATS_Properties *prop,
68 enum GNUNET_HELLO_AddressInfo local_address_info,
69 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
70 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
72 struct PeerInformationMessage *msg;
73 size_t plugin_name_length = strlen (plugin_name) + 1;
75 sizeof (struct PeerInformationMessage) +
78 char buf[msize] GNUNET_ALIGN;
82 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
83 GNUNET_assert (msize < GNUNET_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);
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;
95 GNUNET_ATS_properties_hton (&msg->properties,
98 memset (&msg->properties,
100 sizeof (struct GNUNET_ATS_Properties));
101 msg->address_local_info = htonl (local_address_info);
102 addrp = (char *) &msg[1];
103 GNUNET_memcpy (addrp, plugin_addr, plugin_addr_len);
104 strcpy (&addrp[plugin_addr_len], plugin_name);
107 GNUNET_notification_context_broadcast (nc_pic,
113 struct GNUNET_MQ_Envelope *env;
115 env = GNUNET_MQ_msg_copy (&msg->header);
116 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
123 * Transmit the given performance information to all performance
126 * @param peer peer for which this is an address suggestion
127 * @param plugin_name 0-termintated string specifying the transport plugin
128 * @param plugin_addr binary address for the plugin to use
129 * @param plugin_addr_len number of bytes in @a plugin_addr
130 * @param active #GNUNET_YES if this address is actively used
131 * to maintain a connection to a peer;
132 * #GNUNET_NO if the address is not actively used;
133 * #GNUNET_SYSERR if this address is no longer available for ATS
134 * @param prop performance data for the address
135 * @param local_address_info information about the local flags for the address
136 * @param bandwidth_out assigned outbound bandwidth
137 * @param bandwidth_in assigned inbound bandwidth
140 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
141 const char *plugin_name,
142 const void *plugin_addr,
143 size_t plugin_addr_len,
145 const struct GNUNET_ATS_Properties *prop,
146 enum GNUNET_HELLO_AddressInfo local_address_info,
147 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
148 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
150 GNUNET_break ( (NULL == prop) ||
151 (GNUNET_ATS_NET_UNSPECIFIED != prop->scope) );
162 GNUNET_STATISTICS_update (GSA_stats,
163 "# performance updates given to clients",
170 * Iterator for called from #GAS_addresses_get_peer_info()
172 * @param cls closure with the `struct GNUNET_SERVICE_Client *` to inform.
173 * @param id the peer id
174 * @param plugin_name plugin name
175 * @param plugin_addr address
176 * @param plugin_addr_len length of @a plugin_addr
177 * @param active is address actively used
178 * @param prop performance information
179 * @param local_address_info information about the local flags for the address
180 * @param bandwidth_out current outbound bandwidth assigned to address
181 * @param bandwidth_in current inbound bandwidth assigned to address
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,
190 const struct GNUNET_ATS_Properties *prop,
191 enum GNUNET_HELLO_AddressInfo local_address_info,
192 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
193 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
195 struct GNUNET_SERVICE_Client *client = cls;
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
203 (unsigned int) ntohl (bandwidth_out.value__),
204 (unsigned int) ntohl (bandwidth_in.value__));
205 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
206 notify_client (client,
220 * Register a new performance client.
222 * @param client handle of the new client
223 * @param flag flag specifying the type of the client
226 GAS_performance_add_client (struct GNUNET_SERVICE_Client *client,
229 struct GNUNET_MQ_Handle *mq;
231 mq = GNUNET_SERVICE_client_get_mq (client);
232 if (START_FLAG_PERFORMANCE_WITH_PIC == flag)
234 GNUNET_notification_context_add (nc_pic,
236 GAS_addresses_get_peer_info (NULL,
242 GNUNET_notification_context_add (nc_no_pic,
249 * Initialize performance subsystem.
251 * @param server handle to our server
254 GAS_performance_init ()
256 nc_no_pic = GNUNET_notification_context_create (32);
257 nc_pic = GNUNET_notification_context_create (32);
262 * Shutdown performance subsystem.
265 GAS_performance_done ()
267 GNUNET_notification_context_destroy (nc_no_pic);
269 GNUNET_notification_context_destroy (nc_pic);
273 /* end of gnunet-service-ats_performance.c */