Skip to content

Commit

Permalink
new(AsyncEventPlugin): helper to create async events
Browse files Browse the repository at this point in the history
Reduce the boilerplate to create async events by introducing
AsyncEventPlugin::async_event

Signed-off-by: Angelo Puglisi <[email protected]>
  • Loading branch information
deepskyblue86 authored and poiana committed Dec 13, 2024
1 parent 74a1ccb commit 0e94782
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 47 deletions.
19 changes: 2 additions & 17 deletions falco_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ pub mod parse {
/// use falco_plugin::base::Plugin;
/// use falco_plugin::{async_event_plugin, plugin};
/// use falco_plugin::async_event::{
/// AsyncEvent,
/// AsyncEventPlugin,
/// AsyncHandler,
/// BackgroundTask};
Expand Down Expand Up @@ -249,22 +248,8 @@ pub mod parse {
/// // start a new thread
/// // waiting up to 100ms between events for the stop request
/// self.thread = Some(self.task.spawn(std::time::Duration::from_millis(100), move || {
/// // build an event
/// let event = AsyncEvent {
/// plugin_id: Some(0),
/// name: Some(c"sample_async"),
/// data: Some(b"hello"),
/// };
///
/// let metadata = EventMetadata::default();
///
/// let event = Event {
/// metadata,
/// params: event,
/// };
///
/// // submit it to the main event loop
/// handler.emit(event)?;
/// // submit an async event to the main event loop
/// handler.emit(Self::async_event(c"sample_async", b"hello"))?;
/// Ok(())
/// })?);
/// Ok(())
Expand Down
19 changes: 19 additions & 0 deletions falco_plugin/src/plugin/async_event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::base::Plugin;
use crate::plugin::async_event::async_handler::AsyncHandler;
use crate::plugin::async_event::wrappers::AsyncPluginExported;

use falco_event::events::types::PPME_ASYNCEVENT_E as AsyncEvent;
use falco_event::events::Event;

pub mod async_handler;
pub mod background_task;
#[doc(hidden)]
Expand Down Expand Up @@ -48,4 +51,20 @@ pub trait AsyncEventPlugin: Plugin + AsyncPluginExported {
///
/// **Note**: [`AsyncEventPlugin::start_async`] can be called again, with a different [`AsyncHandler`].
fn stop_async(&mut self) -> Result<(), anyhow::Error>;

/// # A helper method to create an asynchronous event
fn async_event<'a>(name: &'a std::ffi::CStr, data: &'a [u8]) -> Event<AsyncEvent<'a>> {
let event = AsyncEvent {
plugin_id: None, // gets populated by the framework, shall be None
name: Some(name),
data: Some(data),
};

let metadata = falco_event::events::EventMetadata::default();

Event {
metadata,
params: event,
}
}
}
35 changes: 5 additions & 30 deletions falco_plugin_tests/tests/async.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use falco_plugin::anyhow::{self, Error};
use falco_plugin::async_event::{AsyncEvent, AsyncEventPlugin, AsyncHandler, BackgroundTask};
use falco_plugin::async_event::{AsyncEventPlugin, AsyncHandler, BackgroundTask};
use falco_plugin::base::Plugin;
use falco_plugin::event::events::{Event, EventMetadata};
use falco_plugin::extract::EventInput;
use falco_plugin::source::{EventBatch, SourcePlugin, SourcePluginInstance};
use falco_plugin::tables::TablesInput;
Expand Down Expand Up @@ -70,34 +69,10 @@ impl AsyncEventPlugin for DummyPlugin {

self.thread = Some(self.task.spawn(Duration::from_millis(100), move || {
dbg!("emitting event");
let event = AsyncEvent {
plugin_id: Some(0),
name: Some(c"dummy_async"),
data: Some(b"hello"),
};

let metadata = EventMetadata::default();

let event = Event {
metadata,
params: event,
};
handler.emit(event)?;

let event = AsyncEvent {
plugin_id: Some(0),
name: Some(c"invalid_event_name"),
data: Some(b"hello"),
};

let metadata = EventMetadata::default();

let event = Event {
metadata,
params: event,
};
assert!(handler.emit(event).is_err());

handler.emit(Self::async_event(c"dummy_async", b"hello"))?;
assert!(handler
.emit(Self::async_event(c"invalid_event_name", b"hello"))
.is_err());
Ok(())
})?);

Expand Down

0 comments on commit 0e94782

Please sign in to comment.