FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
daemon.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
16#include "../../std/enum.hpp"
17#include "../db.hpp"
18
29{
30
31namespace
32{
33namespace fs = std::filesystem;
34} // anonymous namespace
35
44namespace ns_log
45{
46
47class Logs
48{
49 private:
50 fs::path m_path_dir_log;
51 fs::path m_path_file_parent;
52 fs::path m_path_file_child;
53 fs::path m_path_file_grand;
54
55 Logs();
56
57 public:
58 explicit Logs(fs::path const& path_dir_log);
59
60 [[maybe_unused]] fs::path const& get_path_dir_log() const { return m_path_dir_log; }
61 [[maybe_unused]] fs::path const& get_path_file_parent() const { return m_path_file_parent; }
62 [[maybe_unused]] fs::path const& get_path_file_child() const { return m_path_file_child; }
63 [[maybe_unused]] fs::path const& get_path_file_grand() const { return m_path_file_grand; }
64
65 friend Value<Logs> deserialize(std::string_view str_raw_json) noexcept;
66 friend Value<std::string> serialize(Logs const& logs) noexcept;
67};
68
72inline Logs::Logs()
73 : m_path_dir_log()
74 , m_path_file_parent()
75 , m_path_file_child()
76 , m_path_file_grand()
77{}
78
84inline Logs::Logs(fs::path const& path_dir_log)
85 : m_path_dir_log(path_dir_log)
86 , m_path_file_parent(path_dir_log / "daemon.log")
87 , m_path_file_child(path_dir_log / "{}" / "child.log")
88 , m_path_file_grand(path_dir_log / "{}" / "grand.log")
89{
90 fs::create_directories(m_path_file_parent.parent_path());
91}
92
93
100[[maybe_unused]] [[nodiscard]] inline Value<Logs> deserialize(std::string_view str_raw_json) noexcept
101{
102 return_if(str_raw_json.empty(), Error("D::Empty json data"));
103 // Open DB
104 auto db = Pop(ns_db::from_string(str_raw_json));
105 // Parse log directory path (optional)
106 fs::path path_dir_log = Pop(db("path_dir_log").template value<std::string>());
107 // Create Logs object with the parsed path
108 Logs logs(path_dir_log);
109 // Parse log file paths (optional)
110 logs.m_path_file_parent = Pop(db("path_file_parent").template value<std::string>());
111 logs.m_path_file_child = Pop(db("path_file_child").template value<std::string>());
112 logs.m_path_file_grand = Pop(db("path_file_grand").template value<std::string>());
113 return logs;
114}
115
122[[maybe_unused]] [[nodiscard]] inline Value<std::string> serialize(Logs const& logs) noexcept
123{
124 auto db = ns_db::Db();
125 db("path_dir_log") = logs.m_path_dir_log.string();
126 db("path_file_parent") = logs.m_path_file_parent.string();
127 db("path_file_child") = logs.m_path_file_child.string();
128 db("path_file_grand") = logs.m_path_file_grand.string();
129 return db.dump();
130}
131
132} // namespace ns_log
133
134
135// daemon mode enum
136ENUM(Mode, HOST, GUEST);
137
138class Daemon
139{
140 private:
141 Mode m_mode;
142 pid_t m_pid_reference;
143 fs::path m_path_bin_daemon;
144 fs::path path_fifo_listen;
145
146 Daemon();
147
148 public:
149 Daemon(Mode mode, fs::path const& path_bin_daemon, fs::path const& path_dir_fifo);
150
151 [[maybe_unused]] pid_t get_pid_reference() const { return m_pid_reference; }
152 [[maybe_unused]] fs::path const& get_path_bin_daemon() const { return m_path_bin_daemon; }
153 [[maybe_unused]] fs::path const& get_path_fifo_listen() const { return path_fifo_listen; }
154 [[maybe_unused]] Mode get_mode() const { return m_mode; }
155
156 friend Value<Daemon> deserialize(std::string_view str_raw_json) noexcept;
157 friend Value<std::string> serialize(Daemon const& daemon) noexcept;
158};
159
163inline Daemon::Daemon()
164 : m_mode(Mode::HOST)
165 , m_pid_reference(0)
166 , m_path_bin_daemon()
167 , path_fifo_listen()
168{}
169
173inline Daemon::Daemon(Mode mode, fs::path const& path_bin_daemon, fs::path const& path_dir_fifo)
174 : m_mode(mode)
175 , m_pid_reference(getpid())
176 , m_path_bin_daemon(path_bin_daemon)
177 , path_fifo_listen(path_dir_fifo / "daemon" / std::format("{}.fifo", m_mode.lower()))
178{}
179
186[[maybe_unused]] [[nodiscard]] inline Value<Daemon> deserialize(std::string_view str_raw_json) noexcept
187{
188 Daemon daemon;
189 return_if(str_raw_json.empty(), Error("D::Empty json data"));
190 // Open DB
191 auto db = Pop(ns_db::from_string(str_raw_json));
192 // Parse pid_reference (optional, defaults to current PID)
193 std::string pid_reference = Pop(db("pid_reference").template value<std::string>());
194 daemon.m_pid_reference = Try(std::stoi(pid_reference));
195 // Parse path_bin_daemon (optional, defaults to empty path)
196 daemon.m_path_bin_daemon = Pop(db("path_bin_daemon").template value<std::string>());
197 // Parse paths (optional, defaults to empty paths)
198 daemon.path_fifo_listen = Pop(db("path_fifo_listen").template value<std::string>());
199 // Parse mode (optional, defaults to Host)
200 daemon.m_mode = Pop(Mode::from_string(Pop(db("mode").template value<std::string>())));
201 return daemon;
202}
203
210[[maybe_unused]] [[nodiscard]] inline Value<std::string> serialize(Daemon const& daemon) noexcept
211{
212 auto db = ns_db::Db();
213 db("pid_reference") = std::to_string(daemon.m_pid_reference);
214 db("path_bin_daemon") = daemon.m_path_bin_daemon.string();
215 db("path_fifo_listen") = daemon.path_fifo_listen.string();
216 db("mode") = std::string(daemon.m_mode);
217 return db.dump();
218}
219
220} // namespace ns_db::ns_portal::ns_daemon
221
222/* 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< std::string > serialize(Daemon const &daemon) noexcept
Serializes a Daemon class into a json string.
Definition daemon.hpp:210
friend Value< Daemon > deserialize(std::string_view str_raw_json) noexcept
Deserializes JSON string into a Binds object.
Definition bind.hpp:184
friend Value< Logs > deserialize(std::string_view str_raw_json) noexcept
Deserializes JSON string into a Binds object.
Definition bind.hpp:184
friend Value< std::string > serialize(Logs const &logs) noexcept
Serializes a Logs class into a json string.
Definition daemon.hpp:122
A database that interfaces with nlohmann json.
Custom enumeration class.
Enhanced error handling framework built on std::expected.
Value< Logs > deserialize(std::string_view str_raw_json) noexcept
Deserializes a json string into a Logs class.
Definition daemon.hpp:100
Value< std::string > serialize(Logs const &logs) noexcept
Serializes a Logs class into a json string.
Definition daemon.hpp:122
Portal daemon configuration and management.
Value< std::string > serialize(Daemon const &daemon) noexcept
Serializes a Daemon class into a json string.
Definition daemon.hpp:210
Value< Daemon > deserialize(std::string_view str_raw_json) noexcept
Deserializes a json string into a Daemon class.
Definition daemon.hpp:186
Value< Db > from_string(S &&s)
Parses a JSON string and creates a Db object.
Definition db.hpp:496
Multi-level logging system with file and stdout sinks.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44