1+ /* *
2+ * @file
3+ * @brief Base class and factory for packet input plugins
4+ * @author Pavel Siska <[email protected] > 5+ * @date 2025
6+ *
7+ * This file defines the base class for input plugins, responsible for processing
8+ * incoming packets, maintaining statistics, and integrating with the telemetry system.
9+ * It also includes a factory template for plugin creation.
10+ *
11+ * Copyright (c) 2025 CESNET
12+ *
13+ * SPDX-License-Identifier: BSD-3-Clause
14+ */
15+
16+ #pragma once
17+
18+ #include " api.hpp"
19+ #include " packet.hpp"
20+ #include " parser-stats.hpp"
21+ #include " plugin.hpp"
22+ #include " telemetry-utils.hpp"
23+
24+ #include < cstdint>
25+ #include < memory>
26+ #include < string>
27+
28+ #include < telemetry.hpp>
29+
30+ namespace ipxp {
31+
32+ /* *
33+ * \brief Base class for packet receivers.
34+ *
35+ * InputPlugin is an abstract base class for processing network packets.
36+ * It provides functionality for handling telemetry directories and maintains
37+ * statistics on processed packets.
38+ */
39+ class IPXP_API InputPlugin
40+ : public TelemetryUtils
41+ , public Plugin {
42+ public:
43+ enum class Result {
44+ TIMEOUT = 0 ,
45+ PARSED,
46+ NOT_PARSED,
47+ END_OF_FILE,
48+ ERROR,
49+ };
50+
51+ virtual ~InputPlugin () = default ;
52+
53+ /* *
54+ * @brief Retrieves a block of packets.
55+ * @param packets Reference to a PacketBlock to store received packets.
56+ * @return The result of the packet retrieval operation.
57+ */
58+ virtual Result get (PacketBlock& packets) = 0;
59+
60+ /* *
61+ * @brief Sets the telemetry directories for this plugin.
62+ * @param plugin_dir Shared pointer to the plugin-specific telemetry directory.
63+ * @param queues_dir Shared pointer to the telemetry directory for queues.
64+ */
65+ void set_telemetry_dirs (
66+ std::shared_ptr<telemetry::Directory> plugin_dir,
67+ std::shared_ptr<telemetry::Directory> queues_dir);
68+
69+ // / Number of packets seen by the plugin.
70+ uint64_t m_seen = 0 ;
71+ // / Number of packets successfully parsed.
72+ uint64_t m_parsed = 0 ;
73+ // / Number of packets dropped.
74+ uint64_t m_dropped = 0 ;
75+
76+ protected:
77+ /* *
78+ * @brief Configures the telemetry directories.
79+ *
80+ * This method can be overridden by derived classes to perform additional
81+ * setup for telemetry directories.
82+ *
83+ * @param plugin_dir Shared pointer to the plugin-specific telemetry directory.
84+ * @param queues_dir Shared pointer to the telemetry directory for queues.
85+ */
86+ virtual void configure_telemetry_dirs (
87+ std::shared_ptr<telemetry::Directory> plugin_dir,
88+ std::shared_ptr<telemetry::Directory> queues_dir)
89+ {
90+ (void ) plugin_dir;
91+ (void ) queues_dir;
92+ };
93+
94+ // / Statistics related to packet parsing.
95+ ParserStats m_parser_stats = {};
96+
97+ private:
98+ void create_parser_stats_telemetry (std::shared_ptr<telemetry::Directory> queues_dir);
99+ };
100+
101+ /* *
102+ * @brief Factory template for creating plugins.
103+ *
104+ * This template allows dynamic creation of plugin instances based on the specified
105+ * base class and constructor argument types.
106+ *
107+ * @tparam Base The base class for the plugin.
108+ * @tparam Args The argument types required for the plugin constructor.
109+ */
110+ template <typename Base, typename ... Args>
111+ class IPXP_API PluginFactory;
112+
113+ /* *
114+ * @brief Type alias for the InputPlugin factory.
115+ *
116+ * Provides a factory for creating InputPlugin instances using a string-based constructor.
117+ */
118+ using InputPluginFactory = PluginFactory<InputPlugin, const std::string&>;
119+
120+ } // namespace ipxp
0 commit comments