dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Message.hpp
1// SPDX-FileCopyrightText: 2025-2026 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <chrono>
13#include <concepts>
14#include <memory>
15#include <vector>
16
17// Project includes
18#include "../MimeType.hpp"
19#include "../ports/Kind.hpp"
21#include <dfx-utilities/UUIDGenerator.hpp>
22
23namespace dfx::Core
24{
25class Message;
27using MessagePtr = std::unique_ptr<Message>;
28
30template<typename T>
31concept DerivedFromMessage = std::derived_from<T, Message>;
32
33namespace details
34{
35class Unpacker;
36} // !namespace details
37
39struct blank_construct_t { explicit constexpr blank_construct_t() noexcept = default; };
42
86{
87 friend class details::Unpacker;
88
89public:
93 virtual ~Message();
94
97
99 Utils::UUID const & uuid() const noexcept { return _uuid; }
101 Utils::UUID const & parentUuid() const noexcept { return _parentUuid; }
103 std::vector<std::string> const & pathTaken() const noexcept { return _pathTaken; }
106 std::chrono::microseconds timestamp() const noexcept { return _timestamp; }
107
109
118 template<typename Clock = std::chrono::steady_clock>
119 requires std::chrono::is_clock_v<Clock>
120 std::chrono::time_point<Clock, std::chrono::microseconds> timestampToTimePoint() const noexcept
121 { return std::chrono::time_point<Clock, std::chrono::microseconds>(_timestamp); }
122
129 void addToPathTaken(std::string path) { _pathTaken.emplace_back(std::move(path)); }
130
133
135 bool hasMimeType() const noexcept { return _mimeType.has_value(); }
138 MimeType mimeType() const noexcept { return _mimeType.value_or(MimeType::OctetStream); }
146 void resetMimeType() { _mimeType.reset(); }
147
149
151 virtual Kind kind() const = 0;
152
158 virtual MessagePtr clone() const = 0;
159
160public:
163 template<DerivedFromMessage T>
164 bool is() const noexcept { return dynamic_cast<T const *>(this) != nullptr; }
165
170 template<DerivedFromMessage T>
171 T & as() noexcept { return static_cast<T &>(*this); }
172
177 template<DerivedFromMessage T>
178 T const & as() const noexcept { return static_cast<T const &>(*this); }
179
180public:
187 template<DerivedFromMessage T, typename ... Args>
188 static std::unique_ptr<T> create(Args && ... args)
189 { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); }
190
191public:
197 static clockid_t timestampClock;
198
199protected:
210 virtual void cloneFromBase(Message const & base);
211
212protected:
215 explicit Message(blank_construct_t) noexcept {}
216
226 explicit Message(Message const * parent = nullptr);
227
234 explicit Message(MimeType mimeType, Message const * parent = nullptr);
235
236private:
237 Utils::UUID _uuid{};
238 Utils::UUID _parentUuid{};
239 std::vector<std::string> _pathTaken;
240 std::chrono::microseconds _timestamp{};
241
242private:
243 std::optional<MimeType> _mimeType;
244};
245} // !namespace dfx::Core
Convenience macros to explicitly control copy and move semantics.
Abstract base class for messages exchanged between nodes.
Definition Message.hpp:86
virtual Kind kind() const =0
Connectivity domain of this message (data/control "network").
MimeType mimeType() const noexcept
Get the message MIME type. If no explicit MIME type is set, returns dfx::Core::MimeType::OctetStream.
Definition Message.hpp:138
bool is() const noexcept
Check whether this message is of a given derived type.
Definition Message.hpp:164
DFX_DISABLE_COPY_AND_MOVE(Message)
Messages are non-copyable and non-movable.
static clockid_t timestampClock
Clock used by the constructor to capture timestamps.
Definition Message.hpp:197
static std::unique_ptr< T > create(Args &&... args)
Convenience allocator for derived message types.
Definition Message.hpp:188
void setMimeType(MimeType mimeType)
Set the message MIME type.
Utils::UUID const & parentUuid() const noexcept
UUID of the parent message if created from one, otherwise default UUID value.
Definition Message.hpp:101
std::vector< std::string > const & pathTaken() const noexcept
Sequence of "node.port" hops this message has traversed.
Definition Message.hpp:103
virtual MessagePtr clone() const =0
Clone this message (polymorphic deep copy).
virtual void cloneFromBase(Message const &base)
Copy the base-class state from another message.
void addToPathTaken(std::string path)
Append one hop to the message path trace.
Definition Message.hpp:129
T const & as() const noexcept
Cast this message to a derived type (unchecked).
Definition Message.hpp:178
Message(MimeType mimeType, Message const *parent=nullptr)
Construct a message with an explicit MIME type, optionally inheriting from a parent.
T & as() noexcept
Cast this message to a derived type (unchecked).
Definition Message.hpp:171
void resetMimeType()
Clear the explicit MIME type (message then defaults to octet-stream).
Definition Message.hpp:146
Utils::UUID const & uuid() const noexcept
Unique identifier of this message instance.
Definition Message.hpp:99
virtual ~Message()
Virtual destructor.
Message(Message const *parent=nullptr)
Construct a message, optionally inheriting state from a parent message.
Message(blank_construct_t) noexcept
Construct a completely blank message without any timestamp or UUID attached to it This is mainly used...
Definition Message.hpp:215
std::chrono::time_point< Clock, std::chrono::microseconds > timestampToTimePoint() const noexcept
Convert timestamp() into a time_point of the given clock type.
Definition Message.hpp:120
std::chrono::microseconds timestamp() const noexcept
Construction timestamp in microseconds since timestampClock epoch. This is not "wall clock time" unle...
Definition Message.hpp:106
bool hasMimeType() const noexcept
Whether the message carries an explicit MIME type.
Definition Message.hpp:135
MIME type value object.
Definition MimeType.hpp:44
@ OctetStream
"application/octet-stream"
Definition MimeType.hpp:56
Definition Unpacker.hpp:20
Concept used by dfx::Core::Message::is() and dfx::Core::Message::as() to constrain types.
Definition Message.hpp:31
Definition Framer.hpp:45
Definition Channel.hpp:25
std::unique_ptr< Message > MessagePtr
Unique ownership handle for messages.
Definition Message.hpp:27
constexpr blank_construct_t blank_construct
Tag used in constructor to indicate that the object should be as brut as possible.
Definition Message.hpp:41
Kind
Port kind (connection domain).
Definition Kind.hpp:29
Struct used when you explicitely want an object to be as bare as possible.
Definition Message.hpp:39
128-bit UUID value type.
Definition UUIDGenerator.hpp:33