diff --git a/src/cluster/Serializer.h b/src/cluster/Serializer.h new file mode 100644 index 0000000000..2be72ceca2 --- /dev/null +++ b/src/cluster/Serializer.h @@ -0,0 +1,107 @@ +// See the file "COPYING" in the main distribution directory for copyright. + +// Interfaces to be implemented by event and log serializer components. + +#pragma once + +#include +#include +#include + +#include "zeek/Span.h" +#include "zeek/logging/Types.h" + +namespace zeek::cluster { + +namespace detail { +class Event; + +using byte_buffer = std::vector; +using byte_buffer_span = Span; + +} // namespace detail + + +/** + * This class handles encoding of events into byte buffers and back. + * + * An event and its parameters can be serialized as a message which + * another node can unserialize and then enqueue as an event. + */ +class EventSerializer { +public: + virtual ~EventSerializer() = default; + + /** + * Serialize an event into the given byte buffer. + * + * @param buf The buffer to use for serialization. + * @param event The event to serialize. + * + * @returns True on success, false in exceptional cases (e.g. unsupported serialization). + */ + virtual bool SerializeEvent(detail::byte_buffer& buf, const detail::Event& event) = 0; + + /** + * Unserialize an event from a given byte buffer. + * + * @param buf A span representing a received remote event. + * + * @returns The event, or std::nullopt on error. + */ + virtual std::optional UnserializeEvent(detail::byte_buffer_span buf) = 0; + + /** + * @returns The name of this event serializer instance. + */ + const std::string& Name() { return name; } + +protected: + /** + * Constructor. + */ + EventSerializer(std::string name) : name(std::move(name)) {} + +private: + std::string name; +}; + +/** + * Interface for a serializer for logging::LogRecord instances. + */ +class LogSerializer { +public: + /** + * Constructor. + */ + explicit LogSerializer(std::string name) : name(std::move(name)) {}; + + virtual ~LogSerializer() = default; + + /** + * Serialize log records into a byte buffer. + * + * @param buf The buffer to serialize into. + * @param header The log batch header. + * @param records The actual log writes. + */ + virtual bool SerializeLogWrite(detail::byte_buffer& buf, const logging::detail::LogWriteHeader& header, + zeek::Span records) = 0; + + /** + * Unserialize log writes from a given byte buffer. + * + * @param buf The span representing received log writes. + */ + virtual std::optional UnserializeLogWrite(detail::byte_buffer_span buf) = 0; + + /** + * @returns The name of this log serializer instance. + */ + const std::string& Name() { return name; } + +private: + std::string name; +}; + +} // namespace zeek::cluster