c9485f23b80ea4052f9397715964452150c73329
[oweals/gnunet.git] / src / fs / gnunet-service-fs_cp.h
1 /*
2      This file is part of GNUnet.
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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  * Performance data kept for a peer.
35  */
36 struct GSF_PeerPerformanceData
37 {
38
39   /**
40    * Transport performance data.
41    */
42   struct GNUNET_TRANSPORT_ATS_Information *atsi;
43
44   /**
45    * List of the last clients for which this peer successfully
46    * answered a query.
47    */
48   struct GSF_LocalClient *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
49
50   /**
51    * List of the last PIDs for which
52    * this peer successfully answered a query;
53    * We use 0 to indicate no successful reply.
54    */
55   GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
56
57   /**
58    * Average delay between sending the peer a request and
59    * getting a reply (only calculated over the requests for
60    * which we actually got a reply).   Calculated
61    * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
62    */
63   struct GNUNET_TIME_Relative avg_reply_delay;
64
65   /**
66    * Point in time until which this peer does not want us to migrate content
67    * to it.
68    */
69   struct GNUNET_TIME_Absolute migration_blocked_until;
70
71   /**
72    * Transmission times for the last MAX_QUEUE_PER_PEER
73    * requests for this peer.  Used as a ring buffer, current
74    * offset is stored in 'last_request_times_off'.  If the
75    * oldest entry is more recent than the 'avg_delay', we should
76    * not send any more requests right now.
77    */
78   struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
79
80   /**
81    * How long does it typically take for us to transmit a message
82    * to this peer?  (delay between the request being issued and
83    * the callback being invoked).
84    */
85   struct GNUNET_LOAD_Value *transmission_delay;
86
87   /**
88    * Average priority of successful replies.  Calculated
89    * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
90    */
91   double avg_priority;
92
93   /**
94    * The peer's identity.
95    */
96   GNUNET_PEER_Id pid;
97
98   /**
99    * Trust rating for this peer
100    */
101   uint32_t trust;
102
103   /**
104    * Number of pending queries (replies are not counted)
105    */
106   unsigned int pending_queries;
107
108   /**
109    * Number of pending replies (queries are not counted)
110    */
111   unsigned int pending_replies;
112
113   /**
114    * How many of the last blocks from migration were duplicates?
115    */
116   unsigned int migration_duplication;
117 };
118
119
120 /**
121  * Signature of function called on a connected peer.
122  *
123  * @param cls closure
124  * @param peer identity of the peer
125  * @param cp handle to the connected peer record
126  * @param perf peer performance data
127  */
128 typedef void (*GSF_ConnectedPeerIterator) (void *cls,
129                                            const struct GNUNET_PeerIdentity *
130                                            peer, struct GSF_ConnectedPeer * cp,
131                                            const struct GSF_PeerPerformanceData
132                                            * ppd);
133
134
135 /**
136  * Function called to get a message for transmission.
137  *
138  * @param cls closure
139  * @param buf_size number of bytes available in buf
140  * @param buf where to copy the message, NULL on error (peer disconnect)
141  * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
142  */
143 typedef size_t (*GSF_GetMessageCallback) (void *cls,
144                                           size_t buf_size, void *buf);
145
146
147 /**
148  * Signature of function called on a reservation success or failure.
149  *
150  * @param cls closure
151  * @param cp handle to the connected peer record
152  * @param success GNUNET_YES on success, GNUNET_NO on failure
153  */
154 typedef void (*GSF_PeerReserveCallback) (void *cls,
155                                          struct GSF_ConnectedPeer * cp,
156                                          int success);
157
158
159 /**
160  * Handle to cancel a transmission request.
161  */
162 struct GSF_PeerTransmitHandle;
163
164
165 /**
166  * A peer connected to us.  Setup the connected peer
167  * records.
168  *
169  * @param peer identity of peer that connected
170  * @param atsi performance data for the connection
171  * @return handle to connected peer entry
172  */
173 struct GSF_ConnectedPeer *GSF_peer_connect_handler_ (const struct
174                                                      GNUNET_PeerIdentity *peer,
175                                                      const struct
176                                                      GNUNET_TRANSPORT_ATS_Information
177                                                      *atsi);
178
179
180 /**
181  * Get a handle for a connected peer.
182  *
183  * @param peer peer's identity
184  * @return NULL if this peer is not currently connected
185  */
186 struct GSF_ConnectedPeer *GSF_peer_get_ (const struct GNUNET_PeerIdentity
187                                          *peer);
188
189
190 /**
191  * Transmit a message to the given peer as soon as possible.
192  * If the peer disconnects before the transmission can happen,
193  * the callback is invoked with a 'NULL' buffer.
194  *
195  * @param cp target peer
196  * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO)
197  * @param priority how important is this request?
198  * @param timeout when does this request timeout (call gmc with error)
199  * @param size number of bytes we would like to send to the peer
200  * @param gmc function to call to get the message
201  * @param gmc_cls closure for gmc
202  * @return handle to cancel request
203  */
204 struct GSF_PeerTransmitHandle *GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
205                                                    int is_query,
206                                                    uint32_t priority,
207                                                    struct GNUNET_TIME_Relative
208                                                    timeout, size_t size,
209                                                    GSF_GetMessageCallback gmc,
210                                                    void *gmc_cls);
211
212
213 /**
214  * Cancel an earlier request for transmission.
215  *
216  * @param pth request to cancel
217  */
218 void GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth);
219
220
221 /**
222  * Report on receiving a reply; update the performance record of the given peer.
223  *
224  * @param cp responding peer (will be updated)
225  * @param request_time time at which the original query was transmitted
226  * @param request_priority priority of the original request
227  */
228 void
229 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
230                               struct GNUNET_TIME_Absolute request_time,
231                               uint32_t request_priority);
232
233
234 /**
235  * Report on receiving a reply in response to an initiating client.
236  * Remember that this peer is good for this client.
237  *
238  * @param cp responding peer (will be updated)
239  * @param initiator_client local client on responsible for query
240  */
241 void
242 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
243                                    struct GSF_LocalClient *initiator_client);
244
245
246 /**
247  * Report on receiving a reply in response to an initiating peer.
248  * Remember that this peer is good for this initiating peer.
249  *
250  * @param cp responding peer (will be updated)
251  * @param initiator_peer other peer responsible for query
252  */
253 void
254 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
255                                  const struct GSF_ConnectedPeer
256                                  *initiator_peer);
257
258
259 /**
260  * Method called whenever a given peer has a status change.
261  *
262  * @param cls closure
263  * @param peer peer identity this notification is about
264  * @param bandwidth_in available amount of inbound bandwidth
265  * @param bandwidth_out available amount of outbound bandwidth
266  * @param timeout absolute time when this peer will time out
267  *        unless we see some further activity from it
268  * @param atsi status information
269  */
270 void
271 GSF_peer_status_handler_ (void *cls,
272                           const struct GNUNET_PeerIdentity *peer,
273                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
274                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
275                           struct GNUNET_TIME_Absolute timeout,
276                           const struct GNUNET_TRANSPORT_ATS_Information *atsi);
277
278
279 /**
280  * Handle P2P "MIGRATION_STOP" message.
281  *
282  * @param cls closure, always NULL
283  * @param other the other peer involved (sender or receiver, NULL
284  *        for loopback messages where we are both sender and receiver)
285  * @param message the actual message
286  * @param atsi performance information
287  * @return GNUNET_OK to keep the connection open,
288  *         GNUNET_SYSERR to close it (signal serious error)
289  */
290 int
291 GSF_handle_p2p_migration_stop_ (void *cls,
292                                 const struct GNUNET_PeerIdentity *other,
293                                 const struct GNUNET_MessageHeader *message,
294                                 const struct GNUNET_TRANSPORT_ATS_Information
295                                 *atsi);
296
297
298 /**
299  * Handle P2P "QUERY" message.  Only responsible for creating the
300  * request entry itself and setting up reply callback and cancellation
301  * on peer disconnect.  Does NOT execute the actual request strategy
302  * (planning) or local database operations.
303  *
304  * @param other the other peer involved (sender or receiver, NULL
305  *        for loopback messages where we are both sender and receiver)
306  * @param message the actual message
307  * @return pending request handle, NULL on error
308  */
309 struct GSF_PendingRequest *GSF_handle_p2p_query_ (const struct
310                                                   GNUNET_PeerIdentity *other,
311                                                   const struct
312                                                   GNUNET_MessageHeader
313                                                   *message);
314
315
316 /**
317  * Return the performance data record for the given peer
318  * 
319  * @param cp peer to query
320  * @return performance data record for the peer
321  */
322 struct GSF_PeerPerformanceData *GSF_get_peer_performance_data_ (struct
323                                                                 GSF_ConnectedPeer
324                                                                 *cp);
325
326
327 /**
328  * Ask a peer to stop migrating data to us until the given point
329  * in time.
330  * 
331  * @param cp peer to ask
332  * @param block_time until when to block
333  */
334 void
335 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
336                            struct GNUNET_TIME_Relative block_time);
337
338
339 /**
340  * A peer disconnected from us.  Tear down the connected peer
341  * record.
342  *
343  * @param cls unused
344  * @param peer identity of peer that connected
345  */
346 void
347 GSF_peer_disconnect_handler_ (void *cls,
348                               const struct GNUNET_PeerIdentity *peer);
349
350
351 /**
352  * Notification that a local client disconnected.  Clean up all of our
353  * references to the given handle.
354  *
355  * @param lc handle to the local client (henceforth invalid)
356  */
357 void GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
358
359
360 /**
361  * Notify core about a preference we have for the given peer
362  * (to allocate more resources towards it).  The change will
363  * be communicated the next time we reserve bandwidth with
364  * core (not instantly).
365  *
366  * @param cp peer to reserve bandwidth from
367  * @param pref preference change
368  */
369 void
370 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
371                                        uint64_t pref);
372
373
374 /**
375  * Obtain the identity of a connected peer.
376  *
377  * @param cp peer to reserve bandwidth from
378  * @param id identity to set (written to)
379  */
380 void
381 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
382                                   struct GNUNET_PeerIdentity *id);
383
384
385 /**
386  * Iterate over all connected peers.
387  *
388  * @param it function to call for each peer
389  * @param it_cls closure for it
390  */
391 void GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
392
393
394 /**
395  * Initialize peer management subsystem.
396  */
397 void GSF_connected_peer_init_ (void);
398
399
400 /**
401  * Shutdown peer management subsystem.
402  */
403 void GSF_connected_peer_done_ (void);
404
405
406 #endif
407 /* end of gnunet-service-fs_cp.h */