Browse Source

Tab2Space & Newlines

peercoin
Intel 12 years ago
parent
commit
57c65a4ba2
  1. 60
      src/server/poolserver/Main.cpp
  2. 12
      src/server/poolserver/Server/Server.cpp
  3. 20
      src/server/shared/Configuration/Config.h
  4. 16
      src/server/shared/Database/Database.h
  5. 10
      src/server/shared/Database/MySQL/DatabaseConnectionMySQL.h
  6. 14
      src/server/shared/Database/MySQL/DatabaseWorkerPoolMySQL.h
  7. 116
      src/server/shared/Logging/Log.cpp
  8. 38
      src/server/shared/Logging/Log.h
  9. 2
      src/server/shared/Util.h

60
src/server/poolserver/Main.cpp

@ -9,41 +9,41 @@ bool InitConfig(int argc, char *argv[])
{ {
// Containers // Containers
boost::program_options::options_description descGeneric("Generic Options"); boost::program_options::options_description descGeneric("Generic Options");
boost::program_options::options_description descServer("Server Configuration"); boost::program_options::options_description descServer("Server Configuration");
boost::program_options::options_description descStratum("Stratum Configuration"); boost::program_options::options_description descStratum("Stratum Configuration");
boost::program_options::options_description descLogging("Logging Configuration"); boost::program_options::options_description descLogging("Logging Configuration");
boost::program_options::options_description descDatabase("Database Configuration"); boost::program_options::options_description descDatabase("Database Configuration");
#ifdef WITH_MYSQL #ifdef WITH_MYSQL
boost::program_options::options_description descMySQL("MySQL Configuration"); boost::program_options::options_description descMySQL("MySQL Configuration");
#endif #endif
boost::program_options::options_description cmdlineOptions; boost::program_options::options_description cmdlineOptions;
boost::program_options::options_description fileOptions; boost::program_options::options_description fileOptions;
// Generic // Generic
descGeneric.add_options() descGeneric.add_options()
("version,v", "print version string") ("version,v", "print version string")
("help,h", "produce help message") ("help,h", "produce help message")
("config,c", boost::program_options::value<std::string>()->default_value("poolserver.cfg"),"name of a file of a configuration.") ("config,c", boost::program_options::value<std::string>()->default_value("poolserver.cfg"),"name of a file of a configuration.")
; ;
// Server // Server
descServer.add_options() descServer.add_options()
("MinDiffTime", boost::program_options::value<uint32_t>()->default_value(100), "Minimum server diff time") ("MinDiffTime", boost::program_options::value<uint32_t>()->default_value(100), "Minimum server diff time")
; ;
// Stratum // Stratum
descStratum.add_options() descStratum.add_options()
("StratumHost,sh", boost::program_options::value<std::string>()->default_value("0.0.0.0"), "Stratum server host") ("StratumHost,sh", boost::program_options::value<std::string>()->default_value("0.0.0.0"), "Stratum server host")
("StratumPort,sp", boost::program_options::value<uint16_t>()->default_value(3333), "Stratum server port") ("StratumPort,sp", boost::program_options::value<uint16_t>()->default_value(3333), "Stratum server port")
; ;
// Logging // Logging
descLogging.add_options() descLogging.add_options()
("LogConsoleLevel", boost::program_options::value<uint32_t>()->default_value(LOG_LEVEL_INFO), "Console log level (0-None, 1-Error, 2-Warn, 3-Info, 4-Debug)") ("LogConsoleLevel", boost::program_options::value<uint32_t>()->default_value(LOG_LEVEL_INFO), "Console log level (0-None, 1-Error, 2-Warn, 3-Info, 4-Debug)")
("LogConsoleDebugMask", boost::program_options::value<uint32_t>()->default_value(0), "Console log debug mask") ("LogConsoleDebugMask", boost::program_options::value<uint32_t>()->default_value(0), "Console log debug mask")
("LogFilePath", boost::program_options::value<std::string>()->default_value("."), "File log path") ("LogFilePath", boost::program_options::value<std::string>()->default_value("."), "File log path")
("LogFileLevel", boost::program_options::value<uint32_t>()->default_value(LOG_LEVEL_WARN), "File log level (0-None, 1-Error, 2-Warn, 3-Info, 4-Debug)") ("LogFileLevel", boost::program_options::value<uint32_t>()->default_value(LOG_LEVEL_WARN), "File log level (0-None, 1-Error, 2-Warn, 3-Info, 4-Debug)")
("LogFileDebugMask", boost::program_options::value<uint32_t>()->default_value(0), "File log debug mask") ("LogFileDebugMask", boost::program_options::value<uint32_t>()->default_value(0), "File log debug mask")
; ;
// Database // Database
@ -69,31 +69,31 @@ bool InitConfig(int argc, char *argv[])
store(boost::program_options::command_line_parser(argc, argv).options(cmdlineOptions).run(), sConfig.vm); store(boost::program_options::command_line_parser(argc, argv).options(cmdlineOptions).run(), sConfig.vm);
notify(sConfig.vm); notify(sConfig.vm);
if (sConfig.vm.count("help")) { if (sConfig.vm.count("help")) {
std::cout << cmdlineOptions << std::endl; std::cout << cmdlineOptions << std::endl;
return false; return false;
} }
std::ifstream ifs(sConfig.Get<std::string>("config").c_str()); std::ifstream ifs(sConfig.Get<std::string>("config").c_str());
if (!ifs.is_open()) { if (!ifs.is_open()) {
sLog.Error(LOG_GENERAL, "Failed opening config file: %s", sConfig.Get<std::string>("config").c_str()); sLog.Error(LOG_GENERAL, "Failed opening config file: %s", sConfig.Get<std::string>("config").c_str());
return false; return false;
} }
store(parse_config_file(ifs, fileOptions), sConfig.vm); store(parse_config_file(ifs, fileOptions), sConfig.vm);
notify(sConfig.vm); notify(sConfig.vm);
return true; return true;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (!InitConfig(argc, argv)) if (!InitConfig(argc, argv))
return 0; return 0;
sLog.OpenLogFile(sConfig.Get<std::string>("LogFilePath")); sLog.OpenLogFile(sConfig.Get<std::string>("LogFilePath"));
sLog.Info(LOG_GENERAL, "LogFile Started: %s", sLog.logFileLoc.c_str()); sLog.Info(LOG_GENERAL, "LogFile Started: %s", sLog.logFileLoc.c_str());
Server* server = new Server(); Server* server = new Server();
int exitcode = server->Run(); int exitcode = server->Run();

12
src/server/poolserver/Server/Server.cpp

@ -19,7 +19,7 @@ Server::~Server()
int Server::Run() int Server::Run()
{ {
sLog.Info(LOG_SERVER, "Server is starting..."); sLog.Info(LOG_SERVER, "Server is starting...");
InitDatabase(); InitDatabase();
@ -27,7 +27,7 @@ int Server::Run()
sDatabase.ExecuteAsync("INSERT INTO `test_table` VALUES ('999', 'sync', '1.1')"); sDatabase.ExecuteAsync("INSERT INTO `test_table` VALUES ('999', 'sync', '1.1')");
// Start stratum server // Start stratum server
sLog.Info(LOG_SERVER, "Starting stratum"); sLog.Info(LOG_SERVER, "Starting stratum");
//stratumServer = new Stratum::Server(Config::GetString("STRATUM_IP"), Config::GetInt("STRATUM_PORT")); //stratumServer = new Stratum::Server(Config::GetString("STRATUM_IP"), Config::GetInt("STRATUM_PORT"));
// Init loop vars // Init loop vars
@ -36,10 +36,10 @@ int Server::Run()
running = true; running = true;
// Init diff // Init diff
uint32_t minDiffTime = sConfig.Get<uint32_t>("MinDiffTime"); uint32_t minDiffTime = sConfig.Get<uint32_t>("MinDiffTime");
diffStart = boost::chrono::steady_clock::now(); diffStart = boost::chrono::steady_clock::now();
sLog.Info(LOG_SERVER, "Server is running!"); sLog.Info(LOG_SERVER, "Server is running!");
while (running) while (running)
{ {
@ -60,8 +60,8 @@ int Server::Run()
++serverLoops; ++serverLoops;
if (serverLoops > 50) if (serverLoops > 50)
running = false; running = false;
//std::cout << "Diff: " << diff << ", Loop: " << serverLoops << std::endl; //std::cout << "Diff: " << diff << ", Loop: " << serverLoops << std::endl;
} }

20
src/server/shared/Configuration/Config.h

@ -9,18 +9,18 @@
class Config class Config
{ {
public: public:
Config(); Config();
~Config(); ~Config();
// Reading // Reading
template<class T> template<class T>
T Get(std::string key) T Get(std::string key)
{ {
return vm[key].as<T>(); return vm[key].as<T>();
} }
// Containers // Containers
boost::program_options::variables_map vm; boost::program_options::variables_map vm;
}; };
extern Config sConfig; extern Config sConfig;

16
src/server/shared/Database/Database.h

@ -9,22 +9,22 @@
class Database class Database
{ {
public: public:
// Queries // Queries
virtual bool Execute(const char* query) = 0; virtual bool Execute(const char* query) = 0;
virtual ResultSet* Query(const char* query) = 0; virtual ResultSet* Query(const char* query) = 0;
// Stmt // Stmt
virtual bool Execute(PreparedStatement* stmt) = 0; virtual bool Execute(PreparedStatement* stmt) = 0;
virtual ResultSet* Query(PreparedStatement* stmt) = 0; virtual ResultSet* Query(PreparedStatement* stmt) = 0;
// Async // Async
virtual bool ExecuteAsync(const char* query) = 0; virtual bool ExecuteAsync(const char* query) = 0;
virtual bool ExecuteAsync(PreparedStatement* stmt) = 0; virtual bool ExecuteAsync(PreparedStatement* stmt) = 0;
virtual bool QueryAsync(DatabaseCallback callback, const char* query) = 0; virtual bool QueryAsync(DatabaseCallback callback, const char* query) = 0;
virtual bool QueryAsync(DatabaseCallback callback, PreparedStatement* stmt) = 0; virtual bool QueryAsync(DatabaseCallback callback, PreparedStatement* stmt) = 0;
// Prepared Statements // Prepared Statements
virtual PreparedStatement* GetPreparedStatement(uint32_t index) = 0; virtual PreparedStatement* GetPreparedStatement(uint32_t index) = 0;
}; };
#endif #endif

10
src/server/shared/Database/MySQL/DatabaseConnectionMySQL.h

@ -35,18 +35,18 @@ public:
void Close(); void Close();
// Ping! // Ping!
void Ping() void Ping()
{ {
mysql_ping(_mysql); mysql_ping(_mysql);
} }
// Queries // Queries
bool Execute(const char* query); bool Execute(const char* query);
ResultSetMySQL* Query(const char* query); ResultSetMySQL* Query(const char* query);
// Stmt // Stmt
bool Execute(PreparedStatement* stmt); bool Execute(PreparedStatement* stmt);
ResultSetMySQL* Query(PreparedStatement* stmt); ResultSetMySQL* Query(PreparedStatement* stmt);
// Locking // Locking
bool LockIfReady() bool LockIfReady()

14
src/server/shared/Database/MySQL/DatabaseWorkerPoolMySQL.h

@ -69,14 +69,14 @@ public:
} }
// Queries // Queries
bool Execute(const char* query) bool Execute(const char* query)
{ {
DatabaseConnectionMySQL* conn = GetSyncConnection(); DatabaseConnectionMySQL* conn = GetSyncConnection();
bool result = conn->Execute(query); bool result = conn->Execute(query);
conn->Unlock(); conn->Unlock();
return result; return result;
} }
ResultSetMySQL* Query(const char* query) ResultSetMySQL* Query(const char* query)
{ {
DatabaseConnectionMySQL* conn = GetSyncConnection(); DatabaseConnectionMySQL* conn = GetSyncConnection();
ResultSetMySQL* result = conn->Query(query); ResultSetMySQL* result = conn->Query(query);
@ -92,7 +92,7 @@ public:
conn->Unlock(); conn->Unlock();
return result; return result;
} }
ResultSetMySQL* Query(PreparedStatement* stmt) ResultSetMySQL* Query(PreparedStatement* stmt)
{ {
DatabaseConnectionMySQL* conn = GetSyncConnection(); DatabaseConnectionMySQL* conn = GetSyncConnection();
ResultSetMySQL* result = conn->Query(stmt); ResultSetMySQL* result = conn->Query(stmt);
@ -113,21 +113,21 @@ public:
_asyncQueue->Enqueue(op); _asyncQueue->Enqueue(op);
return true; return true;
} }
bool QueryAsync(DatabaseCallback callback, const char* query) bool QueryAsync(DatabaseCallback callback, const char* query)
{ {
DatabaseQueryOperationMySQL* op = new DatabaseQueryOperationMySQL(query, callback); DatabaseQueryOperationMySQL* op = new DatabaseQueryOperationMySQL(query, callback);
_asyncQueue->Enqueue(op); _asyncQueue->Enqueue(op);
return true; return true;
} }
bool QueryAsync(DatabaseCallback callback, PreparedStatement* stmt) bool QueryAsync(DatabaseCallback callback, PreparedStatement* stmt)
{ {
DatabasePreparedStatementOperationMySQL* op = new DatabasePreparedStatementOperationMySQL(stmt, callback); DatabasePreparedStatementOperationMySQL* op = new DatabasePreparedStatementOperationMySQL(stmt, callback);
_asyncQueue->Enqueue(op); _asyncQueue->Enqueue(op);
return true; return true;
} }
// Prepared Statements // Prepared Statements
PreparedStatement* GetPreparedStatement(uint32_t stmtid) PreparedStatement* GetPreparedStatement(uint32_t stmtid)
{ {
return NULL;//new PreparedStatement(stmtid); return NULL;//new PreparedStatement(stmtid);
} }

116
src/server/shared/Logging/Log.cpp

@ -10,109 +10,109 @@ Log::Log(): logfile(NULL)
Log::~Log() Log::~Log()
{ {
if (logfile) if (logfile)
logfile.close(); logfile.close();
} }
void Log::Error(LogType type, const char * str, ...) void Log::Error(LogType type, const char * str, ...)
{ {
va_list ap; va_list ap;
va_start(ap, str); va_start(ap, str);
char text[MAX_MSG_LEN]; char text[MAX_MSG_LEN];
vsnprintf(text, MAX_MSG_LEN, str, ap); vsnprintf(text, MAX_MSG_LEN, str, ap);
Write(LOG_LEVEL_ERROR, type, std::string(text)); Write(LOG_LEVEL_ERROR, type, std::string(text));
va_end(ap); va_end(ap);
} }
void Log::Warn(LogType type, const char * str, ...) void Log::Warn(LogType type, const char * str, ...)
{ {
va_list ap; va_list ap;
va_start(ap, str); va_start(ap, str);
char text[MAX_MSG_LEN]; char text[MAX_MSG_LEN];
vsnprintf(text, MAX_MSG_LEN, str, ap); vsnprintf(text, MAX_MSG_LEN, str, ap);
Write(LOG_LEVEL_WARN, type, std::string(text)); Write(LOG_LEVEL_WARN, type, std::string(text));
va_end(ap); va_end(ap);
} }
void Log::Info(LogType type, const char * str, ...) void Log::Info(LogType type, const char * str, ...)
{ {
va_list ap; va_list ap;
va_start(ap, str); va_start(ap, str);
char text[MAX_MSG_LEN]; char text[MAX_MSG_LEN];
vsnprintf(text, MAX_MSG_LEN, str, ap); vsnprintf(text, MAX_MSG_LEN, str, ap);
Write(LOG_LEVEL_INFO, type, std::string(text)); Write(LOG_LEVEL_INFO, type, std::string(text));
va_end(ap); va_end(ap);
} }
void Log::Debug(LogType type, const char * str, ...) void Log::Debug(LogType type, const char * str, ...)
{ {
va_list ap; va_list ap;
va_start(ap, str); va_start(ap, str);
char text[MAX_MSG_LEN]; char text[MAX_MSG_LEN];
vsnprintf(text, MAX_MSG_LEN, str, ap); vsnprintf(text, MAX_MSG_LEN, str, ap);
Write(LOG_LEVEL_DEBUG, type, std::string(text)); Write(LOG_LEVEL_DEBUG, type, std::string(text));
va_end(ap); va_end(ap);
} }
void Log::OpenLogFile(std::string path) void Log::OpenLogFile(std::string path)
{ {
if (!logfile) { if (!logfile) {
logFileLoc = path + "/server-" + Util::Date("%Y%m%d-%H%M%S") + ".log"; logFileLoc = path + "/server-" + Util::Date("%Y%m%d-%H%M%S") + ".log";
logfile.open(logFileLoc.c_str()); logfile.open(logFileLoc.c_str());
} }
} }
void Log::Write(LogLevel level, LogType type, std::string msg) void Log::Write(LogLevel level, LogType type, std::string msg)
{ {
boost::lock_guard<boost::mutex> lock(_mutex); boost::lock_guard<boost::mutex> lock(_mutex);
switch(level) switch(level)
{ {
case LOG_LEVEL_ERROR: case LOG_LEVEL_ERROR:
if (sConfig.Get<uint32_t>("LogConsoleLevel") >= level) if (sConfig.Get<uint32_t>("LogConsoleLevel") >= level)
std::cout << "[ERROR] " << msg << std::endl; std::cout << "[ERROR] " << msg << std::endl;
if (sConfig.Get<uint32_t>("LogFileLevel") >= level) if (sConfig.Get<uint32_t>("LogFileLevel") >= level)
AppendFile("[ERROR] " + msg); AppendFile("[ERROR] " + msg);
break; break;
case LOG_LEVEL_WARN: case LOG_LEVEL_WARN:
if (sConfig.Get<uint32_t>("LogConsoleLevel") >= level) if (sConfig.Get<uint32_t>("LogConsoleLevel") >= level)
std::cout << "[WARN] " << msg << std::endl; std::cout << "[WARN] " << msg << std::endl;
if (sConfig.Get<uint32_t>("LogFileLevel") >= level) if (sConfig.Get<uint32_t>("LogFileLevel") >= level)
AppendFile("[WARN] " + msg); AppendFile("[WARN] " + msg);
break; break;
case LOG_LEVEL_INFO: case LOG_LEVEL_INFO:
if (sConfig.Get<uint32_t>("LogConsoleLevel") >= level) if (sConfig.Get<uint32_t>("LogConsoleLevel") >= level)
std::cout << "[INFO] " << msg << std::endl; std::cout << "[INFO] " << msg << std::endl;
if (sConfig.Get<uint32_t>("LogFileLevel") >= level) if (sConfig.Get<uint32_t>("LogFileLevel") >= level)
AppendFile("[INFO] " + msg); AppendFile("[INFO] " + msg);
break; break;
case LOG_LEVEL_DEBUG: case LOG_LEVEL_DEBUG:
if (sConfig.Get<uint32_t>("LogConsoleLevel") >= level) { if (sConfig.Get<uint32_t>("LogConsoleLevel") >= level) {
uint32_t debugmask = sConfig.Get<uint32_t>("LogConsoleDebugMask"); uint32_t debugmask = sConfig.Get<uint32_t>("LogConsoleDebugMask");
if (debugmask & uint32_t(pow(2, type))) if (debugmask & uint32_t(pow(2, type)))
std::cout << "[DEBUG] " << msg << std::endl; std::cout << "[DEBUG] " << msg << std::endl;
} }
if (sConfig.Get<uint32_t>("LogFileLevel") >= level) { if (sConfig.Get<uint32_t>("LogFileLevel") >= level) {
uint32_t debugmask = sConfig.Get<uint32_t>("LogFileDebugMask"); uint32_t debugmask = sConfig.Get<uint32_t>("LogFileDebugMask");
if (debugmask & uint32_t(pow(2, type))) if (debugmask & uint32_t(pow(2, type)))
AppendFile("[DEBUG] " + msg); AppendFile("[DEBUG] " + msg);
} }
break; break;
} }
} }
void Log::AppendFile(std::string msg) void Log::AppendFile(std::string msg)
{ {
if (!logfile) if (!logfile)
return; return;
logfile << msg << std::endl; logfile << msg << std::endl;
} }

38
src/server/shared/Logging/Log.h

@ -14,40 +14,40 @@
enum LogType enum LogType
{ {
LOG_GENERAL = 0, LOG_GENERAL = 0,
LOG_SERVER = 1, LOG_SERVER = 1,
LOG_DATABASE = 2 LOG_DATABASE = 2
}; };
enum LogLevel enum LogLevel
{ {
LOG_LEVEL_NONE = 0, LOG_LEVEL_NONE = 0,
LOG_LEVEL_ERROR = 1, LOG_LEVEL_ERROR = 1,
LOG_LEVEL_WARN = 2, LOG_LEVEL_WARN = 2,
LOG_LEVEL_INFO = 3, LOG_LEVEL_INFO = 3,
LOG_LEVEL_DEBUG = 4 LOG_LEVEL_DEBUG = 4
}; };
class Log class Log
{ {
public: public:
Log(); Log();
~Log(); ~Log();
void Error(LogType type, const char * str, ...) ATTR_PRINTF(3, 4); void Error(LogType type, const char * str, ...) ATTR_PRINTF(3, 4);
void Warn(LogType type, const char * str, ...) ATTR_PRINTF(3, 4); void Warn(LogType type, const char * str, ...) ATTR_PRINTF(3, 4);
void Info(LogType type, const char * str, ...) ATTR_PRINTF(3, 4); void Info(LogType type, const char * str, ...) ATTR_PRINTF(3, 4);
void Debug(LogType type, const char * str, ...) ATTR_PRINTF(3, 4); void Debug(LogType type, const char * str, ...) ATTR_PRINTF(3, 4);
void OpenLogFile(std::string filename); void OpenLogFile(std::string filename);
std::string logFileLoc; std::string logFileLoc;
private: private:
void Write(LogLevel level, LogType type, std::string msg); void Write(LogLevel level, LogType type, std::string msg);
void AppendFile(std::string); void AppendFile(std::string);
std::string logfileloc; std::string logfileloc;
std::ofstream logfile; std::ofstream logfile;
boost::mutex _mutex; boost::mutex _mutex;
}; };

2
src/server/shared/Util.h

@ -10,7 +10,7 @@
namespace Util namespace Util
{ {
std::string Date(const char* format, bool utc = false); std::string Date(const char* format, bool utc = false);
template <typename T> template <typename T>
class SynchronisedQueue class SynchronisedQueue

Loading…
Cancel
Save