From 26c5f6cd776e9fd8ae4a71c57611be823db3e0c2 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 14 Mar 2014 21:22:59 -0400 Subject: [PATCH] 3-hops tunnels for tunnel pool --- Tunnel.cpp | 5 ++++- Tunnel.h | 1 - TunnelPool.cpp | 31 ++++++++++--------------------- TunnelPool.h | 2 +- 4 files changed, 15 insertions(+), 24 deletions(-) diff --git a/Tunnel.cpp b/Tunnel.cpp index d0a2734f..e9e59b86 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -436,6 +436,9 @@ namespace tunnel if (ts > it->second->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) { LogPrint ("Tunnel ", it->second->GetTunnelID (), " expired"); + auto pool = it->second->GetTunnelPool (); + if (pool) + pool->TunnelExpired (it->second); it = m_InboundTunnels.erase (it); } else @@ -496,7 +499,7 @@ namespace tunnel void Tunnels::ManageTunnelPools () { for (auto& it: m_Pools) - it->ManageTunnels (); + it->CreateTunnels (); } void Tunnels::PostTunnelData (I2NPMessage * msg) diff --git a/Tunnel.h b/Tunnel.h index 7d761453..4374c631 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -158,7 +158,6 @@ namespace tunnel const decltype(m_OutboundTunnels)& GetOutboundTunnels () const { return m_OutboundTunnels; }; const decltype(m_InboundTunnels)& GetInboundTunnels () const { return m_InboundTunnels; }; const decltype(m_TransitTunnels)& GetTransitTunnels () const { return m_TransitTunnels; }; - const decltype(m_Pools)& GetTunnelPools () const { return m_Pools; }; }; extern Tunnels tunnels; diff --git a/TunnelPool.cpp b/TunnelPool.cpp index 3bdb55ab..7f14dbec 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -23,6 +23,13 @@ namespace tunnel m_InboundTunnels.insert (createdTunnel); } + void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel) + { + m_InboundTunnels.erase (expiredTunnel); + if (m_Owner) + m_Owner->UpdateLeaseSet (); + } + std::vector TunnelPool::GetInboundTunnels (int num) const { std::vector v; @@ -48,34 +55,16 @@ namespace tunnel OutboundTunnel * outboundTunnel = tunnels.GetNextOutboundTunnel (); LogPrint ("Creating destination inbound tunnel..."); auto firstHop = i2p::data::netdb.GetRandomRouter (outboundTunnel ? outboundTunnel->GetEndpointRouter () : nullptr); + auto secondHop = i2p::data::netdb.GetRandomRouter (firstHop); auto * tunnel = tunnels.CreateTunnel ( new TunnelConfig (std::vector { firstHop, - i2p::data::netdb.GetRandomRouter (firstHop) + secondHop, + i2p::data::netdb.GetRandomRouter (secondHop) }), outboundTunnel); tunnel->SetTunnelPool (this); } - - void TunnelPool::ManageTunnels () - { - uint64_t ts = i2p::util::GetSecondsSinceEpoch (); - bool isLeaseSetUpdated = false; - for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();) - { - if (ts > (*it)->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) - { - LogPrint ("Destination tunnel ", (*it)->GetTunnelID (), " expired"); - m_InboundTunnels.erase (it++); - isLeaseSetUpdated = true; - } - else - ++it; - } - CreateTunnels (); - if (isLeaseSetUpdated && m_Owner) - m_Owner->UpdateLeaseSet (); - } } } diff --git a/TunnelPool.h b/TunnelPool.h index a6af0661..988e83c0 100644 --- a/TunnelPool.h +++ b/TunnelPool.h @@ -25,8 +25,8 @@ namespace tunnel void CreateTunnels (); void TunnelCreated (InboundTunnel * createdTunnel); + void TunnelExpired (InboundTunnel * expiredTunnel); std::vector GetInboundTunnels (int num) const; - void ManageTunnels (); private: