2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file fs/gnunet-service-fs_cp.h
23 * @brief API to handle 'connected peers'
24 * @author Christian Grothoff
26 #ifndef GNUNET_SERVICE_FS_CP_H
27 #define GNUNET_SERVICE_FS_CP_H
30 #include "gnunet-service-fs.h"
34 * Maximum number of outgoing messages we queue per peer.
36 * Performance measurements for 2 peer setup for 50 MB file
37 * (using perf_gnunet_service_fs_p2p):
39 * 24: 2-3 MB/s # ~ 24 MB RAM
40 * 256: 8 MB/s # ~256 MB RAM
42 * Conclusion: 24 should suffice (reasonable
43 * performance, no excessive memory use).
45 #define MAX_QUEUE_PER_PEER 24
48 * Length of the P2P success tracker. Note that having a very long
49 * list can also hurt performance.
51 #define P2P_SUCCESS_LIST_SIZE 8
54 * Length of the CS-2-P success tracker. Note that
55 * having a very long list can also hurt performance.
57 #define CS2P_SUCCESS_LIST_SIZE 8
61 * Performance data kept for a peer.
63 struct GSF_PeerPerformanceData
67 * List of the last clients for which this peer successfully
70 struct GSF_LocalClient *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
73 * List of the last PIDs for which
74 * this peer successfully answered a query;
75 * We use 0 to indicate no successful reply.
77 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
80 * Average delay between sending the peer a request and
81 * getting a reply (only calculated over the requests for
82 * which we actually got a reply). Calculated
83 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
85 struct GNUNET_TIME_Relative avg_reply_delay;
88 * If we get content we already have from this peer, for how
89 * long do we block him? Adjusted based on the fraction of
90 * redundant data we receive, between 1s and 1h.
92 struct GNUNET_TIME_Relative migration_delay;
95 * Point in time until which this peer does not want us to migrate content
98 struct GNUNET_TIME_Absolute migration_blocked_until;
101 * Transmission times for the last MAX_QUEUE_PER_PEER
102 * requests for this peer. Used as a ring buffer, current
103 * offset is stored in 'last_request_times_off'. If the
104 * oldest entry is more recent than the 'avg_delay', we should
105 * not send any more requests right now.
107 struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
110 * How long does it typically take for us to transmit a message
111 * to this peer? (delay between the request being issued and
112 * the callback being invoked).
114 struct GNUNET_LOAD_Value *transmission_delay;
117 * Average priority of successful replies. Calculated
118 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
123 * The peer's identity (interned version).
128 * The peer's identity (pointer).
130 const struct GNUNET_PeerIdentity *peer;
133 * Respect rating for this peer
138 * Number of pending queries (replies are not counted)
140 unsigned int pending_queries;
143 * Number of pending replies (queries are not counted)
145 unsigned int pending_replies;
151 * Signature of function called on a connected peer.
154 * @param peer identity of the peer
155 * @param cp handle to the connected peer record
156 * @param perf peer performance data
159 (*GSF_ConnectedPeerIterator) (void *cls,
160 const struct GNUNET_PeerIdentity *peer,
161 struct GSF_ConnectedPeer *cp,
162 const struct GSF_PeerPerformanceData *ppd);
166 * Function called to get a message for transmission.
169 * @param buf_size number of bytes available in @a buf
170 * @param buf where to copy the message, NULL on error (peer disconnect)
171 * @return number of bytes copied to @a buf, can be 0 (without indicating an error)
174 (*GSF_GetMessageCallback) (void *cls,
180 * Signature of function called on a reservation success or failure.
183 * @param cp handle to the connected peer record
184 * @param success #GNUNET_YES on success, #GNUNET_NO on failure
187 (*GSF_PeerReserveCallback) (void *cls,
188 struct GSF_ConnectedPeer *cp,
193 * Handle to cancel a transmission request.
195 struct GSF_PeerTransmitHandle;
199 * A peer connected to us. Setup the connected peer
203 * @param peer identity of peer that connected
204 * @param mq queue for sending messages to @a peer
205 * @return internal handle for the peer
208 GSF_peer_connect_handler (void *cls,
209 const struct GNUNET_PeerIdentity *peer,
210 struct GNUNET_MQ_Handle *mq);
214 * Get a handle for a connected peer.
216 * @param peer peer's identity
217 * @return NULL if this peer is not currently connected
219 struct GSF_ConnectedPeer *
220 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer);
224 * Update the latency information kept for the given peer.
226 * @param id peer record to update
227 * @param latency current latency value
230 GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
231 struct GNUNET_TIME_Relative latency);
235 * Transmit a message to the given peer as soon as possible.
236 * If the peer disconnects before the transmission can happen,
237 * the callback is invoked with a 'NULL' buffer.
239 * @param cp target peer
240 * @param is_query is this a query (#GNUNET_YES) or content (#GNUNET_NO)
241 * @param priority how important is this request?
242 * @param env envelope of message to send
245 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
248 struct GNUNET_MQ_Envelope *env);
252 * Report on receiving a reply; update the performance record of the given peer.
254 * @param cp responding peer (will be updated)
255 * @param request_time time at which the original query was transmitted
256 * @param request_priority priority of the original request
259 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
260 struct GNUNET_TIME_Absolute request_time,
261 uint32_t request_priority);
265 * Report on receiving a reply in response to an initiating client.
266 * Remember that this peer is good for this client.
268 * @param cp responding peer (will be updated)
269 * @param initiator_client local client on responsible for query
272 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
273 struct GSF_LocalClient *initiator_client);
277 * Report on receiving a reply in response to an initiating peer.
278 * Remember that this peer is good for this initiating peer.
280 * @param cp responding peer (will be updated)
281 * @param initiator_peer other peer responsible for query
284 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
285 const struct GSF_ConnectedPeer
290 * Handle P2P #GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP message.
292 * @param cls closure, the `struct GSF_ConnectedPeer`
293 * @param msm the actual message
296 handle_p2p_migration_stop (void *cls,
297 const struct MigrationStopMessage *message);
301 * Handle P2P "QUERY" message.
303 * @param cls the `struct GSF_ConnectedPeer` of the other sender
304 * @param gm the actual message
307 handle_p2p_get (void *cls,
308 const struct GetMessage *gm);
312 * Return the performance data record for the given peer
314 * @param cp peer to query
315 * @return performance data record for the peer
317 struct GSF_PeerPerformanceData *
318 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp);
322 * Ask a peer to stop migrating data to us until the given point
325 * @param cp peer to ask
326 * @param block_time until when to block
329 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
330 struct GNUNET_TIME_Absolute block_time);
334 * A peer disconnected from us. Tear down the connected peer
338 * @param peer identity of peer that connected
339 * @param internal_cls our `struct GSF_ConnectedPeer` for @a peer
342 GSF_peer_disconnect_handler (void *cls,
343 const struct GNUNET_PeerIdentity *peer,
348 * Notification that a local client disconnected. Clean up all of our
349 * references to the given handle.
351 * @param lc handle to the local client (henceforth invalid)
354 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
358 * Notify core about a preference we have for the given peer
359 * (to allocate more resources towards it). The change will
360 * be communicated the next time we reserve bandwidth with
361 * core (not instantly).
363 * @param cp peer to reserve bandwidth from
364 * @param pref preference change
367 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
372 * Obtain the identity of a connected peer.
374 * @param cp peer to get identity of
375 * @param id identity to set (written to)
378 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
379 struct GNUNET_PeerIdentity *id);
383 * Obtain the identity of a connected peer.
385 * @param cp peer to get identity of
386 * @return reference to peer identity, valid until peer disconnects (!)
388 const struct GNUNET_PeerIdentity *
389 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp);
394 * Iterate over all connected peers.
396 * @param it function to call for each peer
397 * @param it_cls closure for it
400 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
404 * Initialize peer management subsystem.
407 GSF_connected_peer_init_ (void);
411 * Shutdown peer management subsystem.
414 GSF_connected_peer_done_ (void);
418 /* end of gnunet-service-fs_cp.h */