gnunet-service-fs_cp.h 11 KB

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