diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 5aeac9ed..3589c0df 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -156,7 +156,7 @@ namespace util auto leaseSet = i2p::data::netdb.FindLeaseSet (destination); if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) { - i2p::data::netdb.RequestDestination (i2p::data::IdentHash (destination), true); + i2p::data::netdb.Subscribe(destination); std::this_thread::sleep_for (std::chrono::seconds(10)); // wait for 10 seconds leaseSet = i2p::data::netdb.FindLeaseSet (destination); if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) // still no LeaseSet @@ -170,13 +170,6 @@ namespace util return; } } - // we found LeaseSet - if (leaseSet->HasExpiredLeases ()) - { - // we should re-request LeaseSet - LogPrint ("LeaseSet re-requested"); - i2p::data::netdb.RequestDestination (i2p::data::IdentHash (destination), true); - } auto s = i2p::stream::CreateStream (leaseSet); if (s) { diff --git a/NetDb.cpp b/NetDb.cpp index f9da5263..0ebafb66 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -120,10 +120,13 @@ namespace data Explore (); uint64_t ts = i2p::util::GetSecondsSinceEpoch (); - if (ts - lastSave >= 60) // save routers every minute + if (ts - lastSave >= 60) // save routers and validate subscriptions every minute { if (lastSave) + { SaveUpdated (m_NetDbPath); + ValidateSubscriptions (); + } lastSave = ts; } if (ts - lastPublish >= 600) // publish every 10 minutes @@ -660,5 +663,33 @@ namespace data return r; } + void NetDb::Subscribe (const IdentHash& ident) + { + LeaseSet * leaseSet = FindLeaseSet (ident); + if (!leaseSet) + { + LogPrint ("LeaseSet requested"); + RequestDestination (ident, true); + } + m_Subscriptions.insert (ident); + } + + void NetDb::Unsubscribe (const IdentHash& ident) + { + m_Subscriptions.erase (ident); + } + + void NetDb::ValidateSubscriptions () + { + for (auto it : m_Subscriptions) + { + LeaseSet * leaseSet = FindLeaseSet (it); + if (!leaseSet || leaseSet->HasExpiredLeases ()) + { + LogPrint ("LeaseSet re-requested"); + RequestDestination (it, true); + } + } + } } } diff --git a/NetDb.h b/NetDb.h index 68c136bc..39f87df0 100644 --- a/NetDb.h +++ b/NetDb.h @@ -63,7 +63,9 @@ namespace data void AddLeaseSet (uint8_t * buf, int len); RouterInfo * FindRouter (const IdentHash& ident) const; LeaseSet * FindLeaseSet (const IdentHash& destination) const; - + void Subscribe (const IdentHash& ident); // keep LeaseSets upto date + void Unsubscribe (const IdentHash& ident); + void RequestDestination (const char * b32); // in base32 void RequestDestination (const IdentHash& destination, bool isLeaseSet = false); @@ -83,6 +85,7 @@ namespace data void Run (); // exploratory thread void Explore (); void Publish (); + void ValidateSubscriptions (); const RouterInfo * GetClosestFloodfill (const IdentHash& destination, const std::set& excluded) const; RequestedDestination * CreateRequestedDestination (const IdentHash& dest, @@ -95,6 +98,7 @@ namespace data std::map m_LeaseSets; std::map m_RouterInfos; std::map m_RequestedDestinations; + std::set m_Subscriptions; bool m_IsRunning; int m_ReseedRetries;