i2pd/api/api.cpp

109 lines
2.5 KiB
C++
Raw Normal View History

2014-11-17 22:42:45 +03:00
#include <string>
#include <map>
#include "Log.h"
#include "NetDb.h"
#include "Transports.h"
#include "Tunnel.h"
#include "RouterContext.h"
#include "Identity.h"
#include "Destination.h"
#include "util.h"
2014-11-17 20:37:40 +03:00
#include "api.h"
namespace i2p
{
namespace api
{
2014-11-18 17:33:58 +03:00
void InitI2P (int argc, char* argv[], const char * appName)
2014-11-17 22:42:45 +03:00
{
2014-11-18 17:33:58 +03:00
i2p::util::filesystem::SetAppName (appName);
2014-11-17 22:42:45 +03:00
i2p::util::config::OptionParser(argc, argv);
i2p::context.Init ();
}
void StartI2P ()
{
2014-11-17 23:28:52 +03:00
StartLog (i2p::util::filesystem::GetAppName () + ".log");
2014-11-17 22:42:45 +03:00
i2p::data::netdb.Start();
LogPrint("NetDB started");
i2p::transport::transports.Start();
LogPrint("Transports started");
i2p::tunnel::tunnels.Start();
LogPrint("Tunnels started");
}
void StopI2P ()
{
LogPrint("Shutdown started.");
i2p::tunnel::tunnels.Stop();
LogPrint("Tunnels stoped");
i2p::transport::transports.Stop();
LogPrint("Transports stoped");
i2p::data::netdb.Stop();
LogPrint("NetDB stoped");
StopLog ();
}
2014-11-18 01:00:30 +03:00
2014-11-18 17:33:58 +03:00
i2p::client::ClientDestination * CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic)
2014-11-18 01:00:30 +03:00
{
2014-11-18 17:33:58 +03:00
auto localDestination = new i2p::client::ClientDestination (keys, isPublic);
2014-11-18 01:00:30 +03:00
localDestination->Start ();
return localDestination;
}
2014-11-18 17:33:58 +03:00
i2p::client::ClientDestination * CreateLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType)
2014-11-18 01:00:30 +03:00
{
2014-11-18 17:33:58 +03:00
auto localDestination = new i2p::client::ClientDestination (isPublic, sigType);
2014-11-18 01:00:30 +03:00
localDestination->Start ();
return localDestination;
}
void DestroyLocalDestination (i2p::client::ClientDestination * dest)
{
if (dest)
{
dest->Stop ();
delete dest;
}
}
2014-11-18 17:33:58 +03:00
void RequestLeaseSet (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote)
{
if (dest)
i2p::data::netdb.RequestDestination (remote, true, dest->GetTunnelPool ());
}
2014-11-23 19:33:58 +03:00
std::shared_ptr<i2p::stream::Stream> CreateStream (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote)
2014-11-18 17:33:58 +03:00
{
auto leaseSet = i2p::data::netdb.FindLeaseSet (remote);
if (leaseSet)
{
auto stream = dest->CreateStream (*leaseSet);
stream->Send (nullptr, 0); // connect
return stream;
}
else
{
RequestLeaseSet (dest, remote);
return nullptr;
}
}
void AcceptStream (i2p::client::ClientDestination * dest, const i2p::stream::StreamingDestination::Acceptor& acceptor)
{
if (dest)
dest->AcceptStreams (acceptor);
}
2014-11-23 19:33:58 +03:00
void DestroyStream (std::shared_ptr<i2p::stream::Stream> stream)
2014-11-18 17:33:58 +03:00
{
if (stream)
{
stream->Close ();
i2p::stream::DeleteStream (stream);
}
}
2014-11-17 20:37:40 +03:00
}
}