FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
dispatcher.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <string>
12#include <filesystem>
13#include <unistd.h>
14
17#include "../db.hpp"
18#include "daemon.hpp"
19
29{
30
31namespace
32{
33namespace fs = std::filesystem;
34} // anonymous namespace
35
36using ns_db::ns_portal::ns_daemon::Mode;
37
38struct Logs
39{
40 fs::path path_dir_log;
41 Logs(fs::path const& path_dir_log)
42 : path_dir_log(path_dir_log)
43 {}
44};
45
46class Dispatcher
47{
48 private:
49 Mode m_mode;
50 fs::path m_path_dir_fifo;
51 fs::path m_path_fifo_daemon;
52 fs::path m_path_file_log;
53
54 Dispatcher();
55
56 public:
57 explicit Dispatcher(pid_t pid, Mode mode, fs::path const& path_dir_app, Logs const& logs);
58
59 [[maybe_unused]] fs::path get_path_dir_fifo() const { return m_path_dir_fifo; }
60 [[maybe_unused]] fs::path get_path_fifo_daemon() const { return m_path_fifo_daemon; }
61 [[maybe_unused]] fs::path get_path_file_log() const { return m_path_file_log; }
62 // Serialization
63 friend Value<Dispatcher> deserialize(std::string_view str_raw_json) noexcept;
64 friend Value<std::string> serialize(Dispatcher const& dispatcher) noexcept;
65};
66
70inline Dispatcher::Dispatcher()
71 : m_mode(Mode::HOST)
72 , m_path_dir_fifo()
73 , m_path_fifo_daemon()
74 , m_path_file_log()
75{}
76
84inline Dispatcher::Dispatcher(pid_t pid, Mode mode, fs::path const& path_dir_app, Logs const& logs)
85 : m_mode(mode)
86 , m_path_dir_fifo(ns_fs::placeholders_replace(path_dir_app / "instance" / "{}" / "portal" / "dispatcher" / "fifo", pid))
87 , m_path_fifo_daemon(ns_fs::placeholders_replace(path_dir_app / "instance" / "{}" / "portal" / "daemon" / "{}.fifo", pid, mode.lower()))
88 , m_path_file_log(logs.path_dir_log / mode.lower() / std::format("{}.log", pid))
89{
90 fs::create_directories(m_path_file_log.parent_path());
91}
92
93
100[[maybe_unused]] [[nodiscard]] inline Value<Dispatcher> deserialize(std::string_view str_raw_json) noexcept
101{
102 Dispatcher dispatcher;
103 return_if(str_raw_json.empty(), Error("D::Empty json data"));
104 // Open DB
105 auto db = Pop(ns_db::from_string(str_raw_json));
106 // Parse path_dir_fifo
107 dispatcher.m_path_dir_fifo = Pop(db("path_dir_fifo").template value<std::string>());
108 // Parse path_fifo_daemon
109 dispatcher.m_path_fifo_daemon = Pop(db("path_fifo_daemon").template value<std::string>());
110 // Parse path_file_log
111 dispatcher.m_path_file_log = Pop(db("path_file_log").template value<std::string>());
112 return dispatcher;
113}
114
121[[maybe_unused]] [[nodiscard]] inline Value<std::string> serialize(Dispatcher const& dispatcher) noexcept
122{
123 auto db = ns_db::Db();
124 db("path_dir_fifo") = dispatcher.get_path_dir_fifo().string();
125 db("path_fifo_daemon") = dispatcher.get_path_fifo_daemon().string();
126 db("path_file_log") = dispatcher.get_path_file_log().string();
127 return db.dump();
128}
129
130} // namespace ns_db::ns_portal::ns_dispatcher
131
132/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
A type-safe wrapper around nlohmann::json for database operations.
Definition db.hpp:64
friend Value< Dispatcher > deserialize(std::string_view str_raw_json) noexcept
Deserializes JSON string into a Binds object.
Definition bind.hpp:184
friend Value< std::string > serialize(Dispatcher const &dispatcher) noexcept
Serializes a Dispatcher class into a json string.
Defines a class that manages FlatImage's portal configuration.
A database that interfaces with nlohmann json.
Enhanced error handling framework built on std::expected.
Portal command dispatcher configuration.
Value< Dispatcher > deserialize(std::string_view str_raw_json) noexcept
Deserializes a json string into a Dispatcher class.
Value< std::string > serialize(Dispatcher const &dispatcher) noexcept
Serializes a Dispatcher class into a json string.
Value< Db > from_string(S &&s)
Parses a JSON string and creates a Db object.
Definition db.hpp:496
Enhanced filesystem utilities wrapping std::filesystem.
Filesystem helpers.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44