fix #3904, also simplify and clean up gnunet-publish shutdown logic in general
[oweals/gnunet.git] / src / fs / gnunet-service-fs_cp.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @file fs/gnunet-service-fs_cp.h
23  * @brief API to handle 'connected peers'
24  * @author Christian Grothoff
25  */
26 #ifndef GNUNET_SERVICE_FS_CP_H
27 #define GNUNET_SERVICE_FS_CP_H
28
29 #include "fs.h"
30 #include "gnunet-service-fs.h"
31
32
33 /**
34  * Maximum number of outgoing messages we queue per peer.
35  *
36  * Performance measurements for 2 peer setup for 50 MB file
37  * (using perf_gnunet_service_fs_p2p):
38  *
39  *  24: 2-3 MB/s # ~ 24 MB RAM
40  * 256:   8 MB/s # ~256 MB RAM
41  *
42  * Conclusion: 24 should suffice (reasonable
43  * performance, no excessive memory use).
44  */
45 #define MAX_QUEUE_PER_PEER 24
46
47 /**
48  * Length of the P2P success tracker.  Note that having a very long
49  * list can also hurt performance.
50  */
51 #define P2P_SUCCESS_LIST_SIZE 8
52
53 /**
54  * Length of the CS-2-P success tracker.  Note that
55  * having a very long list can also hurt performance.
56  */
57 #define CS2P_SUCCESS_LIST_SIZE 8
58
59
60 /**
61  * Performance data kept for a peer.
62  */
63 struct GSF_PeerPerformanceData
64 {
65
66   /**
67    * List of the last clients for which this peer successfully
68    * answered a query.
69    */
70   struct GSF_LocalClient *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
71
72   /**
73    * List of the last PIDs for which
74    * this peer successfully answered a query;
75    * We use 0 to indicate no successful reply.
76    */
77   GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
78
79   /**
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
84    */
85   struct GNUNET_TIME_Relative avg_reply_delay;
86
87   /**
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.
91    */
92   struct GNUNET_TIME_Relative migration_delay;
93
94   /**
95    * Point in time until which this peer does not want us to migrate content
96    * to it.
97    */
98   struct GNUNET_TIME_Absolute migration_blocked_until;
99
100   /**
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.
106    */
107   struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
108
109   /**
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).
113    */
114   struct GNUNET_LOAD_Value *transmission_delay;
115
116   /**
117    * Average priority of successful replies.  Calculated
118    * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
119    */
120   double avg_priority;
121
122   /**
123    * The peer's identity.
124    */
125   GNUNET_PEER_Id pid;
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  * Function called after the creation of a connected peer record is complete.
189  *
190  * @param cls closure
191  * @param cp handle to the newly created connected peer record
192  */
193 typedef void
194 (*GSF_ConnectedPeerCreationCallback) (void *cls,
195                                       struct GSF_ConnectedPeer *cp);
196
197
198 /**
199  * Handle to cancel a transmission request.
200  */
201 struct GSF_PeerTransmitHandle;
202
203
204 /**
205  * A peer connected to us.  Setup the connected peer
206  * records.
207  *
208  * @param peer identity of peer that connected
209  * @param creation_cb callback function when the record is created.
210  * @param creation_cb_cls closure for @creation_cb
211  */
212 void
213 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
214                            GSF_ConnectedPeerCreationCallback creation_cb,
215                            void *creation_cb_cls);
216
217
218 /**
219  * Get a handle for a connected peer.
220  *
221  * @param peer peer's identity
222  * @return NULL if this peer is not currently connected
223  */
224 struct GSF_ConnectedPeer *
225 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer);
226
227
228 /**
229  * Update the latency information kept for the given peer.
230  *
231  * @param id peer record to update
232  * @param latency current latency value
233  */
234 void
235 GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
236                           struct GNUNET_TIME_Relative latency);
237
238
239 /**
240  * Transmit a message to the given peer as soon as possible.
241  * If the peer disconnects before the transmission can happen,
242  * the callback is invoked with a 'NULL' buffer.
243  *
244  * @param cp target peer
245  * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO)
246  * @param priority how important is this request?
247  * @param timeout when does this request timeout (call gmc with error)
248  * @param size number of bytes we would like to send to the peer
249  * @param gmc function to call to get the message
250  * @param gmc_cls closure for gmc
251  * @return handle to cancel request
252  */
253 struct GSF_PeerTransmitHandle *
254 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
255                     int is_query,
256                     uint32_t priority,
257                     struct GNUNET_TIME_Relative timeout,
258                     size_t size, GSF_GetMessageCallback gmc,
259                     void *gmc_cls);
260
261
262 /**
263  * Cancel an earlier request for transmission.
264  *
265  * @param pth request to cancel
266  */
267 void
268 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth);
269
270
271 /**
272  * Report on receiving a reply; update the performance record of the given peer.
273  *
274  * @param cp responding peer (will be updated)
275  * @param request_time time at which the original query was transmitted
276  * @param request_priority priority of the original request
277  */
278 void
279 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
280                               struct GNUNET_TIME_Absolute request_time,
281                               uint32_t request_priority);
282
283
284 /**
285  * Report on receiving a reply in response to an initiating client.
286  * Remember that this peer is good for this client.
287  *
288  * @param cp responding peer (will be updated)
289  * @param initiator_client local client on responsible for query
290  */
291 void
292 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
293                                    struct GSF_LocalClient *initiator_client);
294
295
296 /**
297  * Report on receiving a reply in response to an initiating peer.
298  * Remember that this peer is good for this initiating peer.
299  *
300  * @param cp responding peer (will be updated)
301  * @param initiator_peer other peer responsible for query
302  */
303 void
304 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
305                                  const struct GSF_ConnectedPeer
306                                  *initiator_peer);
307
308
309 /**
310  * Handle P2P "MIGRATION_STOP" message.
311  *
312  * @param cls closure, always NULL
313  * @param other the other peer involved (sender or receiver, NULL
314  *        for loopback messages where we are both sender and receiver)
315  * @param message the actual message
316  * @return #GNUNET_OK to keep the connection open,
317  *         #GNUNET_SYSERR to close it (signal serious error)
318  */
319 int
320 GSF_handle_p2p_migration_stop_ (void *cls,
321                                 const struct GNUNET_PeerIdentity *other,
322                                 const struct GNUNET_MessageHeader *message);
323
324
325 /**
326  * Handle P2P "QUERY" message.  Only responsible for creating the
327  * request entry itself and setting up reply callback and cancellation
328  * on peer disconnect.  Does NOT execute the actual request strategy
329  * (planning) or local database operations.
330  *
331  * @param other the other peer involved (sender or receiver, NULL
332  *        for loopback messages where we are both sender and receiver)
333  * @param message the actual message
334  * @return pending request handle, NULL on error
335  */
336 struct GSF_PendingRequest *
337 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
338                        const struct GNUNET_MessageHeader *message);
339
340
341 /**
342  * Return the performance data record for the given peer
343  *
344  * @param cp peer to query
345  * @return performance data record for the peer
346  */
347 struct GSF_PeerPerformanceData *
348 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp);
349
350
351 /**
352  * Ask a peer to stop migrating data to us until the given point
353  * in time.
354  *
355  * @param cp peer to ask
356  * @param block_time until when to block
357  */
358 void
359 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
360                            struct GNUNET_TIME_Absolute block_time);
361
362
363 /**
364  * A peer disconnected from us.  Tear down the connected peer
365  * record.
366  *
367  * @param cls unused
368  * @param peer identity of peer that connected
369  */
370 void
371 GSF_peer_disconnect_handler_ (void *cls,
372                               const struct GNUNET_PeerIdentity *peer);
373
374
375 /**
376  * Notification that a local client disconnected.  Clean up all of our
377  * references to the given handle.
378  *
379  * @param lc handle to the local client (henceforth invalid)
380  */
381 void
382 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
383
384
385 /**
386  * Notify core about a preference we have for the given peer
387  * (to allocate more resources towards it).  The change will
388  * be communicated the next time we reserve bandwidth with
389  * core (not instantly).
390  *
391  * @param cp peer to reserve bandwidth from
392  * @param pref preference change
393  */
394 void
395 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
396                                        uint64_t pref);
397
398
399 /**
400  * Obtain the identity of a connected peer.
401  *
402  * @param cp peer to get identity of
403  * @param id identity to set (written to)
404  */
405 void
406 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
407                                   struct GNUNET_PeerIdentity *id);
408
409
410 /**
411  * Obtain the identity of a connected peer.
412  *
413  * @param cp peer to get identity of
414  * @return reference to peer identity, valid until peer disconnects (!)
415  */
416 const struct GNUNET_PeerIdentity *
417 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp);
418
419
420
421 /**
422  * Iterate over all connected peers.
423  *
424  * @param it function to call for each peer
425  * @param it_cls closure for it
426  */
427 void
428 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
429
430
431 /**
432  * Initialize peer management subsystem.
433  */
434 void
435 GSF_connected_peer_init_ (void);
436
437
438 /**
439  * Shutdown peer management subsystem.
440  */
441 void
442 GSF_connected_peer_done_ (void);
443
444
445 #endif
446 /* end of gnunet-service-fs_cp.h */