Browse Source

First push

peercoin
Intel 12 years ago committed by root
commit
be5c691c80
  1. 17
      .gitignore
  2. 43
      CMakeLists.txt
  3. 176
      cmake/macros/FindMySQL.cmake
  4. 4
      cmake/options.cmake
  5. 42
      cmake/showoptions.cmake
  6. 6
      src/CMakeLists.txt
  7. 11
      src/server/CMakeLists.txt
  8. 41
      src/server/poolserver/CMakeLists.txt
  9. 83
      src/server/poolserver/Main.cpp
  10. 69
      src/server/poolserver/Server/Server.cpp
  11. 28
      src/server/poolserver/Server/Server.h
  12. 32
      src/server/shared/CMakeLists.txt
  13. 15
      src/server/shared/Configuration/Config.cpp
  14. 28
      src/server/shared/Configuration/Config.h
  15. 15
      src/server/shared/Database/CMakeLists.txt
  16. 0
      src/server/shared/Database/database.cpp
  17. 33
      src/server/shared/Database/database.h
  18. 116
      src/server/shared/Logging/Log.cpp
  19. 53
      src/server/shared/Logging/Log.h
  20. 2
      src/server/shared/Stratum/CMakeLists.txt
  21. 0
      src/server/shared/Stratum/Protocol.h
  22. 6
      src/server/shared/Stratum/Server.cpp
  23. 20
      src/server/shared/Stratum/Server.h
  24. 0
      src/server/shared/Stratum/session.cpp
  25. 0
      src/server/shared/Stratum/session.h
  26. 27
      src/server/shared/Util.h
  27. 0
      src/server/statsserver/CMakeLists.txt

17
.gitignore vendored

@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
build*/
.directory
.mailmap
*.orig
*.rej
*~
.hg/
*.kdev*
.DS_Store
CMakeLists.txt.user
*.bak
*.patch
*.diff
*.REMOTE.*
*.BACKUP.*
*.BASE.*
*.LOCAL.*

43
CMakeLists.txt

@ -0,0 +1,43 @@ @@ -0,0 +1,43 @@
# Project name!
project(PoolServer)
# CMake policies
cmake_minimum_required(VERSION 2.6)
# Set macros
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/macros")
# build in Release-mode by default if not explicitly set
if( NOT CMAKE_BUILD_TYPE )
set(CMAKE_BUILD_TYPE "Release")
endif()
# Install path
if( PREFIX )
set(CMAKE_INSTALL_PREFIX "${PREFIX}")
endif()
# Build options
include(cmake/options.cmake)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_ALL_DYN_LINK ON)
# Boost
find_package(Boost 1.53 COMPONENTS thread chrono program_options date_time REQUIRED)
message(status "** Boost Include: ${Boost_INCLUDE_DIR}")
message(status "** Boost Libraries: ${Boost_LIBRARY_DIRS}")
message(status "** Boost Libraries: ${Boost_LIBRARIES}")
# Mysql
if( MYSQL )
find_package(MySQL REQUIRED)
endif()
# Print options
include(cmake/showoptions.cmake)
# Add sources
add_subdirectory(src)

176
cmake/macros/FindMySQL.cmake

@ -0,0 +1,176 @@ @@ -0,0 +1,176 @@
#
# Find the MySQL client includes and library
#
# This module defines
# MYSQL_INCLUDE_DIR, where to find mysql.h
# MYSQL_LIBRARIES, the libraries to link against to connect to MySQL
# MYSQL_FOUND, if false, you cannot build anything that requires MySQL.
# also defined, but not for general use are
# MYSQL_LIBRARY, where to find the MySQL library.
set( MYSQL_FOUND 0 )
if( UNIX )
set(MYSQL_CONFIG_PREFER_PATH "$ENV{MYSQL_HOME}/bin" CACHE FILEPATH
"preferred path to MySQL (mysql_config)"
)
find_program(MYSQL_CONFIG mysql_config
${MYSQL_CONFIG_PREFER_PATH}
/usr/local/mysql/bin/
/usr/local/bin/
/usr/bin/
)
if( MYSQL_CONFIG )
message(STATUS "Using mysql-config: ${MYSQL_CONFIG}")
# set INCLUDE_DIR
exec_program(${MYSQL_CONFIG}
ARGS --include
OUTPUT_VARIABLE MY_TMP
)
string(REGEX REPLACE "-I([^ ]*)( .*)?" "\\1" MY_TMP "${MY_TMP}")
set(MYSQL_ADD_INCLUDE_PATH ${MY_TMP} CACHE FILEPATH INTERNAL)
#message("[DEBUG] MYSQL ADD_INCLUDE_PATH : ${MYSQL_ADD_INCLUDE_PATH}")
# set LIBRARY_DIR
exec_program(${MYSQL_CONFIG}
ARGS --libs_r
OUTPUT_VARIABLE MY_TMP
)
set(MYSQL_ADD_LIBRARIES "")
string(REGEX MATCHALL "-l[^ ]*" MYSQL_LIB_LIST "${MY_TMP}")
foreach(LIB ${MYSQL_LIB_LIST})
string(REGEX REPLACE "[ ]*-l([^ ]*)" "\\1" LIB "${LIB}")
list(APPEND MYSQL_ADD_LIBRARIES "${LIB}")
#message("[DEBUG] MYSQL ADD_LIBRARIES : ${MYSQL_ADD_LIBRARIES}")
endforeach(LIB ${MYSQL_LIB_LIST})
set(MYSQL_ADD_LIBRARIES_PATH "")
string(REGEX MATCHALL "-L[^ ]*" MYSQL_LIBDIR_LIST "${MY_TMP}")
foreach(LIB ${MYSQL_LIBDIR_LIST})
string(REGEX REPLACE "[ ]*-L([^ ]*)" "\\1" LIB "${LIB}")
list(APPEND MYSQL_ADD_LIBRARIES_PATH "${LIB}")
#message("[DEBUG] MYSQL ADD_LIBRARIES_PATH : ${MYSQL_ADD_LIBRARIES_PATH}")
endforeach(LIB ${MYSQL_LIBS})
else( MYSQL_CONFIG )
set(MYSQL_ADD_LIBRARIES "")
list(APPEND MYSQL_ADD_LIBRARIES "mysqlclient_r")
endif( MYSQL_CONFIG )
endif( UNIX )
find_path(MYSQL_INCLUDE_DIR
NAMES
mysql.h
PATHS
${MYSQL_ADD_INCLUDE_PATH}
/usr/include
/usr/include/mysql
/usr/local/include
/usr/local/include/mysql
/usr/local/mysql/include
"C:/Program Files/MySQL/MySQL Server 5.6/include"
"C:/Program Files/MySQL/MySQL Server 5.5/include"
"C:/Program Files/MySQL/MySQL Server 5.1/include"
"C:/Program Files/MySQL/MySQL Server 5.0/include"
"C:/Program Files/MySQL/include"
"C:/MySQL/include"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.6;Location]/include"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.5;Location]/include"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.1;Location]/include"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.0;Location]/include"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.6;Location]/include"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.5;Location]/include"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.1;Location]/include"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.0;Location]/include"
"$ENV{ProgramFiles}/MySQL/*/include"
"$ENV{SystemDrive}/MySQL/*/include"
"c:/msys/local/include"
DOC
"Specify the directory containing mysql.h."
)
if( UNIX )
foreach(LIB ${MYSQL_ADD_LIBRARIES})
find_library( MYSQL_LIBRARY
NAMES
mysql libmysql ${LIB}
PATHS
${MYSQL_ADD_LIBRARIES_PATH}
/usr/lib
/usr/lib/mysql
/usr/local/lib
/usr/local/lib/mysql
/usr/local/mysql/lib
DOC "Specify the location of the mysql library here."
)
endforeach(LIB ${MYSQL_ADD_LIBRARY})
endif( UNIX )
if( WIN32 )
find_library( MYSQL_LIBRARY
NAMES
libmysql
PATHS
${MYSQL_ADD_LIBRARIES_PATH}
"C:/Program Files/MySQL/MySQL Server 5.6/lib/opt"
"C:/Program Files/MySQL/MySQL Server 5.5/lib/opt"
"C:/Program Files/MySQL/MySQL Server 5.1/lib/opt"
"C:/Program Files/MySQL/MySQL Server 5.0/lib/opt"
"C:/Program Files/MySQL/lib"
"C:/MySQL/lib/debug"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.6;Location]/lib"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.6;Location]/lib/opt"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.5;Location]/lib"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.5;Location]/lib/opt"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.1;Location]/lib"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.1;Location]/lib/opt"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.0;Location]/lib"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server 5.0;Location]/lib/opt"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.6;Location]/lib"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.6;Location]/lib/opt"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.5;Location]/lib"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.5;Location]/lib/opt"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.1;Location]/lib"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.1;Location]/lib/opt"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.0;Location]/lib"
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server 5.0;Location]/lib/opt"
"$ENV{ProgramFiles}/MySQL/*/lib/opt"
"$ENV{SystemDrive}/MySQL/*/lib/opt"
"c:/msys/local/include"
DOC "Specify the location of the mysql library here."
)
endif( WIN32 )
# On Windows you typically don't need to include any extra libraries
# to build MYSQL stuff.
if( NOT WIN32 )
find_library( MYSQL_EXTRA_LIBRARIES
NAMES
z zlib
PATHS
/usr/lib
/usr/local/lib
DOC
"if more libraries are necessary to link in a MySQL client (typically zlib), specify them here."
)
else( NOT WIN32 )
set( MYSQL_EXTRA_LIBRARIES "" )
endif( NOT WIN32 )
if( MYSQL_LIBRARY )
if( MYSQL_INCLUDE_DIR )
set( MYSQL_FOUND 1 )
message(STATUS "Found MySQL library: ${MYSQL_LIBRARY}")
message(STATUS "Found MySQL headers: ${MYSQL_INCLUDE_DIR}")
else( MYSQL_INCLUDE_DIR )
message(FATAL_ERROR "Could not find MySQL headers! Please install the development libraries and headers")
endif( MYSQL_INCLUDE_DIR )
mark_as_advanced( MYSQL_FOUND MYSQL_LIBRARY MYSQL_EXTRA_LIBRARIES MYSQL_INCLUDE_DIR )
else( MYSQL_LIBRARY )
message(FATAL_ERROR "Could not find the MySQL libraries! Please install the development libraries and headers")
endif( MYSQL_LIBRARY )

4
cmake/options.cmake

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
option(POOLSERVER "Build Pool Server" 1)
option(STRATUM "Build Pool Server with Stratum" 1)
option(STATSSERVER "Build Stats Server" 1)
option(MYSQL "Use MySQL database" 1)

42
cmake/showoptions.cmake

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
# output generic information about the core and buildtype chosen
message("")
# message("* PoolServer revision : ${rev_hash} ${rev_date} (${rev_branch} branch)")
if( UNIX )
message("* Buildtype : ${CMAKE_BUILD_TYPE}")
endif()
message("")
# output information about installation-directories and locations
message("* Install core to : ${CMAKE_INSTALL_PREFIX}")
message("")
# Show infomation about the options selected during configuration
if(POOLSERVER)
message("* Build Pool Server : Yes (default)")
else()
message("* Build Pool Server : No")
endif()
if(STRATUM)
message("* Build with Stratum : Yes (default)")
add_definitions(-DWITH_STRATUM)
else()
message("* Build with Stratum : No")
endif()
if(STATSSERVER)
message("* Build Stats Server : Yes (default)")
else()
message("* Build Stats Server : No")
endif()
if( MYSQL )
message("* Use MySQL database : Yes (default)")
add_definitions(-DWITH_MYSQL)
else()
message("* Use MySQL database : No")
endif()
message("")

6
src/CMakeLists.txt

@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
add_subdirectory(server)
if(TOOLS)
add_subdirectory(tools)
endif(TOOLS)

11
src/server/CMakeLists.txt

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
if(POOLSERVER)
add_subdirectory(shared)
add_subdirectory(poolserver)
endif()
if(STATSSERVER)
if(NOT POOLSERVER)
add_subdirectory(shared)
endif()
add_subdirectory(statsserver)
endif()

41
src/server/poolserver/CMakeLists.txt

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
# Add sources
file(GLOB_RECURSE sources_Server Server/*.cpp Server/*.h)
file(GLOB sources_localdir *.cpp *.h)
set(sources_Poolserver
${sources_Server}
${sources_localdir}
)
include_directories(
${CMAKE_SOURCE_DIR}/src/server/shared
${CMAKE_SOURCE_DIR}/src/server/shared/Configuration
${CMAKE_SOURCE_DIR}/src/server/shared/Database
${CMAKE_SOURCE_DIR}/src/server/shared/Logging
${CMAKE_SOURCE_DIR}/src/server/poolserver/Server
${Boost_INCLUDE_DIR}
${MYSQL_INCLUDE_DIR}
)
# Create executable
add_executable(poolserver
${sources_Poolserver}
)
message(status "SHared files: ${sources_Shared}")
# Link libraries
target_link_libraries(poolserver
shared
${MYSQL_LIBRARY}
${Boost_LIBRARIES}
/usr/local/lib/libboost_thread.so.1.53.0
/usr/local/lib/libboost_program_options.so
)
# Install
if(UNIX)
install(TARGETS poolserver DESTINATION bin)
elseif(WIN32)
install(TARGETS poolserver DESTINATION "${CMAKE_INSTALL_PREFIX}")
endif()

83
src/server/poolserver/Main.cpp

@ -0,0 +1,83 @@ @@ -0,0 +1,83 @@
#include <iostream>
#include "Server.h"
#include "Config.h"
#include "Log.h"
#include <boost/program_options.hpp>
#include <iostream>
bool InitConfig(int argc, char *argv[])
{
// Containers
boost::program_options::options_description descGeneric;
boost::program_options::options_description descServer;
boost::program_options::options_description descStratum;
boost::program_options::options_description descLogging;
boost::program_options::options_description cmdlineOptions;
boost::program_options::options_description fileOptions;
// Generic
descGeneric.add_options()
("version,v", "print version string")
("help,h", "produce help message")
("config,c", boost::program_options::value<std::string>()->default_value("settings.cfg"),"name of a file of a configuration.")
;
// Server
descServer.add_options()
("MinDiffTime", boost::program_options::value<uint32_t>()->default_value(100), "Minimum server diff time")
;
// Stratum
descStratum.add_options()
("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")
;
// Logging
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)")
("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")
("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")
;
cmdlineOptions.add(descGeneric).add(descServer).add(descStratum).add(descLogging);
fileOptions.add(descServer).add(descStratum).add(descLogging);
store(boost::program_options::command_line_parser(argc, argv).options(cmdlineOptions).run(), sConfig.vm);
notify(sConfig.vm);
if (sConfig.vm.count("help")) {
std::cout << cmdlineOptions << std::endl;
return false;
}
std::ifstream ifs(sConfig.Get<std::string>("LogFilePath").c_str());
if (!ifs) {
sLog.Error(LOG_GENERAL, "Failed opening config file: %s", sConfig.Get<std::string>("LogFilePath").c_str());
return false;
}
store(parse_config_file(ifs, fileOptions), sConfig.vm);
notify(sConfig.vm);
return true;
}
int main(int argc, char *argv[])
{
if (!InitConfig(argc, argv))
return 0;
sLog.OpenLogFile(sConfig.Get<std::string>("LogFilePath"));
sLog.Info(LOG_GENERAL, "LogFile Started: %s", sLog.logFileLoc.c_str());
Server* server = new Server();
int exitcode = server->Run();
delete server;
return exitcode;
}

69
src/server/poolserver/Server/Server.cpp

@ -0,0 +1,69 @@ @@ -0,0 +1,69 @@
#include "Server.h"
#include "Config.h"
#include "Log.h"
#include "Stratum/Server.h"
#include <boost/thread.hpp>
#include <iostream>
Server::Server() : serverLoops(0)
{
}
Server::~Server()
{
//delete stratumServer;
}
int Server::Run()
{
sLog.Info(LOG_SERVER, "Server is starting...");
// Start stratum server
sLog.Info(LOG_SERVER, "Starting stratum");
//stratumServer = new Stratum::Server(Config::GetString("STRATUM_IP"), Config::GetInt("STRATUM_PORT"));
// Init loop vars
uint32_t sleepDuration = 0;
int exitcode = 0;
running = true;
// Init diff
uint32_t minDiffTime = sConfig.Get<uint32_t>("MinDiffTime");
diffStart = boost::chrono::steady_clock::now();
sLog.Info(LOG_SERVER, "Server is running!");
while (running)
{
// Calc time diff
boost::chrono::steady_clock::time_point now = boost::chrono::steady_clock::now();
uint32_t diff = boost::chrono::duration_cast<boost::chrono::milliseconds>(now - diffStart).count();
diffStart = now;
// Update
Update(diff);
// Mercy for CPU
if (diff < minDiffTime+sleepDuration) {
sleepDuration = minDiffTime - diff + sleepDuration;
boost::this_thread::sleep_for(boost::chrono::milliseconds(sleepDuration));
} else
sleepDuration = 0;
++serverLoops;
if (serverLoops > 50)
running = false;
//std::cout << "Diff: " << diff << ", Loop: " << serverLoops << std::endl;
}
sLog.Info(LOG_SERVER, "Server is stopping...");
return exitcode;
}
void Server::Update(uint32_t diff)
{
}

28
src/server/poolserver/Server/Server.h

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
#ifndef SERVER_H
#define SERVER_H
#include "Stratum/Server.h"
#include <boost/thread.hpp>
#include <boost/cstdint.hpp>
#include <boost/chrono.hpp>
#define SERVER_MIN_DIFF 100
class Server
{
public:
Server();
~Server();
Stratum::Server* stratumServer;
boost::chrono::steady_clock::time_point diffStart;
bool running;
uint64_t serverLoops;
int Run();
void Update(uint32_t);
};
#endif

32
src/server/shared/CMakeLists.txt

@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
add_subdirectory(Database)
file(GLOB_RECURSE sources_Configuration Configuration/*.cpp Configuration/*.h)
file(GLOB_RECURSE sources_Logging Logging/*.cpp Logging/*.h)
file(GLOB_RECURSE sources_Stratum Stratum/*.cpp Stratum/*.h)
file(GLOB sources_localdir *.cpp *.h)
set(sources_Shared
${sources_Configuration}
${sources_Database}
${sources_Logging}
${sources_Stratum}
${sources_localdir}
)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/Configuration
${CMAKE_CURRENT_SOURCE_DIR}/Database
${CMAKE_CURRENT_SOURCE_DIR}/Logging
${MYSQL_INCLUDE_DIR}
)
add_library(shared STATIC
${sources_Shared}
)
target_link_libraries(shared
${ACE_LIBRARY}
)

15
src/server/shared/Configuration/Config.cpp

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
#include "Config.h"
#include "Log.h"
#include <fstream>
#include <iostream>
Config sConfig;
Config::Config()
{
}
Config::~Config()
{
}

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

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
#ifndef CONFIG_H
#define CONFIG_H
#include <cassert>
#include <cstring>
#include <boost/program_options.hpp>
#include <boost/cstdint.hpp>
class Config
{
public:
Config();
~Config();
// Reading
template<class T>
T Get(std::string key)
{
return vm[key].as<T>();
}
// Containers
boost::program_options::variables_map vm;
};
extern Config sConfig;
#endif

15
src/server/shared/Database/CMakeLists.txt

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
file(GLOB sources_MySQL MySQL/*.cpp MySQL/*.h)
file(GLOB sources_localdir *.cpp *.h)
set(sources_Database
${sources_localdir}
)
if(MYSQL)
set(sources_Database
${sources_Database}
${sources_MySQL}
${MYSQL_INCLUDE_DIR}
)
endif()

0
src/server/shared/Database/database.cpp

33
src/server/shared/Database/database.h

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
#ifndef DATABASE_H
#define DATABASE_H
class Database;
class ResultSet;
class Fields;
class PreparedStatement;
enum PreparedStatementEnum
{
STMT_INSERT_SHARE = 1,
};
class Database
{
public:
// Ping!
virtual Ping();
// Queries
virtual void Execute(const char* query);
virtual void Execute(PreparedStatement* stmt);
virtual ResultSet* Query(const char* query);
virtual ResultSet* Query(PreparedStatement* stmt);
// Prepared Statements
virtual PreparedStatement* GetPreparedStatement(PreparedStatementEnum smtid);
};
#endif

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

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

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

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
#ifndef LOG_H
#define LOG_H
#include <string>
#include <fstream>
#include <iostream>
#include <cmath>
#include <cstdarg>
#include <boost/cstdint.hpp>
#define MAX_MSG_LEN 32*1024
#define ATTR_PRINTF(F, V) __attribute__ ((format (printf, F, V)))
enum LogType
{
LOG_GENERAL = 0,
LOG_SERVER = 2,
};
enum LogLevel
{
LOG_LEVEL_NONE = 0,
LOG_LEVEL_ERROR = 1,
LOG_LEVEL_WARN = 2,
LOG_LEVEL_INFO = 3,
LOG_LEVEL_DEBUG = 4
};
class Log
{
public:
Log();
~Log();
void Error(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 Debug(LogType type, const char * str, ...) ATTR_PRINTF(3, 4);
void OpenLogFile(std::string filename);
std::string logFileLoc;
private:
void Write(LogLevel level, LogType type, std::string msg);
void AppendFile(std::string);
std::string logfileloc;
std::ofstream logfile;
};
extern Log sLog;
#endif

2
src/server/shared/Stratum/CMakeLists.txt

@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
# Add sources
file(GLOB sources_stratum *.cpp *.h)

0
src/server/shared/Stratum/Protocol.h

6
src/server/shared/Stratum/Server.cpp

@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
#include "Server.h"
Stratum::Server::Server(std::string ip, uint32_t port)
{
}

20
src/server/shared/Stratum/Server.h

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
#ifndef STRATUM_SERVER_H
#define STRATUM_SERVER_H
#include <boost/cstdint.hpp>
#include <string>
namespace Stratum
{
class Server
{
public:
Server(std::string ip, uint32_t port);
~Server();
private:
};
}
#endif

0
src/server/shared/Stratum/session.cpp

0
src/server/shared/Stratum/session.h

27
src/server/shared/Util.h

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
#ifndef UTIL_H
#define UTIL_H
#include <sstream>
#include <iostream>
#include <boost/date_time.hpp>
namespace Util
{
std::string Date(const char* format, bool utc = false)
{
boost::posix_time::ptime now;
if (utc)
now = boost::posix_time::second_clock::universal_time();
else
now = boost::posix_time::second_clock::local_time();
std::stringstream ss;
boost::posix_time::time_facet *facet = new boost::posix_time::time_facet(format);
ss.imbue(std::locale(std::cout.getloc(), facet));
ss << now;
return ss.str();
}
}
#endif

0
src/server/statsserver/CMakeLists.txt

Loading…
Cancel
Save