From 8f128811627d21a1f559aa8554e2b441f1bef3ed Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 7 Dec 2014 21:04:02 -0500 Subject: [PATCH] BOB 'clear' command --- BOB.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++--------------- BOB.h | 3 +++ 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/BOB.cpp b/BOB.cpp index 700b8edd..e4188487 100644 --- a/BOB.cpp +++ b/BOB.cpp @@ -198,24 +198,42 @@ namespace client void BOBDestination::Stop () { - if (m_OutboundTunnel) m_OutboundTunnel->Stop (); - if (m_InboundTunnel) m_InboundTunnel->Stop (); + StopTunnels (); m_LocalDestination.Stop (); } + void BOBDestination::StopTunnels () + { + if (m_OutboundTunnel) + { + m_OutboundTunnel->Stop (); + delete m_OutboundTunnel; + m_OutboundTunnel = nullptr; + } + if (m_InboundTunnel) + { + m_InboundTunnel->Stop (); + delete m_InboundTunnel; + m_InboundTunnel = nullptr; + } + } + void BOBDestination::CreateInboundTunnel (int port) { - m_InboundTunnel = new BOBI2PInboundTunnel (m_Service, port, &m_LocalDestination); + if (!m_InboundTunnel) + m_InboundTunnel = new BOBI2PInboundTunnel (m_Service, port, &m_LocalDestination); } void BOBDestination::CreateOutboundTunnel (const std::string& address, int port, bool quiet) { - m_OutboundTunnel = new BOBI2POutboundTunnel (m_Service, address, port, &m_LocalDestination, quiet); + if (!m_OutboundTunnel) + m_OutboundTunnel = new BOBI2POutboundTunnel (m_Service, address, port, &m_LocalDestination, quiet); } BOBCommandSession::BOBCommandSession (BOBCommandChannel& owner): m_Owner (owner), m_Socket (m_Owner.GetService ()), m_ReceiveBufferOffset (0), - m_IsOpen (true), m_IsQuiet (false), m_InPort (0), m_OutPort (0) + m_IsOpen (true), m_IsQuiet (false), m_InPort (0), m_OutPort (0), + m_CurrentDestination (nullptr) { } @@ -365,14 +383,17 @@ namespace client void BOBCommandSession::StartCommandHandler (const char * operand, size_t len) { LogPrint (eLogDebug, "BOB: start ", m_Nickname); - BOBDestination * dest = new BOBDestination (m_Owner.GetService (), - *context.CreateNewLocalDestination (m_Keys, true, &m_Options)); + if (!m_CurrentDestination) + { + m_CurrentDestination = new BOBDestination (m_Owner.GetService (), + *context.CreateNewLocalDestination (m_Keys, true, &m_Options)); + m_Owner.AddDestination (m_Nickname, m_CurrentDestination); + } if (m_InPort) - dest->CreateInboundTunnel (m_InPort); + m_CurrentDestination->CreateInboundTunnel (m_InPort); if (m_OutPort && !m_Address.empty ()) - dest->CreateOutboundTunnel (m_Address, m_OutPort, m_IsQuiet); - m_Owner.AddDestination (m_Nickname, dest); - dest->Start (); + m_CurrentDestination->CreateOutboundTunnel (m_Address, m_OutPort, m_IsQuiet); + m_CurrentDestination->Start (); SendReplyOK ("tunnel starting"); } @@ -381,7 +402,7 @@ namespace client auto dest = m_Owner.FindDestination (m_Nickname); if (dest) { - dest->Stop (); + dest->StopTunnels (); SendReplyOK ("tunnel stopping"); } else @@ -400,10 +421,10 @@ namespace client void BOBCommandSession::GetNickCommandHandler (const char * operand, size_t len) { LogPrint (eLogDebug, "BOB: getnick ", operand); - auto dest = m_Owner.FindDestination (operand); - if (dest) + m_CurrentDestination = m_Owner.FindDestination (operand); + if (m_CurrentDestination) { - m_Keys = dest->GetKeys (); + m_Keys = m_CurrentDestination->GetKeys (); m_Nickname = operand; std::string msg ("Nickname set to "); msg += operand; @@ -489,7 +510,7 @@ namespace client void BOBCommandSession::ClearCommandHandler (const char * operand, size_t len) { LogPrint (eLogDebug, "BOB: clear"); - // TODO + m_Owner.DeleteDestination (m_Nickname); SendReplyOK ("cleared"); } @@ -570,7 +591,7 @@ namespace client m_Thread = nullptr; } } - + void BOBCommandChannel::Run () { while (m_IsRunning) @@ -591,6 +612,17 @@ namespace client m_Destinations[name] = dest; } + void BOBCommandChannel::DeleteDestination (const std::string& name) + { + auto it = m_Destinations.find (name); + if (it != m_Destinations.end ()) + { + it->second->Stop (); + delete it->second; + m_Destinations.erase (it); + } + } + BOBDestination * BOBCommandChannel::FindDestination (const std::string& name) { auto it = m_Destinations.find (name); diff --git a/BOB.h b/BOB.h index bbd54cf4..f8921665 100644 --- a/BOB.h +++ b/BOB.h @@ -124,6 +124,7 @@ namespace client void Start (); void Stop (); + void StopTunnels (); void CreateInboundTunnel (int port); void CreateOutboundTunnel (const std::string& address, int port, bool quiet); const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination.GetPrivateKeys (); }; @@ -191,6 +192,7 @@ namespace client int m_InPort, m_OutPort; i2p::data::PrivateKeys m_Keys; std::map m_Options; + BOBDestination * m_CurrentDestination; }; typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len); @@ -206,6 +208,7 @@ namespace client boost::asio::io_service& GetService () { return m_Service; }; void AddDestination (const std::string& name, BOBDestination * dest); + void DeleteDestination (const std::string& name); BOBDestination * FindDestination (const std::string& name); private: