From ad9d7931f577394a5e61c0758013cbfdf23dbc5c Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 14 Jan 2015 16:11:09 -0500 Subject: [PATCH] RequestComplete for RouterInfo --- NetDb.cpp | 57 ++++++++++++++++++++++++++++++++++++------------------- NetDb.h | 17 ++++++++++++----- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/NetDb.cpp b/NetDb.cpp index 85589700..dcb72ac0 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -47,6 +47,24 @@ namespace data m_ExcludedPeers.clear (); } + void RequestedDestination::Success (std::shared_ptr r) + { + if (m_RequestComplete) + { + m_RequestComplete (r); + m_RequestComplete = nullptr; + } + } + + void RequestedDestination::Fail () + { + if (m_RequestComplete) + { + m_RequestComplete (nullptr); + m_RequestComplete = nullptr; + } + } + #ifndef _WIN32 const char NetDb::m_NetDbPath[] = "/netDb"; #else @@ -191,7 +209,6 @@ namespace data void NetDb::AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len) { - DeleteRequestedDestination (ident); auto r = FindRouter (ident); if (r) { @@ -203,23 +220,31 @@ namespace data else { LogPrint ("New RouterInfo added"); - auto newRouter = std::make_shared (buf, len); + auto r = std::make_shared (buf, len); { std::unique_lock l(m_RouterInfosMutex); - m_RouterInfos[newRouter->GetIdentHash ()] = newRouter; + m_RouterInfos[r->GetIdentHash ()] = r; } - if (newRouter->IsFloodfill ()) + if (r->IsFloodfill ()) { std::unique_lock l(m_FloodfillsMutex); - m_Floodfills.push_back (newRouter); + m_Floodfills.push_back (r); } } + // take care about requested destination + auto it = m_RequestedDestinations.find (ident); + if (it != m_RequestedDestinations.end ()) + { + it->second->Success (r); + std::unique_lock l(m_RequestedDestinationsMutex); + delete it->second; + m_RequestedDestinations.erase (it); + } } void NetDb::AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, i2p::tunnel::InboundTunnel * from) { - DeleteRequestedDestination (ident); if (!from) // unsolicited LS must be received directly { auto it = m_LeaseSets.find(ident); @@ -414,16 +439,19 @@ namespace data } } - void NetDb::RequestDestination (const IdentHash& destination) + void NetDb::RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete) { // request RouterInfo directly RequestedDestination * dest = CreateRequestedDestination (destination, false); + if (requestComplete) + dest->SetRequestComplete (requestComplete); auto floodfill = GetClosestFloodfill (destination, dest->GetExcludedPeers ()); if (floodfill) transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ())); else { LogPrint (eLogError, "No floodfills found"); + dest->Fail (); DeleteRequestedDestination (dest); } } @@ -521,6 +549,7 @@ namespace data if (deleteDest) { // no more requests for the destinationation. delete it + it->second->Fail (); delete it->second; m_RequestedDestinations.erase (it); } @@ -528,6 +557,7 @@ namespace data else { // no more requests for detination possible. delete it + it->second->Fail (); delete it->second; m_RequestedDestinations.erase (it); } @@ -720,19 +750,6 @@ namespace data else return it->second; } - - bool NetDb::DeleteRequestedDestination (const IdentHash& dest) - { - auto it = m_RequestedDestinations.find (dest); - if (it != m_RequestedDestinations.end ()) - { - std::unique_lock l(m_RequestedDestinationsMutex); - delete it->second; - m_RequestedDestinations.erase (it); - return true; - } - return false; - } void NetDb::DeleteRequestedDestination (RequestedDestination * dest) { diff --git a/NetDb.h b/NetDb.h index 24d0a499..c74ea113 100644 --- a/NetDb.h +++ b/NetDb.h @@ -21,12 +21,15 @@ namespace i2p namespace data { class RequestedDestination - { + { public: + typedef std::function)> RequestComplete; + RequestedDestination (const IdentHash& destination, bool isExploratory = false): m_Destination (destination), m_IsExploratory (isExploratory), m_CreationTime (0) {}; - + ~RequestedDestination () { if (m_RequestComplete) m_RequestComplete (nullptr); }; + const IdentHash& GetDestination () const { return m_Destination; }; int GetNumExcludedPeers () const { return m_ExcludedPeers.size (); }; const std::set& GetExcludedPeers () { return m_ExcludedPeers; }; @@ -36,13 +39,18 @@ namespace data uint64_t GetCreationTime () const { return m_CreationTime; }; I2NPMessage * CreateRequestMessage (std::shared_ptr, const i2p::tunnel::InboundTunnel * replyTunnel); I2NPMessage * CreateRequestMessage (const IdentHash& floodfill); - + + void SetRequestComplete (const RequestComplete& requestComplete) { m_RequestComplete = requestComplete; }; + void Success (std::shared_ptr r); + void Fail (); + private: IdentHash m_Destination; bool m_IsExploratory; std::set m_ExcludedPeers; uint64_t m_CreationTime; + RequestComplete m_RequestComplete; }; class NetDb @@ -61,7 +69,7 @@ namespace data std::shared_ptr FindRouter (const IdentHash& ident) const; LeaseSet * FindLeaseSet (const IdentHash& destination) const; - void RequestDestination (const IdentHash& destination); + void RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete = nullptr); void HandleDatabaseStoreMsg (I2NPMessage * msg); void HandleDatabaseSearchReplyMsg (I2NPMessage * msg); @@ -92,7 +100,6 @@ namespace data void ManageRequests (); RequestedDestination * CreateRequestedDestination (const IdentHash& dest, bool isExploratory = false); - bool DeleteRequestedDestination (const IdentHash& dest); // returns true if found void DeleteRequestedDestination (RequestedDestination * dest); template