dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Transport.hpp
1// SPDX-FileCopyrightText: 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 <concepts>
13#include <memory>
14
15// Third-party includes
16#include <nlohmann/json.hpp>
17
18// Project includes
19#include "../Endpoint.hpp"
21
84
85namespace dfx::FdWatch
86{
87class Poller;
88} // !namespace dfx::FdWatch
89
90namespace dfx::Core
91{
92class Channel;
93class Transport;
94
96template<typename T>
97concept DerivedFromTransport = std::derived_from<T, Transport>;
98
124{
125 friend class Channel;
126
127public:
129 enum class Role
130 {
133 };
134
135public:
142
145
147 virtual ~Transport() = default;
148
151
153 Role role() const noexcept { return _role; }
154
156 bool isSource() const noexcept { return _role == Role::Source; }
158 bool isDestination() const noexcept { return _role == Role::Destination; }
159
161 Endpoint const & endpoint() const noexcept { return _endpoint; }
162
165 Channel * channel() const noexcept { return _channel; }
166
169 Port * port() const noexcept { return _endpoint.port(); }
170
173 NodePtr node() const noexcept { return _endpoint.node(); }
174
176 nlohmann::json const & config() const noexcept { return _config; }
177
179
180public:
186 template<DerivedFromTransport T>
187 bool is() const noexcept { return dynamic_cast<T const *>(this) != nullptr; }
188
197 template<DerivedFromTransport T>
198 T & as() noexcept { return static_cast<T &>(*this); }
199
208 template<DerivedFromTransport T>
209 T const & as() const noexcept { return static_cast<T const &>(*this); }
210
211public:
215 virtual void start(FdWatch::Poller & poller) = 0;
216
218 virtual void stop() noexcept = 0;
219
220private:
221 // This method is called in the Channel ctor
222 void _attachToChannel(Channel * channel);
223
224protected:
225 Role const _role;
226
227 Endpoint _endpoint;
228 nlohmann::json _config;
229
230private:
231 Channel * _channel = nullptr;
232};
233
235using TransportPtr = std::unique_ptr<Transport>;
236} // !namespace dfx::Core
Convenience macros to explicitly control copy and move semantics.
Orchestrator connecting a source transport to a destination transport.
Definition Channel.hpp:58
Definition Endpoint.hpp:24
Base class for both input and output ports.
Definition Port.hpp:66
Abstract base class for all message carriers within the dfx framework.
Definition Transport.hpp:124
Port * port() const noexcept
Gets the port associated with this transport.
Definition Transport.hpp:169
Endpoint const & endpoint() const noexcept
Gets the endpoint descriptor associated with this transport.
Definition Transport.hpp:161
bool isSource() const noexcept
Returns true if this transport is a source.
Definition Transport.hpp:156
NodePtr node() const noexcept
Gets the node associated with this transport.
Definition Transport.hpp:173
Channel * channel() const noexcept
Gets the channel which this transport is attached to if any.
Definition Transport.hpp:165
virtual ~Transport()=default
Virtual destructor ensuring proper cleanup of derived transports.
virtual void start(FdWatch::Poller &poller)=0
Activates the transport and begins I/O operations.
bool is() const noexcept
Check whether this transport is of a given derived type.
Definition Transport.hpp:187
bool isDestination() const noexcept
Returns true if this transport is a destination.
Definition Transport.hpp:158
T & as() noexcept
Cast this transport to a derived type (unchecked).
Definition Transport.hpp:198
DFX_DISABLE_COPY_AND_MOVE(Transport)
Transport is not copiable nor movable.
nlohmann::json const & config() const noexcept
Gets the JSON configuration used to initialize this transport.
Definition Transport.hpp:176
Role role() const noexcept
Returns the Role (Source or Destination) of this transport.
Definition Transport.hpp:153
virtual void stop() noexcept=0
Deactivates the transport and releases system resources.
Role
Defines the operational role of a transport relative to a channel.
Definition Transport.hpp:130
@ Destination
The transport acts as a message consumer for the channel.
Definition Transport.hpp:132
@ Source
The transport acts as a message producer for the channel.
Definition Transport.hpp:131
Transport(Role role, Endpoint endpoint, nlohmann::json config)
Constructs a transport instance.
T const & as() const noexcept
Cast this transport to a derived type (unchecked).
Definition Transport.hpp:209
Abstract interface for FD-based event polling.
Definition Poller.hpp:37
Concept used by dfx::Core::Transport::as() and dfx::Core::Transport::is() to constrain types.
Definition Transport.hpp:97
Definition Channel.hpp:25
std::unique_ptr< Transport > TransportPtr
Unique ownership handle for transports.
Definition Transport.hpp:235
std::shared_ptr< Node > NodePtr
Shared ownership pointer type for Nodes..
Definition Endpoint.hpp:21
Definition SocketClient.hpp:23
STL namespace.