Server¶
server::update¶
-
gboolean server::update(gpointer)¶
Main server update callback.
This function is a periodic callback invoked at a fixed interval to manage and handle incoming client requests. It acts as the central control point for processing client interactions and orchestrating server-side operations within the specified time intervals.
server::GateIO¶
-
class GateIO¶
Implements the socket communication layer with the outside world.
Operable only with a single client. As soon as client connection is detected it begins listening on the specified port number for incoming client requests, collects and encapsulates them into tasks (see Task). The incoming tasks are extracted and handled by the top-level logic TaskResolver in the main thread. Once the tasks are resolved by the TaskResolver, they are returned to be sent back to the client app as a response. Moving Task across threads happens in server::update.
Note
The GateIO instance should be created and managed from the main thread, while its internal processing and IO operations are performed asynchronously in a separate thread. This separation ensures smooth IO behavior and responsiveness of the application.
GateIO is not started automatically upon creation, you have to use the ‘start’ method with the port number.
The socket is initialized in a non-blocking mode to function properly in a multithreaded environment.
Public Functions
-
inline bool is_running() const¶
Returns a bool indicating whether or not the port listening process is currently running.
- Returns:
True if the port listening process is running, false otherwise.
-
void take_received_tasks(std::vector<TaskPtr> &tasks)¶
Transfers ownership of received tasks to the caller.
This method moves all received tasks from the internal storage to the provided vector. After calling this method, the internal list of received tasks will be cleared.
- Parameters:
tasks – A reference to a vector where the received tasks will be moved.
-
void move_tasks_to_send_queue(std::vector<TaskPtr> &tasks)¶
Moves tasks to the send queue.
This method moves the tasks to the send queue. Each task is moved from the input vector to the send queue, and the input vector remains empty after the operation.
- Parameters:
tasks – A reference to a vector containing the tasks to be moved to the send queue.
-
void print_logs()¶
Prints log messages for the GateIO.
Note
Must be called from the main thread since it’s invoke std::cout. Calling this method from other threads may result in unexpected behavior.
-
void start(int port_num)¶
Starts the server on the specified port number.
This method starts the server to listen for incoming connections on the specified port number. Once started,the server will continue running in a separate thread and will accept connection only from a single client attempting to connect to the specified port.
- Parameters:
port_num – The port number on which the server will listen for incoming connection.
-
void stop()¶
Stops the server and terminates the listening thread.
This method stops the server and terminates the listening thread. After calling this method, the server will no longer accept incoming connections and the listening thread will be terminated.
Note
This method should be called when the server needs to be shut down gracefully.
server::Task¶
-
namespace comm¶
-
class Task¶
Implements the server task.
This structure aids in encapsulating the client request, request result, and result status. It generates a JSON data structure to be sent back to the client as a response.
Public Functions
-
Task(int job_id, comm::CMD cmd, const std::string &options = "")¶
Constructs a new Task object.
- Parameters:
job_id – The ID of the job associated with the task.
cmd – The command ID (see comm::CMD) associated with the task.
options – Additional options for the task (default: empty string).
-
inline int job_id() const¶
Gets the job ID associated with the task.
- Returns:
The job ID.
-
inline comm::CMD cmd() const¶
Gets the command ID associated with the task.
- Returns:
The command ID (see comm::CMD).
-
void chop_num_sent_bytes_from_response_buffer(std::size_t bytes_sent_num)¶
Removes the specified number of bytes from the response buffer.
This method removes the specified number of bytes from the beginning of the response buffer. It is typically used after sending a response to discard the bytes that have been sent.
- Parameters:
bytes_sent_num – The number of bytes to remove from the response buffer.
-
bool options_match(const std::unique_ptr<Task> &other)¶
Checks if the options of this task match the options of another task.
This method compares the options of this task with the options of another task and returns true if they match, otherwise returns false.
- Parameters:
other – The other task to compare options with.
- Returns:
True if the options match, false otherwise.
-
inline const std::string &response_buffer() const¶
Retrieves the response buffer.
This method returns a constant reference to the response buffer, which contains the data after task execution.
- Returns:
A constant reference to the response buffer.
-
inline bool is_finished() const¶
Checks if the task has finished execution.
This method returns true if the task has finished execution; otherwise, it returns false.
- Returns:
True if the task has finished execution, false otherwise.
-
inline bool has_error() const¶
Checks if the task has encountered an error.
This method returns true if the task has encountered an error; otherwise, it returns false.
- Returns:
True if the task has encountered an error, false otherwise.
-
inline const std::string &error() const¶
Retrieves the error message associated with the task.
This method returns the error message associated with the task, if any.
- Returns:
A constant reference to the error message string.
-
inline std::size_t orig_reponse_bytes_num() const¶
Retrieves the original number of response bytes.
This method returns the original number of response bytes before any chopping operation.
- Returns:
The original number of response bytes.
-
inline bool is_response_fully_sent() const¶
Checks if the response has been fully sent.
This method returns true if the entire response has been successfully sent, otherwise it returns false.
- Returns:
True if the response has been fully sent, false otherwise.
-
void set_fail(const std::string &error)¶
Marks the task as failed with the specified error message.
This method sets the task’s error message to the provided error string, indicating that the task has failed.
- Parameters:
error – The error message describing the reason for the task’s failure.
-
void set_success()¶
Marks the task as successfully completed.
-
void set_success(std::string &&result)¶
Marks the task as successfully completed with the specified result.
This method marks the task as successfully completed and stores the result. It takes an rvalue reference to a string, allowing for efficient move semantics.
- Parameters:
result – An rvalue reference to a string describing the result of the task execution. The content of this string will be moved into the task’s result storage.
-
std::string info(bool skip_duration = false) const¶
Generates a string containing information about the task.
This method generates a string containing information about the task, including its identifier, command, options, and optionally its duration.
- Parameters:
skip_duration – If true, the duration information will be omitted from the string.
- Returns:
A string containing information about the task.
-
inline const comm::TelegramHeader &telegram_header() const¶
Retrieves the TelegramHeader associated with the task.
This method returns a reference to the TelegramHeader associated with the task.
- Returns:
A reference to the TelegramHeader associated with the task.
-
Task(int job_id, comm::CMD cmd, const std::string &options = "")¶
server::TaskResolver¶
-
class TaskResolver¶
Resolve server task.
Process and resolve server task, store result and status for processed task.
Public Functions
-
TaskResolver() = default¶
Default constructor for TaskResolver.
-
void own_task(TaskPtr &&task)¶
Takes ownership of a task.
This method takes ownership of a task by moving it into the TaskResolver’s internal task queue. After calling this method, the task will be owned and managed by the TaskResolver.
Note
After calling this method, the caller should avoid accessing or modifying the task object.
- Parameters:
task – The task to take ownership of. After calling this method, the task object will be in a valid but unspecified state.
-
bool update(ezgl::application *app)¶
Resolve queued tasks.
- Parameters:
app – A pointer to the ezgl::application object representing the application instance.
-
void take_finished_tasks(std::vector<TaskPtr> &tasks)¶
Extracts finished tasks from the internal task queue.
This function removes finished tasks from the internal task queue and appends them to the provided vector. After this operation, the internal task queue will no longer hold the extracted tasks.
- Parameters:
tasks – A reference to a vector where the finished tasks will be appended.
-
TaskResolver() = default¶
-
struct CritPathsResult¶
Structure to retain the calculation result of the critical path.
It contains the critical path list and the generated report as a string.
Public Functions
-
inline bool is_valid() const¶
Checks if the CritPathsResult contains report.
- Returns:
True if contains report, false otherwise.
-
inline bool is_valid() const¶
-
CritPathsResultPtr server::calc_critical_path(const std::string &report_type, int crit_path_num, e_timing_report_detail details_level, bool is_flat_routing)¶
Helper function to calculate critical path timing report with specified parameters.
Calculates the critical path.
This function calculates the critical path based on the provided parameters.
- Parameters:
type – The type of the critical path. Must be either “setup” or “hold”.
crit_path_num – The max number of critical paths to record.
details_level – The level of detail for the timing report. See e_timing_report_detail.
is_flat_routing – Indicates whether flat routing should be used.
- Returns:
A
CritPathsResultPtr
which is a pointer to the result of the critical path calculation (see CritPathsResult).
comm::Telegram¶
-
class TelegramHeader¶
The fixed size byte sequence where the metadata of a telegram message is stored.
This structure is used to describe the message frame sequence in order to successfully extract it. The TelegramHeader structure follows this format:
[ 4 bytes ]: SIGNATURE - A 4-byte constant sequence “I”, “P”, “A”, “\0” which indicates the valid start of a TelegramHeader.
[ 4 bytes ]: DATA_LENGTH - A 4-byte field where the data length is stored, allowing for proper identification of the start and end of the TelegramFrame sequence.
[ 4 bytes ]: DATA_CHECKSUM - A 4-byte field where the data checksum is stored to validate the attached data.
[ 1 byte ]: COMPRESSOR_ID - A 1-byte field where the compressor ID is stored. If it’s null, it means the telegram body is not compressed (in text/json format). Otherwise, the telegram body is compressed. Currently, only zlib compression for the telegram body is supported, which is specified with COMPRESSOR_ID=’z’.
Note
: The DATA_CHECKSUM field can be used to check the integrity of the telegram body on the client app side.
Public Functions
-
explicit TelegramHeader(uint32_t length, uint32_t check_sum, uint8_t compressor_id = 0)¶
Constructs a TelegramHeader object with the specified length, checksum, and optional compressor ID.
- Parameters:
length – The length of the telegram body.
check_sum – The checksum of the telegram body.
compressor_id – The compressor ID used for compressing the telegram body (default is 0).
-
explicit TelegramHeader(const ByteArray &buffer)¶
Constructs a TelegramHeader object from the provided byte buffer.
This constructor initializes a TelegramHeader object using the length and checksum information taken from the provided byte buffer.
- Parameters:
buffer – The ByteArray containing the header data of the telegram.
-
inline bool is_valid() const¶
To checks if the TelegramHeader is valid.
- Returns:
True if the TelegramHeader is valid, false otherwise.
-
inline const ByteArray &buffer() const¶
Retrieves the buffer associated with the TelegramHeader.
This method returns a constant reference to the buffer associated with the TelegramHeader.
- Returns:
A constant reference to the buffer.
-
inline uint32_t body_bytes_num() const¶
Retrieves the number of bytes in the telegram body.
- Returns:
The number of bytes in the telegram body.
-
inline uint32_t body_check_sum() const¶
Retrieves the checksum of telegram body.
- Returns:
The checksum of telegram body.
-
inline uint8_t compressor_id() const¶
Retrieves the compressor ID used for compressing telegram body.
- Returns:
The compressor ID of the telegram body. 0 if the telegram body is not compressed.
-
inline bool is_body_compressed() const¶
Checks if the telegram body is compressed.
- Returns:
True if the telegram body is compressed; otherwise, false.
Public Static Functions
-
static comm::TelegramHeader construct_from_body(const std::string_view &body, uint8_t compressor_id = 0)¶
Constructs a TelegramHeader based on the provided body data.
- Parameters:
body – The body data used to calculate the size and checksum.
compressor_id – The ID of the compressor used for compression (default is 0, means no compressor is used).
- Returns:
A TelegramHeader object constructed from the provided body data.
-
static inline constexpr size_t size()¶
Returns the total size of the TelegramHeader.
This static constexpr method returns the total size of the TelegramHeader, including all its components.
- Returns:
The total size of the TelegramHeader.
-
struct TelegramFrame¶
Structure representing a TelegramFrame.
A TelegramFrame consists of a TelegramHeader followed by data.
Public Members
-
TelegramHeader header¶
header The TelegramHeader containing metadata about the telegram message.
-
TelegramHeader header¶
-
class TelegramBuffer¶
Implements Telegram Buffer as a wrapper over BytesArray.
It aggregates received bytes and assists in extracting telegram frames ( TelegramFrame ) from the raw byte buffer.
Public Functions
-
inline explicit TelegramBuffer(std::size_t size_hint = DEFAULT_SIZE_HINT)¶
Constructs a TelegramBuffer object with a specified size hint.
This constructor initializes a TelegramBuffer object with a specified size hint for the raw buffer.
-
inline bool empty()¶
Check if internal byte buffer is empty.
- Returns:
true if the internal byte buffer is empty, false otherwise.
-
inline void clear()¶
Clear internal byte buffer.
-
void append(const ByteArray &data)¶
Append bytes to the internal byte buffer.
- Parameters:
data – The byte array whose contents will be appended to internal byte buffer.
-
void take_telegram_frames(std::vector<TelegramFramePtr> &frames)¶
Extracts well-formed telegram frames from the internal byte buffer.
- Parameters:
frames – A reference to a vector where the extracted telegram frames will be stored.
-
void take_errors(std::vector<std::string> &errors)¶
Takes errors from the internal storage.
This function retrieves errors stored internally and moves them into the provided vector. After calling this function, the internal error storage will be cleared.
Note
After calling this function, the internal error storage will be cleared.
- Parameters:
errors – A vector to which the errors will be moved.
-
inline explicit TelegramBuffer(std::size_t size_hint = DEFAULT_SIZE_HINT)¶
-
class ByteArray : public std::vector<char>¶
ByteArray is a simple wrapper over std::vector<char> that provides a user-friendly interface for manipulating array data..
Public Functions
-
inline explicit ByteArray(const char *data)¶
Constructs a ByteArray from a null-terminated C string.
Constructs a ByteArray object from the specified null-terminated C string. The constructor interprets the input string as a sequence of bytes until the null terminator ‘\0’ is encountered, and initializes the ByteArray with those bytes.
- Parameters:
data – A pointer to the null-terminated C string from which to construct the ByteArray.
-
inline ByteArray(const char *data, std::size_t size)¶
Constructs a ByteArray from a raw character array.
Constructs a ByteArray object from the specified raw character array, with the given size. This constructor interprets the input data as a sequence of bytes and initializes the ByteArray with those bytes.
- Parameters:
data – A pointer to the raw character array from which to construct the ByteArray.
size – The size of the raw character array, in bytes.
-
inline explicit ByteArray(std::size_t size_hint = DEFAULT_SIZE_HINT)¶
Constructs a byte array with the specified size hint.
This constructor initializes the byte array with an initial capacity determined by the size hint.
- Parameters:
size_hint – The initial capacity hint for the byte array.
-
template<typename Iterator>
inline ByteArray(Iterator first, Iterator last)¶ Constructs a byte array from the elements in the range [first, last).
This constructor initializes the byte array with the elements in the range [first, last), where
first
andlast
are iterators defining the range.- Template Parameters:
Iterator – The type of iterator used to specify the range.
- Parameters:
first – An iterator to the first element in the range.
last – An iterator to the last element in the range.
-
inline void append(const ByteArray &appendix)¶
Appends the content of another byte array to the end of this byte array.
This function adds all the bytes from the specified byte array
appendix
to the end of this byte array.- Parameters:
appendix – The byte array whose content is to be appended.
-
inline void append(char b)¶
Appends a byte to the end of the byte array.
This function adds the specified byte to the end of the byte array.
- Parameters:
b – The byte to append to the byte array.
-
inline std::pair<bool, std::size_t> find_sequence(const char *sequence, std::size_t sequence_size)¶
Finds the position of the specified sequence in the byte array.
This function searches for the specified sequence of characters within the byte array. If the sequence is found, it returns a pair containing
true
and the starting index of the sequence. If the sequence is not found, it returns a pair containingfalse
and0
.- Parameters:
sequence – A pointer to the sequence of characters to search for.
sequence_size – The size of the sequence to search for.
- Returns:
A
std::pair
where the first element is a boolean indicating whether the sequence was found (true
) or not (false
), and the second element is the starting index of the sequence if found.
-
inline operator std::string_view() const¶
Converts the container to a std::string_view.
This operator allows the container to be implicitly converted to a
std::string_view
, providing a non-owning view into the container’s data.- Returns:
A
std::string_view
representing the container’s data.
-
inline uint32_t calc_check_sum()¶
Calculates the checksum of the elements in the container.
This function iterates over each element in the container and adds their unsigned integer representations to the sum. The result is returned as a 32-bit unsigned integer.
- Returns:
The checksum of the elements in the container.
Public Static Functions
-
template<typename T>
static inline uint32_t calc_check_sum(const T &iterable)¶ Calculates the checksum of the elements in the given iterable container.
This function template calculates the checksum of the elements in the provided iterable container. It iterates over each element in the container and adds their unsigned integer representations to the sum. The result is returned as a 32-bit unsigned integer.
- Template Parameters:
T – The type of the iterable container.
- Parameters:
iterable – The iterable container whose elements checksum is to be calculated.
- Returns:
The checksum of the elements in the iterable container.
-
inline explicit ByteArray(const char *data)¶
Parsers¶
-
class TelegramOptions¶
Option class Parser.
Parse the string of options in the format “TYPE:KEY1:VALUE1;TYPE:KEY2:VALUE2”, for example “int:path_num:11;string:path_type:debug;int:details_level:3;bool:is_flat_routing:0”. It provides a simple interface to check value presence and access them.
Public Functions
-
TelegramOptions(const std::string &data, const std::vector<std::string> &expected_keys)¶
Constructs a TelegramOptions object with the provided data and expected keys.
This constructor initializes a TelegramOptions object with the given data string and a vector of expected keys. It parses the data string and validates that such data has all required keys. If some keys are absent, it collects the errors.
- Parameters:
data – The data string containing the options.
expected_keys – A vector of strings representing the expected keys in the options.
-
inline bool has_errors() const¶
Checks if there are any errors present.
This function returns true if there are errors present in the error container, otherwise it returns false.
- Returns:
True if there are errors present, false otherwise.
-
std::map<std::size_t, std::set<std::size_t>> get_map_of_sets(const std::string &key)¶
Retrieves a map of sets associated with the specified key.
This function retrieves a map of sets associated with the specified key.
Note
The map of sets is used to store the critical path index (map key) and the associated set of selected sub-path element indexes (map value).
- Parameters:
key – The key of the map of sets to retrieve.
- Returns:
The map of sets associated with the specified key.
-
std::string get_string(const std::string &key)¶
Retrieves the string associated with the specified key.
This function retrieves the string associated with the specified key. The key is used to identify the desired string value.
- Parameters:
key – The key of the string to retrieve.
- Returns:
The string associated with the specified key.
-
int get_int(const std::string &key, int fail_value)¶
Retrieves the integer value associated with the specified key.
This function retrieves the integer value associated with the specified key. If the key is found, its corresponding integer value is returned. If the key is not found, the specified fail_value is returned instead.
- Parameters:
key – The key whose associated integer value is to be retrieved.
fail_value – The value to return if the key is not found.
- Returns:
The integer value associated with the specified key, or fail_value if the key is not found.
-
bool get_bool(const std::string &key, bool fail_value)¶
Retrieves the boolean value associated with the specified key.
This function retrieves the boolean value associated with the specified key.
- Parameters:
key – The key whose associated boolean value is to be retrieved.
fail_value – The value to return if the key is not found.
- Returns:
The boolean value associated with the specified key, or fail_value if the key is not found.
-
std::string errors_str() const¶
Retrieves a concatenated string of all errors stored in the container.
This function retrieves a concatenated string of all errors stored in the container. It concatenates all error strings stored in the container and returns the result. If there are no errors stored in the container, an empty string is returned.
- Returns:
A concatenated string of all errors stored in the container.
-
TelegramOptions(const std::string &data, const std::vector<std::string> &expected_keys)¶
-
class TelegramParser¶
Dummy JSON parser.
This class provides helper methods to extract values for a keys as “JOB_ID”, “CMD”, “OPTIONS”, “DATA”, or “STATUS from a JSON schema structured as follows: {JOB_ID:num, CMD:enum, OPTIONS:string, DATA:string, STATUS:num}.
Public Static Functions
-
static std::optional<int> try_extract_field_job_id(const std::string &message)¶
Attempts to extract the JOB_ID field from a given message.
This function parses the provided message and attempts to extract the JOB_ID field from it. If the JOB_ID field is found and successfully extracted, it is returned as an optional integer. If the JOB_ID field is not found or cannot be parsed as an integer, an empty optional is returned.
- Parameters:
message – The message from which to extract the JOB_ID field.
- Returns:
An optional integer containing the extracted JOB_ID if successful, otherwise an empty optional.
-
static std::optional<int> try_extract_field_cmd(const std::string &message)¶
Attempts to extract the CMD field from a given message.
This function parses the provided message and attempts to extract the CMD field from it. If the CMD field is found and successfully extracted, it is returned as an optional integer. If the CMD field is not found or cannot be parsed as an integer, an empty optional is returned.
- Parameters:
message – The message from which to extract the CMD field.
- Returns:
An optional integer containing the extracted CMD if successful, otherwise an empty optional.
-
static std::optional<std::string> try_extract_field_options(const std::string &message)¶
Attempts to extract the OPTIONS field from a given message.
This function parses the provided message and attempts to extract the OPTIONS field from it. If the OPTIONS field is found and successfully extracted, it is returned as an optional string. If the OPTIONS field is not found an empty optional is returned.
- Parameters:
message – The message from which to extract the OPTIONS field.
- Returns:
An optional string containing the extracted OPTIONS if successful, otherwise an empty optional.
-
static std::optional<std::string> try_extract_field_data(const std::string &message)¶
Attempts to extract the DATA field from a given message.
This function parses the provided message and attempts to extract the DATA field from it. If the DATA field is found and successfully extracted, it is returned as an optional string. If the DATA field is not found an empty optional is returned.
- Parameters:
message – The message from which to extract the DATA field.
- Returns:
An optional string containing the extracted DATA if successful, otherwise an empty optional.
-
static std::optional<int> try_extract_field_status(const std::string &message)¶
Attempts to extract the STATUS field from a given message.
This function parses the provided message and attempts to extract the STATUS field from it. If the STATUS field is found and successfully extracted, it is returned as an optional integer. If the STATUS field is not found or cannot be parsed as an integer, an empty optional is returned.
- Parameters:
message – The message from which to extract the STATUS field.
- Returns:
An optional integer containing the extracted STATUS if successful, otherwise an empty optional.
-
static std::optional<int> try_extract_field_job_id(const std::string &message)¶
Compression utils¶
-
std::optional<std::string> try_compress(const std::string &decompressed)¶
Compresses the input sequence using zlib.
This function takes a string representing the decompressed data as input and compresses it using zlib. If compression is successful, the compressed data is returned as an optional string. If compression fails, an empty optional is returned.
- Parameters:
decompressed – The input string representing the decompressed data.
- Returns:
An optional string containing the compressed data if compression is successful, or an empty optional if compression fails.
-
std::optional<std::string> try_decompress(const std::string &compressed)¶
Decompresses the compressed sequence using zlib.
This function takes a string representing the compressed data as input and decompresses it using zlib. If decompression is successful, the decompressed data is returned as an optional string. If decompression fails, an empty optional is returned.
- Parameters:
compressed – The input string representing the compressed data.
- Returns:
An optional string containing the decompressed data if decompression is successful, or an empty optional if decompression fails.