-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use super::*; | ||
|
||
use futures::{future, Future}; | ||
|
||
use std::{cell::RefCell, io::Error, rc::Rc}; | ||
|
||
/// Helper for sharing a context between multiple clients, | ||
/// i.e. when addressing multiple slave devices in turn. | ||
#[derive(Default)] | ||
struct SharedContext { | ||
context: Option<Rc<RefCell<Context>>>, | ||
} | ||
|
||
impl SharedContext { | ||
/// Create an instance by wrapping an initial, optional context. | ||
fn new(initial_context: Option<Context>) -> Self { | ||
Self { | ||
context: initial_context.map(RefCell::new).map(Rc::new), | ||
} | ||
} | ||
|
||
/// Disconnect and drop the wrapped context reference. | ||
fn disconnect(&mut self) -> impl Future<Item = (), Error = Error> { | ||
if let Some(context) = self.context.take() { | ||
future::Either::A(context.borrow().disconnect()) | ||
} else { | ||
future::Either::B(future::ok(())) | ||
} | ||
} | ||
|
||
/// Reconnect by replacing the wrapped context reference. | ||
fn reconnect(&mut self, context: Context) { | ||
self.context = Some(Rc::new(RefCell::new(context))); | ||
} | ||
|
||
fn shared_context(&self) -> Option<Rc<RefCell<Context>>> { | ||
self.context.as_ref().map(Rc::clone) | ||
} | ||
} | ||
|
||
/// Factory trait for creating a new context. | ||
pub trait NewContext { | ||
fn new_context(&self) -> Box<dyn Future<Item = Context, Error = Error>>; | ||
} | ||
|
||
/// Reconnectable environment with a shared context. | ||
pub struct SharedEnvironment { | ||
shared_context: SharedContext, | ||
new_context: Box<dyn NewContext>, | ||
} | ||
|
||
impl SharedEnvironment { | ||
/// Create a new instance with an optional, initial context and | ||
/// a trait object for reconnecting the shared context on demand. | ||
pub fn new(inital_context: Option<Context>, new_context: Box<dyn NewContext>) -> Self { | ||
Self { | ||
shared_context: SharedContext::new(inital_context), | ||
new_context, | ||
} | ||
} | ||
|
||
/// Obtain a shared reference of the wrapped context. | ||
/// | ||
/// The result should only be used temporarily for subsequent | ||
/// requests and must not be stored. | ||
pub fn shared_context(&self) -> Option<Rc<RefCell<Context>>> { | ||
self.shared_context.shared_context() | ||
} | ||
} | ||
|
||
pub fn reconnect_shared_context( | ||
shared_env: &Rc<RefCell<SharedEnvironment>>, | ||
) -> impl Future<Item = (), Error = Error> { | ||
let disconnected_env = Rc::clone(shared_env); | ||
shared_env | ||
.borrow_mut() | ||
.shared_context | ||
.disconnect() | ||
.and_then(move |()| { | ||
debug_assert!(!disconnected_env.borrow().shared_context().is_none()); | ||
let reconnected_env = Rc::clone(&disconnected_env); | ||
disconnected_env | ||
.borrow() | ||
.new_context | ||
.new_context() | ||
.map(move |context| { | ||
reconnected_env | ||
.borrow_mut() | ||
.shared_context | ||
.reconnect(context) | ||
}) | ||
}) | ||
} |