BOB 'clear' command

This commit is contained in:
orignal 2014-12-07 21:04:02 -05:00
parent 85624e4f4b
commit 8f12881162
2 changed files with 52 additions and 17 deletions

66
BOB.cpp
View File

@ -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);

3
BOB.h
View File

@ -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<std::string, std::string> 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: