Browse Source

Using the d-pointer to hide HTTPProxy, HTTPServer, add logfile ofstream

pull/69/head
chertov 11 years ago
parent
commit
e3ff849aa4
  1. 88
      Daemon.cpp
  2. 28
      Daemon.h
  3. 58
      DaemonWin32.cpp

88
Daemon.cpp

@ -2,6 +2,8 @@
#define _CRT_SECURE_NO_WARNINGS // to use freopen #define _CRT_SECURE_NO_WARNINGS // to use freopen
#endif #endif
#include <thread>
#include "Daemon.h" #include "Daemon.h"
#include "Log.h" #include "Log.h"
@ -15,15 +17,42 @@
#include "Garlic.h" #include "Garlic.h"
#include "util.h" #include "util.h"
#include "Streaming.h" #include "Streaming.h"
#include "HTTPServer.h"
#include "HTTPProxy.h"
namespace i2p namespace i2p
{ {
namespace util namespace util
{ {
bool Daemon_Singleton::start() class Daemon_Singleton::Daemon_Singleton_Private
{
public:
Daemon_Singleton_Private() : httpServer(nullptr), httpProxy(nullptr) { };
~Daemon_Singleton_Private() {
delete httpServer;
delete httpProxy;
};
i2p::util::HTTPServer *httpServer;
i2p::proxy::HTTPProxy *httpProxy;
};
Daemon_Singleton::Daemon_Singleton() : d(*new Daemon_Singleton_Private()), running(1) {};
Daemon_Singleton::~Daemon_Singleton() {
delete &d;
};
bool Daemon_Singleton::init(int argc, char* argv[])
{ {
i2p::util::config::OptionParser(argc, argv);
LogPrint("\n\n\n\ni2pd starting\n");
LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string());
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
isDaemon = i2p::util::config::GetArg("-daemon", 0); isDaemon = i2p::util::config::GetArg("-daemon", 0);
isLogging = i2p::util::config::GetArg("-log", 0); isLogging = i2p::util::config::GetArg("-log", 1);
//TODO: This is an ugly workaround. fix it. //TODO: This is an ugly workaround. fix it.
//TODO: Autodetect public IP. //TODO: Autodetect public IP.
@ -32,27 +61,51 @@ namespace i2p
if (isLogging == 1) if (isLogging == 1)
{ {
std::string logfile = i2p::util::filesystem::GetDataDir().string(); std::string logfile_path = i2p::util::filesystem::GetDataDir().string();
#ifndef _WIN32 #ifndef _WIN32
logfile.append("/debug.log"); logfile_path.append("/debug.log");
#else #else
logfile.append("\\debug.log"); logfile_path.append("\\debug.log");
#endif #endif
freopen(logfile.c_str(), "a", stdout); logfile.open(logfile_path, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
//logfile = freopen(logfile_path.c_str(), "a", stdout);
if (!logfile.is_open())
{
exit(-17);
}
//std::streambuf * old = std::cout.rdbuf(logfile.rdbuf());
LogPrint("Logging to file enabled."); LogPrint("Logging to file enabled.");
LogPrint("CMD parameters:");
for (int i = 0; i < argc; ++i)
LogPrint(i, " ", argv[i]);
}
return true;
} }
httpServer = new i2p::util::HTTPServer(i2p::util::config::GetArg("-httpport", 7070)); bool Daemon_Singleton::start()
httpServer->Start(); {
d.httpServer = new i2p::util::HTTPServer(i2p::util::config::GetArg("-httpport", 7070));
d.httpServer->Start();
LogPrint("HTTPServer started", EVENTLOG_INFORMATION_TYPE);
i2p::data::netdb.Start(); i2p::data::netdb.Start();
LogPrint("NetDB started", EVENTLOG_INFORMATION_TYPE);
i2p::transports.Start(); i2p::transports.Start();
LogPrint("Transports started", EVENTLOG_INFORMATION_TYPE);
i2p::tunnel::tunnels.Start(); i2p::tunnel::tunnels.Start();
LogPrint("Tunnels started", EVENTLOG_INFORMATION_TYPE);
i2p::garlic::routing.Start(); i2p::garlic::routing.Start();
LogPrint("Routing started", EVENTLOG_INFORMATION_TYPE);
i2p::stream::StartStreaming(); i2p::stream::StartStreaming();
LogPrint("Streaming started", EVENTLOG_INFORMATION_TYPE);
httpProxy = new i2p::proxy::HTTPProxy(i2p::util::config::GetArg("-httpproxyport", 4446)); d.httpProxy = new i2p::proxy::HTTPProxy(i2p::util::config::GetArg("-httpproxyport", 4446));
httpProxy->Start(); d.httpProxy->Start();
LogPrint("Proxy started", EVENTLOG_INFORMATION_TYPE);
return true; return true;
} }
@ -61,16 +114,23 @@ namespace i2p
{ {
LogPrint("Shutdown started."); LogPrint("Shutdown started.");
httpProxy->Stop(); d.httpProxy->Stop();
LogPrint("HTTPProxy stoped", EVENTLOG_INFORMATION_TYPE);
i2p::stream::StopStreaming(); i2p::stream::StopStreaming();
LogPrint("Streaming stoped", EVENTLOG_INFORMATION_TYPE);
i2p::garlic::routing.Stop(); i2p::garlic::routing.Stop();
LogPrint("Routing stoped", EVENTLOG_INFORMATION_TYPE);
i2p::tunnel::tunnels.Stop(); i2p::tunnel::tunnels.Stop();
LogPrint("Tunnels stoped", EVENTLOG_INFORMATION_TYPE);
i2p::transports.Stop(); i2p::transports.Stop();
LogPrint("Transports stoped", EVENTLOG_INFORMATION_TYPE);
i2p::data::netdb.Stop(); i2p::data::netdb.Stop();
httpServer->Stop(); LogPrint("NetDB stoped", EVENTLOG_INFORMATION_TYPE);
d.httpServer->Stop();
LogPrint("HTTPServer stoped", EVENTLOG_INFORMATION_TYPE);
delete httpProxy; httpProxy = NULL; delete d.httpProxy; d.httpProxy = nullptr;
delete httpServer; httpServer = NULL; delete d.httpServer; d.httpServer = nullptr;
if (isLogging == 1) if (isLogging == 1)
{ {

28
Daemon.h

@ -1,15 +1,21 @@
#pragma once #pragma once
#include <fstream>
#include "HTTPServer.h" #ifdef _WIN32
#include "HTTPProxy.h" #define Daemon i2p::util::DaemonWin32::Instance()
#else
#define Daemon i2p::util::DaemonLinux::Instance()
#endif
namespace i2p namespace i2p
{ {
namespace util namespace util
{ {
class Daemon_Singleton_Private;
class Daemon_Singleton class Daemon_Singleton
{ {
public: public:
virtual bool init(int argc, char* argv[]);
virtual bool start(); virtual bool start();
virtual bool stop(); virtual bool stop();
@ -18,20 +24,18 @@ namespace i2p
int running; int running;
private: std::ofstream logfile;
i2p::util::HTTPServer *httpServer;
i2p::proxy::HTTPProxy *httpProxy;
protected: protected:
Daemon_Singleton() : running(1) {}; Daemon_Singleton();
virtual ~Daemon_Singleton() { virtual ~Daemon_Singleton();
delete httpServer;
delete httpProxy; // d-pointer for httpServer, httpProxy, etc.
}; class Daemon_Singleton_Private;
Daemon_Singleton_Private &d;
}; };
#ifdef _WIN32 #ifdef _WIN32
#define Daemon i2p::util::DaemonWin32::Instance()
class DaemonWin32 : public Daemon_Singleton class DaemonWin32 : public Daemon_Singleton
{ {
public: public:
@ -41,11 +45,11 @@ namespace i2p
return instance; return instance;
} }
virtual bool init(int argc, char* argv[]);
virtual bool start(); virtual bool start();
virtual bool stop(); virtual bool stop();
}; };
#else #else
#define Daemon i2p::util::DaemonLinux::Instance()
class DaemonLinux : public Daemon_Singleton class DaemonLinux : public Daemon_Singleton
{ {
public: public:

58
DaemonWin32.cpp

@ -1,22 +1,74 @@
#include "Daemon.h" #include "Daemon.h"
#include "util.h"
#include "Log.h"
#ifdef _WIN32 #ifdef _WIN32
#include "./Win32/Win32Service.h" #include "./Win32/Win32Service.h"
namespace i2p namespace i2p
{ {
namespace util namespace util
{ {
bool DaemonWin32::start() bool DaemonWin32::init(int argc, char* argv[])
{ {
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
SetConsoleCP(1251); SetConsoleCP(1251);
SetConsoleOutputCP(1251); SetConsoleOutputCP(1251);
setlocale(LC_ALL, "Russian"); setlocale(LC_ALL, "Russian");
service_control(isDaemon); if (!Daemon_Singleton::init(argc, argv)) return false;
if (I2PService::isService())
isDaemon = 1;
else
isDaemon = 0;
std::string serviceControl = i2p::util::config::GetArg("-service", "none");
if (serviceControl == "install")
{
InstallService(
SERVICE_NAME, // Name of service
SERVICE_DISPLAY_NAME, // Name to display
SERVICE_START_TYPE, // Service start type
SERVICE_DEPENDENCIES, // Dependencies
SERVICE_ACCOUNT, // Service running account
SERVICE_PASSWORD // Password of the account
);
exit(0);
}
else if (serviceControl == "remove")
{
UninstallService(SERVICE_NAME);
exit(0);
}
else if (serviceControl != "none")
{
printf(" --service=install to install the service.\n");
printf(" --service=remove to remove the service.\n");
}
if (isDaemon == 1)
{
LogPrint("Service session");
I2PService service(SERVICE_NAME);
if (!I2PService::Run(service))
{
LogPrint("Service failed to run w/err 0x%08lx\n", GetLastError());
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
else
LogPrint("User session");
return true;
}
bool DaemonWin32::start()
{
setlocale(LC_CTYPE, "");
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
setlocale(LC_ALL, "Russian");
return Daemon_Singleton::start(); return Daemon_Singleton::start();
} }

Loading…
Cancel
Save