diff --git a/src/cluster/Backend.cc b/src/cluster/Backend.cc index 21600a731b..e7aed60faa 100644 --- a/src/cluster/Backend.cc +++ b/src/cluster/Backend.cc @@ -27,41 +27,6 @@ using namespace zeek::cluster; -double Event::Timestamp() const { - if ( meta ) { - for ( const auto& m : *meta ) { - if ( m.Id() == static_cast(zeek::detail::MetadataType::NetworkTimestamp) ) - return m.Val()->AsTime(); - } - } - - return zeek::detail::NO_TIMESTAMP; -} - -bool Event::AddMetadata(const EnumValPtr& id, zeek::ValPtr val) { - if ( ! id || ! val ) - return false; - - const auto* desc = zeek::event_registry->LookupMetadata(id->Get()); - if ( ! desc ) - return false; - - if ( ! same_type(val->GetType(), desc->Type()) ) - return false; - - if ( ! meta ) - meta = std::make_unique(); - - // Internally stored as zeek_uint_t for serializers. - meta->emplace_back(desc->Id(), std::move(val)); - - return true; -} - -std::tuple Event::Take() && { - return {handler, std::move(args), std::move(meta)}; -} - bool detail::LocalEventHandlingStrategy::DoProcessEvent(std::string_view topic, cluster::Event e) { auto [handler, args, meta] = std::move(e).Take(); zeek::event_mgr.Enqueue(std::move(meta), handler, std::move(args), util::detail::SOURCE_BROKER); diff --git a/src/cluster/Backend.h b/src/cluster/Backend.h index db91f235bf..cc475d2ec1 100644 --- a/src/cluster/Backend.h +++ b/src/cluster/Backend.h @@ -9,13 +9,13 @@ #include #include -#include "zeek/Event.h" #include "zeek/EventHandler.h" #include "zeek/Span.h" #include "zeek/Tag.h" #include "zeek/Val.h" #include "zeek/ZeekArgs.h" #include "zeek/cluster/BifSupport.h" +#include "zeek/cluster/Event.h" #include "zeek/cluster/OnLoop.h" #include "zeek/cluster/Serializer.h" #include "zeek/cluster/Telemetry.h" @@ -30,73 +30,6 @@ class OnLoopProcess; namespace cluster { -/** - * Cluster event class. - */ -class Event { -public: - /** - * Constructor. - */ - Event(const EventHandlerPtr& handler, zeek::Args args, zeek::detail::EventMetadataVectorPtr meta) - : handler(handler), args(std::move(args)), meta(std::move(meta)) {} - - /** - * @return The name of the event. - */ - std::string_view HandlerName() const { return handler->Name(); } - - /** - * @return The event's handler. - */ - const EventHandlerPtr& Handler() const { return handler; } - - /** - * @return The event's arguments. - */ - const zeek::Args& Args() const { return args; } - /** - * @return The event's arguments. - */ - zeek::Args& Args() { return args; } - - /** - * @return The network timestamp metadata of this event or -1.0 if not set. - */ - double Timestamp() const; - - /** - * Add metadata to this cluster event. - * - * The used metadata \a id has to be registered via the Zeek script-layer - * function EventMetadata::register(), or via the C++ API - * EventMgr::RegisterMetadata() during an InitPostScript() hook. - * - * Non-registered metadata will not be added and false is returned. - * - * @param id The enum value identifying the event metadata. - * @param val The value to use. - - * @return true if \a val was was added, else false. - */ - bool AddMetadata(const EnumValPtr& id, ValPtr val); - - /** - * @return A pointer to the metadata vector, or nullptr if no Metadata has been added yet. - */ - const zeek::detail::EventMetadataVector* Metadata() const { return meta.get(); } - - /** - * Move data out of this event as preparation for Enqueue() - */ - std::tuple Take() &&; - -private: - EventHandlerPtr handler; - zeek::Args args; - zeek::detail::EventMetadataVectorPtr meta; -}; - namespace detail { /** diff --git a/src/cluster/CMakeLists.txt b/src/cluster/CMakeLists.txt index 29f8a9debe..2bc9bddc16 100644 --- a/src/cluster/CMakeLists.txt +++ b/src/cluster/CMakeLists.txt @@ -7,6 +7,7 @@ zeek_add_subdir_library( Backend.cc BifSupport.cc Component.cc + Event.cc Manager.cc Telemetry.cc BIFS diff --git a/src/cluster/Event.cc b/src/cluster/Event.cc new file mode 100644 index 0000000000..f75ff5434c --- /dev/null +++ b/src/cluster/Event.cc @@ -0,0 +1,43 @@ +// See the file "COPYING" in the main distribution directory for copyright. + +#include "zeek/cluster/Event.h" + +#include "zeek/Event.h" +#include "zeek/EventRegistry.h" + +using namespace zeek::cluster; + +double Event::Timestamp() const { + if ( meta ) { + for ( const auto& m : *meta ) { + if ( m.Id() == static_cast(zeek::detail::MetadataType::NetworkTimestamp) ) + return m.Val()->AsTime(); + } + } + + return zeek::detail::NO_TIMESTAMP; +} + +bool Event::AddMetadata(const EnumValPtr& id, zeek::ValPtr val) { + if ( ! id || ! val ) + return false; + + const auto* desc = zeek::event_registry->LookupMetadata(id->Get()); + if ( ! desc ) + return false; + + if ( ! same_type(val->GetType(), desc->Type()) ) + return false; + + if ( ! meta ) + meta = std::make_unique(); + + // Internally stored as zeek_uint_t for serializers. + meta->emplace_back(desc->Id(), std::move(val)); + + return true; +} + +std::tuple Event::Take() && { + return {handler, std::move(args), std::move(meta)}; +} diff --git a/src/cluster/Event.h b/src/cluster/Event.h new file mode 100644 index 0000000000..56d8470a25 --- /dev/null +++ b/src/cluster/Event.h @@ -0,0 +1,82 @@ +// See the file "COPYING" in the main distribution directory for copyright. + +// Val-based representation of an event for cluster communication. + +#pragma once + +#include "zeek/Event.h" +#include "zeek/EventHandler.h" +#include "zeek/Val.h" +#include "zeek/ZeekArgs.h" + +namespace zeek::cluster { + +/** + * Cluster event class. + */ +class Event { +public: + /** + * Constructor. + */ + Event(const EventHandlerPtr& handler, zeek::Args args, zeek::detail::EventMetadataVectorPtr meta) + : handler(handler), args(std::move(args)), meta(std::move(meta)) {} + + /** + * @return The name of the event. + */ + std::string_view HandlerName() const { return handler->Name(); } + + /** + * @return The event's handler. + */ + const EventHandlerPtr& Handler() const { return handler; } + + /** + * @return The event's arguments. + */ + const zeek::Args& Args() const { return args; } + /** + * @return The event's arguments. + */ + zeek::Args& Args() { return args; } + + /** + * @return The network timestamp metadata of this event or -1.0 if not set. + */ + double Timestamp() const; + + /** + * Add metadata to this cluster event. + * + * The used metadata \a id has to be registered via the Zeek script-layer + * function EventMetadata::register(), or via the C++ API + * EventMgr::RegisterMetadata() during an InitPostScript() hook. + * + * Non-registered metadata will not be added and false is returned. + * + * @param id The enum value identifying the event metadata. + * @param val The value to use. + + * @return true if \a val was was added, else false. + */ + bool AddMetadata(const EnumValPtr& id, ValPtr val); + + /** + * @return A pointer to the metadata vector, or nullptr if no Metadata has been added yet. + */ + const zeek::detail::EventMetadataVector* Metadata() const { return meta.get(); } + + /** + * Move data out of this event as preparation for Enqueue() + */ + std::tuple Take() &&; + +private: + EventHandlerPtr handler; + zeek::Args args; + zeek::detail::EventMetadataVectorPtr meta; +}; + + +} // namespace zeek::cluster diff --git a/src/cluster/serializer/broker/Serializer.cc b/src/cluster/serializer/broker/Serializer.cc index 1abdca9d3b..e17ea9a38d 100644 --- a/src/cluster/serializer/broker/Serializer.cc +++ b/src/cluster/serializer/broker/Serializer.cc @@ -12,7 +12,7 @@ #include "zeek/Reporter.h" #include "zeek/Type.h" #include "zeek/broker/Data.h" -#include "zeek/cluster/Backend.h" +#include "zeek/cluster/Event.h" #include "broker/data.bif.h" #include "broker/data_envelope.hh"