i2pd/Daemon.cpp

153 lines
3.5 KiB
C++
Raw Normal View History

#include <thread>
2014-04-20 05:54:34 +04:00
#include "Daemon.h"
#include "Log.h"
#include "base64.h"
2014-10-17 19:42:05 +04:00
#include "version.h"
2014-04-20 05:54:34 +04:00
#include "Transports.h"
#include "NTCPSession.h"
#include "RouterInfo.h"
#include "RouterContext.h"
#include "Tunnel.h"
#include "NetDb.h"
#include "Garlic.h"
#include "util.h"
#include "Streaming.h"
#include "Destination.h"
#include "HTTPServer.h"
2014-10-16 04:52:17 +04:00
#include "ClientContext.h"
2014-04-20 05:54:34 +04:00
#ifdef USE_UPNP
#include "UPnP.h"
#endif
2014-04-20 05:54:34 +04:00
namespace i2p
{
namespace util
{
class Daemon_Singleton::Daemon_Singleton_Private
2014-04-20 05:54:34 +04:00
{
public:
Daemon_Singleton_Private() : httpServer(nullptr)
{};
~Daemon_Singleton_Private()
{
delete httpServer;
};
i2p::util::HTTPServer *httpServer;
};
2014-04-23 04:43:58 +04:00
Daemon_Singleton::Daemon_Singleton() : running(1), d(*new Daemon_Singleton_Private()) {};
Daemon_Singleton::~Daemon_Singleton() {
delete &d;
};
2014-10-17 17:55:41 +04:00
bool Daemon_Singleton::IsService () const
{
#ifndef _WIN32
return i2p::util::config::GetArg("-service", 0);
#else
return false;
#endif
}
bool Daemon_Singleton::init(int argc, char* argv[])
{
i2p::util::config::OptionParser(argc, argv);
2014-09-04 17:31:42 +04:00
i2p::context.Init ();
LogPrint("\n\n\n\ni2pd starting\n");
2014-10-17 19:42:05 +04:00
LogPrint("Version ", VERSION);
LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string());
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
2014-04-20 05:54:34 +04:00
isDaemon = i2p::util::config::GetArg("-daemon", 0);
isLogging = i2p::util::config::GetArg("-log", 1);
2014-04-20 05:54:34 +04:00
2014-09-11 17:32:34 +04:00
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])
2014-10-29 20:49:21 +03:00
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (host));
2014-04-20 05:54:34 +04:00
2014-09-09 00:43:20 +04:00
if (i2p::util::config::GetArg("-unreachable", 0))
i2p::context.SetUnreachable ();
2015-01-28 23:12:15 +03:00
if (i2p::util::config::GetArg("-floodfill", 0))
i2p::context.SetFloodfill (true);
i2p::context.SetSupportsV6 (i2p::util::config::GetArg("-v6", 0));
2014-07-02 21:48:45 +04:00
LogPrint("CMD parameters:");
for (int i = 0; i < argc; ++i)
LogPrint(i, " ", argv[i]);
return true;
}
bool Daemon_Singleton::start()
{
// initialize log
if (isLogging)
2014-04-20 05:54:34 +04:00
{
2014-10-26 05:11:31 +03:00
if (isDaemon)
{
std::string logfile_path = IsService () ? "/var/log" : i2p::util::filesystem::GetDataDir().string();
#ifndef _WIN32
2014-10-26 05:11:31 +03:00
logfile_path.append("/i2pd.log");
#else
2014-10-26 05:11:31 +03:00
logfile_path.append("\\i2pd.log");
#endif
2014-10-26 05:11:31 +03:00
StartLog (logfile_path);
}
else
StartLog (""); // write to stdout
}
2014-07-02 21:48:45 +04:00
d.httpServer = new i2p::util::HTTPServer(i2p::util::config::GetArg("-httpport", 7070));
d.httpServer->Start();
2014-07-14 20:40:06 +04:00
LogPrint("HTTP Server started");
2014-04-20 05:54:34 +04:00
i2p::data::netdb.Start();
2014-04-23 02:37:24 +04:00
LogPrint("NetDB started");
i2p::transport::transports.Start();
2014-04-23 02:37:24 +04:00
LogPrint("Transports started");
2014-04-20 05:54:34 +04:00
i2p::tunnel::tunnels.Start();
2014-04-23 02:37:24 +04:00
LogPrint("Tunnels started");
2014-10-16 04:52:17 +04:00
i2p::client::context.Start ();
LogPrint("Client started");
#ifdef USE_UPNP
i2p::UPnP::upnpc.Start();
LogPrint("UPnP module loaded");
#endif
2014-04-20 05:54:34 +04:00
return true;
}
bool Daemon_Singleton::stop()
{
LogPrint("Shutdown started.");
2014-10-16 04:52:17 +04:00
i2p::client::context.Stop();
LogPrint("Client stoped");
2014-04-20 05:54:34 +04:00
i2p::tunnel::tunnels.Stop();
2014-04-23 02:37:24 +04:00
LogPrint("Tunnels stoped");
i2p::transport::transports.Stop();
2014-04-23 02:37:24 +04:00
LogPrint("Transports stoped");
2014-04-20 05:54:34 +04:00
i2p::data::netdb.Stop();
2014-04-23 02:37:24 +04:00
LogPrint("NetDB stoped");
d.httpServer->Stop();
2014-07-14 20:40:06 +04:00
LogPrint("HTTP Server stoped");
#ifdef USE_UPNP
i2p::UPnP::upnpc.Stop();
#endif
2014-07-02 21:48:45 +04:00
StopLog ();
2014-08-14 18:20:22 +04:00
delete d.httpServer; d.httpServer = nullptr;
2014-04-20 05:54:34 +04:00
return true;
}
}
2014-04-23 02:37:24 +04:00
}