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