Browse Source

BOB 'clear' command

pull/115/head
orignal 10 years ago
parent
commit
8f12881162
  1. 66
      BOB.cpp
  2. 3
      BOB.h

66
BOB.cpp

@ -198,24 +198,42 @@ namespace client
void BOBDestination::Stop () void BOBDestination::Stop ()
{ {
if (m_OutboundTunnel) m_OutboundTunnel->Stop (); StopTunnels ();
if (m_InboundTunnel) m_InboundTunnel->Stop ();
m_LocalDestination.Stop (); 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) 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) 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): BOBCommandSession::BOBCommandSession (BOBCommandChannel& owner):
m_Owner (owner), m_Socket (m_Owner.GetService ()), m_ReceiveBufferOffset (0), 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) void BOBCommandSession::StartCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: start ", m_Nickname); LogPrint (eLogDebug, "BOB: start ", m_Nickname);
BOBDestination * dest = new BOBDestination (m_Owner.GetService (), if (!m_CurrentDestination)
*context.CreateNewLocalDestination (m_Keys, true, &m_Options)); {
m_CurrentDestination = new BOBDestination (m_Owner.GetService (),
*context.CreateNewLocalDestination (m_Keys, true, &m_Options));
m_Owner.AddDestination (m_Nickname, m_CurrentDestination);
}
if (m_InPort) if (m_InPort)
dest->CreateInboundTunnel (m_InPort); m_CurrentDestination->CreateInboundTunnel (m_InPort);
if (m_OutPort && !m_Address.empty ()) if (m_OutPort && !m_Address.empty ())
dest->CreateOutboundTunnel (m_Address, m_OutPort, m_IsQuiet); m_CurrentDestination->CreateOutboundTunnel (m_Address, m_OutPort, m_IsQuiet);
m_Owner.AddDestination (m_Nickname, dest); m_CurrentDestination->Start ();
dest->Start ();
SendReplyOK ("tunnel starting"); SendReplyOK ("tunnel starting");
} }
@ -381,7 +402,7 @@ namespace client
auto dest = m_Owner.FindDestination (m_Nickname); auto dest = m_Owner.FindDestination (m_Nickname);
if (dest) if (dest)
{ {
dest->Stop (); dest->StopTunnels ();
SendReplyOK ("tunnel stopping"); SendReplyOK ("tunnel stopping");
} }
else else
@ -400,10 +421,10 @@ namespace client
void BOBCommandSession::GetNickCommandHandler (const char * operand, size_t len) void BOBCommandSession::GetNickCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: getnick ", operand); LogPrint (eLogDebug, "BOB: getnick ", operand);
auto dest = m_Owner.FindDestination (operand); m_CurrentDestination = m_Owner.FindDestination (operand);
if (dest) if (m_CurrentDestination)
{ {
m_Keys = dest->GetKeys (); m_Keys = m_CurrentDestination->GetKeys ();
m_Nickname = operand; m_Nickname = operand;
std::string msg ("Nickname set to "); std::string msg ("Nickname set to ");
msg += operand; msg += operand;
@ -489,7 +510,7 @@ namespace client
void BOBCommandSession::ClearCommandHandler (const char * operand, size_t len) void BOBCommandSession::ClearCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: clear"); LogPrint (eLogDebug, "BOB: clear");
// TODO m_Owner.DeleteDestination (m_Nickname);
SendReplyOK ("cleared"); SendReplyOK ("cleared");
} }
@ -570,7 +591,7 @@ namespace client
m_Thread = nullptr; m_Thread = nullptr;
} }
} }
void BOBCommandChannel::Run () void BOBCommandChannel::Run ()
{ {
while (m_IsRunning) while (m_IsRunning)
@ -591,6 +612,17 @@ namespace client
m_Destinations[name] = dest; 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) BOBDestination * BOBCommandChannel::FindDestination (const std::string& name)
{ {
auto it = m_Destinations.find (name); auto it = m_Destinations.find (name);

3
BOB.h

@ -124,6 +124,7 @@ namespace client
void Start (); void Start ();
void Stop (); void Stop ();
void StopTunnels ();
void CreateInboundTunnel (int port); void CreateInboundTunnel (int port);
void CreateOutboundTunnel (const std::string& address, int port, bool quiet); void CreateOutboundTunnel (const std::string& address, int port, bool quiet);
const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination.GetPrivateKeys (); }; const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination.GetPrivateKeys (); };
@ -191,6 +192,7 @@ namespace client
int m_InPort, m_OutPort; int m_InPort, m_OutPort;
i2p::data::PrivateKeys m_Keys; i2p::data::PrivateKeys m_Keys;
std::map<std::string, std::string> m_Options; std::map<std::string, std::string> m_Options;
BOBDestination * m_CurrentDestination;
}; };
typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len); typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len);
@ -206,6 +208,7 @@ namespace client
boost::asio::io_service& GetService () { return m_Service; }; boost::asio::io_service& GetService () { return m_Service; };
void AddDestination (const std::string& name, BOBDestination * dest); void AddDestination (const std::string& name, BOBDestination * dest);
void DeleteDestination (const std::string& name);
BOBDestination * FindDestination (const std::string& name); BOBDestination * FindDestination (const std::string& name);
private: private:

Loading…
Cancel
Save