Skip to content

Commit

Permalink
iox-#9 API draft for reactor
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Aug 25, 2023
1 parent 553140e commit 921cda9
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
pub mod introspection;
pub mod marker;
pub mod reactor;

mod error;
pub use error::IceoryxError;
Expand Down
28 changes: 28 additions & 0 deletions src/reactor/control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project
// SPDX-FileContributor: Mathias Kraus

//! Control to register/unregister handler and stop the reactor
use super::{Event, Handler, State};

use std::sync::Arc;

pub struct Control {
condition_variable: Arc<Box<ffi::ConditionVariable>>,
// TODO queue sender
}

impl Control {
pub(super) fn new(condition_variable: Arc<Box<ffi::ConditionVariable>>) -> Self {
Self { condition_variable }
}

pub fn attach_event<T: Event>(source: Box<dyn Event>, handler: Box<Handler<T>>) {
unimplemented!()
}

pub fn attach_state<T: State>(source: Box<dyn State>, handler: Box<Handler<T>>) {
unimplemented!()
}
}
16 changes: 16 additions & 0 deletions src/reactor/demultiplexer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project
// SPDX-FileContributor: Mathias Kraus

use std::sync::Arc;

pub(super) struct Demultiplexer {
condition_variable: Arc<Box<ffi::ConditionVariable>>,
// TODO queue receiver
}

impl Demultiplexer {
pub(super) fn new(condition_variable: Arc<Box<ffi::ConditionVariable>>) -> Self {
Self { condition_variable }
}
}
22 changes: 22 additions & 0 deletions src/reactor/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project
// SPDX-FileContributor: Mathias Kraus

//! Dispatches the events according to the registered handler
use super::Demultiplexer;

use std::sync::Arc;

pub struct Dispatcher {
demux: Demultiplexer,
// TODO queue receiver
}

impl Dispatcher {
pub(super) fn new(condition_variable: Arc<Box<ffi::ConditionVariable>>) -> Self {
Self {
demux: Demultiplexer::new(condition_variable),
}
}
}
15 changes: 15 additions & 0 deletions src/reactor/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project
// SPDX-FileContributor: Mathias Kraus

//! Event handler dispatched by the reactor
use std::marker::PhantomData;

pub trait Event {}

pub trait State {}

pub struct Handler<T> {
_phantom: PhantomData<T>,
}
42 changes: 42 additions & 0 deletions src/reactor/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project
// SPDX-FileContributor: Mathias Kraus

//! Implements the reactor pattern
//!
//! Subscriber can use the `Control` to register at the reactor and the `Dispatcher`
//! calls the registered handler functions.
use std::sync::Arc;

mod control;
pub use control::Control;

mod demultiplexer;
use demultiplexer::Demultiplexer;

mod dispatcher;
pub use dispatcher::Dispatcher;

mod handler;
pub use handler::Event;
pub use handler::Handler;
pub use handler::State;

struct Reactor {
control: Control,
dispatcher: Dispatcher,
}

impl Reactor {
pub fn new() -> Self {
let condition_variable = Arc::new(ffi::ConditionVariable::new());
let control = Control::new(condition_variable.clone());
let dispatcher = Dispatcher::new(condition_variable);

Self {
control,
dispatcher,
}
}
}

0 comments on commit 921cda9

Please sign in to comment.