dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Message.hpp
1// SPDX-FileCopyrightText: 2025 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
76{
77public:
81 virtual ~Message();
82
85
87 Utils::UUID const & uuid() const noexcept { return _uuid; }
89 Utils::UUID const & parentUuid() const noexcept { return _parentUuid; }
91 std::vector<std::string> const & pathTaken() const noexcept { return _pathTaken; }
94 std::chrono::microseconds timestamp() const noexcept { return _timestamp; }
95
97
106 template<typename Clock = std::chrono::steady_clock>
107 requires std::chrono::is_clock_v<Clock>
108 std::chrono::time_point<Clock, std::chrono::microseconds> timestampToTimePoint() const noexcept
109 { return std::chrono::time_point<Clock, std::chrono::microseconds>(_timestamp); }
110
117 void addToPathTaken(std::string path) { _pathTaken.emplace_back(std::move(path)); }
118
121
123 bool hasMimeType() const noexcept { return _mimeType.has_value(); }
126 MimeType mimeType() const noexcept { return _mimeType.value_or(MimeType::OctetStream); }
134 void resetMimeType() { _mimeType.reset(); }
135
137
139 virtual Kind kind() const = 0;
140
146 virtual MessagePtr clone() const = 0;
147
148public:
151 template<DerivedFromMessage T>
152 bool is() const noexcept { return dynamic_cast<T const *>(this) != nullptr; }
153
158 template<DerivedFromMessage T>
159 T & as() noexcept { return static_cast<T &>(*this); }
160
165 template<DerivedFromMessage T>
166 T const & as() const noexcept { return static_cast<T const &>(*this); }
167
168public:
175 template<DerivedFromMessage T, typename ... Args>
176 static std::unique_ptr<T> create(Args && ... args)
177 { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); }
178
179public:
185 static clockid_t timestampClock;
186
187protected:
198 virtual void cloneFromBase(Message const & base);
199
200protected:
210 Message(MessagePtr const & parent = nullptr);
211
218 Message(MimeType mimeType, MessagePtr const & parent = nullptr);
219
220private:
221 Utils::UUID _uuid;
222 Utils::UUID _parentUuid;
223 std::vector<std::string> _pathTaken;
224 std::chrono::microseconds _timestamp{};
225
226private:
227 std::optional<MimeType> _mimeType;
228};
229} // !namespace dfx::Core
Convenience macros to explicitly control copy and move semantics.
Abstract base class for messages exchanged between nodes.
Definition Message.hpp:76
Message(MessagePtr const &parent=nullptr)
Construct a message, optionally inheriting state from a parent message.
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:126
bool is() const noexcept
Check whether this message is of a given derived type.
Definition Message.hpp:152
static clockid_t timestampClock
Clock used by the constructor to capture timestamps.
Definition Message.hpp:185
static std::unique_ptr< T > create(Args &&... args)
Convenience allocator for derived message types.
Definition Message.hpp:176
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:89
std::vector< std::string > const & pathTaken() const noexcept
Sequence of "node.port" hops this message has traversed.
Definition Message.hpp:91
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:117
T const & as() const noexcept
Cast this message to a derived type (unchecked).
Definition Message.hpp:166
T & as() noexcept
Cast this message to a derived type (unchecked).
Definition Message.hpp:159
void resetMimeType()
Clear the explicit MIME type (message then defaults to octet-stream).
Definition Message.hpp:134
Utils::UUID const & uuid() const noexcept
Unique identifier of this message instance.
Definition Message.hpp:87
virtual ~Message()
Virtual destructor.
DISABLE_COPY_AND_MOVE(Message)
Messages are non-copyable and non-movable.
Message(MimeType mimeType, MessagePtr const &parent=nullptr)
Construct a message with an explicit MIME type, optionally inheriting from a parent.
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:108
std::chrono::microseconds timestamp() const noexcept
Construction timestamp in microseconds since timestampClock epoch. This is not "wall clock time" unle...
Definition Message.hpp:94
bool hasMimeType() const noexcept
Whether the message carries an explicit MIME type.
Definition Message.hpp:123
MIME type value object.
Definition MimeType.hpp:44
@ OctetStream
"application/octet-stream"
Definition MimeType.hpp:56
Concept used by dfx::Core::Message::is() and dfx::Core::Message::as() to constrain types.
Definition Message.hpp:31
Definition Channel.hpp:22
std::unique_ptr< Message > MessagePtr
Unique ownership handle for messages.
Definition Message.hpp:27
Kind
Port kind (connection domain).
Definition Kind.hpp:29
128-bit UUID value type.
Definition UUIDGenerator.hpp:33