FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
message.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <string>
12#include <vector>
13#include <filesystem>
14
17#include "../db.hpp"
18
29{
30
31namespace
32{
33namespace fs = std::filesystem;
34} // anonymous namespace
35
36class Message
37{
38 private:
39 std::vector<std::string> m_command;
40 fs::path m_stdin;
41 fs::path m_stdout;
42 fs::path m_stderr;
43 fs::path m_exit;
44 fs::path m_pid;
45 std::vector<std::string> m_environment;
46
47 Message();
48
49 public:
50 Message(pid_t pid
51 , std::vector<std::string> const& command
52 , fs::path const& path_dir_fifo
53 , std::vector<std::string> const& environment
54 );
55
56 [[maybe_unused]] std::vector<std::string> const& get_command() const { return m_command; }
57 [[maybe_unused]] fs::path get_stdin() const { return m_stdin; }
58 [[maybe_unused]] fs::path get_stdout() const { return m_stdout; }
59 [[maybe_unused]] fs::path get_stderr() const { return m_stderr; }
60 [[maybe_unused]] fs::path get_exit() const { return m_exit; }
61 [[maybe_unused]] fs::path get_pid() const { return m_pid; }
62 [[maybe_unused]] std::vector<std::string> const& get_environment() const { return m_environment; }
63
64 friend Value<Message> deserialize(std::string_view str_raw_json) noexcept;
65 friend Value<std::string> serialize(Message const& message) noexcept;
66};
67
71inline Message::Message()
72 : m_command()
73 , m_stdin()
74 , m_stdout()
75 , m_stderr()
76 , m_exit()
77 , m_pid()
78 , m_environment()
79{}
80
81
85inline Message::Message(pid_t pid
86 , std::vector<std::string> const& command
87 , fs::path const& path_dir_fifo
88 , std::vector<std::string> const& environment
89 ) : m_command(command)
90 , m_stdin(ns_fs::placeholders_replace(path_dir_fifo / "{}" / "stdin.fifo", pid))
91 , m_stdout(ns_fs::placeholders_replace(path_dir_fifo / "{}" / "stdout.fifo", pid))
92 , m_stderr(ns_fs::placeholders_replace(path_dir_fifo / "{}" / "stderr.fifo", pid))
93 , m_exit(ns_fs::placeholders_replace(path_dir_fifo / "{}" / "exit.fifo", pid))
94 , m_pid(ns_fs::placeholders_replace(path_dir_fifo / "{}" / "pid.fifo", pid))
95 , m_environment(environment)
96{}
97
104[[maybe_unused]] [[nodiscard]] inline Value<Message> deserialize(std::string_view str_raw_json) noexcept
105{
106 Message message;
107 return_if(str_raw_json.empty(), Error("D::Empty json data"));
108 // Open DB
109 auto db = Pop(ns_db::from_string(str_raw_json));
110 // Parse command (required)
111 message.m_command = Pop(db("command").template value<std::vector<std::string>>());
112 // Parse FIFO paths (required)
113 message.m_stdin = Pop(db("stdin").template value<std::string>());
114 message.m_stdout = Pop(db("stdout").template value<std::string>());
115 message.m_stderr = Pop(db("stderr").template value<std::string>());
116 message.m_exit = Pop(db("exit").template value<std::string>());
117 message.m_pid = Pop(db("pid").template value<std::string>());
118 // Parse environment variables (required)
119 message.m_environment = Pop(db("environment").template value<std::vector<std::string>>());
120 return message;
121}
122
129[[maybe_unused]] [[nodiscard]] inline Value<std::string> serialize(Message const& message) noexcept
130{
131 auto db = ns_db::Db();
132 db("command") = message.m_command;
133 db("stdin") = message.get_stdin().string();
134 db("stdout") = message.get_stdout().string();
135 db("stderr") = message.get_stderr().string();
136 db("exit") = message.get_exit().string();
137 db("pid") = message.get_pid().string();
138 db("environment") = message.get_environment();
139 return db.dump();
140}
141
142} // namespace ns_db::ns_portal::ns_message
143
144/* 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(Message const &message) noexcept
Serializes a Message class into a json string.
Definition message.hpp:129
friend Value< Message > deserialize(std::string_view str_raw_json) noexcept
Deserializes JSON string into a Binds object.
Definition bind.hpp:184
A database that interfaces with nlohmann json.
Enhanced error handling framework built on std::expected.
Portal IPC message serialization and deserialization.
Value< Message > deserialize(std::string_view str_raw_json) noexcept
Deserializes a json string into a Message class.
Definition message.hpp:104
Value< std::string > serialize(Message const &message) noexcept
Serializes a Message class into a json string.
Definition message.hpp:129
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