Horizon Official Technical Documentation
CommandLineProcess Class Reference

#include <Server.hpp>

+ Inheritance diagram for CommandLineProcess:
+ Collaboration diagram for CommandLineProcess:

Public Member Functions

 CommandLineProcess (Kernel *kernel)
 
void process ()
 
void queue (CLICommand &&cmdMgr)
 
void add_function (std::string cmd, std::function< bool(std::string)> func)
 
std::function< bool(std::string)> find (std::string &cmd)
 
void initialize (int segment_number=1) override
 
void finalize () override
 
void command_complete (CLICommand, bool)
 
void cli_thread_start ()
 
bool clicmd_shutdown (std::string)
 CLI Commands. More...
 
bool clicmd_kernel_info (std::string)
 
bool is_initialized () override
 
bool is_finalized () override
 
- Public Member Functions inherited from KernelComponent
 KernelComponent (Kernel *kernel, Horizon::System::runtime_module_type module_type)
 
virtual void initialize (int segment_number=1)
 
virtual void finalize ()
 
virtual bool is_initialized ()
 
virtual bool is_finalized ()
 
void set_segment_number (int64_t segment_number)
 
int64_t get_segment_number ()
 
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)
 
const std::string get_uuid_string ()
 
const std::string get_type_string ()
 
Horizon::System::SystemRoutineManagerget_system_routine_manager ()
 
Kernelget_kernel ()
 
void set_thread_cpu_id (int cpu_id)
 
int get_thread_cpu_id ()
 
void set_thread_update_rate (double rate)
 
double get_thread_update_rate ()
 
void set_total_execution_time (int time)
 
int get_total_execution_time ()
 
void calculate_and_set_cpu_load ()
 

Private Attributes

std::unordered_map< std::string, std::function< bool(std::string)> > _cli_function_map
 
std::queue< CLICommand_cli_cmd_queue
 
std::thread _cli_thread
 
std::atomic< bool > _is_initialized {false}
 
std::atomic< bool > _is_finalized {false}
 
std::atomic< bool > _is_running_command {false}
 

Constructor & Destructor Documentation

◆ CommandLineProcess()

CommandLineProcess::CommandLineProcess ( Kernel kernel)
inline
KernelComponent(Kernel *kernel, Horizon::System::runtime_module_type module_type)
Definition: Server.hpp:194
@ RUNTIME_COMMANDLINE
Definition: System.hpp:83

Member Function Documentation

◆ add_function()

void CommandLineProcess::add_function ( std::string  cmd,
std::function< bool(std::string)>  func 
)
inline
332{ _cli_function_map.insert(std::make_pair(cmd, func)); };
std::unordered_map< std::string, std::function< bool(std::string)> > _cli_function_map
Definition: Server.hpp:361

References _cli_function_map.

Referenced by initialize().

+ Here is the caller graph for this function:

◆ cli_thread_start()

void CommandLineProcess::cli_thread_start ( )
198{
200 {
201 std::string command;
202
203 process();
204#if WIN32
205 DWORD cpu = GetCurrentProcessorNumber();
206 if (get_thread_cpu_id() != (int) cpu)
208#elif __linux__
209 int cpu = sched_getcpu();
210 if (get_thread_cpu_id() != cpu)
212#endif
214
215 try {
216 while(_is_running_command.load() == true)
217 {
218 std::this_thread::sleep_for(std::chrono::seconds(1));
219 };
220 std::cout << TERMINAL_STR;
221 std::getline(std::cin, command);
222 std::flush(std::cout);
223 if (command.size()) {
224 std::vector<std::string> separated_args;
225 boost::algorithm::split(separated_args, command, boost::algorithm::is_any_of(" "));
226
227 if (find(separated_args[0]) == nullptr)
228 HLog(error) << "Command '" << separated_args[0] << "' not found.";
229 else
230 _is_running_command.exchange(true);
231 }
232 } catch(std::bad_alloc &e) {
233 HLog(error) << "Failed to allocate memory for command line input.";
235 set_shutdown_signal(SIGTERM);
236 break;
237 } catch(std::length_error &e) {
238 HLog(error) << "Command line input too long.";
240 set_shutdown_signal(SIGTERM);
241 break;
242 }
243
244 if (command.size()) {
245 queue(CLICommand((char *)command.c_str(), std::bind(&CommandLineProcess::command_complete, this, std::placeholders::_1, std::placeholders::_2)));
246 std::this_thread::sleep_for(std::chrono::microseconds(MAX_CORE_UPDATE_INTERVAL * 1)); // Sleep until core has updated.
247 } else if (feof(stdin)) {
249 set_shutdown_signal(SIGTERM);
250 }
251 }
252}
#define MAX_CORE_UPDATE_INTERVAL
Definition: Horizon.hpp:63
#define HLog(type)
Definition: Logger.hpp:122
shutdown_stages get_shutdown_stage()
Definition: Server.hpp:74
#define TERMINAL_STR
Definition: Server.hpp:60
void set_shutdown_signal(int signal)
Definition: Server.hpp:73
@ SHUTDOWN_NOT_STARTED
Definition: Server.hpp:64
@ SHUTDOWN_INITIATED
Definition: Server.hpp:65
void set_shutdown_stage(shutdown_stages new_stage)
Definition: Server.hpp:75
Definition: Server.hpp:289
std::atomic< bool > _is_running_command
Definition: Server.hpp:367
std::function< bool(std::string)> find(std::string &cmd)
Definition: Server.hpp:335
void command_complete(CLICommand, bool)
Definition: Server.hpp:344
void queue(CLICommand &&cmdMgr)
Definition: Server.hpp:331
void process()
Definition: Server.cpp:164
void set_thread_cpu_id(int cpu_id)
Definition: Server.hpp:239
void calculate_and_set_cpu_load()
Definition: Server.hpp:257
int get_thread_cpu_id()
Definition: Server.hpp:240

References _is_running_command, KernelComponent::calculate_and_set_cpu_load(), command_complete(), find(), get_shutdown_stage(), KernelComponent::get_thread_cpu_id(), HLog, MAX_CORE_UPDATE_INTERVAL, process(), queue(), set_shutdown_signal(), set_shutdown_stage(), KernelComponent::set_thread_cpu_id(), SHUTDOWN_INITIATED, SHUTDOWN_NOT_STARTED, and TERMINAL_STR.

Referenced by initialize().

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

◆ clicmd_kernel_info()

bool CommandLineProcess::clicmd_kernel_info ( std::string  )
101{
102 std::string red = "\033[31m";
103 std::string green = "\033[32m";
104 std::string reset = "\033[0m";
105
107 HLog(info) << "Horizon Kernel (C) 2024 Initialized with " << get_kernel()->get_components().size() << " components.";
108
109 while (get_kernel()->get_signal_interrupt_command_line_loop() == true) {
110 int total_execution_time = 0.0;
111
112 std::cout << "[Status]"
113 << std::setw(15) << "Component (Segment #)"
114 << std::setw(15) << "CPU #"
115 << std::setw(15) << "Execution Time"
116 << std::setw(15) << "Update Rate"
117 << std::flush << std::endl;
118 for (auto i = get_kernel()->get_components().begin(); i != get_kernel()->get_components().end(); i++) {
119 std::string online = std::string(red + "Offline" + reset);
120 if (i->second.ptr->is_initialized() == true)
121 online = std::string(green + "Online" + reset);
122 std::cout << "\r\033[K" << std::flush;
123
124 double limited_update_rate = std::round(i->second.ptr->get_thread_update_rate() * 100.0) / 100.0;
125
126 total_execution_time += i->second.ptr->get_total_execution_time();
127 std::cout << "[" << online << "]"
128 << std::setw(15) << i->second.ptr->get_type_string() << " (" << i->second.segment_number << ")"
129 << std::setw(15) << i->second.ptr->get_thread_cpu_id()
130 << std::setw(15) << i->second.ptr->get_total_execution_time() << "ns"
131 << std::setw(15) << limited_update_rate << "/s."
132 << std::flush << std::endl;
133 }
134
135 std::cout << "Total Execution Time: " << total_execution_time << "ns" << std::endl;
136
137 std::cout << "\033[" << get_kernel()->get_components().size() + 2 << "A";
138 std::this_thread::sleep_for(std::chrono::seconds(1));
139 }
140 std::cout << "\033[ " << get_kernel()->get_components().size() + 2 << "B";
141 return true;
142}
Kernel * get_kernel()
Definition: Server.hpp:237
void set_signal_interrupt_command_line_loop(bool signal)
Definition: Server.hpp:540
KernelComponents & get_components()
Definition: Server.hpp:528

References Kernel::get_components(), KernelComponent::get_kernel(), HLog, and Kernel::set_signal_interrupt_command_line_loop().

Referenced by initialize().

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

◆ clicmd_shutdown()

bool CommandLineProcess::clicmd_shutdown ( std::string  )

CLI Commands.

94{
96 set_shutdown_signal(SIGTERM);
97 return true;
98}

References set_shutdown_signal(), set_shutdown_stage(), and SHUTDOWN_INITIATED.

Referenced by initialize().

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

◆ command_complete()

void CommandLineProcess::command_complete ( CLICommand  ,
bool   
)
inline
345 {
346 fflush(stdout);
347 }

Referenced by cli_thread_start().

+ Here is the caller graph for this function:

◆ finalize()

void CommandLineProcess::finalize ( )
overridevirtual

Reimplemented from KernelComponent.

157{
158 if (_cli_thread.joinable())
159 _cli_thread.detach();
160
161 _is_finalized.exchange(true);
162}
std::atomic< bool > _is_finalized
Definition: Server.hpp:366
std::thread _cli_thread
Definition: Server.hpp:364

References _cli_thread, and _is_finalized.

◆ find()

std::function< bool(std::string)> CommandLineProcess::find ( std::string &  cmd)
inline
336 {
337 auto it = _cli_function_map.find(cmd);
338 return (it != _cli_function_map.end()) ? it->second : nullptr;
339 }

References _cli_function_map.

Referenced by cli_thread_start(), and process().

+ Here is the caller graph for this function:

◆ initialize()

void CommandLineProcess::initialize ( int  segment_number = 1)
overridevirtual

Reimplemented from KernelComponent.

145{
146
147 _cli_thread = std::thread(std::bind(&CommandLineProcess::cli_thread_start, this));
148
149 add_function("shutdown", std::bind(&CommandLineProcess::clicmd_shutdown, this, std::placeholders::_1));
150 add_function("kernel-info", std::bind(&CommandLineProcess::clicmd_kernel_info, this, std::placeholders::_1));
151 set_segment_number(segment_number);
152
153 _is_initialized.exchange(true);
154}
std::atomic< bool > _is_initialized
Definition: Server.hpp:365
void cli_thread_start()
Definition: Server.cpp:197
bool clicmd_shutdown(std::string)
CLI Commands.
Definition: Server.cpp:93
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
void set_segment_number(int64_t segment_number)
Definition: Server.hpp:206

References _cli_thread, _is_initialized, add_function(), cli_thread_start(), clicmd_kernel_info(), clicmd_shutdown(), and KernelComponent::set_segment_number().

+ Here is the call graph for this function:

◆ is_finalized()

bool CommandLineProcess::is_finalized ( )
inlineoverridevirtual

Reimplemented from KernelComponent.

358{ return _is_finalized; }

References _is_finalized.

◆ is_initialized()

bool CommandLineProcess::is_initialized ( )
inlineoverridevirtual

Reimplemented from KernelComponent.

357{ return _is_initialized; }

References _is_initialized.

◆ process()

void CommandLineProcess::process ( )

Error on this line, need to fix it. Particularly with VS Code debugger. It is probably only in the debugger because it acts wierd with line inputs. We will disable the command line on test runs for now. Exception thrown at 0x00007FFA60FAF39C in ZoneSystemTest.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000851FAFE830. Failed to process command line input: bad allocation

165{
166 while (_cli_cmd_queue.size()) {
173 try {
174 CLICommand command = _cli_cmd_queue.front();
175 _cli_cmd_queue.pop();
176
177 bool ret = false;
178 std::vector<std::string> separated_args;
179 boost::algorithm::split(separated_args, command.m_command, boost::algorithm::is_any_of(" "));
180
181 std::function<bool(std::string)> cmd_func = find(separated_args[0]);
182
183 if (cmd_func) {
184 ret = cmd_func(command.m_command);
185 _is_running_command.exchange(false);
186 }
187
188 if (command.m_finish_func != nullptr)
189 command.m_finish_func(command, ret);
190 } catch(std::exception &e) {
191 HLog(error) << "Failed to process command line input: " << e.what();
192 return;
193 }
194 }
195}
FinishFunc m_finish_func
Completion handler function.
Definition: Server.hpp:323
std::string m_command
Command string.
Definition: Server.hpp:322
std::queue< CLICommand > _cli_cmd_queue
Definition: Server.hpp:363

References _cli_cmd_queue, _is_running_command, find(), HLog, CLICommand::m_command, and CLICommand::m_finish_func.

Referenced by cli_thread_start().

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

◆ queue()

void CommandLineProcess::queue ( CLICommand &&  cmdMgr)
inline
331{ _cli_cmd_queue.push(std::move(cmdMgr)); }

References _cli_cmd_queue.

Referenced by cli_thread_start().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _cli_cmd_queue

std::queue<CLICommand> CommandLineProcess::_cli_cmd_queue
private

Referenced by process(), and queue().

◆ _cli_function_map

std::unordered_map<std::string, std::function<bool(std::string)> > CommandLineProcess::_cli_function_map
private

Referenced by add_function(), and find().

◆ _cli_thread

std::thread CommandLineProcess::_cli_thread
private

Referenced by finalize(), and initialize().

◆ _is_finalized

std::atomic<bool> CommandLineProcess::_is_finalized {false}
private

Referenced by finalize(), and is_finalized().

◆ _is_initialized

std::atomic<bool> CommandLineProcess::_is_initialized {false}
private

Referenced by initialize(), and is_initialized().

◆ _is_running_command

std::atomic<bool> CommandLineProcess::_is_running_command {false}
private

Referenced by cli_thread_start(), and process().


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