|
4 | 4 | #include "flowRecord.hpp" |
5 | 5 | #include "packet.hpp" |
6 | 6 |
|
| 7 | +#include <array> |
7 | 8 | #include <cstdint> |
| 9 | +#include <memory> |
8 | 10 | #include <string> |
| 11 | +#include <type_traits> |
9 | 12 |
|
| 13 | +/** |
| 14 | + * @brief Represents the possible actions a processing plugin can request after handling a packet. |
| 15 | + */ |
10 | 16 | enum class FlowAction : int { |
11 | 17 | /** |
12 | | - * @brief Request complete flow data (packets + metadata). |
| 18 | + * Request complete flow data (packets + metadata). |
13 | 19 | */ |
14 | 20 | RequestFullData, |
15 | 21 |
|
16 | 22 | /** |
17 | | - * @brief Request only trimmed flow data (no payload). |
| 23 | + * Request only trimmed flow data (no payload). |
18 | 24 | */ |
19 | 25 | RequestTrimmedData, |
20 | 26 |
|
21 | 27 | /** |
22 | | - * @brief Indicate that no further processing is needed for this flow. |
| 28 | + * Indicate that no further processing is needed for this flow. |
23 | 29 | */ |
24 | 30 | RequestNoData, |
25 | 31 |
|
26 | 32 | /** |
27 | | - * @brief Export the flow immediately and erase its record. |
| 33 | + * Export the flow immediately and erase its record. |
28 | 34 | */ |
29 | 35 | Flush, |
30 | 36 |
|
31 | 37 | /** |
32 | | - * @brief Export the flow immediately, erase its record, and re-insert new flow. |
| 38 | + * Export the flow immediately, erase its record, and re-insert a new flow. |
33 | 39 | */ |
34 | 40 | FlushAndReinsert |
35 | 41 | }; |
36 | 42 |
|
| 43 | +/** |
| 44 | + * @brief Abstract base class for all flow-processing plugins. |
| 45 | + * |
| 46 | + * Provides a common interface for plugins that react to flow lifecycle events. |
| 47 | + */ |
37 | 48 | class ProcessPlugin { |
38 | 49 | public: |
39 | 50 | ProcessPlugin() = default; |
| 51 | + virtual ~ProcessPlugin() = default; |
40 | 52 |
|
| 53 | + /** |
| 54 | + * @brief Called when a new flow is created. |
| 55 | + * |
| 56 | + * @param flowRecord Reference to the new flow record. |
| 57 | + * @param packet Packet that triggered the flow creation. |
| 58 | + * @return Requested action after processing. |
| 59 | + */ |
41 | 60 | virtual FlowAction onFlowCreate(FlowRecord& flowRecord, const Packet& packet) |
42 | 61 | { |
43 | 62 | (void) flowRecord; |
44 | 63 | (void) packet; |
45 | | - |
46 | 64 | return FlowAction::RequestNoData; |
47 | 65 | } |
48 | 66 |
|
| 67 | + /** |
| 68 | + * @brief Called when an existing flow is updated with a new packet. |
| 69 | + * |
| 70 | + * @param flowRecord Reference to the existing flow record. |
| 71 | + * @param packet The incoming packet. |
| 72 | + * @return Requested action after processing. |
| 73 | + */ |
49 | 74 | virtual FlowAction onFlowUpdate(FlowRecord& flowRecord, const Packet& packet) |
50 | 75 | { |
51 | 76 | (void) flowRecord; |
52 | 77 | (void) packet; |
53 | | - |
54 | 78 | return FlowAction::RequestNoData; |
55 | 79 | } |
56 | 80 |
|
| 81 | + /** |
| 82 | + * @brief Called right before a flow is exported. |
| 83 | + * |
| 84 | + * Can be used for cleanup or finalization. |
| 85 | + */ |
57 | 86 | virtual void onFlowExport() {} |
58 | 87 |
|
| 88 | + /** |
| 89 | + * @brief Returns a pointer to the data to be exported. |
| 90 | + * |
| 91 | + * Typically points to a POD structure. |
| 92 | + * |
| 93 | + * @return Const void pointer to export data. |
| 94 | + */ |
59 | 95 | virtual const void* getExportData() const noexcept = 0; |
60 | 96 |
|
| 97 | + /** |
| 98 | + * @brief Clone the plugin into pre-allocated memory. |
| 99 | + * |
| 100 | + * Uses placement new semantics. |
| 101 | + * |
| 102 | + * @param constructAtAddress Address where the clone should be constructed. |
| 103 | + * @return Pointer to the newly constructed clone. |
| 104 | + */ |
61 | 105 | virtual ProcessPlugin* clone(std::byte* constructAtAddress) const = 0; |
62 | 106 |
|
| 107 | + /** |
| 108 | + * @brief Returns the unique name of the plugin. |
| 109 | + * |
| 110 | + * Used e.g. for schema identification or logging. |
| 111 | + * |
| 112 | + * @return Name of the plugin. |
| 113 | + */ |
63 | 114 | virtual std::string getName() const = 0; |
| 115 | +}; |
64 | 116 |
|
65 | | - virtual ~ProcessPlugin() = default; |
| 117 | +/** |
| 118 | + * @brief CRTP base class that provides default clone() implementation. |
| 119 | + * |
| 120 | + * Use this class as a base if your derived plugin has copy constructor. |
| 121 | + */ |
| 122 | +template<typename Derived> |
| 123 | +class ProcessPluginWithClone : public ProcessPlugin { |
| 124 | +public: |
| 125 | + ProcessPluginWithClone() = default; |
| 126 | + ~ProcessPluginWithClone() override = default; |
| 127 | + |
| 128 | + /** |
| 129 | + * @brief Clone the derived plugin using placement new. |
| 130 | + */ |
| 131 | + ProcessPlugin* clone(std::byte* constructAtAddress) const override |
| 132 | + { |
| 133 | + return std::construct_at( |
| 134 | + reinterpret_cast<Derived*>(constructAtAddress), |
| 135 | + static_cast<const Derived&>(*this)); |
| 136 | + } |
66 | 137 | }; |
67 | 138 |
|
| 139 | +/** |
| 140 | + * @brief Helper to determine the number of enum values. |
| 141 | + * |
| 142 | + * Requires that the enum defines a final enumerator named `FIELDS_SIZE`. |
| 143 | + * |
| 144 | + * @tparam E The enum type. |
| 145 | + * @return Number of elements. |
| 146 | + */ |
68 | 147 | template<typename E> |
69 | | -constexpr std::size_t enum_size() |
| 148 | +constexpr uint8_t enum_size() |
70 | 149 | { |
71 | | - return static_cast<std::size_t>(E::FIELDS_SIZE); |
| 150 | + return static_cast<uint8_t>(E::FIELDS_SIZE); |
72 | 151 | } |
73 | 152 |
|
74 | | -template<typename Enum, typename T, std::size_t Size> |
| 153 | +/** |
| 154 | + * @brief Fixed-size array indexed by enum class. |
| 155 | + * |
| 156 | + * Simplifies code by allowing strongly typed enum indexing. |
| 157 | + * |
| 158 | + * @tparam Enum Enum class type (must be contiguous, starting at 0). |
| 159 | + * @tparam T Stored type. |
| 160 | + * @tparam Size Size of the enum (should match number of fields). |
| 161 | + */ |
| 162 | +template<typename Enum, typename T, uint8_t Size> |
75 | 163 | class EnumArray { |
76 | 164 | public: |
77 | 165 | static_assert(std::is_enum_v<Enum>, "EnumArray requires an enum type"); |
78 | 166 |
|
79 | | - // Přetížený operator[], který umožní indexovat pomocí enum class |
80 | | - T& operator[](Enum index) { return m_data[static_cast<std::size_t>(index)]; } |
| 167 | + /** |
| 168 | + * @brief Access element by enum index. |
| 169 | + * |
| 170 | + * @param index Enum value. |
| 171 | + * @return Reference to the stored value. |
| 172 | + */ |
| 173 | + T& operator[](Enum index) { return m_data[static_cast<uint8_t>(index)]; } |
81 | 174 |
|
82 | | - const T& operator[](Enum index) const { return m_data[static_cast<std::size_t>(index)]; } |
| 175 | + /** |
| 176 | + * @brief Const access to element by enum index. |
| 177 | + * |
| 178 | + * @param index Enum value. |
| 179 | + * @return Const reference to the stored value. |
| 180 | + */ |
| 181 | + const T& operator[](Enum index) const { return m_data[static_cast<uint8_t>(index)]; } |
83 | 182 |
|
84 | 183 | private: |
85 | 184 | std::array<T, Size> m_data; |
86 | 185 | }; |
87 | 186 |
|
| 187 | +/** |
| 188 | + * @brief Storage for field handlers indexed by enum. |
| 189 | + * |
| 190 | + * Designed to hold field accessors for a plugin schema. |
| 191 | + * |
| 192 | + * @tparam Enum Enum type used to represent individual fields. |
| 193 | + */ |
| 194 | + |
88 | 195 | template<typename Enum> |
89 | | -class FieldHandlers { |
90 | | -protected: |
91 | | - EnumArray<Enum, FieldHandler, enum_size<Enum>()> m_fieldHandlers; |
92 | | -}; |
| 196 | +using FieldHandlers = EnumArray<Enum, FieldHandler, enum_size<Enum>()>; |
0 commit comments