i2pd/TunnelPool.cpp

74 lines
1.7 KiB
C++
Raw Normal View History

2014-03-14 20:35:02 +04:00
#include "Tunnel.h"
#include "NetDb.h"
2014-03-15 04:24:12 +04:00
#include "Timestamp.h"
2014-03-14 20:35:02 +04:00
#include "TunnelPool.h"
namespace i2p
{
namespace tunnel
{
TunnelPool::TunnelPool (i2p::data::LocalDestination * owner, int numTunnels):
m_Owner (owner), m_NumTunnels (numTunnels)
2014-03-14 20:35:02 +04:00
{
}
TunnelPool::~TunnelPool ()
{
for (auto it: m_InboundTunnels)
2014-03-15 04:51:51 +04:00
it->SetTunnelPool (nullptr);
}
void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel)
{
m_InboundTunnels.insert (createdTunnel);
2014-03-15 17:16:55 +04:00
if (m_Owner)
m_Owner->UpdateLeaseSet ();
}
2014-03-15 05:22:59 +04:00
void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel)
{
m_InboundTunnels.erase (expiredTunnel);
if (m_Owner)
m_Owner->UpdateLeaseSet ();
}
std::vector<InboundTunnel *> TunnelPool::GetInboundTunnels (int num) const
{
std::vector<InboundTunnel *> v;
int i = 0;
for (auto it : m_InboundTunnels)
{
if (i >= num) break;
v.push_back (it);
i++;
}
return v;
}
void TunnelPool::CreateTunnels ()
{
2014-03-15 04:24:12 +04:00
int num = m_InboundTunnels.size ();
for (int i = num; i < m_NumTunnels; i++)
CreateInboundTunnel ();
}
void TunnelPool::CreateInboundTunnel ()
{
OutboundTunnel * outboundTunnel = tunnels.GetNextOutboundTunnel ();
LogPrint ("Creating destination inbound tunnel...");
auto firstHop = i2p::data::netdb.GetRandomRouter (outboundTunnel ? outboundTunnel->GetEndpointRouter () : nullptr);
2014-03-15 05:22:59 +04:00
auto secondHop = i2p::data::netdb.GetRandomRouter (firstHop);
auto * tunnel = tunnels.CreateTunnel<InboundTunnel> (
new TunnelConfig (std::vector<const i2p::data::RouterInfo *>
{
firstHop,
2014-03-15 17:16:55 +04:00
secondHop
// TODO: swithc to 3-hops later
/*i2p::data::netdb.GetRandomRouter (secondHop) */
}),
outboundTunnel);
2014-03-15 04:51:51 +04:00
tunnel->SetTunnelPool (this);
2014-03-14 20:35:02 +04:00
}
}
}