30#ifndef HORIZON_SERVER_HPP
31#define HORIZON_SERVER_HPP
37#include <boost/lexical_cast.hpp>
38#include <boost/uuid/uuid.hpp>
39#include <boost/uuid/uuid_io.hpp>
40#include <boost/uuid/random_generator.hpp>
43#include <boost/mysql/error_with_diagnostics.hpp>
44#include <boost/mysql/handshake_params.hpp>
45#include <boost/mysql/results.hpp>
46#include <boost/mysql/tcp_ssl.hpp>
48#include <boost/asio/io_context.hpp>
49#include <boost/asio/ip/tcp.hpp>
50#include <boost/asio/ssl/context.hpp>
51#include <boost/system/system_error.hpp>
53#include <boost/chrono.hpp>
60#define TERMINAL_STR "Horizon $> "
92template <
typename Key,
typename Value>
100 void add(Key key, Value value) {
_table.insert(key, value); }
104 Value
get(Key key, Value
const &default_value = Value()) {
return _table.at(key, default_value); }
111template <
class Storage>
120 void add(
typename Storage::key_type key,
typename Storage::value_type value) {
_storage->add(key, value); }
124 typename Storage::value_type
get(
typename Storage::key_type key,
typename Storage::value_type default_value =
typename Storage::value_type()) {
return _storage->get(key, default_value); }
130 std::map<typename Storage::key_type, typename Storage::value_type>
get_map() {
return _storage->_table.get_map(); }
137template <
typename... SharedPriorityResourceMediums>
151 template <std::
size_t Priority>
156 template <std::
size_t Index,
typename Key,
typename Value>
157 void add(Key key, Value value)
162 template <std::
size_t Index,
typename Key>
168 template <std::
size_t Index,
typename Key,
typename Value>
171 return std::get<Index>(
_resources).get(key, default_value);
174 template <std::
size_t Index>
180 template <std::
size_t Index>
231 default:
return "Unknown";
262 auto current_time = std::chrono::steady_clock::now();
264 if (diff_time >= 1e9) {
335 std::function<bool(std::string)>
find(std::string &cmd)
341 void initialize(
int segment_number = 1)
override;
385 HLog(error) <<
"Database not configured";
399 void initialize(boost::asio::io_context &io_context,
int segment_number, std::string host,
int port, std::string user, std::string pass, std::string database);
411 std::shared_ptr<boost::mysql::tcp_ssl_connection>
get_connection();
418 std::shared_ptr<boost::asio::ssl::context>
_ssl_ctx{
nullptr};
419 std::shared_ptr<boost::mysql::tcp_ssl_connection>
_connection{
nullptr};
433 std::shared_ptr<KernelComponent>
ptr;
452 template <
typename T>
459 return std::static_pointer_cast<T>(it->second.ptr);
462 template <
typename T>
466 if (c.second.type == type && c.second.segment_number == segment_number) {
467 return std::static_pointer_cast<T>(c.second.ptr);
474 template <
typename T>
479 holder.
ptr = std::make_shared<T>(std::move(component));
480 _components.insert(std::pair(component.get_uuid_string(), holder));
483 template <
typename T>
489 holder.
ptr = std::static_pointer_cast<KernelComponent>(component);
490 _components.insert(std::pair(component->get_uuid_string(), holder));
496 if (c.second.type == type && c.second.segment_number == segment_number) {
507 if (c.second.type == type)
513 template <
typename ComponentType, std::
size_t Priority,
typename Key,
typename Value>
518 auto component = get_component_of_type<ComponentType>(module_t, i + 1);
519 if (component->get_resource_manager().template get_resource<Priority, Key, Value>(resource_key, resource_not_found_value) != resource_not_found_value)
#define HLog(type)
Definition: Logger.hpp:122
shutdown_stages get_shutdown_stage()
Definition: Server.hpp:74
std::atomic< int > _shutdown_signal
Definition: Server.cpp:51
std::map< std::string, kernel_component_state_holder > KernelComponents
Definition: Server.hpp:437
void set_shutdown_signal(int signal)
Definition: Server.hpp:73
kernel_resource_priority_type
Definition: Server.hpp:78
@ RESOURCE_PRIORITY_SECONDARY
Definition: Server.hpp:80
@ RESOURCE_PRIORITY_DENARY
Definition: Server.hpp:88
@ RESOURCE_PRIORITY_SENARY
Definition: Server.hpp:84
@ RESOURCE_PRIORITY_OCTONARY
Definition: Server.hpp:86
@ RESOURCE_PRIORITY_SEPTENARY
Definition: Server.hpp:85
@ RESOURCE_PRIORITY_PRIMARY
Definition: Server.hpp:79
@ RESOURCE_PRIORITY_TERTIARY
Definition: Server.hpp:81
@ RESOURCE_PRIORITY_NONARY
Definition: Server.hpp:87
@ MAX_KERNEL_SEGMENT_PRIORITIES
Definition: Server.hpp:89
@ RESOURCE_PRIORITY_QUINARY
Definition: Server.hpp:83
@ RESOURCE_PRIORITY_QUATERNARY
Definition: Server.hpp:82
std::atomic< shutdown_stages > _shutdown_stage
Definition: Server.cpp:50
shutdown_stages
Definition: Server.hpp:63
@ SHUTDOWN_COMPLETE
Definition: Server.hpp:67
@ SHUTDOWN_NOT_STARTED
Definition: Server.hpp:64
@ SHUTDOWN_CLEANUP_COMPLETE
Definition: Server.hpp:66
@ SHUTDOWN_INITIATED
Definition: Server.hpp:65
void set_shutdown_stage(shutdown_stages new_stage)
Definition: Server.hpp:75
Definition: Server.hpp:289
FinishFunc m_finish_func
Completion handler function.
Definition: Server.hpp:323
~CLICommand()
Definition: Server.hpp:301
std::string m_command
Command string.
Definition: Server.hpp:322
CLICommand(char *command, FinishFunc finish_func)
Definition: Server.hpp:295
CLICommand()
Definition: Server.hpp:293
CLICommand(CLICommand &&command)
Definition: Server.hpp:311
CLICommand operator=(CLICommand &command)
Definition: Server.hpp:317
std::function< void(CLICommand, bool)> FinishFunc
Definition: Server.hpp:291
CLICommand(CLICommand &command)
Definition: Server.hpp:305
Definition: Server.hpp:327
void finalize() override
Definition: Server.cpp:156
bool is_initialized() override
Definition: Server.hpp:357
std::atomic< bool > _is_running_command
Definition: Server.hpp:367
bool is_finalized() override
Definition: Server.hpp:358
std::function< bool(std::string)> find(std::string &cmd)
Definition: Server.hpp:335
std::atomic< bool > _is_finalized
Definition: Server.hpp:366
void command_complete(CLICommand, bool)
Definition: Server.hpp:344
void queue(CLICommand &&cmdMgr)
Definition: Server.hpp:331
std::atomic< bool > _is_initialized
Definition: Server.hpp:365
CommandLineProcess(Kernel *kernel)
Definition: Server.hpp:329
std::queue< CLICommand > _cli_cmd_queue
Definition: Server.hpp:363
std::thread _cli_thread
Definition: Server.hpp:364
void initialize(int segment_number=1) override
Definition: Server.cpp:144
std::unordered_map< std::string, std::function< bool(std::string)> > _cli_function_map
Definition: Server.hpp:361
void cli_thread_start()
Definition: Server.cpp:197
bool clicmd_shutdown(std::string)
CLI Commands.
Definition: Server.cpp:93
void process()
Definition: Server.cpp:164
void add_function(std::string cmd, std::function< bool(std::string)> func)
Definition: Server.hpp:332
bool clicmd_kernel_info(std::string)
Definition: Server.cpp:100
Definition: Server.hpp:371
std::string _host
Definition: Server.hpp:420
DatabaseProcess()
Definition: Server.hpp:373
void finalize() override
Definition: Server.hpp:401
void reinitialize()
Definition: Server.hpp:389
std::shared_ptr< boost::mysql::tcp_ssl_connection > get_connection()
Definition: Server.cpp:254
~DatabaseProcess()
Definition: Server.hpp:376
bool is_initialized() override
Definition: Server.hpp:413
std::atomic< bool > _is_finalized
Definition: Server.hpp:426
bool is_finalized() override
Definition: Server.hpp:414
DatabaseProcess(Kernel *kernel)
Definition: Server.hpp:375
std::string _user
Definition: Server.hpp:422
std::shared_ptr< boost::mysql::tcp_ssl_connection > _connection
Definition: Server.hpp:419
std::string _pass
Definition: Server.hpp:423
int _port
Definition: Server.hpp:421
boost::asio::io_context * _io_context
Definition: Server.hpp:417
std::shared_ptr< boost::asio::ssl::context > _ssl_ctx
Definition: Server.hpp:418
std::atomic< bool > _is_initialized
Definition: Server.hpp:425
std::string _database
Definition: Server.hpp:424
void initialize(int segment_number=1) override
Definition: Server.hpp:382
Definition: System.hpp:600
Definition: Server.hpp:192
std::atomic< int > _total_execution_time_average
Definition: Server.hpp:285
std::chrono::steady_clock::time_point _last_total_execution_time_update
Definition: Server.hpp:282
virtual void initialize(int segment_number=1)
Definition: Server.hpp:197
Horizon::System::runtime_module_type _module_type
Definition: Server.hpp:274
int _total_execution_time_aggregate
Definition: Server.hpp:284
void set_thread_cpu_id(int cpu_id)
Definition: Server.hpp:239
virtual bool is_finalized()
Definition: Server.hpp:204
Kernel * get_kernel()
Definition: Server.hpp:237
Horizon::System::SystemRoutineManager & get_system_routine_manager()
Definition: Server.hpp:235
int64_t get_segment_number()
Definition: Server.hpp:207
void set_total_execution_time(int time)
Definition: Server.hpp:245
virtual bool is_initialized()
Definition: Server.hpp:203
int _update_count
Definition: Server.hpp:280
void system_routine_queue_push(std::shared_ptr< Horizon::System::RuntimeContext > context)
Definition: Server.cpp:75
std::chrono::steady_clock::time_point _last_thread_update_rate_time
Definition: Server.hpp:281
void calculate_and_set_cpu_load()
Definition: Server.hpp:257
void system_routine_process_queue()
Definition: Server.cpp:77
boost::uuids::uuid _uuid
Definition: Server.hpp:276
std::atomic< int64_t > _segment_number
Definition: Server.hpp:273
void set_thread_update_rate(double rate)
Definition: Server.hpp:242
Horizon::System::SystemRoutineManager _hsr_manager
Definition: Server.hpp:275
double get_thread_update_rate()
Definition: Server.hpp:243
virtual void finalize()
Definition: Server.hpp:201
KernelComponent(Kernel *kernel, Horizon::System::runtime_module_type module_type)
Definition: Server.hpp:194
const std::string get_uuid_string()
Definition: Server.hpp:214
std::atomic< double > _thread_update_rate
Definition: Server.hpp:283
int get_total_execution_time()
Definition: Server.hpp:255
Kernel * _kernel
Definition: Server.hpp:279
void set_segment_number(int64_t segment_number)
Definition: Server.hpp:206
const std::string get_type_string()
Definition: Server.hpp:216
int get_thread_cpu_id()
Definition: Server.hpp:240
std::atomic< int > _thread_cpu_id
Definition: Server.hpp:278
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)
Definition: Server.cpp:78
Definition: Server.hpp:440
Horizon::System::SystemRoutineManager _hsr_manager
Definition: Server.hpp:548
std::shared_ptr< T > get_component(std::string uuid)
Definition: Server.hpp:453
std::shared_ptr< T > get_component_of_type(Horizon::System::runtime_module_type type, int segment_number=1)
Definition: Server.hpp:463
Kernel(general_server_configuration &config)
Definition: Server.cpp:53
KernelComponents _components
Definition: Server.hpp:545
int get_segment_number_for_resource(Horizon::System::runtime_module_type module_t, Key resource_key, Value resource_not_found_value)
Definition: Server.hpp:514
void system_routine_queue_push(std::shared_ptr< Horizon::System::RuntimeContext > context)
Definition: Server.cpp:62
boost::asio::io_context & get_io_context()
Definition: Server.cpp:70
void register_component(Horizon::System::runtime_module_type type, T &&component)
Definition: Server.hpp:475
void system_routine_process_queue()
Definition: Server.cpp:64
void register_component(Horizon::System::runtime_module_type type, std::shared_ptr< T > component)
Definition: Server.hpp:484
bool get_signal_interrupt_command_line_loop()
Definition: Server.hpp:541
int get_registered_component_count_of_type(Horizon::System::runtime_module_type type)
Definition: Server.hpp:503
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)
Definition: Server.cpp:65
void deregister_component(Horizon::System::runtime_module_type type, int segment_number=1)
Definition: Server.hpp:493
int get_component_count()
Definition: Server.hpp:526
virtual void post_finalize()=0
Definition: Server.cpp:88
virtual void initialize()=0
std::atomic< bool > _signal_interrupt_command_line_loop
Definition: Server.hpp:550
void set_signal_interrupt_command_line_loop(bool signal)
Definition: Server.hpp:540
general_server_configuration _config
Definition: Server.hpp:546
~Kernel()
Definition: Server.cpp:54
virtual void post_initialize()=0
Definition: Server.cpp:83
virtual void finalize()=0
KernelComponents & get_components()
Definition: Server.hpp:528
Horizon::System::SystemRoutineManager & get_system_routine_manager()
Definition: Server.hpp:535
boost::asio::io_context _io_context_global
Definition: Server.hpp:544
struct general_server_configuration & general_conf()
Definition: Server.hpp:450
Definition: LockedLookupTable.hpp:44
Definition: Server.hpp:554
virtual void initialize()
Definition: Server.cpp:425
virtual void post_finalize()
Definition: Server.cpp:459
bool parse_common_configs(sol::table &cfg)
Definition: Server.cpp:372
~Server()
Definition: Server.cpp:316
virtual void finalize()
Definition: Server.cpp:439
std::shared_ptr< boost::mysql::tcp_ssl_connection > get_database_connection()
Definition: Server.hpp:574
struct general_server_configuration & general_conf()
Definition: Server.hpp:570
virtual void post_initialize()
Definition: Server.cpp:450
struct general_server_configuration general_config
Definition: Server.hpp:583
void print_help()
Definition: Server.cpp:320
Server()
Definition: Server.cpp:300
bool test_database_connection()
Definition: Server.cpp:464
void parse_exec_args(const char *argv[], int argc)
Definition: Server.cpp:325
Definition: Server.hpp:138
Value get_resource(Key key, Value const &default_value=Value())
Definition: Server.hpp:169
SharedPriorityResourceManager(const SharedPriorityResourceManager &other)
Definition: Server.hpp:143
SharedPriorityResourceManager(SharedPriorityResourceManager &&other)
Definition: Server.hpp:145
SharedPriorityResourceManager(SharedPriorityResourceMediums... mediums)
Definition: Server.hpp:140
void remove(Key key)
Definition: Server.hpp:163
SharedPriorityResourceManager & operator=(const SharedPriorityResourceManager &other)
Definition: Server.hpp:147
void add(Key key, Value value)
Definition: Server.hpp:157
int size()
Definition: Server.hpp:175
SharedPriorityResourceManager & operator=(SharedPriorityResourceManager &&other)
Definition: Server.hpp:149
std::tuple< SharedPriorityResourceMediums... > _resources
Definition: Server.hpp:187
auto & get_medium()
Definition: Server.hpp:152
void clear()
Definition: Server.hpp:181
Definition: Server.hpp:113
void clear()
Definition: Server.hpp:128
std::map< typename Storage::key_type, typename Storage::value_type > get_map()
Definition: Server.hpp:130
void remove(typename Storage::key_type key)
Definition: Server.hpp:122
void add(typename Storage::key_type key, typename Storage::value_type value)
Definition: Server.hpp:120
int64_t get_priority()
Definition: Server.hpp:117
SharedPriorityResourceMedium(int priority, std::shared_ptr< Storage > storage)
Definition: Server.hpp:115
int64_t _priority
Definition: Server.hpp:133
Storage::value_type get(typename Storage::key_type key, typename Storage::value_type default_value=typename Storage::value_type())
Definition: Server.hpp:124
Storage get_storage()
Definition: Server.hpp:118
int size()
Definition: Server.hpp:126
std::shared_ptr< Storage > _storage
Definition: Server.hpp:134
size_t count(GridTypeListContainer< SPECIFIC_TYPE > const &elements, SPECIFIC_TYPE *)
Definition: GridReferenceContainer.hpp:100
runtime_module_type
Definition: System.hpp:81
@ RUNTIME_CLIENT_NETWORKING
Definition: System.hpp:89
@ RUNTIME_DATABASE
Definition: System.hpp:88
@ RUNTIME_NETWORKING
Definition: System.hpp:84
@ RUNTIME_WEB_SOCKET
Definition: System.hpp:91
@ RUNTIME_HTTP_SERVICE
Definition: System.hpp:90
@ RUNTIME_MAIN
Definition: System.hpp:82
@ RUNTIME_SCRIPTVM
Definition: System.hpp:87
@ RUNTIME_GAMELOGIC
Definition: System.hpp:86
@ RUNTIME_COMMANDLINE
Definition: System.hpp:83
@ RUNTIME_PERSISTENCE
Definition: System.hpp:85
@ RUNTIME_RUNTIME
Definition: System.hpp:92
runtime_synchronization_method
Definition: System.hpp:97
Definition: Element.hpp:7
Definition: ServerConfiguration.hpp:44
Definition: Server.hpp:430
int segment_number
Definition: Server.hpp:434
Horizon::System::runtime_module_type type
Definition: Server.hpp:432
std::shared_ptr< KernelComponent > ptr
Definition: Server.hpp:433
~kernel_component_state_holder()
Definition: Server.hpp:431
Definition: Server.hpp:93
LockedLookupTable< Key, Value > _table
Definition: Server.hpp:96
void add(Key key, Value value)
Definition: Server.hpp:100
Key key_type
Definition: Server.hpp:94
int size()
Definition: Server.hpp:106
void clear()
Definition: Server.hpp:108
void remove(Key key)
Definition: Server.hpp:102
s_segment_storage()
Definition: Server.hpp:97
Value value_type
Definition: Server.hpp:95
Value get(Key key, Value const &default_value=Value())
Definition: Server.hpp:104
~s_segment_storage()
Definition: Server.hpp:98