30#ifndef HORIZON_SYSTEM_ROUTINES_HPP
31#define HORIZON_SYSTEM_ROUTINES_HPP
34#include <boost/lockfree/spsc_queue.hpp>
39#define BOOST_UUID_FORCE_AUTO_LINK 1
40#include <boost/uuid/uuid.hpp>
41#include <boost/uuid/uuid_io.hpp>
42#include <boost/uuid/random_generator.hpp>
103class RuntimeContextChain;
118class SystemRoutineManager;
232 void push(std::shared_ptr<WorkContext> seg) {
_queue.push(std::move(seg)); }
233 std::shared_ptr<WorkContext>
pop()
238 std::shared_ptr<WorkContext> ret;
245 std::shared_ptr<WorkContext> context =
nullptr;
251 while((context =
pop()) !=
nullptr) {
265 if (context->execute() ==
false) {
274 if (failed ==
true) {
286 boost::lockfree::spsc_queue<std::shared_ptr<WorkContext>, boost::lockfree::capacity<100>>
_queue;
340template <
typename T =
int>
356 if (
this != &other) {
377template <
typename PayloadType>
384 Result(std::vector<PayloadType> payload_vector)
402 Work(std::shared_ptr<RuntimeRoutineContext> runtime_context)
408 std::cerr <<
"Nothing to execute." << std::endl;
558 virtual void push(std::shared_ptr<RuntimeContext> seg) {
_queue.push(std::move(seg)); }
559 virtual std::shared_ptr<RuntimeContext>
pop()
564 std::shared_ptr<RuntimeContext> ret;
575 boost::lockfree::spsc_queue<std::shared_ptr<RuntimeContext>, boost::lockfree::capacity<100>>
_queue;
615 std::cerr <<
"SystemRoutineManager for module type (" << module_type <<
") failed to start. Invalid module type." << std::endl;
628 _runtime_map.emplace(context_chain->get_uuid_string(), data);
631 virtual std::shared_ptr<const runtime_map_data>
find_runtime(std::string name)
634 return (it !=
_runtime_map.end()) ? std::make_shared<const runtime_map_data>(it->second) :
nullptr;
644 void push(std::shared_ptr<RuntimeContextChain> chain) {
648 void push(std::shared_ptr<RuntimeContext> seg) {
653 std::shared_ptr<RuntimeContext>
pop()
658 std::shared_ptr<RuntimeContext> ret;
668 std::shared_ptr<RuntimeContextChain> ret;
679 std::shared_ptr<RuntimeContext> context =
pop();
680 if (context !=
nullptr)
685 std::shared_ptr<RuntimeContextChain> routine_chain =
pop_chain();
686 if (routine_chain !=
nullptr)
687 routine_chain->get_queue_manager().process();
693 boost::lockfree::spsc_queue<std::shared_ptr<RuntimeContext>, boost::lockfree::capacity<100>>
_system_queue;
694 boost::lockfree::spsc_queue<std::shared_ptr<RuntimeContextChain>, boost::lockfree::capacity<100>>
_system_queue_chain;
#define HLog(type)
Definition: Logger.hpp:122
Definition: System.hpp:342
virtual bool has_many()
Definition: System.hpp:365
T _result
Definition: System.hpp:370
virtual std::vector< T > get_many()
Definition: System.hpp:366
virtual void set_many(std::vector< T > result_vector)
Definition: System.hpp:367
ContextWithResult()
Definition: System.hpp:344
ContextWithResult & operator=(const ContextWithResult &other)
Definition: System.hpp:354
std::vector< T > _result_vector
Definition: System.hpp:371
ContextWithResult(std::vector< T > result_vector)
Definition: System.hpp:346
virtual T get_one()
Definition: System.hpp:362
virtual void set_one(T result)
Definition: System.hpp:363
ContextWithResult(T result)
Definition: System.hpp:345
ContextWithResult(const ContextWithResult &other)
Definition: System.hpp:348
Definition: System.hpp:379
Result(std::vector< PayloadType > payload_vector)
Definition: System.hpp:384
Result()
Definition: System.hpp:381
Result(PayloadType payload)
Definition: System.hpp:382
Definition: System.hpp:478
bool failed()
Definition: System.hpp:537
bool start()
Definition: System.hpp:480
bool completed()
Definition: System.hpp:526
std::atomic< enum runtime_routine_chain_status > _status
Definition: System.hpp:550
bool pause()
Definition: System.hpp:504
bool stop()
Definition: System.hpp:491
runtime_routine_chain_status get_status()
Definition: System.hpp:548
bool cancel()
Definition: System.hpp:515
Definition: System.hpp:554
RuntimeContextChain * _chain
Definition: System.hpp:574
ContextControlAgent & _control_agent
Definition: System.hpp:573
virtual std::shared_ptr< RuntimeContext > pop()
Definition: System.hpp:559
boost::lockfree::spsc_queue< std::shared_ptr< RuntimeContext >, boost::lockfree::capacity< 100 > > _queue
Definition: System.hpp:575
virtual void push(std::shared_ptr< RuntimeContext > seg)
Definition: System.hpp:558
ContextQueueManager(ContextControlAgent &control_agent, RuntimeContextChain *chain)
Definition: System.hpp:556
std::atomic< bool > _paused
Definition: System.hpp:576
virtual bool is_paused()
Definition: System.hpp:571
virtual bool process()
Definition: System.cpp:50
Definition: System.hpp:471
bool process()
Definition: System.hpp:581
runtime_module_type _module_t
Definition: System.hpp:596
ContextQueueManager & get_queue_manager()
Definition: System.hpp:583
ContextQueueManager _queue_manager
Definition: System.hpp:586
void push(std::shared_ptr< RuntimeContext > context)
Definition: System.hpp:580
ContextControlAgent & get_control_agent()
Definition: System.hpp:584
ContextControlAgent _control_agent
Definition: System.hpp:587
runtime_module_type get_module_type()
Definition: System.hpp:589
RuntimeContextChain(runtime_module_type run_module)
Definition: System.hpp:473
std::shared_ptr< RuntimeContext > pop()
Definition: System.hpp:579
std::string get_uuid_string()
Definition: System.hpp:592
boost::uuids::uuid _uuid
Definition: System.hpp:595
void set_module_type(runtime_module_type module_t)
Definition: System.hpp:590
Definition: System.hpp:128
virtual bool has_result()
Definition: System.hpp:130
Definition: System.hpp:137
WorkContext()
Definition: System.hpp:139
boost::uuids::uuid _uuid
Definition: System.hpp:148
std::string get_uuid_string()
Definition: System.hpp:146
virtual bool execute()
Definition: System.hpp:141
Definition: System.hpp:152
bool pause()
Definition: System.hpp:178
runtime_work_queue_status get_status()
Definition: System.hpp:222
bool failed()
Definition: System.hpp:211
bool cancel()
Definition: System.hpp:189
bool start()
Definition: System.hpp:154
bool stop()
Definition: System.hpp:165
bool completed()
Definition: System.hpp:200
std::atomic< enum runtime_work_queue_status > _status
Definition: System.hpp:224
Definition: System.hpp:228
std::shared_ptr< WorkContext > pop()
Definition: System.hpp:233
WorkQueueManager(WorkControlAgent &control_agent)
Definition: System.hpp:230
bool process()
Definition: System.hpp:243
WorkControlAgent & _control_agent
Definition: System.hpp:285
bool is_paused()
Definition: System.hpp:284
std::atomic< bool > _paused
Definition: System.hpp:287
void push(std::shared_ptr< WorkContext > seg)
Definition: System.hpp:232
boost::lockfree::spsc_queue< std::shared_ptr< WorkContext >, boost::lockfree::capacity< 100 > > _queue
Definition: System.hpp:286
Definition: System.hpp:121
void set_context_state(runtime_context_state state=RUNTIME_CONTEXT_STATE_INACTIVE)
Definition: System.hpp:319
WorkControlAgent & get_control_agent()
Definition: System.hpp:307
std::string get_uuid_string()
Definition: System.hpp:311
runtime_synchronization_method get_synchronization_method()
Definition: System.hpp:326
runtime_synchronization_method _synchronization_t
Definition: System.hpp:336
void dispatch()
Definition: System.cpp:48
void push(std::shared_ptr< WorkContext > context)
Definition: System.hpp:291
RuntimeContext(SystemRoutineManager &hsr_manager, runtime_synchronization_method sync_t=RUNTIME_SYNC_NONE)
Definition: System.hpp:123
SystemRoutineManager & get_routine_manager()
Definition: System.hpp:309
SystemRoutineManager & _hsr_manager
Definition: System.hpp:331
WorkControlAgent _control_agent
Definition: System.hpp:332
runtime_context_result get_context_result()
Definition: System.hpp:317
virtual bool run()
Definition: System.hpp:293
void set_context_result(runtime_context_result pass=RUNTIME_CONTEXT_NO_STATE)
Definition: System.hpp:313
WorkQueueManager _queue_manager
Definition: System.hpp:333
std::atomic< enum runtime_context_state > _context_state_t
Definition: System.hpp:337
void set_synchronization_method(runtime_synchronization_method sync)
Definition: System.hpp:325
WorkQueueManager & get_queue_manager()
Definition: System.hpp:306
runtime_context_state get_context_state()
Definition: System.hpp:323
boost::uuids::uuid _uuid
Definition: System.hpp:334
std::atomic< enum runtime_context_result > _result
Definition: System.hpp:335
std::shared_ptr< WorkContext > pop()
Definition: System.hpp:290
Definition: System.hpp:417
std::string _error_message
Definition: System.hpp:449
Work * _work
Definition: System.hpp:451
std::string get_status_message()
Definition: System.hpp:430
std::string _warning_message
Definition: System.hpp:448
MessageAgent(std::shared_ptr< RuntimeRoutineContext > runtime_context, Work *work)
Definition: System.hpp:419
~MessageAgent()
Definition: System.hpp:421
std::shared_ptr< RuntimeRoutineContext > get_runtime_context()
Definition: System.hpp:423
void set_error_message(std::string message)
Definition: System.hpp:439
std::weak_ptr< RuntimeRoutineContext > _runtime_context
Definition: System.hpp:450
void set_status_message(std::string message)
Definition: System.hpp:425
std::string _status_message
Definition: System.hpp:447
void set_warning_message(std::string message)
Definition: System.hpp:432
std::string get_warning_message()
Definition: System.hpp:437
std::string get_error_message()
Definition: System.hpp:444
Definition: System.hpp:400
void set_runtime_context(std::shared_ptr< RuntimeRoutineContext > runtime_context)
Definition: System.hpp:413
MessageAgent & get_message_agent()
Definition: System.hpp:455
virtual bool execute()
Definition: System.hpp:406
~Work()
Definition: System.hpp:404
MessageAgent _message_agent
Definition: System.hpp:458
std::shared_ptr< RuntimeRoutineContext > get_runtime_context()
Definition: System.hpp:412
std::weak_ptr< RuntimeRoutineContext > _runtime_context
Definition: System.hpp:457
Work(std::shared_ptr< RuntimeRoutineContext > runtime_context)
Definition: System.hpp:402
Definition: System.hpp:389
std::vector< std::string > _warning_messages
Definition: System.hpp:464
std::vector< std::string > _status_messages
Definition: System.hpp:463
void warning_message(std::string message)
Definition: System.hpp:396
void status_message(std::string message)
Definition: System.hpp:395
std::mutex _runtime_synchronization_mutex
Definition: System.hpp:467
std::mutex & get_runtime_synchronization_mutex()
Definition: System.hpp:461
std::vector< std::string > _error_messages
Definition: System.hpp:465
void error_message(std::string message)
Definition: System.hpp:397
RuntimeRoutineContext(Server *s, runtime_synchronization_method sync_t=RUNTIME_SYNC_NONE)
Definition: System.cpp:33
Definition: System.hpp:600
std::map< std::string, runtime_map_data > runtime_map
Definition: System.hpp:609
runtime_map _runtime_map
Definition: System.hpp:697
virtual int get_runtime_routine_count()
Definition: System.hpp:637
void push(std::shared_ptr< RuntimeContext > seg)
Definition: System.hpp:648
runtime_module_type _module_type
Definition: System.hpp:698
runtime_module_type get_module_type()
Definition: System.hpp:642
virtual std::shared_ptr< const runtime_map_data > find_runtime(std::string name)
Definition: System.hpp:631
std::shared_ptr< RuntimeContextChain > pop_chain()
Definition: System.hpp:663
void push(std::shared_ptr< RuntimeContextChain > chain)
Definition: System.hpp:644
SystemRoutineManager(runtime_module_type module_type)
Definition: System.hpp:611
virtual void register_(runtime_module_type module_t, runtime_synchronization_method sync_t, std::shared_ptr< RuntimeContext > context)
Definition: System.hpp:619
void process_queue()
Definition: System.hpp:676
std::shared_ptr< RuntimeContext > pop()
Definition: System.hpp:653
virtual void register_(runtime_module_type module_t, runtime_synchronization_method sync_t, std::shared_ptr< RuntimeContextChain > context_chain)
Definition: System.hpp:625
boost::lockfree::spsc_queue< std::shared_ptr< RuntimeContextChain >, boost::lockfree::capacity< 100 > > _system_queue_chain
Definition: System.hpp:694
boost::lockfree::spsc_queue< std::shared_ptr< RuntimeContext >, boost::lockfree::capacity< 100 > > _system_queue
Definition: System.hpp:693
Definition: Server.hpp:192
Definition: Server.hpp:554
runtime_context_result
Definition: System.hpp:106
@ RUNTIME_CONTEXT_FAIL
Definition: System.hpp:108
@ RUNTIME_CONTEXT_PASS
Definition: System.hpp:109
@ RUNTIME_CONTEXT_NO_STATE
Definition: System.hpp:107
runtime_work_run_result
Definition: System.hpp:74
@ RUNTIME_WORK_RUN_SUCCEEDED
Definition: System.hpp:76
@ RUNTIME_WORK_RUN_BYPASS
Definition: System.hpp:77
@ RUNTIME_WORK_RUN_FAILED
Definition: System.hpp:75
runtime_context_state
Definition: System.hpp:113
@ RUNTIME_CONTEXT_STATE_ACTIVE
Definition: System.hpp:115
@ RUNTIME_CONTEXT_STATE_WAITING
Definition: System.hpp:116
@ RUNTIME_CONTEXT_STATE_INACTIVE
Definition: System.hpp:114
runtime_routine_chain_status
Definition: System.hpp:52
@ RUNTIME_ROUTINE_CHAIN_COMPLETED
Definition: System.hpp:59
@ RUNTIME_ROUTINE_CHAIN_CANCELLED
Definition: System.hpp:58
@ RUNTIME_ROUTINE_CHAIN_NOT_STARTED
Definition: System.hpp:53
@ RUNTIME_ROUTINE_CHAIN_STARTED
Definition: System.hpp:57
@ RUNTIME_ROUTINE_CHAIN_FAILED
Definition: System.hpp:55
@ RUNTIME_ROUTINE_CHAIN_PAUSED
Definition: System.hpp:56
@ RUNTIME_ROUTINE_CHAIN_STOPPED
Definition: System.hpp:54
runtime_work_queue_status
Definition: System.hpp:63
@ RUNTIME_WORK_QUEUE_PAUSED
Definition: System.hpp:67
@ RUNTIME_WORK_QUEUE_STOPPED
Definition: System.hpp:65
@ RUNTIME_WORK_QUEUE_COMPLETED
Definition: System.hpp:70
@ RUNTIME_WORK_QUEUE_NOT_STARTED
Definition: System.hpp:64
@ RUNTIME_WORK_QUEUE_STARTED
Definition: System.hpp:68
@ RUNTIME_WORK_QUEUE_FAILED
Definition: System.hpp:66
@ RUNTIME_WORK_QUEUE_CANCELLED
Definition: System.hpp:69
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_MODULE_MAX
Definition: System.hpp:93
@ 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
@ RUNTIME_SYNC_WAIT_CHECK_STATE
Definition: System.hpp:100
@ RUNTIME_SYNC_WAIT_NO_CHECK_STATE
Definition: System.hpp:99
@ RUNTIME_SYNC_NONE
Definition: System.hpp:98
Definition: Element.hpp:7
Definition: System.hpp:602
runtime_synchronization_method sync_t
Definition: System.hpp:604
std::shared_ptr< RuntimeContext > context
Definition: System.hpp:605
runtime_module_type module_t
Definition: System.hpp:603
std::shared_ptr< RuntimeContextChain > context_chain
Definition: System.hpp:606