mirror of
https://github.com/logos-messaging/logos-messaging-rust-bindings.git
synced 2026-01-06 16:03:07 +00:00
Implemented safety layer on top of node
This commit is contained in:
parent
239a9bc6e6
commit
25e767b699
@ -1,2 +1,66 @@
|
|||||||
mod config;
|
mod config;
|
||||||
mod node;
|
mod node;
|
||||||
|
|
||||||
|
// std
|
||||||
|
use multiaddr::Multiaddr;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
// crates
|
||||||
|
// internal
|
||||||
|
use crate::general::Result;
|
||||||
|
|
||||||
|
pub use config::WakuNodeConfig;
|
||||||
|
|
||||||
|
/// Shared flag to check if a waku node is already running in the current process
|
||||||
|
const WAKU_NODE_RUNNING: Mutex<bool> = Mutex::new(false);
|
||||||
|
|
||||||
|
/// Marker trait to disallow undesired waku node states in the handle
|
||||||
|
pub trait WakuNodeState {}
|
||||||
|
|
||||||
|
/// Waku node initialized state
|
||||||
|
pub struct Initialized;
|
||||||
|
|
||||||
|
/// Waku node running state
|
||||||
|
pub struct Running;
|
||||||
|
|
||||||
|
impl WakuNodeState for Initialized {}
|
||||||
|
impl WakuNodeState for Running {}
|
||||||
|
|
||||||
|
pub struct WakuNodeHandle<State: WakuNodeState>(PhantomData<State>);
|
||||||
|
|
||||||
|
impl<State: WakuNodeState> WakuNodeHandle<State> {
|
||||||
|
pub fn peer_id(&self) -> Result<String> {
|
||||||
|
node::waku_peer_id()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn listen_addresses(&self) -> Result<Vec<Multiaddr>> {
|
||||||
|
node::waku_listen_addressses()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WakuNodeHandle<Initialized> {
|
||||||
|
pub fn start(self) -> Result<WakuNodeHandle<Running>> {
|
||||||
|
let flag = WAKU_NODE_RUNNING;
|
||||||
|
let mut node_running = flag.lock().expect("Access to the mutex at some point");
|
||||||
|
if *node_running {
|
||||||
|
return Err("Waku node is already running".into());
|
||||||
|
}
|
||||||
|
match node::waku_start() {
|
||||||
|
Ok(_) => {
|
||||||
|
*node_running = true;
|
||||||
|
Ok(WakuNodeHandle(Default::default()))
|
||||||
|
}
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WakuNodeHandle<Running> {
|
||||||
|
pub fn stop(self) -> Result<()> {
|
||||||
|
node::waku_stop().map(|_| ())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<WakuNodeHandle<Initialized>> {
|
||||||
|
node::waku_new(config).map(|_| WakuNodeHandle(Default::default()))
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user