9bf36186c34ad4e10014a553ec93e9622009f9c9
[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    * Number of pending queries (replies are not counted)
94    */
95   unsigned int pending_queries;
96
97   /**
98    * Number of pending replies (queries are not counted)
99    */
100   unsigned int pending_replies;
101
102 };
103
104
105 /**
106  * Signature of function called on a connected peer.
107  *
108  * @param cls closure
109  * @param peer identity of the peer
110  * @param cp handle to the connected peer record
111  * @param perf peer performance data
112  */
113 typedef void (*GSF_ConnectedPeerIterator)(void *cls,
114                                           const struct GNUNET_PeerIdentity *peer,
115                                           struct GSF_ConnectedPeer *cp,
116                                           const struct GSF_PeerPerformanceData *ppd);
117
118
119 /**
120  * Function called to get a message for transmission.
121  *
122  * @param cls closure
123  * @param buf_size number of bytes available in buf
124  * @param buf where to copy the message, NULL on error (peer disconnect)
125  * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
126  */
127 typedef size_t (*GSF_GetMessageCallback)(void *cls,
128                                          size_t buf_size,
129                                          void *buf);
130
131
132 /**
133  * Signature of function called on a reservation success or failure.
134  *
135  * @param cls closure
136  * @param cp handle to the connected peer record
137  * @param success GNUNET_YES on success, GNUNET_NO on failure
138  */
139 typedef void (*GSF_PeerReserveCallback)(void *cls,
140                                         struct GSF_ConnectedPeer *cp,
141                                         int success);
142
143
144 /**
145  * Handle to cancel a transmission request.
146  */
147 struct GSF_PeerTransmitHandle;
148
149
150 /**
151  * A peer connected to us.  Setup the connected peer
152  * records.
153  *
154  * @param peer identity of peer that connected
155  * @param atsi performance data for the connection
156  * @return handle to connected peer entry
157  */
158 struct GSF_ConnectedPeer *
159 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
160                            const struct GNUNET_TRANSPORT_ATS_Information *atsi);
161
162
163 /**
164  * Transmit a message to the given peer as soon as possible.
165  * If the peer disconnects before the transmission can happen,
166  * the callback is invoked with a 'NULL' buffer.
167  *
168  * @param peer target peer
169  * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO)
170  * @param priority how important is this request?
171  * @param timeout when does this request timeout (call gmc with error)
172  * @param size number of bytes we would like to send to the peer
173  * @param gmc function to call to get the message
174  * @param gmc_cls closure for gmc
175  * @return handle to cancel request
176  */
177 struct GSF_PeerTransmitHandle *
178 GSF_peer_transmit_ (struct GSF_ConnectedPeer *peer,
179                     int is_query,
180                     uint32_t priority,
181                     struct GNUNET_TIME_Relative timeout,
182                     size_t size,
183                     GSF_GetMessageCallback gmc,
184                     void *gmc_cls);
185
186
187 /**
188  * Cancel an earlier request for transmission.
189  *
190  * @param pth request to cancel
191  */
192 void
193 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth);
194
195
196 /**
197  * Report on receiving a reply; update the performance record of the given peer.
198  *
199  * @param cp responding peer (will be updated)
200  * @param request_time time at which the original query was transmitted
201  * @param request_priority priority of the original request
202  * @param initiator_client local client on responsible for query (or NULL)
203  * @param initiator_peer other peer responsible for query (or NULL)
204  */
205 void
206 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
207                               struct GNUNET_TIME_Absolute request_time,
208                               uint32_t request_priority,
209                               const struct GSF_LocalClient *initiator_client,
210                               const struct GSF_ConnectedPeer *initiator_peer);
211
212
213 /**
214  * Method called whenever a given peer has a status change.
215  *
216  * @param cls closure
217  * @param peer peer identity this notification is about
218  * @param bandwidth_in available amount of inbound bandwidth
219  * @param bandwidth_out available amount of outbound bandwidth
220  * @param timeout absolute time when this peer will time out
221  *        unless we see some further activity from it
222  * @param atsi status information
223  */
224 void
225 GSF_peer_status_handler_ (void *cls,
226                           const struct GNUNET_PeerIdentity *peer,
227                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
228                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
229                           struct GNUNET_TIME_Absolute timeout,
230                           const struct GNUNET_TRANSPORT_ATS_Information *atsi);
231
232
233 /**
234  * Handle P2P "MIGRATION_STOP" message.
235  *
236  * @param cls closure, always NULL
237  * @param other the other peer involved (sender or receiver, NULL
238  *        for loopback messages where we are both sender and receiver)
239  * @param message the actual message
240  * @param atsi performance information
241  * @return GNUNET_OK to keep the connection open,
242  *         GNUNET_SYSERR to close it (signal serious error)
243  */
244 int
245 GSF_handle_p2p_migration_stop_ (void *cls,
246                                 const struct GNUNET_PeerIdentity *other,
247                                 const struct GNUNET_MessageHeader *message,
248                                 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
249
250
251 /**
252  * Handle P2P "QUERY" message.  Only responsible for creating the
253  * request entry itself and setting up reply callback and cancellation
254  * on peer disconnect.  Does NOT execute the actual request strategy
255  * (planning).
256  *
257  * @param other the other peer involved (sender or receiver, NULL
258  *        for loopback messages where we are both sender and receiver)
259  * @param message the actual message
260  * @return pending request handle, NULL on error
261  */
262 struct GSF_PendingRequest *
263 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
264                        const struct GNUNET_MessageHeader *message);
265
266
267 /**
268  * A peer disconnected from us.  Tear down the connected peer
269  * record.
270  *
271  * @param cls unused
272  * @param peer identity of peer that connected
273  */
274 void
275 GSF_peer_disconnect_handler_ (void *cls,
276                               const struct GNUNET_PeerIdentity *peer);
277
278
279 /**
280  * Notification that a local client disconnected.  Clean up all of our
281  * references to the given handle.
282  *
283  * @param lc handle to the local client (henceforth invalid)
284  */
285 void
286 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
287
288
289 /**
290  * Notify core about a preference we have for the given peer
291  * (to allocate more resources towards it).  The change will
292  * be communicated the next time we reserve bandwidth with
293  * core (not instantly).
294  *
295  * @param cp peer to reserve bandwidth from
296  * @param pref preference change
297  */
298 void
299 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
300                                        uint64_t pref);
301
302
303 /**
304  * Obtain the identity of a connected peer.
305  *
306  * @param cp peer to reserve bandwidth from
307  * @param id identity to set (written to)
308  */
309 void
310 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
311                                   struct GNUNET_PeerIdentity *id);
312
313
314 /**
315  * Iterate over all connected peers.
316  *
317  * @param it function to call for each peer
318  * @param it_cls closure for it
319  */
320 void
321 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
322                               void *it_cls);
323
324
325 /**
326  * Initialize peer management subsystem.
327  *
328  * @param cfg configuration to use
329  */
330 void
331 GSF_connected_peer_init_ (struct GNUNET_CONFIGURATION_Handle *cfg);
332
333
334 /**
335  * Shutdown peer management subsystem.
336  */
337 void
338 GSF_connected_peer_done_ (void);
339
340
341 #endif
342 /* end of gnunet-service-fs_cp.h */