/src/zeek/src/cluster/serializer/broker/Serializer.h
Line | Count | Source |
1 | | // See the file "COPYING" in the main distribution directory for copyright. |
2 | | |
3 | | #pragma once |
4 | | |
5 | | #include <memory> |
6 | | #include <vector> |
7 | | |
8 | | #include "zeek/cluster/Serializer.h" |
9 | | |
10 | | namespace broker::zeek { |
11 | | class Event; |
12 | | } |
13 | | |
14 | | namespace zeek { |
15 | | |
16 | | namespace detail { |
17 | | class MetadataEntry; |
18 | | |
19 | | using EventMetadataVector = std::vector<MetadataEntry>; |
20 | | using EventMetadataVectorPtr = std::unique_ptr<EventMetadataVector>; |
21 | | |
22 | | } // namespace detail |
23 | | |
24 | | namespace cluster::detail { |
25 | | |
26 | | /** |
27 | | * Produce a EventMetadataVectorPtr from a broker event. |
28 | | * |
29 | | * The implementation relies on @ref zeek::EventRegistry::LookupMetadata() |
30 | | * to find expected metadata types. If there's no metadata at all attached |
31 | | * to this event, returns a nullptr, |
32 | | * |
33 | | * @param ev The broker event. |
34 | | |
35 | | * @return Pointer to a @ref zeek::detail::EventMetadataVector holding values for all known metadata. |
36 | | */ |
37 | | zeek::detail::EventMetadataVectorPtr metadata_vector_from_broker_event(const broker::zeek::Event& ev); |
38 | | |
39 | | /** |
40 | | * Convert a broker::zeek::Event to cluster::detail::Event by looking |
41 | | * it up in Zeek's event handler registry and converting event arguments |
42 | | * to the appropriate Val instances. |
43 | | * |
44 | | * @param ev The broker side event. |
45 | | * @returns A zeek::cluster::Event instance, or std::nullopt if the conversion failed. |
46 | | */ |
47 | | std::optional<cluster::Event> to_zeek_event(const broker::zeek::Event& ev); |
48 | | |
49 | | /** |
50 | | * Convert a cluster::detail::Event to a broker::zeek::Event. |
51 | | * |
52 | | * @param ev The cluster::detail::Event |
53 | | * @return A broker::zeek::Event to be serialized, or nullopt in case of errors. |
54 | | */ |
55 | | std::optional<broker::zeek::Event> to_broker_event(const cluster::Event& ev); |
56 | | |
57 | | // Implementation of the EventSerializer using the existing broker::detail::val_to_data() |
58 | | // and broker::format::bin::v1::encode(). |
59 | | class BrokerBinV1_Serializer : public EventSerializer { |
60 | | public: |
61 | 65 | BrokerBinV1_Serializer() : EventSerializer("broker-bin-v1") {} |
62 | | |
63 | | bool SerializeEvent(byte_buffer& buf, const cluster::Event& event) override; |
64 | | |
65 | | std::optional<cluster::Event> UnserializeEvent(byte_buffer_span buf) override; |
66 | | }; |
67 | | |
68 | | // Implementation of the EventSerializer that uses the existing broker::detail::val_to_data() |
69 | | // and broker::format::json::v1::encode() |
70 | | class BrokerJsonV1_Serializer : public EventSerializer { |
71 | | public: |
72 | 0 | BrokerJsonV1_Serializer() : EventSerializer("broker-json-v1") {} |
73 | | |
74 | | bool SerializeEvent(byte_buffer& buf, const cluster::Event& event) override; |
75 | | |
76 | | std::optional<cluster::Event> UnserializeEvent(byte_buffer_span buf) override; |
77 | | }; |
78 | | |
79 | | } // namespace cluster::detail |
80 | | } // namespace zeek |