started custom event

This commit is contained in:
Dominik Maier 2020-12-11 00:58:15 +01:00
parent 70b3c237e6
commit 7e65193d53

View File

@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::io::Write; use std::io::Write;
use crate::corpus::Corpus; use crate::{corpus::Corpus, serde_anymap::SerdeAny};
use crate::engines::State; use crate::engines::State;
use crate::executors::Executor; use crate::executors::Executor;
use crate::inputs::Input; use crate::inputs::Input;
@ -32,47 +32,25 @@ pub enum BrokerEventResult {
pub trait ShowStats {} pub trait ShowStats {}
/* /// A custom event, for own messages, with own handler.
pub trait CustomEvent<I>: SerdeAny + Serialize
/// A custom event, in case a user wants to extend the features (at compile time)
pub trait CustomEvent<I>
where where
S: State<I, R>,
C: Corpus<I, R>,
E: Executor<I>,
I: Input, I: Input,
R: Rand,
{ {
/// Returns the name of this event /// Returns the name of this event
fn name(&self) -> &str; fn name(&self) -> &str;
/// This method will be called in the broker /// This method will be called in the broker
fn handle_in_broker(&self, broker: &dyn EventManager<C, E, I, R, Self>, state: &mut State<I, R>, corpus: &mut C) -> Result<BrokerEventResult, AflError>; fn handle_in_broker(&self) -> Result<BrokerEventResult, AflError>;
/// This method will be called in the clients after handle_in_broker (unless BrokerEventResult::Handled) was returned in handle_in_broker /// This method will be called in the clients after handle_in_broker (unless BrokerEventResult::Handled) was returned in handle_in_broker
fn handle_in_client(&self, client: &dyn EventManager<C, E, I, R, Self>, state: &mut State<I, R>, corpus: &mut C) -> Result<(), AflError>; fn handle_in_client(&self) -> Result<(), AflError>;
} }
struct UnusedCustomEvent {}
impl<C, E, I, R> CustomEvent<I> for UnusedCustomEvent<I>
where
S: State<I, R>,
C: Corpus<I, R>,
E: Executor<I>,
I: Input,
R: Rand,
{
fn name(&self) -> &str {"No custom events"}
fn handle_in_broker(&self, broker: &dyn EventManager<C, E, I, R, Self>, state: &mut State<I, R>, corpus: &mut C) {Ok(BrokerEventResult::Handled)}
fn handle_in_client(&self, client: &dyn EventManager<C, E, I, R, Self>, state: &mut State<I, R>, corpus: &mut C) {Ok(())}
}
*/
/// Events sent around in the library /// Events sent around in the library
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(bound = "I: serde::de::DeserializeOwned")] #[serde(bound = "I: serde::de::DeserializeOwned")]
pub enum Event<'a, I> pub enum Event<'a, I>
where where
I: Input, I: Input,
// CE: CustomEvent<I>,
{ {
LoadInitial { LoadInitial {
sender_id: u64, sender_id: u64,
@ -108,7 +86,11 @@ where
None { None {
phantom: PhantomData<I>, phantom: PhantomData<I>,
}, },
//Custom {sender_id: u64, custom_event: CE}, Custom {
sender_id: u64,
// TODO: Allow custom events
// custom_event: Box<dyn CustomEvent<I>>,
},
} }
impl<'a, I> Event<'a, I> impl<'a, I> Event<'a, I>
@ -150,7 +132,7 @@ where
phantom: _, phantom: _,
} => "Log", } => "Log",
Event::None { phantom: _ } => "None", Event::None { phantom: _ } => "None",
//Event::Custom {sender_id, custom_event} => custom_event.name(), Event::Custom {sender_id, /*custom_event} => custom_event.name()*/} => "todo",
} }
} }
@ -172,7 +154,6 @@ where
} }
} }
// TODO serialize and deserialize, defaults to serde
} }
pub trait EventManager<C, E, I, R> pub trait EventManager<C, E, I, R>
@ -252,8 +233,8 @@ where
}, },
Event::None { Event::None {
phantom: _, phantom: _,
} => Ok(BrokerEventResult::Handled) } => Ok(BrokerEventResult::Handled),
//Event::Custom {sender_id, custom_event} => custom_event.handle_in_broker(state, corpus), Event::Custom {sender_id, /*custom_event} => custom_event.handle_in_broker(state, corpus)*/} => Ok(BrokerEventResult::Forward),
//_ => Ok(BrokerEventResult::Forward), //_ => Ok(BrokerEventResult::Forward),
} }
} }
@ -397,7 +378,6 @@ where
E: Executor<I>, E: Executor<I>,
I: Input, I: Input,
R: Rand, R: Rand,
//CE: CustomEvent<I>,
{ {
/// Fire an Event /// Fire an Event
fn fire<'a>( fn fire<'a>(
@ -498,8 +478,8 @@ where
}, },
Event::None { Event::None {
phantom: _, phantom: _,
} => Ok(BrokerEventResult::Handled) } => Ok(BrokerEventResult::Handled),
//Event::Custom {sender_id, custom_event} => custom_event.handle_in_broker(state, corpus), Event::Custom {sender_id, /*custom_event} => custom_event.handle_in_broker(state, corpus)*/} => Ok(BrokerEventResult::Forward),
//_ => Ok(BrokerEventResult::Forward), //_ => Ok(BrokerEventResult::Forward),
} }
} }