mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
cluster::Event: Move implementation into cluster/Event.{h,cc}
This commit is contained in:
parent
bda70067ec
commit
40389603c2
6 changed files with 128 additions and 104 deletions
|
@ -27,41 +27,6 @@
|
||||||
|
|
||||||
using namespace zeek::cluster;
|
using namespace zeek::cluster;
|
||||||
|
|
||||||
double Event::Timestamp() const {
|
|
||||||
if ( meta ) {
|
|
||||||
for ( const auto& m : *meta ) {
|
|
||||||
if ( m.Id() == static_cast<zeek_uint_t>(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<zeek::detail::EventMetadataVector>();
|
|
||||||
|
|
||||||
// Internally stored as zeek_uint_t for serializers.
|
|
||||||
meta->emplace_back(desc->Id(), std::move(val));
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::tuple<zeek::EventHandlerPtr, zeek::Args, zeek::detail::EventMetadataVectorPtr> Event::Take() && {
|
|
||||||
return {handler, std::move(args), std::move(meta)};
|
|
||||||
}
|
|
||||||
|
|
||||||
bool detail::LocalEventHandlingStrategy::DoProcessEvent(std::string_view topic, cluster::Event e) {
|
bool detail::LocalEventHandlingStrategy::DoProcessEvent(std::string_view topic, cluster::Event e) {
|
||||||
auto [handler, args, meta] = std::move(e).Take();
|
auto [handler, args, meta] = std::move(e).Take();
|
||||||
zeek::event_mgr.Enqueue(std::move(meta), handler, std::move(args), util::detail::SOURCE_BROKER);
|
zeek::event_mgr.Enqueue(std::move(meta), handler, std::move(args), util::detail::SOURCE_BROKER);
|
||||||
|
|
|
@ -9,13 +9,13 @@
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
|
||||||
#include "zeek/Event.h"
|
|
||||||
#include "zeek/EventHandler.h"
|
#include "zeek/EventHandler.h"
|
||||||
#include "zeek/Span.h"
|
#include "zeek/Span.h"
|
||||||
#include "zeek/Tag.h"
|
#include "zeek/Tag.h"
|
||||||
#include "zeek/Val.h"
|
#include "zeek/Val.h"
|
||||||
#include "zeek/ZeekArgs.h"
|
#include "zeek/ZeekArgs.h"
|
||||||
#include "zeek/cluster/BifSupport.h"
|
#include "zeek/cluster/BifSupport.h"
|
||||||
|
#include "zeek/cluster/Event.h"
|
||||||
#include "zeek/cluster/OnLoop.h"
|
#include "zeek/cluster/OnLoop.h"
|
||||||
#include "zeek/cluster/Serializer.h"
|
#include "zeek/cluster/Serializer.h"
|
||||||
#include "zeek/cluster/Telemetry.h"
|
#include "zeek/cluster/Telemetry.h"
|
||||||
|
@ -30,73 +30,6 @@ class OnLoopProcess;
|
||||||
|
|
||||||
namespace cluster {
|
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<zeek::EventHandlerPtr, zeek::Args, zeek::detail::EventMetadataVectorPtr> Take() &&;
|
|
||||||
|
|
||||||
private:
|
|
||||||
EventHandlerPtr handler;
|
|
||||||
zeek::Args args;
|
|
||||||
zeek::detail::EventMetadataVectorPtr meta;
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -7,6 +7,7 @@ zeek_add_subdir_library(
|
||||||
Backend.cc
|
Backend.cc
|
||||||
BifSupport.cc
|
BifSupport.cc
|
||||||
Component.cc
|
Component.cc
|
||||||
|
Event.cc
|
||||||
Manager.cc
|
Manager.cc
|
||||||
Telemetry.cc
|
Telemetry.cc
|
||||||
BIFS
|
BIFS
|
||||||
|
|
43
src/cluster/Event.cc
Normal file
43
src/cluster/Event.cc
Normal file
|
@ -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_uint_t>(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<zeek::detail::EventMetadataVector>();
|
||||||
|
|
||||||
|
// Internally stored as zeek_uint_t for serializers.
|
||||||
|
meta->emplace_back(desc->Id(), std::move(val));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::tuple<zeek::EventHandlerPtr, zeek::Args, zeek::detail::EventMetadataVectorPtr> Event::Take() && {
|
||||||
|
return {handler, std::move(args), std::move(meta)};
|
||||||
|
}
|
82
src/cluster/Event.h
Normal file
82
src/cluster/Event.h
Normal file
|
@ -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<zeek::EventHandlerPtr, zeek::Args, zeek::detail::EventMetadataVectorPtr> Take() &&;
|
||||||
|
|
||||||
|
private:
|
||||||
|
EventHandlerPtr handler;
|
||||||
|
zeek::Args args;
|
||||||
|
zeek::detail::EventMetadataVectorPtr meta;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace zeek::cluster
|
|
@ -12,7 +12,7 @@
|
||||||
#include "zeek/Reporter.h"
|
#include "zeek/Reporter.h"
|
||||||
#include "zeek/Type.h"
|
#include "zeek/Type.h"
|
||||||
#include "zeek/broker/Data.h"
|
#include "zeek/broker/Data.h"
|
||||||
#include "zeek/cluster/Backend.h"
|
#include "zeek/cluster/Event.h"
|
||||||
|
|
||||||
#include "broker/data.bif.h"
|
#include "broker/data.bif.h"
|
||||||
#include "broker/data_envelope.hh"
|
#include "broker/data_envelope.hh"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue