Browse Source

RequestComplete for RouterInfo

pull/150/head
orignal 10 years ago
parent
commit
ad9d7931f5
  1. 57
      NetDb.cpp
  2. 17
      NetDb.h

57
NetDb.cpp

@ -47,6 +47,24 @@ namespace data @@ -47,6 +47,24 @@ namespace data
m_ExcludedPeers.clear ();
}
void RequestedDestination::Success (std::shared_ptr<RouterInfo> 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 @@ -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 @@ -203,23 +220,31 @@ namespace data
else
{
LogPrint ("New RouterInfo added");
auto newRouter = std::make_shared<RouterInfo> (buf, len);
auto r = std::make_shared<RouterInfo> (buf, len);
{
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
m_RouterInfos[newRouter->GetIdentHash ()] = newRouter;
m_RouterInfos[r->GetIdentHash ()] = r;
}
if (newRouter->IsFloodfill ())
if (r->IsFloodfill ())
{
std::unique_lock<std::mutex> 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<std::mutex> 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 @@ -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 @@ -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 @@ -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 @@ -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<std::mutex> l(m_RequestedDestinationsMutex);
delete it->second;
m_RequestedDestinations.erase (it);
return true;
}
return false;
}
void NetDb::DeleteRequestedDestination (RequestedDestination * dest)
{

17
NetDb.h

@ -21,12 +21,15 @@ namespace i2p @@ -21,12 +21,15 @@ namespace i2p
namespace data
{
class RequestedDestination
{
{
public:
typedef std::function<void (std::shared_ptr<RouterInfo>)> 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<IdentHash>& GetExcludedPeers () { return m_ExcludedPeers; };
@ -36,13 +39,18 @@ namespace data @@ -36,13 +39,18 @@ namespace data
uint64_t GetCreationTime () const { return m_CreationTime; };
I2NPMessage * CreateRequestMessage (std::shared_ptr<const RouterInfo>, const i2p::tunnel::InboundTunnel * replyTunnel);
I2NPMessage * CreateRequestMessage (const IdentHash& floodfill);
void SetRequestComplete (const RequestComplete& requestComplete) { m_RequestComplete = requestComplete; };
void Success (std::shared_ptr<RouterInfo> r);
void Fail ();
private:
IdentHash m_Destination;
bool m_IsExploratory;
std::set<IdentHash> m_ExcludedPeers;
uint64_t m_CreationTime;
RequestComplete m_RequestComplete;
};
class NetDb
@ -61,7 +69,7 @@ namespace data @@ -61,7 +69,7 @@ namespace data
std::shared_ptr<RouterInfo> 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 @@ -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<typename Filter>

Loading…
Cancel
Save