glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / fs / gnunet-service-fs_cp.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
4
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.
9
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.
14 */
15
16 /**
17  * @file fs/gnunet-service-fs_cp.h
18  * @brief API to handle 'connected peers'
19  * @author Christian Grothoff
20  */
21 #ifndef GNUNET_SERVICE_FS_CP_H
22 #define GNUNET_SERVICE_FS_CP_H
23
24 #include "fs.h"
25 #include "gnunet-service-fs.h"
26
27
28 /**
29  * Maximum number of outgoing messages we queue per peer.
30  *
31  * Performance measurements for 2 peer setup for 50 MB file
32  * (using perf_gnunet_service_fs_p2p):
33  *
34  *  24: 2-3 MB/s # ~ 24 MB RAM
35  * 256:   8 MB/s # ~256 MB RAM
36  *
37  * Conclusion: 24 should suffice (reasonable
38  * performance, no excessive memory use).
39  */
40 #define MAX_QUEUE_PER_PEER 24
41
42 /**
43  * Length of the P2P success tracker.  Note that having a very long
44  * list can also hurt performance.
45  */
46 #define P2P_SUCCESS_LIST_SIZE 8
47
48 /**
49  * Length of the CS-2-P success tracker.  Note that
50  * having a very long list can also hurt performance.
51  */
52 #define CS2P_SUCCESS_LIST_SIZE 8
53
54
55 /**
56  * Performance data kept for a peer.
57  */
58 struct GSF_PeerPerformanceData
59 {
60
61   /**
62    * List of the last clients for which this peer successfully
63    * answered a query.
64    */
65   struct GSF_LocalClient *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
66
67   /**
68    * List of the last PIDs for which
69    * this peer successfully answered a query;
70    * We use 0 to indicate no successful reply.
71    */
72   GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
73
74   /**
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
79    */
80   struct GNUNET_TIME_Relative avg_reply_delay;
81
82   /**
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.
86    */
87   struct GNUNET_TIME_Relative migration_delay;
88
89   /**
90    * Point in time until which this peer does not want us to migrate content
91    * to it.
92    */
93   struct GNUNET_TIME_Absolute migration_blocked_until;
94
95   /**
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.
101    */
102   struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
103
104   /**
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).
108    */
109   struct GNUNET_LOAD_Value *transmission_delay;
110
111   /**
112    * Average priority of successful replies.  Calculated
113    * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
114    */
115   double avg_priority;
116
117   /**
118    * The peer's identity (interned version).
119    */
120   GNUNET_PEER_Id pid;
121
122   /**
123    * The peer's identity (pointer).
124    */
125   const struct GNUNET_PeerIdentity *peer;
126   
127   /**
128    * Respect rating for this peer
129    */
130   uint32_t respect;
131
132   /**
133    * Number of pending queries (replies are not counted)
134    */
135   unsigned int pending_queries;
136
137   /**
138    * Number of pending replies (queries are not counted)
139    */
140   unsigned int pending_replies;
141
142 };
143
144
145 /**
146  * Signature of function called on a connected peer.
147  *
148  * @param cls closure
149  * @param peer identity of the peer
150  * @param cp handle to the connected peer record
151  * @param perf peer performance data
152  */
153 typedef void
154 (*GSF_ConnectedPeerIterator) (void *cls,
155                               const struct GNUNET_PeerIdentity *peer,
156                               struct GSF_ConnectedPeer *cp,
157                               const struct GSF_PeerPerformanceData *ppd);
158
159
160 /**
161  * Function called to get a message for transmission.
162  *
163  * @param cls closure
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)
167  */
168 typedef size_t
169 (*GSF_GetMessageCallback) (void *cls,
170                            size_t buf_size,
171                            void *buf);
172
173
174 /**
175  * Signature of function called on a reservation success or failure.
176  *
177  * @param cls closure
178  * @param cp handle to the connected peer record
179  * @param success #GNUNET_YES on success, #GNUNET_NO on failure
180  */
181 typedef void
182 (*GSF_PeerReserveCallback) (void *cls,
183                             struct GSF_ConnectedPeer *cp,
184                             int success);
185
186
187 /**
188  * Handle to cancel a transmission request.
189  */
190 struct GSF_PeerTransmitHandle;
191
192
193 /**
194  * A peer connected to us.  Setup the connected peer
195  * records.
196  *
197  * @param cls NULL
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
201  */
202 void *
203 GSF_peer_connect_handler (void *cls,
204                           const struct GNUNET_PeerIdentity *peer,
205                           struct GNUNET_MQ_Handle *mq);
206
207
208 /**
209  * Get a handle for a connected peer.
210  *
211  * @param peer peer's identity
212  * @return NULL if this peer is not currently connected
213  */
214 struct GSF_ConnectedPeer *
215 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer);
216
217
218 /**
219  * Update the latency information kept for the given peer.
220  *
221  * @param id peer record to update
222  * @param latency current latency value
223  */
224 void
225 GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
226                           struct GNUNET_TIME_Relative latency);
227
228
229 /**
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.
233  *
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
238  */
239 void
240 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
241                     int is_query,
242                     uint32_t priority,
243                     struct GNUNET_MQ_Envelope *env);
244
245
246 /**
247  * Report on receiving a reply; update the performance record of the given peer.
248  *
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
252  */
253 void
254 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
255                               struct GNUNET_TIME_Absolute request_time,
256                               uint32_t request_priority);
257
258
259 /**
260  * Report on receiving a reply in response to an initiating client.
261  * Remember that this peer is good for this client.
262  *
263  * @param cp responding peer (will be updated)
264  * @param initiator_client local client on responsible for query
265  */
266 void
267 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
268                                    struct GSF_LocalClient *initiator_client);
269
270
271 /**
272  * Report on receiving a reply in response to an initiating peer.
273  * Remember that this peer is good for this initiating peer.
274  *
275  * @param cp responding peer (will be updated)
276  * @param initiator_peer other peer responsible for query
277  */
278 void
279 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
280                                  const struct GSF_ConnectedPeer
281                                  *initiator_peer);
282
283
284 /**
285  * Handle P2P #GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP message.
286  *
287  * @param cls closure, the `struct GSF_ConnectedPeer`
288  * @param msm the actual message
289  */
290 void
291 handle_p2p_migration_stop (void *cls,
292                            const struct MigrationStopMessage *message);
293
294
295 /**
296  * Handle P2P "QUERY" message.
297  *
298  * @param cls the `struct GSF_ConnectedPeer` of the other sender
299  * @param gm the actual message
300  */
301 void
302 handle_p2p_get (void *cls,
303                 const struct GetMessage *gm);
304
305
306 /**
307  * Return the performance data record for the given peer
308  *
309  * @param cp peer to query
310  * @return performance data record for the peer
311  */
312 struct GSF_PeerPerformanceData *
313 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp);
314
315
316 /**
317  * Ask a peer to stop migrating data to us until the given point
318  * in time.
319  *
320  * @param cp peer to ask
321  * @param block_time until when to block
322  */
323 void
324 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
325                            struct GNUNET_TIME_Absolute block_time);
326
327
328 /**
329  * A peer disconnected from us.  Tear down the connected peer
330  * record.
331  *
332  * @param cls unused
333  * @param peer identity of peer that connected
334  * @param internal_cls our `struct GSF_ConnectedPeer` for @a peer
335  */
336 void
337 GSF_peer_disconnect_handler (void *cls,
338                              const struct GNUNET_PeerIdentity *peer,
339                              void *internal_cls);
340
341
342 /**
343  * Notification that a local client disconnected.  Clean up all of our
344  * references to the given handle.
345  *
346  * @param lc handle to the local client (henceforth invalid)
347  */
348 void
349 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
350
351
352 /**
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).
357  *
358  * @param cp peer to reserve bandwidth from
359  * @param pref preference change
360  */
361 void
362 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
363                                        uint64_t pref);
364
365
366 /**
367  * Obtain the identity of a connected peer.
368  *
369  * @param cp peer to get identity of
370  * @param id identity to set (written to)
371  */
372 void
373 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
374                                   struct GNUNET_PeerIdentity *id);
375
376
377 /**
378  * Obtain the identity of a connected peer.
379  *
380  * @param cp peer to get identity of
381  * @return reference to peer identity, valid until peer disconnects (!)
382  */
383 const struct GNUNET_PeerIdentity *
384 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp);
385
386
387
388 /**
389  * Iterate over all connected peers.
390  *
391  * @param it function to call for each peer
392  * @param it_cls closure for it
393  */
394 void
395 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
396
397
398 /**
399  * Initialize peer management subsystem.
400  */
401 void
402 GSF_connected_peer_init_ (void);
403
404
405 /**
406  * Shutdown peer management subsystem.
407  */
408 void
409 GSF_connected_peer_done_ (void);
410
411
412 #endif
413 /* end of gnunet-service-fs_cp.h */