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 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.
17 * @file fs/gnunet-service-fs_cp.h
18 * @brief API to handle 'connected peers'
19 * @author Christian Grothoff
21 #ifndef GNUNET_SERVICE_FS_CP_H
22 #define GNUNET_SERVICE_FS_CP_H
25 #include "gnunet-service-fs.h"
29 * Maximum number of outgoing messages we queue per peer.
31 * Performance measurements for 2 peer setup for 50 MB file
32 * (using perf_gnunet_service_fs_p2p):
34 * 24: 2-3 MB/s # ~ 24 MB RAM
35 * 256: 8 MB/s # ~256 MB RAM
37 * Conclusion: 24 should suffice (reasonable
38 * performance, no excessive memory use).
40 #define MAX_QUEUE_PER_PEER 24
43 * Length of the P2P success tracker. Note that having a very long
44 * list can also hurt performance.
46 #define P2P_SUCCESS_LIST_SIZE 8
49 * Length of the CS-2-P success tracker. Note that
50 * having a very long list can also hurt performance.
52 #define CS2P_SUCCESS_LIST_SIZE 8
56 * Performance data kept for a peer.
58 struct GSF_PeerPerformanceData
62 * List of the last clients for which this peer successfully
65 struct GSF_LocalClient *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
68 * List of the last PIDs for which
69 * this peer successfully answered a query;
70 * We use 0 to indicate no successful reply.
72 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
75 * Average delay between sending the peer a request and
76 * getting a reply (only calculated over the requests for
77 * which we actually got a reply). Calculated
78 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
80 struct GNUNET_TIME_Relative avg_reply_delay;
83 * If we get content we already have from this peer, for how
84 * long do we block him? Adjusted based on the fraction of
85 * redundant data we receive, between 1s and 1h.
87 struct GNUNET_TIME_Relative migration_delay;
90 * Point in time until which this peer does not want us to migrate content
93 struct GNUNET_TIME_Absolute migration_blocked_until;
96 * Transmission times for the last MAX_QUEUE_PER_PEER
97 * requests for this peer. Used as a ring buffer, current
98 * offset is stored in 'last_request_times_off'. If the
99 * oldest entry is more recent than the 'avg_delay', we should
100 * not send any more requests right now.
102 struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
105 * How long does it typically take for us to transmit a message
106 * to this peer? (delay between the request being issued and
107 * the callback being invoked).
109 struct GNUNET_LOAD_Value *transmission_delay;
112 * Average priority of successful replies. Calculated
113 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
118 * The peer's identity (interned version).
123 * The peer's identity (pointer).
125 const struct GNUNET_PeerIdentity *peer;
128 * Respect rating for this peer
133 * Number of pending queries (replies are not counted)
135 unsigned int pending_queries;
138 * Number of pending replies (queries are not counted)
140 unsigned int pending_replies;
146 * Signature of function called on a connected peer.
149 * @param peer identity of the peer
150 * @param cp handle to the connected peer record
151 * @param perf peer performance data
154 (*GSF_ConnectedPeerIterator) (void *cls,
155 const struct GNUNET_PeerIdentity *peer,
156 struct GSF_ConnectedPeer *cp,
157 const struct GSF_PeerPerformanceData *ppd);
161 * Function called to get a message for transmission.
164 * @param buf_size number of bytes available in @a buf
165 * @param buf where to copy the message, NULL on error (peer disconnect)
166 * @return number of bytes copied to @a buf, can be 0 (without indicating an error)
169 (*GSF_GetMessageCallback) (void *cls,
175 * Signature of function called on a reservation success or failure.
178 * @param cp handle to the connected peer record
179 * @param success #GNUNET_YES on success, #GNUNET_NO on failure
182 (*GSF_PeerReserveCallback) (void *cls,
183 struct GSF_ConnectedPeer *cp,
188 * Handle to cancel a transmission request.
190 struct GSF_PeerTransmitHandle;
194 * A peer connected to us. Setup the connected peer
198 * @param peer identity of peer that connected
199 * @param mq queue for sending messages to @a peer
200 * @return internal handle for the peer
203 GSF_peer_connect_handler (void *cls,
204 const struct GNUNET_PeerIdentity *peer,
205 struct GNUNET_MQ_Handle *mq);
209 * Get a handle for a connected peer.
211 * @param peer peer's identity
212 * @return NULL if this peer is not currently connected
214 struct GSF_ConnectedPeer *
215 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer);
219 * Update the latency information kept for the given peer.
221 * @param id peer record to update
222 * @param latency current latency value
225 GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
226 struct GNUNET_TIME_Relative latency);
230 * Transmit a message to the given peer as soon as possible.
231 * If the peer disconnects before the transmission can happen,
232 * the callback is invoked with a 'NULL' buffer.
234 * @param cp target peer
235 * @param is_query is this a query (#GNUNET_YES) or content (#GNUNET_NO)
236 * @param priority how important is this request?
237 * @param env envelope of message to send
240 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
243 struct GNUNET_MQ_Envelope *env);
247 * Report on receiving a reply; update the performance record of the given peer.
249 * @param cp responding peer (will be updated)
250 * @param request_time time at which the original query was transmitted
251 * @param request_priority priority of the original request
254 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
255 struct GNUNET_TIME_Absolute request_time,
256 uint32_t request_priority);
260 * Report on receiving a reply in response to an initiating client.
261 * Remember that this peer is good for this client.
263 * @param cp responding peer (will be updated)
264 * @param initiator_client local client on responsible for query
267 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
268 struct GSF_LocalClient *initiator_client);
272 * Report on receiving a reply in response to an initiating peer.
273 * Remember that this peer is good for this initiating peer.
275 * @param cp responding peer (will be updated)
276 * @param initiator_peer other peer responsible for query
279 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
280 const struct GSF_ConnectedPeer
285 * Handle P2P #GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP message.
287 * @param cls closure, the `struct GSF_ConnectedPeer`
288 * @param msm the actual message
291 handle_p2p_migration_stop (void *cls,
292 const struct MigrationStopMessage *message);
296 * Handle P2P "QUERY" message.
298 * @param cls the `struct GSF_ConnectedPeer` of the other sender
299 * @param gm the actual message
302 handle_p2p_get (void *cls,
303 const struct GetMessage *gm);
307 * Return the performance data record for the given peer
309 * @param cp peer to query
310 * @return performance data record for the peer
312 struct GSF_PeerPerformanceData *
313 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp);
317 * Ask a peer to stop migrating data to us until the given point
320 * @param cp peer to ask
321 * @param block_time until when to block
324 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
325 struct GNUNET_TIME_Absolute block_time);
329 * A peer disconnected from us. Tear down the connected peer
333 * @param peer identity of peer that connected
334 * @param internal_cls our `struct GSF_ConnectedPeer` for @a peer
337 GSF_peer_disconnect_handler (void *cls,
338 const struct GNUNET_PeerIdentity *peer,
343 * Notification that a local client disconnected. Clean up all of our
344 * references to the given handle.
346 * @param lc handle to the local client (henceforth invalid)
349 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
353 * Notify core about a preference we have for the given peer
354 * (to allocate more resources towards it). The change will
355 * be communicated the next time we reserve bandwidth with
356 * core (not instantly).
358 * @param cp peer to reserve bandwidth from
359 * @param pref preference change
362 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
367 * Obtain the identity of a connected peer.
369 * @param cp peer to get identity of
370 * @param id identity to set (written to)
373 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
374 struct GNUNET_PeerIdentity *id);
378 * Obtain the identity of a connected peer.
380 * @param cp peer to get identity of
381 * @return reference to peer identity, valid until peer disconnects (!)
383 const struct GNUNET_PeerIdentity *
384 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp);
389 * Iterate over all connected peers.
391 * @param it function to call for each peer
392 * @param it_cls closure for it
395 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
399 * Initialize peer management subsystem.
402 GSF_connected_peer_init_ (void);
406 * Shutdown peer management subsystem.
409 GSF_connected_peer_done_ (void);
413 /* end of gnunet-service-fs_cp.h */