8886199b539e6455ca21fed1c4b8bdde44584b1a
[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 void
191 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth);
192
193
194 /**
195  * Report on receiving a reply; update the performance record of the given peer.
196  *
197  * @param peer responding peer (will be updated)
198  * @param request_time time at which the original query was transmitted
199  * @param request_priority priority of the original request
200  * @param initiator_client local client on responsible for query (or NULL)
201  * @param initiator_peer other peer responsible for query (or NULL)
202  */
203 void
204 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *peer,
205                               GNUNET_TIME_Absolute request_time,
206                               uint32_t request_priority,
207                               const struct GSF_LocalClient *initiator_client,
208                               const struct GSF_ConnectedPeer *initiator_peer);
209
210
211 /**
212  * Method called whenever a given peer has a status change.
213  *
214  * @param cls closure
215  * @param peer peer identity this notification is about
216  * @param bandwidth_in available amount of inbound bandwidth
217  * @param bandwidth_out available amount of outbound bandwidth
218  * @param timeout absolute time when this peer will time out
219  *        unless we see some further activity from it
220  * @param atsi status information
221  */
222 void
223 GSF_peer_status_handler_ (void *cls,
224                           const struct GNUNET_PeerIdentity *peer,
225                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
226                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
227                           struct GNUNET_TIME_Absolute timeout,
228                           const struct GNUNET_TRANSPORT_ATS_Information *atsi);
229
230
231 /**
232  * A peer disconnected from us.  Tear down the connected peer
233  * record.
234  *
235  * @param cls unused
236  * @param peer identity of peer that connected
237  */
238 void
239 GSF_peer_disconnect_handler_ (void *cls,
240                               const struct GNUNET_PeerIdentity *peer);
241
242
243 /**
244  * Notification that a local client disconnected.  Clean up all of our
245  * references to the given handle.
246  *
247  * @param lc handle to the local client (henceforth invalid)
248  */
249 void
250 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
251
252
253 /**
254  * Iterate over all connected peers.
255  *
256  * @param it function to call for each peer
257  * @param it_cls closure for it
258  */
259 void
260 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
261                               void *it_cls);
262
263
264 // FIXME: should we allow queueing multiple reservation requests?
265 // FIXME: what about cancellation?
266 // FIXME: change docu on peer disconnect handling?
267 /**
268  * Try to reserve bandwidth (to receive data FROM the given peer).
269  * This function must only be called ONCE per connected peer at a
270  * time; it can be called again after the 'rc' callback was invoked.
271  * If the peer disconnects, the request is (silently!) ignored (and
272  * the requester is responsible to register for notification about the
273  * peer disconnect if any special action needs to be taken in this
274  * case).
275  *
276  * @param cp peer to reserve bandwidth from
277  * @param size number of bytes to reserve
278  * @param rc function to call upon reservation success
279  * @param rc_cls closure for rc
280  */
281 void
282 GSF_connected_peer_reserve_ (struct GSF_ConnectedPeer *cp,
283                              size_t size,
284                              GSF_PeerReserveCallback rc,
285                              void *rc_cls);
286
287
288 #endif
289 /* end of gnunet-service-fs_cp.h */