NetDbRequests.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #ifndef NETDB_REQUESTS_H__
  2. #define NETDB_REQUESTS_H__
  3. #include <memory>
  4. #include <set>
  5. #include <map>
  6. #include "Identity.h"
  7. #include "RouterInfo.h"
  8. namespace i2p
  9. {
  10. namespace data
  11. {
  12. class RequestedDestination
  13. {
  14. public:
  15. typedef std::function<void (std::shared_ptr<RouterInfo>)> RequestComplete;
  16. RequestedDestination (const IdentHash& destination, bool isExploratory = false):
  17. m_Destination (destination), m_IsExploratory (isExploratory), m_CreationTime (0) {};
  18. ~RequestedDestination () { if (m_RequestComplete) m_RequestComplete (nullptr); };
  19. const IdentHash& GetDestination () const { return m_Destination; };
  20. int GetNumExcludedPeers () const { return m_ExcludedPeers.size (); };
  21. const std::set<IdentHash>& GetExcludedPeers () { return m_ExcludedPeers; };
  22. void ClearExcludedPeers ();
  23. bool IsExploratory () const { return m_IsExploratory; };
  24. bool IsExcluded (const IdentHash& ident) const { return m_ExcludedPeers.count (ident); };
  25. uint64_t GetCreationTime () const { return m_CreationTime; };
  26. std::shared_ptr<I2NPMessage> CreateRequestMessage (std::shared_ptr<const RouterInfo>, std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel);
  27. std::shared_ptr<I2NPMessage> CreateRequestMessage (const IdentHash& floodfill);
  28. void SetRequestComplete (const RequestComplete& requestComplete) { m_RequestComplete = requestComplete; };
  29. bool IsRequestComplete () const { return m_RequestComplete != nullptr; };
  30. void Success (std::shared_ptr<RouterInfo> r);
  31. void Fail ();
  32. private:
  33. IdentHash m_Destination;
  34. bool m_IsExploratory;
  35. std::set<IdentHash> m_ExcludedPeers;
  36. uint64_t m_CreationTime;
  37. RequestComplete m_RequestComplete;
  38. };
  39. class NetDbRequests
  40. {
  41. public:
  42. void Start ();
  43. void Stop ();
  44. std::shared_ptr<RequestedDestination> CreateRequest (const IdentHash& destination, bool isExploratory, RequestedDestination::RequestComplete requestComplete = nullptr);
  45. void RequestComplete (const IdentHash& ident, std::shared_ptr<RouterInfo> r);
  46. std::shared_ptr<RequestedDestination> FindRequest (const IdentHash& ident) const;
  47. void ManageRequests ();
  48. private:
  49. mutable std::mutex m_RequestedDestinationsMutex;
  50. std::map<IdentHash, std::shared_ptr<RequestedDestination> > m_RequestedDestinations;
  51. };
  52. }
  53. }
  54. #endif