Horizon Official Technical Documentation
Server Class Reference

#include <Server.hpp>

+ Inheritance diagram for Server:
+ Collaboration diagram for Server:

Public Member Functions

 Server ()
 
 ~Server ()
 
void parse_exec_args (const char *argv[], int argc)
 
virtual void initialize ()
 
virtual void finalize ()
 
virtual void post_initialize ()
 
virtual void post_finalize ()
 
void print_help ()
 
struct general_server_configurationgeneral_conf ()
 
bool parse_common_configs (sol::table &cfg)
 
std::shared_ptr< boost::mysql::tcp_ssl_connection > get_database_connection ()
 
bool test_database_connection ()
 
- Public Member Functions inherited from Kernel
 Kernel (general_server_configuration &config)
 
 ~Kernel ()
 
virtual void initialize ()=0
 
virtual void finalize ()=0
 
virtual void post_initialize ()=0
 
virtual void post_finalize ()=0
 
struct general_server_configurationgeneral_conf ()
 
template<typename T >
std::shared_ptr< T > get_component (std::string uuid)
 
template<typename T >
std::shared_ptr< T > get_component_of_type (Horizon::System::runtime_module_type type, int segment_number=1)
 
template<typename T >
void register_component (Horizon::System::runtime_module_type type, T &&component)
 
template<typename T >
void register_component (Horizon::System::runtime_module_type type, std::shared_ptr< T > component)
 
void deregister_component (Horizon::System::runtime_module_type type, int segment_number=1)
 
int get_registered_component_count_of_type (Horizon::System::runtime_module_type type)
 
template<typename ComponentType , std::size_t Priority, typename Key , typename Value >
int get_segment_number_for_resource (Horizon::System::runtime_module_type module_t, Key resource_key, Value resource_not_found_value)
 
int get_component_count ()
 
KernelComponentsget_components ()
 
void system_routine_queue_push (std::shared_ptr< Horizon::System::RuntimeContext > context)
 
void system_routine_queue_push (std::shared_ptr< Horizon::System::RuntimeContextChain > context)
 
void system_routine_process_queue ()
 
void system_routine_register (Horizon::System::runtime_module_type module_t, Horizon::System::runtime_synchronization_method sync_t, std::shared_ptr< Horizon::System::RuntimeContext > context)
 
Horizon::System::SystemRoutineManagerget_system_routine_manager ()
 
boost::asio::io_context & get_io_context ()
 
void set_signal_interrupt_command_line_loop (bool signal)
 
bool get_signal_interrupt_command_line_loop ()
 

Protected Attributes

struct general_server_configuration general_config
 
- Protected Attributes inherited from Kernel
boost::asio::io_context _io_context_global
 
KernelComponents _components
 
general_server_configuration _config
 

Constructor & Destructor Documentation

◆ Server()

Server::Server ( )
301{
302 auto now = std::chrono::system_clock::now();
303 auto in_time_t = std::chrono::system_clock::to_time_t(now);
304
305 HLog(info) << "Copyright (c) https://github.com/horizonxyz/horizon";
306 HLog(info) << "Date: " << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d");
307 HLog(info) << "Compile CXX Flags: " << _CMAKE_CXX_FLAGS;
308 HLog(info) << "Version: " << VER_PRODUCTVERSION_STR;
309 HLog(info) << "Last Update: " << _DATE;
310 HLog(info) << "Branch: " << _BRANCH;
311 HLog(info) << "Copyright: " << VER_LEGALCOPYRIGHT_STR;
312 HLog(info) << "Boost Version: v" << (BOOST_VERSION / 100000) << "." << (BOOST_VERSION / 100 % 1000) << "." << (BOOST_VERSION % 100);
313 HLog(info) << "Client Information: " << CLIENT_TYPE << " " << PACKET_VERSION;
314}
#define PACKET_VERSION
Definition: Horizon.hpp:46
#define CLIENT_TYPE
Definition: Horizon.hpp:43
#define HLog(type)
Definition: Logger.hpp:122
Kernel(general_server_configuration &config)
Definition: Server.cpp:53
struct general_server_configuration & general_conf()
Definition: Server.hpp:570

References CLIENT_TYPE, HLog, and PACKET_VERSION.

◆ ~Server()

Server::~Server ( )
317{
318}

Member Function Documentation

◆ finalize()

void Server::finalize ( )
virtual

Implements Kernel.

Reimplemented in Horizon::Zone::ZoneKernel, Horizon::Zone::ZoneServer, Horizon::Auth::AuthServer, and Horizon::Char::CharServer.

440{
441 if (!general_conf().is_test_run() && !general_conf().is_test_run_minimal()) {
442 get_component_of_type<CommandLineProcess>(Horizon::System::RUNTIME_COMMANDLINE)->finalize();
444 }
445
446 get_component_of_type<DatabaseProcess>(Horizon::System::RUNTIME_DATABASE)->finalize();
448}
void deregister_component(Horizon::System::runtime_module_type type, int segment_number=1)
Definition: Server.hpp:493
@ RUNTIME_DATABASE
Definition: System.hpp:88
@ RUNTIME_COMMANDLINE
Definition: System.hpp:83

References Kernel::deregister_component(), general_conf(), Horizon::System::RUNTIME_COMMANDLINE, and Horizon::System::RUNTIME_DATABASE.

Referenced by Horizon::Zone::ZoneKernel::finalize(), Horizon::Auth::AuthServer::finalize(), and Horizon::Char::CharServer::finalize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ general_conf()

◆ get_database_connection()

std::shared_ptr< boost::mysql::tcp_ssl_connection > Server::get_database_connection ( )
inline
575 {
576 return get_component_of_type<DatabaseProcess>(Horizon::System::RUNTIME_DATABASE)->get_connection();
577 }

References Horizon::System::RUNTIME_DATABASE.

Referenced by Horizon::Auth::AuthServer::clicmd_create_new_account(), Horizon::Auth::AuthServer::clicmd_reset_password(), and test_database_connection().

+ Here is the caller graph for this function:

◆ initialize()

void Server::initialize ( )
virtual

Initialize Commandline Interface

Implements Kernel.

Reimplemented in Horizon::Zone::ZoneKernel, Horizon::Zone::ZoneServer, Horizon::Auth::AuthServer, and Horizon::Char::CharServer.

426{
430 if (general_conf().is_test_run() && general_conf().is_test_run_minimal()) {
431 HLog(info) << "Command line not supported during test-runs... skipping.";
432 } else {
433 HLog(info) << "Horizon Command-Line initializing...";
434 register_component(Horizon::System::RUNTIME_COMMANDLINE, std::make_shared<CommandLineProcess>(this));
435 get_component_of_type<CommandLineProcess>(Horizon::System::RUNTIME_COMMANDLINE)->initialize();
436 }
437}
void register_component(Horizon::System::runtime_module_type type, T &&component)
Definition: Server.hpp:475

References general_conf(), HLog, Kernel::register_component(), and Horizon::System::RUNTIME_COMMANDLINE.

Referenced by Horizon::Zone::ZoneKernel::initialize(), Horizon::Auth::AuthServer::initialize(), and Horizon::Char::CharServer::initialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_common_configs()

bool Server::parse_common_configs ( sol::table &  cfg)
373{
374 std::string tmp_string{""};
375
376 general_conf().set_listen_ip(tbl.get_or("bind_ip", std::string("127.0.0.1")));
377 general_conf().set_listen_port(tbl.get_or("bind_port", 0));
378
379 if (general_conf().get_listen_port() == 0) {
380 HLog(error) << "Invalid or non-existent configuration for 'bind_port', Halting...";
381 return false;
382 }
383
384 sol::table db_tbl = tbl.get<sol::table>("database_config");
385
386 try {
387 general_conf().set_db_host(db_tbl.get_or<std::string>("host", "127.0.0.1"));
388 general_conf().set_db_user(db_tbl.get_or<std::string>("user", "horizon"));
389 general_conf().set_db_database(db_tbl.get_or<std::string>("db", "horizon"));
390 general_conf().set_db_pass(db_tbl.get_or<std::string>("pass", "horizon"));
391 general_conf().set_db_port(db_tbl.get_or<uint16_t>("port", 33060));
392
393 register_component(Horizon::System::RUNTIME_DATABASE, std::make_shared<DatabaseProcess>(this));
394
395 get_component_of_type<DatabaseProcess>(Horizon::System::RUNTIME_DATABASE)->initialize(
397 1,
398 general_conf().get_db_host(),
399 general_conf().get_db_port(),
400 general_conf().get_db_user(),
401 general_conf().get_db_pass(),
402 general_conf().get_db_database());
403
404 HLog(info) << "Database tcp://" << general_conf().get_db_user()
405 << ":" << general_conf().get_db_pass()
406 << "@" << general_conf().get_db_host()
407 << ":" << general_conf().get_db_port()
408 << "/" << general_conf().get_db_database()
409 << (test_database_connection() ? " (connected)" : "(not connected)");
410 }
411 catch (const boost::mysql::error_with_diagnostics &error) {
412 HLog(error) << error.what() << ".";
413 return false;
414 }
415 catch (const std::exception &error) {
416 HLog(error) << error.what() << ".";
417 return false;
418 }
419
420 return true;
421}
boost::asio::io_context & get_io_context()
Definition: Server.cpp:70
bool test_database_connection()
Definition: Server.cpp:464
void set_listen_ip(const std::string &listen_ip)
Definition: ServerConfiguration.hpp:62
const std::string & get_db_host() const
Definition: ServerConfiguration.hpp:69
void set_db_user(std::string &&user)
Definition: ServerConfiguration.hpp:73
const std::string & get_db_pass() const
Definition: ServerConfiguration.hpp:75
void set_db_port(uint16_t port)
Definition: ServerConfiguration.hpp:82
const uint16_t & get_db_port() const
Definition: ServerConfiguration.hpp:81
const std::string & get_db_user() const
Definition: ServerConfiguration.hpp:72
void set_listen_port(uint16_t listen_port)
Definition: ServerConfiguration.hpp:65
void set_db_pass(std::string &&pass)
Definition: ServerConfiguration.hpp:76
const std::string & get_db_database() const
Definition: ServerConfiguration.hpp:78
void set_db_database(std::string &&database)
Definition: ServerConfiguration.hpp:79
void set_db_host(std::string &&host)
Definition: ServerConfiguration.hpp:70

References general_conf(), general_server_configuration::get_db_database(), general_server_configuration::get_db_host(), general_server_configuration::get_db_pass(), general_server_configuration::get_db_port(), general_server_configuration::get_db_user(), Kernel::get_io_context(), HLog, Kernel::register_component(), Horizon::System::RUNTIME_DATABASE, general_server_configuration::set_db_database(), general_server_configuration::set_db_host(), general_server_configuration::set_db_pass(), general_server_configuration::set_db_port(), general_server_configuration::set_db_user(), general_server_configuration::set_listen_ip(), general_server_configuration::set_listen_port(), and test_database_connection().

Referenced by Horizon::Auth::AuthServer::read_config(), Horizon::Char::CharServer::read_config(), and Horizon::Zone::ZoneServer::read_config().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_exec_args()

void Server::parse_exec_args ( const char *  argv[],
int  argc 
)
326{
327 std::string started_with_args;
328 for (int i = 1; i < argc; ++i) {
329 started_with_args.append(argv[i]).append(" ");
330
331 std::string arg(argv[i]);
332 std::vector<std::string> separated_args;
333 boost::algorithm::split(separated_args, arg, boost::algorithm::is_any_of("="));
334
335 if (separated_args.size() == 1) {
336 std::string arg = separated_args.at(0);
337 if (arg.compare("--test-run") == 0) {
338 HLog(info) << "Horizon test mode (without command-line, network) intiated.";
340 } else if (arg.compare("--test-run-with-network") == 0) {
341 HLog(info) << "Horizon test mode (with network) intiated.";
343 } else if (arg.compare("--help") == 0) {
344 print_help();
345 } else if (arg.compare("--version") == 0) {
346 HLog(info) << "Version: " << VER_PRODUCTVERSION_STR;
347 }
348 } else if (separated_args.size() == 0) {
349 HLog(error) << "Horizon started with no command line arguments.";
350 print_help();
351 } else {
352 std::string arg = separated_args.at(0);
353 std::string val = separated_args.at(1);
354
355 if (arg.compare("--with-config") == 0) {
356 HLog(info) << "Loading configurations from '" << val << "'.";
358 if (!exists(general_conf().get_config_file_path())) {
359 HLog(error) << "Configuration file path '" << val << "' does not exist!";
360 continue;
361 }
362 }
363 }
364 }
365
366 HLog(info) << "Started with args:" << started_with_args;
367}
@ TEST_RUN_MINIMAL
Definition: ServerConfiguration.hpp:40
@ TEST_RUN_WITH_NETWORK
Definition: ServerConfiguration.hpp:41
void print_help()
Definition: Server.cpp:320
void set_test_run(test_run_type type)
Definition: ServerConfiguration.hpp:47
void set_config_file_path(const boost::filesystem::path &path)
Definition: ServerConfiguration.hpp:56

References general_conf(), HLog, print_help(), general_server_configuration::set_config_file_path(), general_server_configuration::set_test_run(), TEST_RUN_MINIMAL, and TEST_RUN_WITH_NETWORK.

+ Here is the call graph for this function:

◆ post_finalize()

void Server::post_finalize ( )
virtual

Implements Kernel.

460{
462}
virtual void post_finalize()=0
Definition: Server.cpp:88

References Kernel::post_finalize().

Referenced by Horizon::Zone::ZoneServer::finalize(), Horizon::Auth::AuthServer::finalize(), and Horizon::Char::CharServer::finalize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ post_initialize()

void Server::post_initialize ( )
virtual

Implements Kernel.

451{
453
454 for (auto i = _components.begin(); i != _components.end(); i++) {
455 HLog(info) << "Kernel component '" << i->second.ptr->get_type_string() << " (" << i->second.segment_number << ")': " << (i->second.ptr->is_initialized() == true ? "Online" : "Offline (Starting)") << " { CPU: " << i->second.ptr->get_thread_cpu_id() << ", uuid: " << i->first << " }";
456 }
457}
KernelComponents _components
Definition: Server.hpp:545
virtual void post_initialize()=0
Definition: Server.cpp:83

References Kernel::_components, HLog, and Kernel::post_initialize().

Referenced by Horizon::Zone::ZoneServer::initialize(), Horizon::Auth::AuthServer::initialize(), and Horizon::Char::CharServer::initialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ print_help()

void Server::print_help ( )
321{
322 HLog(error) << "usage: <server> [--version] [--help] [--with-config=<file_with_path>]";
323}

References HLog.

Referenced by parse_exec_args().

+ Here is the caller graph for this function:

◆ test_database_connection()

bool Server::test_database_connection ( )
465{
466 try {
467 const char *sql = "SELECT 'Hello World!'";
468 boost::mysql::results result;
469 get_database_connection()->execute(sql, result);
470 if (result.rows().at(0).at(0).as_string().compare("Hello World!") == 0)
471 return true;
472 } catch (boost::mysql::error_with_diagnostics &error) {
473 HLog(error) << error.what();
474 }
475 return false;
476}
std::shared_ptr< boost::mysql::tcp_ssl_connection > get_database_connection()
Definition: Server.hpp:574

References get_database_connection(), and HLog.

Referenced by parse_common_configs().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ general_config

struct general_server_configuration Server::general_config
protected

Referenced by general_conf().


The documentation for this class was generated from the following files: