From 921cda96b6257c43840560d7e6f9c3320753c63a Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Fri, 25 Aug 2023 14:14:44 +0200 Subject: [PATCH] iox-#9 API draft for reactor --- src/lib.rs | 1 + src/reactor/control.rs | 28 ++++++++++++++++++++++++ src/reactor/demultiplexer.rs | 16 ++++++++++++++ src/reactor/dispatcher.rs | 22 +++++++++++++++++++ src/reactor/handler.rs | 15 +++++++++++++ src/reactor/mod.rs | 42 ++++++++++++++++++++++++++++++++++++ 6 files changed, 124 insertions(+) create mode 100644 src/reactor/control.rs create mode 100644 src/reactor/demultiplexer.rs create mode 100644 src/reactor/dispatcher.rs create mode 100644 src/reactor/handler.rs create mode 100644 src/reactor/mod.rs diff --git a/src/lib.rs b/src/lib.rs index d2353c6..763dfa6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,7 @@ pub mod introspection; pub mod marker; +pub mod reactor; mod error; pub use error::IceoryxError; diff --git a/src/reactor/control.rs b/src/reactor/control.rs new file mode 100644 index 0000000..ed6ceef --- /dev/null +++ b/src/reactor/control.rs @@ -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>, + // TODO queue sender +} + +impl Control { + pub(super) fn new(condition_variable: Arc>) -> Self { + Self { condition_variable } + } + + pub fn attach_event(source: Box, handler: Box>) { + unimplemented!() + } + + pub fn attach_state(source: Box, handler: Box>) { + unimplemented!() + } +} diff --git a/src/reactor/demultiplexer.rs b/src/reactor/demultiplexer.rs new file mode 100644 index 0000000..2a964e7 --- /dev/null +++ b/src/reactor/demultiplexer.rs @@ -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>, + // TODO queue receiver +} + +impl Demultiplexer { + pub(super) fn new(condition_variable: Arc>) -> Self { + Self { condition_variable } + } +} diff --git a/src/reactor/dispatcher.rs b/src/reactor/dispatcher.rs new file mode 100644 index 0000000..59ef533 --- /dev/null +++ b/src/reactor/dispatcher.rs @@ -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>) -> Self { + Self { + demux: Demultiplexer::new(condition_variable), + } + } +} diff --git a/src/reactor/handler.rs b/src/reactor/handler.rs new file mode 100644 index 0000000..4de1915 --- /dev/null +++ b/src/reactor/handler.rs @@ -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 { + _phantom: PhantomData, +} diff --git a/src/reactor/mod.rs b/src/reactor/mod.rs new file mode 100644 index 0000000..3b175b8 --- /dev/null +++ b/src/reactor/mod.rs @@ -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, + } + } +}