2 This file is part of GNUnet.
3 Copyright (C) 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * (with MAX_DATASTORE_QUEUE = 1 and RETRY_PROBABILITY_INV = 1):
39 * 2: 1700 kb/s, 1372 kb/s
40 * 8: 2117 kb/s, 1284 kb/s, 1112 kb/s
41 * 16: 3500 kb/s, 3200 kb/s, 3388 kb/s
42 * 32: 3441 kb/s, 3163 kb/s, 3277 kb/s
43 * 128: 1700 kb/s; 2010 kb/s, 3383 kb/s, 1156 kb/s
45 * Conclusion: 16 seems to be a pretty good value (stable
46 * and high performance, no excessive memory use).
48 #define MAX_QUEUE_PER_PEER 16
51 * Length of the P2P success tracker. Note that having a very long
52 * list can also hurt performance.
54 #define P2P_SUCCESS_LIST_SIZE 8
57 * Length of the CS-2-P success tracker. Note that
58 * having a very long list can also hurt performance.
60 #define CS2P_SUCCESS_LIST_SIZE 8
64 * Performance data kept for a peer.
66 struct GSF_PeerPerformanceData
70 * List of the last clients for which this peer successfully
73 struct GSF_LocalClient *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
76 * List of the last PIDs for which
77 * this peer successfully answered a query;
78 * We use 0 to indicate no successful reply.
80 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
83 * Average delay between sending the peer a request and
84 * getting a reply (only calculated over the requests for
85 * which we actually got a reply). Calculated
86 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
88 struct GNUNET_TIME_Relative avg_reply_delay;
91 * If we get content we already have from this peer, for how
92 * long do we block him? Adjusted based on the fraction of
93 * redundant data we receive, between 1s and 1h.
95 struct GNUNET_TIME_Relative migration_delay;
98 * Point in time until which this peer does not want us to migrate content
101 struct GNUNET_TIME_Absolute migration_blocked_until;
104 * Transmission times for the last MAX_QUEUE_PER_PEER
105 * requests for this peer. Used as a ring buffer, current
106 * offset is stored in 'last_request_times_off'. If the
107 * oldest entry is more recent than the 'avg_delay', we should
108 * not send any more requests right now.
110 struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
113 * How long does it typically take for us to transmit a message
114 * to this peer? (delay between the request being issued and
115 * the callback being invoked).
117 struct GNUNET_LOAD_Value *transmission_delay;
120 * Average priority of successful replies. Calculated
121 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
126 * The peer's identity.
131 * Respect rating for this peer
136 * Number of pending queries (replies are not counted)
138 unsigned int pending_queries;
141 * Number of pending replies (queries are not counted)
143 unsigned int pending_replies;
149 * Signature of function called on a connected peer.
152 * @param peer identity of the peer
153 * @param cp handle to the connected peer record
154 * @param perf peer performance data
157 (*GSF_ConnectedPeerIterator) (void *cls,
158 const struct GNUNET_PeerIdentity *peer,
159 struct GSF_ConnectedPeer *cp,
160 const struct GSF_PeerPerformanceData *ppd);
164 * Function called to get a message for transmission.
167 * @param buf_size number of bytes available in @a buf
168 * @param buf where to copy the message, NULL on error (peer disconnect)
169 * @return number of bytes copied to @a buf, can be 0 (without indicating an error)
172 (*GSF_GetMessageCallback) (void *cls,
178 * Signature of function called on a reservation success or failure.
181 * @param cp handle to the connected peer record
182 * @param success #GNUNET_YES on success, #GNUNET_NO on failure
185 (*GSF_PeerReserveCallback) (void *cls,
186 struct GSF_ConnectedPeer *cp,
191 * Function called after the creation of a connected peer record is complete.
194 * @param cp handle to the newly created connected peer record
197 (*GSF_ConnectedPeerCreationCallback) (void *cls,
198 struct GSF_ConnectedPeer *cp);
202 * Handle to cancel a transmission request.
204 struct GSF_PeerTransmitHandle;
208 * A peer connected to us. Setup the connected peer
211 * @param peer identity of peer that connected
212 * @param creation_cb callback function when the record is created.
213 * @param creation_cb_cls closure for @creation_cb
216 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
217 GSF_ConnectedPeerCreationCallback creation_cb,
218 void *creation_cb_cls);
222 * Get a handle for a connected peer.
224 * @param peer peer's identity
225 * @return NULL if this peer is not currently connected
227 struct GSF_ConnectedPeer *
228 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer);
232 * Update the latency information kept for the given peer.
234 * @param id peer record to update
235 * @param latency current latency value
238 GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
239 struct GNUNET_TIME_Relative latency);
243 * Transmit a message to the given peer as soon as possible.
244 * If the peer disconnects before the transmission can happen,
245 * the callback is invoked with a 'NULL' buffer.
247 * @param cp target peer
248 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO)
249 * @param priority how important is this request?
250 * @param timeout when does this request timeout (call gmc with error)
251 * @param size number of bytes we would like to send to the peer
252 * @param gmc function to call to get the message
253 * @param gmc_cls closure for gmc
254 * @return handle to cancel request
256 struct GSF_PeerTransmitHandle *
257 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
260 struct GNUNET_TIME_Relative timeout,
261 size_t size, GSF_GetMessageCallback gmc,
266 * Cancel an earlier request for transmission.
268 * @param pth request to cancel
271 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth);
275 * Report on receiving a reply; update the performance record of the given peer.
277 * @param cp responding peer (will be updated)
278 * @param request_time time at which the original query was transmitted
279 * @param request_priority priority of the original request
282 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
283 struct GNUNET_TIME_Absolute request_time,
284 uint32_t request_priority);
288 * Report on receiving a reply in response to an initiating client.
289 * Remember that this peer is good for this client.
291 * @param cp responding peer (will be updated)
292 * @param initiator_client local client on responsible for query
295 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
296 struct GSF_LocalClient *initiator_client);
300 * Report on receiving a reply in response to an initiating peer.
301 * Remember that this peer is good for this initiating peer.
303 * @param cp responding peer (will be updated)
304 * @param initiator_peer other peer responsible for query
307 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
308 const struct GSF_ConnectedPeer
313 * Handle P2P "MIGRATION_STOP" message.
315 * @param cls closure, always NULL
316 * @param other the other peer involved (sender or receiver, NULL
317 * for loopback messages where we are both sender and receiver)
318 * @param message the actual message
319 * @return #GNUNET_OK to keep the connection open,
320 * #GNUNET_SYSERR to close it (signal serious error)
323 GSF_handle_p2p_migration_stop_ (void *cls,
324 const struct GNUNET_PeerIdentity *other,
325 const struct GNUNET_MessageHeader *message);
329 * Handle P2P "QUERY" message. Only responsible for creating the
330 * request entry itself and setting up reply callback and cancellation
331 * on peer disconnect. Does NOT execute the actual request strategy
332 * (planning) or local database operations.
334 * @param other the other peer involved (sender or receiver, NULL
335 * for loopback messages where we are both sender and receiver)
336 * @param message the actual message
337 * @return pending request handle, NULL on error
339 struct GSF_PendingRequest *
340 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
341 const struct GNUNET_MessageHeader *message);
345 * Return the performance data record for the given peer
347 * @param cp peer to query
348 * @return performance data record for the peer
350 struct GSF_PeerPerformanceData *
351 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp);
355 * Ask a peer to stop migrating data to us until the given point
358 * @param cp peer to ask
359 * @param block_time until when to block
362 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
363 struct GNUNET_TIME_Absolute block_time);
367 * A peer disconnected from us. Tear down the connected peer
371 * @param peer identity of peer that connected
374 GSF_peer_disconnect_handler_ (void *cls,
375 const struct GNUNET_PeerIdentity *peer);
379 * Notification that a local client disconnected. Clean up all of our
380 * references to the given handle.
382 * @param lc handle to the local client (henceforth invalid)
385 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
389 * Notify core about a preference we have for the given peer
390 * (to allocate more resources towards it). The change will
391 * be communicated the next time we reserve bandwidth with
392 * core (not instantly).
394 * @param cp peer to reserve bandwidth from
395 * @param pref preference change
398 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
403 * Obtain the identity of a connected peer.
405 * @param cp peer to get identity of
406 * @param id identity to set (written to)
409 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
410 struct GNUNET_PeerIdentity *id);
414 * Obtain the identity of a connected peer.
416 * @param cp peer to get identity of
417 * @return reference to peer identity, valid until peer disconnects (!)
419 const struct GNUNET_PeerIdentity *
420 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp);
425 * Iterate over all connected peers.
427 * @param it function to call for each peer
428 * @param it_cls closure for it
431 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
435 * Initialize peer management subsystem.
438 GSF_connected_peer_init_ (void);
442 * Shutdown peer management subsystem.
445 GSF_connected_peer_done_ (void);
449 /* end of gnunet-service-fs_cp.h */