From be5c691c8081b66a78530d876cc5a224ca2cbef5 Mon Sep 17 00:00:00 2001 From: Intel Date: Tue, 23 Jul 2013 16:39:44 +0300 Subject: [PATCH] First push --- .gitignore | 17 ++ CMakeLists.txt | 43 +++++ cmake/macros/FindMySQL.cmake | 176 +++++++++++++++++++++ cmake/options.cmake | 4 + cmake/showoptions.cmake | 42 +++++ src/CMakeLists.txt | 6 + src/server/CMakeLists.txt | 11 ++ src/server/poolserver/CMakeLists.txt | 41 +++++ src/server/poolserver/Main.cpp | 83 ++++++++++ src/server/poolserver/Server/Server.cpp | 69 ++++++++ src/server/poolserver/Server/Server.h | 28 ++++ src/server/shared/CMakeLists.txt | 32 ++++ src/server/shared/Configuration/Config.cpp | 15 ++ src/server/shared/Configuration/Config.h | 28 ++++ src/server/shared/Database/CMakeLists.txt | 15 ++ src/server/shared/Database/database.cpp | 0 src/server/shared/Database/database.h | 33 ++++ src/server/shared/Logging/Log.cpp | 116 ++++++++++++++ src/server/shared/Logging/Log.h | 53 +++++++ src/server/shared/Stratum/CMakeLists.txt | 2 + src/server/shared/Stratum/Protocol.h | 0 src/server/shared/Stratum/Server.cpp | 6 + src/server/shared/Stratum/Server.h | 20 +++ src/server/shared/Stratum/session.cpp | 0 src/server/shared/Stratum/session.h | 0 src/server/shared/Util.h | 27 ++++ src/server/statsserver/CMakeLists.txt | 0 27 files changed, 867 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 cmake/macros/FindMySQL.cmake create mode 100644 cmake/options.cmake create mode 100644 cmake/showoptions.cmake create mode 100644 src/CMakeLists.txt create mode 100644 src/server/CMakeLists.txt create mode 100644 src/server/poolserver/CMakeLists.txt create mode 100644 src/server/poolserver/Main.cpp create mode 100644 src/server/poolserver/Server/Server.cpp create mode 100644 src/server/poolserver/Server/Server.h create mode 100644 src/server/shared/CMakeLists.txt create mode 100644 src/server/shared/Configuration/Config.cpp create mode 100644 src/server/shared/Configuration/Config.h create mode 100644 src/server/shared/Database/CMakeLists.txt create mode 100644 src/server/shared/Database/database.cpp create mode 100644 src/server/shared/Database/database.h create mode 100644 src/server/shared/Logging/Log.cpp create mode 100644 src/server/shared/Logging/Log.h create mode 100644 src/server/shared/Stratum/CMakeLists.txt create mode 100644 src/server/shared/Stratum/Protocol.h create mode 100644 src/server/shared/Stratum/Server.cpp create mode 100644 src/server/shared/Stratum/Server.h create mode 100644 src/server/shared/Stratum/session.cpp create mode 100644 src/server/shared/Stratum/session.h create mode 100644 src/server/shared/Util.h create mode 100644 src/server/statsserver/CMakeLists.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bf03687 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +build*/ +.directory +.mailmap +*.orig +*.rej +*~ +.hg/ +*.kdev* +.DS_Store +CMakeLists.txt.user +*.bak +*.patch +*.diff +*.REMOTE.* +*.BACKUP.* +*.BASE.* +*.LOCAL.* \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..86af6d2 --- /dev/null +++ b/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/cmake/macros/FindMySQL.cmake b/cmake/macros/FindMySQL.cmake new file mode 100644 index 0000000..55c889e --- /dev/null +++ b/cmake/macros/FindMySQL.cmake @@ -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 ) \ No newline at end of file diff --git a/cmake/options.cmake b/cmake/options.cmake new file mode 100644 index 0000000..7417071 --- /dev/null +++ b/cmake/options.cmake @@ -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) diff --git a/cmake/showoptions.cmake b/cmake/showoptions.cmake new file mode 100644 index 0000000..424e59b --- /dev/null +++ b/cmake/showoptions.cmake @@ -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("") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..751074f --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,6 @@ + +add_subdirectory(server) + +if(TOOLS) + add_subdirectory(tools) +endif(TOOLS) \ No newline at end of file diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt new file mode 100644 index 0000000..3e4d933 --- /dev/null +++ b/src/server/CMakeLists.txt @@ -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() \ No newline at end of file diff --git a/src/server/poolserver/CMakeLists.txt b/src/server/poolserver/CMakeLists.txt new file mode 100644 index 0000000..9dde2f0 --- /dev/null +++ b/src/server/poolserver/CMakeLists.txt @@ -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() diff --git a/src/server/poolserver/Main.cpp b/src/server/poolserver/Main.cpp new file mode 100644 index 0000000..3a1f828 --- /dev/null +++ b/src/server/poolserver/Main.cpp @@ -0,0 +1,83 @@ +#include + +#include "Server.h" +#include "Config.h" +#include "Log.h" +#include +#include + +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()->default_value("settings.cfg"),"name of a file of a configuration.") + ; + + // Server + descServer.add_options() + ("MinDiffTime", boost::program_options::value()->default_value(100), "Minimum server diff time") + ; + + // Stratum + descStratum.add_options() + ("StratumHost,sh", boost::program_options::value()->default_value("0.0.0.0"), "Stratum server host") + ("StratumPort,sp", boost::program_options::value()->default_value(3333), "Stratum server port") + ; + + // Logging + descLogging.add_options() + ("LogConsoleLevel", boost::program_options::value()->default_value(LOG_LEVEL_INFO), "Console log level (0-None, 1-Error, 2-Warn, 3-Info, 4-Debug)") + ("LogConsoleDebugMask", boost::program_options::value()->default_value(0), "Console log debug mask") + ("LogFilePath", boost::program_options::value()->default_value("."), "File log path") + ("LogFileLevel", boost::program_options::value()->default_value(LOG_LEVEL_WARN), "File log level (0-None, 1-Error, 2-Warn, 3-Info, 4-Debug)") + ("LogFileDebugMask", boost::program_options::value()->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("LogFilePath").c_str()); + + if (!ifs) { + sLog.Error(LOG_GENERAL, "Failed opening config file: %s", sConfig.Get("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("LogFilePath")); + sLog.Info(LOG_GENERAL, "LogFile Started: %s", sLog.logFileLoc.c_str()); + + Server* server = new Server(); + int exitcode = server->Run(); + delete server; + + return exitcode; +} diff --git a/src/server/poolserver/Server/Server.cpp b/src/server/poolserver/Server/Server.cpp new file mode 100644 index 0000000..96b66e4 --- /dev/null +++ b/src/server/poolserver/Server/Server.cpp @@ -0,0 +1,69 @@ +#include "Server.h" +#include "Config.h" +#include "Log.h" +#include "Stratum/Server.h" + +#include +#include + +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("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(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) +{ + +} diff --git a/src/server/poolserver/Server/Server.h b/src/server/poolserver/Server/Server.h new file mode 100644 index 0000000..2da48bb --- /dev/null +++ b/src/server/poolserver/Server/Server.h @@ -0,0 +1,28 @@ +#ifndef SERVER_H +#define SERVER_H + +#include "Stratum/Server.h" + +#include +#include +#include + +#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 diff --git a/src/server/shared/CMakeLists.txt b/src/server/shared/CMakeLists.txt new file mode 100644 index 0000000..8daf97f --- /dev/null +++ b/src/server/shared/CMakeLists.txt @@ -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} +) \ No newline at end of file diff --git a/src/server/shared/Configuration/Config.cpp b/src/server/shared/Configuration/Config.cpp new file mode 100644 index 0000000..fffb803 --- /dev/null +++ b/src/server/shared/Configuration/Config.cpp @@ -0,0 +1,15 @@ +#include "Config.h" +#include "Log.h" + +#include +#include + +Config sConfig; + +Config::Config() +{ +} + +Config::~Config() +{ +} \ No newline at end of file diff --git a/src/server/shared/Configuration/Config.h b/src/server/shared/Configuration/Config.h new file mode 100644 index 0000000..a4c0e73 --- /dev/null +++ b/src/server/shared/Configuration/Config.h @@ -0,0 +1,28 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include +#include +#include +#include + +class Config +{ +public: + Config(); + ~Config(); + + // Reading + template + T Get(std::string key) + { + return vm[key].as(); + } + + // Containers + boost::program_options::variables_map vm; +}; + +extern Config sConfig; + +#endif diff --git a/src/server/shared/Database/CMakeLists.txt b/src/server/shared/Database/CMakeLists.txt new file mode 100644 index 0000000..d353b8d --- /dev/null +++ b/src/server/shared/Database/CMakeLists.txt @@ -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() \ No newline at end of file diff --git a/src/server/shared/Database/database.cpp b/src/server/shared/Database/database.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/server/shared/Database/database.h b/src/server/shared/Database/database.h new file mode 100644 index 0000000..3ec4c53 --- /dev/null +++ b/src/server/shared/Database/database.h @@ -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 \ No newline at end of file diff --git a/src/server/shared/Logging/Log.cpp b/src/server/shared/Logging/Log.cpp new file mode 100644 index 0000000..fa1ee53 --- /dev/null +++ b/src/server/shared/Logging/Log.cpp @@ -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("LogConsoleLevel") >= level) + std::cout << "[ERROR] " << msg << std::endl; + if (sConfig.Get("LogFileLevel") >= level) + AppendFile("[ERROR] " + msg); + break; + case LOG_LEVEL_WARN: + if (sConfig.Get("LogConsoleLevel") >= level) + std::cout << "[WARN] " << msg << std::endl; + if (sConfig.Get("LogFileLevel") >= level) + AppendFile("[WARN] " + msg); + break; + case LOG_LEVEL_INFO: + if (sConfig.Get("LogConsoleLevel") >= level) + std::cout << "[INFO] " << msg << std::endl; + if (sConfig.Get("LogFileLevel") >= level) + AppendFile("[INFO] " + msg); + break; + case LOG_LEVEL_DEBUG: + if (sConfig.Get("LogConsoleLevel") >= level) { + uint32_t debugmask = sConfig.Get("LogConsoleDebugMask"); + if (debugmask & uint32_t(pow(2, type))) + std::cout << "[DEBUG] " << msg << std::endl; + } + if (sConfig.Get("LogFileLevel") >= level) { + uint32_t debugmask = sConfig.Get("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; +} \ No newline at end of file diff --git a/src/server/shared/Logging/Log.h b/src/server/shared/Logging/Log.h new file mode 100644 index 0000000..ae3c7dd --- /dev/null +++ b/src/server/shared/Logging/Log.h @@ -0,0 +1,53 @@ +#ifndef LOG_H +#define LOG_H + +#include +#include +#include +#include +#include +#include + +#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 diff --git a/src/server/shared/Stratum/CMakeLists.txt b/src/server/shared/Stratum/CMakeLists.txt new file mode 100644 index 0000000..0af5306 --- /dev/null +++ b/src/server/shared/Stratum/CMakeLists.txt @@ -0,0 +1,2 @@ +# Add sources +file(GLOB sources_stratum *.cpp *.h) \ No newline at end of file diff --git a/src/server/shared/Stratum/Protocol.h b/src/server/shared/Stratum/Protocol.h new file mode 100644 index 0000000..e69de29 diff --git a/src/server/shared/Stratum/Server.cpp b/src/server/shared/Stratum/Server.cpp new file mode 100644 index 0000000..6e4379c --- /dev/null +++ b/src/server/shared/Stratum/Server.cpp @@ -0,0 +1,6 @@ +#include "Server.h" + +Stratum::Server::Server(std::string ip, uint32_t port) +{ + +} diff --git a/src/server/shared/Stratum/Server.h b/src/server/shared/Stratum/Server.h new file mode 100644 index 0000000..7e62420 --- /dev/null +++ b/src/server/shared/Stratum/Server.h @@ -0,0 +1,20 @@ +#ifndef STRATUM_SERVER_H +#define STRATUM_SERVER_H + +#include +#include + +namespace Stratum +{ + class Server + { + public: + Server(std::string ip, uint32_t port); + ~Server(); + + private: + + }; +} + +#endif diff --git a/src/server/shared/Stratum/session.cpp b/src/server/shared/Stratum/session.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/server/shared/Stratum/session.h b/src/server/shared/Stratum/session.h new file mode 100644 index 0000000..e69de29 diff --git a/src/server/shared/Util.h b/src/server/shared/Util.h new file mode 100644 index 0000000..8c76ee1 --- /dev/null +++ b/src/server/shared/Util.h @@ -0,0 +1,27 @@ +#ifndef UTIL_H +#define UTIL_H + +#include +#include +#include + +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 \ No newline at end of file diff --git a/src/server/statsserver/CMakeLists.txt b/src/server/statsserver/CMakeLists.txt new file mode 100644 index 0000000..e69de29