Skip to content

Commit

Permalink
use shared_ptr for TunnelPool
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 20, 2015
1 parent e09da5c commit ebb5c53
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Destination.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace client
virtual void Stop ();
bool IsRunning () const { return m_IsRunning; };
boost::asio::io_service& GetService () { return m_Service; };
i2p::tunnel::TunnelPool * GetTunnelPool () { return m_Pool; };
std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () { return m_Pool; };
bool IsReady () const { return m_LeaseSet && m_LeaseSet->HasNonExpiredLeases (); };
const i2p::data::LeaseSet * FindLeaseSet (const i2p::data::IdentHash& ident);
bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr);
Expand Down Expand Up @@ -121,7 +121,7 @@ namespace client
std::map<i2p::data::IdentHash, i2p::data::LeaseSet *> m_RemoteLeaseSets;
std::map<i2p::data::IdentHash, LeaseSetRequest *> m_LeaseSetRequests;

i2p::tunnel::TunnelPool * m_Pool;
std::shared_ptr<i2p::tunnel::TunnelPool> m_Pool;
i2p::data::LeaseSet * m_LeaseSet;
bool m_IsPublic;
uint32_t m_PublishReplyToken;
Expand Down
21 changes: 7 additions & 14 deletions Tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ namespace tunnel

Tunnels tunnels;

Tunnels::Tunnels (): m_IsRunning (false), m_Thread (nullptr), m_ExploratoryPool (nullptr)
Tunnels::Tunnels (): m_IsRunning (false), m_Thread (nullptr)
{
}

Expand All @@ -213,9 +213,6 @@ namespace tunnel
delete it.second;
m_PendingTunnels.clear ();*/

for (auto& it: m_Pools)
delete it;
m_Pools.clear ();
}

InboundTunnel * Tunnels::GetInboundTunnel (uint32_t tunnelID)
Expand Down Expand Up @@ -280,15 +277,15 @@ namespace tunnel
return tunnel;
}

TunnelPool * Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops)
std::shared_ptr<TunnelPool> Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops)
{
auto pool = new TunnelPool (localDestination, numInboundHops, numOutboundHops);
auto pool = std::make_shared<TunnelPool> (localDestination, numInboundHops, numOutboundHops);
std::unique_lock<std::mutex> l(m_PoolsMutex);
m_Pools.push_back (pool);
return pool;
}

void Tunnels::DeleteTunnelPool (TunnelPool * pool)
void Tunnels::DeleteTunnelPool (std::shared_ptr<TunnelPool> pool)
{
if (pool)
{
Expand All @@ -297,14 +294,10 @@ namespace tunnel
std::unique_lock<std::mutex> l(m_PoolsMutex);
m_Pools.remove (pool);
}
for (auto it: m_PendingTunnels)
if (it.second->GetTunnelPool () == pool)
it.second->SetTunnelPool (nullptr);
delete pool;
}
}

void Tunnels::StopTunnelPool (TunnelPool * pool)
void Tunnels::StopTunnelPool (std::shared_ptr<TunnelPool> pool)
{
if (pool)
{
Expand Down Expand Up @@ -547,8 +540,8 @@ namespace tunnel
std::unique_lock<std::mutex> l(m_PoolsMutex);
for (auto it: m_Pools)
{
TunnelPool * pool = it;
if (pool->IsActive ())
auto pool = it;
if (pool && pool->IsActive ())
{
pool->CreateTunnels ();
pool->TestTunnels ();
Expand Down
19 changes: 10 additions & 9 deletions Tunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <thread>
#include <mutex>
#include <memory>
#include "Queue.h"
#include "TunnelConfig.h"
#include "TunnelPool.h"
Expand Down Expand Up @@ -54,8 +55,8 @@ namespace tunnel
bool IsEstablished () const { return m_State == eTunnelStateEstablished; };
bool IsFailed () const { return m_State == eTunnelStateFailed; };

TunnelPool * GetTunnelPool () const { return m_Pool; };
void SetTunnelPool (TunnelPool * pool) { m_Pool = pool; };
std::shared_ptr<TunnelPool> GetTunnelPool () const { return m_Pool; };
void SetTunnelPool (std::shared_ptr<TunnelPool> pool) { m_Pool = pool; };

bool HandleTunnelBuildResponse (uint8_t * msg, size_t len);

Expand All @@ -67,7 +68,7 @@ namespace tunnel
private:

TunnelConfig * m_Config;
TunnelPool * m_Pool; // pool, tunnel belongs to, or null
std::shared_ptr<TunnelPool> m_Pool; // pool, tunnel belongs to, or null
TunnelState m_State;
};

Expand Down Expand Up @@ -121,7 +122,7 @@ namespace tunnel
Tunnel * GetPendingTunnel (uint32_t replyMsgID);
InboundTunnel * GetNextInboundTunnel ();
OutboundTunnel * GetNextOutboundTunnel ();
TunnelPool * GetExploratoryPool () const { return m_ExploratoryPool; };
std::shared_ptr<TunnelPool> GetExploratoryPool () const { return m_ExploratoryPool; };
TransitTunnel * GetTransitTunnel (uint32_t tunnelID);
int GetTransitTunnelsExpirationTimeout ();
void AddTransitTunnel (TransitTunnel * tunnel);
Expand All @@ -130,9 +131,9 @@ namespace tunnel
void PostTunnelData (I2NPMessage * msg);
template<class TTunnel>
TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0);
TunnelPool * CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops);
void DeleteTunnelPool (TunnelPool * pool);
void StopTunnelPool (TunnelPool * pool);
std::shared_ptr<TunnelPool> CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops);
void DeleteTunnelPool (std::shared_ptr<TunnelPool> pool);
void StopTunnelPool (std::shared_ptr<TunnelPool> pool);

private:

Expand All @@ -158,8 +159,8 @@ namespace tunnel
std::mutex m_TransitTunnelsMutex;
std::map<uint32_t, TransitTunnel *> m_TransitTunnels;
std::mutex m_PoolsMutex;
std::list<TunnelPool *> m_Pools;
TunnelPool * m_ExploratoryPool;
std::list<std::shared_ptr<TunnelPool>> m_Pools;
std::shared_ptr<TunnelPool> m_ExploratoryPool;
i2p::util::Queue<I2NPMessage> m_Queue;

public:
Expand Down
8 changes: 4 additions & 4 deletions TunnelPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ namespace tunnel
}
std::reverse (hops.begin (), hops.end ());
auto * tunnel = tunnels.CreateTunnel<InboundTunnel> (new TunnelConfig (hops), outboundTunnel);
tunnel->SetTunnelPool (this);
tunnel->SetTunnelPool (shared_from_this ());
}

void TunnelPool::RecreateInboundTunnel (InboundTunnel * tunnel)
Expand All @@ -310,7 +310,7 @@ namespace tunnel
outboundTunnel = tunnels.GetNextOutboundTunnel ();
LogPrint ("Re-creating destination inbound tunnel...");
auto * newTunnel = tunnels.CreateTunnel<InboundTunnel> (tunnel->GetTunnelConfig ()->Clone (), outboundTunnel);
newTunnel->SetTunnelPool (this);
newTunnel->SetTunnelPool (shared_from_this());
}

void TunnelPool::CreateOutboundTunnel ()
Expand All @@ -333,7 +333,7 @@ namespace tunnel

auto * tunnel = tunnels.CreateTunnel<OutboundTunnel> (
new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ()));
tunnel->SetTunnelPool (this);
tunnel->SetTunnelPool (shared_from_this ());
}
else
LogPrint ("Can't create outbound tunnel. No inbound tunnels found");
Expand All @@ -349,7 +349,7 @@ namespace tunnel
LogPrint ("Re-creating destination outbound tunnel...");
auto * newTunnel = tunnels.CreateTunnel<OutboundTunnel> (
tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ()));
newTunnel->SetTunnelPool (this);
newTunnel->SetTunnelPool (shared_from_this ());
}
else
LogPrint ("Can't re-create outbound tunnel. No inbound tunnels found");
Expand Down
3 changes: 2 additions & 1 deletion TunnelPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>
#include <utility>
#include <mutex>
#include <memory>
#include "Identity.h"
#include "LeaseSet.h"
#include "RouterInfo.h"
Expand All @@ -22,7 +23,7 @@ namespace tunnel
class InboundTunnel;
class OutboundTunnel;

class TunnelPool // per local destination
class TunnelPool: public std::enable_shared_from_this<TunnelPool> // per local destination
{
public:

Expand Down

0 comments on commit ebb5c53

Please sign in to comment.