dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Capture.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 <memory>
13
14// Project includes
15#include "sinks/FileSink.hpp"
16#include "sinks/PipeSink.hpp"
17#include "sinks/TcpSink.hpp"
18#include <dfx-hooks/Graph.hpp>
19#include <dfx-hooks/Port.hpp>
20#include <dfx-utilities/Thread.hpp>
21
22namespace dfx::Core
23{
24using NodeWPtr = std::weak_ptr<Node>;
25} // !namespace dfx::Core
26
27namespace dfx::Pcapng
28{
29class Worker;
30
66class Capture : public Hooks::Port, public Hooks::Graph
67{
68public:
73 explicit Capture(bool getHwAndOsInfo = true);
74
81
89 template<typename ... Args>
90 void setFileSink(Args && ... args)
91 { _setFileSinkImpl(std::make_unique<FileSink>(std::forward<Args>(args)...)); }
92
95
103 template<typename ... Args>
104 void setPipeSink(Args && ... args)
105 { _setPipeSinkImpl(std::make_unique<PipeSink>(std::forward<Args>(args)...)); }
106
109
117 template<typename ... Args>
118 void setTcpSink(Args && ... args)
119 { _setTcpSinkImpl(std::make_unique<TcpSink>(std::forward<Args>(args)...)); }
120
123
124public:
132 void setUserAppInfo(std::string info);
133
134public:
142 void onMessageSent(Core::OutputPort const & port, Core::Message const & message) override;
143
151 void onMessageReceived(Core::InputPort const & port, Core::Message const & message) override;
152
159 void onNodeCreated(Core::NodePtr node) override;
160
166 void onNodeRemoved(Core::NodePtr node) override;
167
172
173private:
174 void _setFileSinkImpl(SinkPtr sink);
175 void _setPipeSinkImpl(SinkPtr sink);
176 void _setTcpSinkImpl(SinkPtr sink);
177
178 Worker & _worker();
179 Worker const & _worker() const;
180
181private:
182 std::vector<Core::NodeWPtr> _nodes;
183 Utils::Thread _thread;
184};
185} // !namespace dfx::Pcapng
Incoming message endpoint attached to a node.
Definition InputPort.hpp:57
Abstract base class for messages exchanged between nodes.
Definition Message.hpp:86
Outgoing message endpoint attached to a node.
Definition OutputPort.hpp:47
Hooks related to the lifecycle of graph objects (nodes and channels).
Definition Graph.hpp:54
Port-level message observation hook.
Definition Port.hpp:44
void setPipeSink(Args &&... args)
Enable pipe output using a PipeSink.
Definition Capture.hpp:104
void onChannelRemoved(Core::ChannelPtr) override
Graph hook: channel removal is currently ignored for capture.
Definition Capture.hpp:171
void setUserAppInfo(std::string info)
Set application-specific information to embed in the capture.
Capture(bool getHwAndOsInfo=true)
Construct a capture hook.
void unsetTcpSink()
Disable the TCP sink if enabled.
void onMessageSent(Core::OutputPort const &port, Core::Message const &message) override
Port hook: called when a message is sent from an output port.
void unsetPipeSink()
Disable the pipe sink if enabled.
void onChannelCreated(Core::ChannelPtr) override
Graph hook: channel creation is currently ignored for capture.
Definition Capture.hpp:169
void setFileSink(Args &&... args)
Enable file output using a FileSink.
Definition Capture.hpp:90
void onNodeRemoved(Core::NodePtr node) override
Graph hook: called when a node is removed.
void onNodeCreated(Core::NodePtr node) override
Graph hook: called when a node is created.
void setTcpSink(Args &&... args)
Enable TCP output using a TcpSink.
Definition Capture.hpp:118
void onMessageReceived(Core::InputPort const &port, Core::Message const &message) override
Port hook: called when a message is received by an input port.
void unsetFileSink()
Disable the file sink if enabled.
~Capture()
Destroy the capture hook and stop background capture work.
Background capture worker responsible for PCAPNG serialization and sink I/O.
Definition Worker.hpp:73
A managed execution wrapper that ensures a Runnable's full lifecycle occurs within the worker thread.
Definition Thread.hpp:67
Definition Channel.hpp:25
std::shared_ptr< Node > NodePtr
Shared ownership pointer type for Nodes..
Definition Endpoint.hpp:21
std::weak_ptr< Node > NodeWPtr
Weak pointer type for Nodes.
Definition Node.hpp:66
std::shared_ptr< Channel > ChannelPtr
Shared ownership handle for channels.
Definition Channel.hpp:137
Definition Capture.hpp:28
std::unique_ptr< Sink > SinkPtr
Owning pointer type for sinks.
Definition Sink.hpp:122