i2pd/client/Daemon.cpp

158 lines
4.9 KiB
C++
Raw Normal View History

#include <thread>
2014-04-20 01:54:34 +00:00
#include "Daemon.h"
2015-07-30 13:34:56 +00:00
#include "util/Log.h"
2014-10-17 15:42:05 +00:00
#include "version.h"
2015-07-27 20:17:31 +00:00
#include "transport/Transports.h"
#include "transport/NTCPSession.h"
2014-04-20 01:54:34 +00:00
#include "RouterInfo.h"
#include "RouterContext.h"
2015-07-30 14:25:43 +00:00
#include "tunnel/Tunnel.h"
2015-08-26 20:17:10 +00:00
#include "NetworkDatabase.h"
2014-04-20 01:54:34 +00:00
#include "Garlic.h"
2015-07-28 16:37:06 +00:00
#include "util/util.h"
2014-04-20 01:54:34 +00:00
#include "Streaming.h"
#include "Destination.h"
#include "HTTPServer.h"
2014-10-16 00:52:17 +00:00
#include "ClientContext.h"
2014-04-20 01:54:34 +00:00
namespace i2p
{
namespace util
{
class Daemon_Singleton::Daemon_Singleton_Private
{
public:
Daemon_Singleton_Private() : httpServer(nullptr)
{};
~Daemon_Singleton_Private()
{
delete httpServer;
};
i2p::util::HTTPServer *httpServer;
};
Daemon_Singleton::Daemon_Singleton() : running(1), d(*new Daemon_Singleton_Private()) {};
Daemon_Singleton::~Daemon_Singleton() {
delete &d;
};
bool Daemon_Singleton::IsService () const
{
2014-10-17 13:55:41 +00:00
#ifndef _WIN32
return i2p::util::config::GetArg("-service", 0);
2014-10-17 13:55:41 +00:00
#else
return false;
2014-10-17 13:55:41 +00:00
#endif
}
bool Daemon_Singleton::init(int argc, char* argv[])
{
i2p::util::config::OptionParser(argc, argv);
i2p::context.Init ();
LogPrint("\n\n\n\ni2pd starting\n");
LogPrint("Version ", VERSION);
LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string());
2015-09-18 12:19:06 +00:00
i2p::util::filesystem::ReadConfigFile(
i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs
);
if(i2p::util::config::HasArg("-install")) {
try {
i2p::util::filesystem::InstallFiles();
LogPrint("Succesfully installed all files.");
} catch(const std::runtime_error& e) {
LogPrint(eLogError, "Failed to install: ", e.what());
return false;
}
}
isDaemon = i2p::util::config::GetArg("-daemon", 0);
isLogging = i2p::util::config::GetArg("-log", 1);
int port = i2p::util::config::GetArg("-port", 0);
if (port)
i2p::context.UpdatePort (port);
const char * host = i2p::util::config::GetCharArg("-host", "");
if (host && host[0])
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (host));
i2p::context.SetSupportsV6 (i2p::util::config::GetArg("-v6", 0));
i2p::context.SetFloodfill (i2p::util::config::GetArg("-floodfill", 0));
auto bandwidth = i2p::util::config::GetArg("-bandwidth", "");
if (bandwidth.length () > 0)
{
if (bandwidth[0] > 'L')
i2p::context.SetHighBandwidth ();
else
i2p::context.SetLowBandwidth ();
}
LogPrint("CMD parameters:");
for (int i = 0; i < argc; ++i)
LogPrint(i, " ", argv[i]);
return true;
}
bool Daemon_Singleton::start()
{
// initialize log
if (isLogging)
{
if (isDaemon)
{
std::string logfile_path = IsService () ? "/var/log" : i2p::util::filesystem::GetDataDir().string();
#ifndef _WIN32
logfile_path.append("/i2pd.log");
#else
logfile_path.append("\\i2pd.log");
#endif
StartLog (logfile_path);
}
else
StartLog (""); // write to stdout
}
d.httpServer = new i2p::util::HTTPServer(
i2p::util::config::GetArg("-httpaddress", "127.0.0.1"),
i2p::util::config::GetArg("-httpport", 7070)
);
d.httpServer->Start();
LogPrint("HTTP Server started");
i2p::data::netdb.Start();
LogPrint("NetDB started");
i2p::transport::transports.Start();
LogPrint("Transports started");
i2p::tunnel::tunnels.Start();
LogPrint("Tunnels started");
i2p::client::context.Start ();
LogPrint("Client started");
return true;
}
bool Daemon_Singleton::stop()
{
LogPrint("Shutdown started.");
i2p::client::context.Stop();
LogPrint("Client stopped");
i2p::tunnel::tunnels.Stop();
LogPrint("Tunnels stopped");
i2p::transport::transports.Stop();
LogPrint("Transports stopped");
i2p::data::netdb.Stop();
LogPrint("NetDB stopped");
d.httpServer->Stop();
LogPrint("HTTP Server stopped");
StopLog ();
delete d.httpServer; d.httpServer = nullptr;
return true;
}
}
2014-04-22 22:37:24 +00:00
}