-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow plugins to fail at startup, and Zenohd to react to that failure
- Loading branch information
Pierre Avital
committed
Jul 3, 2023
1 parent
02e5f70
commit b0a97ea
Showing
4 changed files
with
144 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::sync::{Arc, Mutex}; | ||
use std::thread; | ||
use std::time::{Duration, Instant}; | ||
use zenoh::prelude::sync::*; | ||
use zenoh::subscriber::Subscriber; | ||
|
||
#[derive(Clone, Copy)] | ||
struct Chrono(Instant); | ||
impl Chrono { | ||
fn now() -> Self { | ||
Self(Instant::now()) | ||
} | ||
} | ||
impl std::fmt::Display for Chrono { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let t = self.0.elapsed(); | ||
write!(f, "{:.3}", t.as_secs_f32()) | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
struct Node { | ||
id: String, | ||
} | ||
|
||
struct NodeWrapper<'a> { | ||
node: Arc<Mutex<Node>>, | ||
session: Arc<Session>, | ||
subscription: Option<Subscriber<'a, ()>>, | ||
} | ||
|
||
impl NodeWrapper<'_> { | ||
fn new(id: String) -> Self { | ||
let node = Node::new(id); | ||
let node = Arc::new(Mutex::new(node)); | ||
let session = zenoh::open(config::default()).res().unwrap().into_arc(); | ||
Self { | ||
node, | ||
session, | ||
subscription: None, | ||
} | ||
} | ||
|
||
fn run(&mut self, chrono: Chrono) { | ||
let node_clone = Arc::clone(&self.node); | ||
|
||
let subscription = self | ||
.session | ||
.declare_subscriber("key/expression") | ||
.reliable() | ||
.callback(move |sample| { | ||
let value = sample.value.to_string(); | ||
println!("{chrono} RECV {value}"); | ||
let mut node = node_clone.lock().unwrap(); | ||
println!("{chrono} LOCK {value}"); | ||
node.id = value.clone(); | ||
thread::sleep(Duration::from_millis(200)); | ||
println!("{chrono} UNLOCK {value}"); | ||
}) | ||
.res() | ||
.unwrap(); | ||
self.subscription = Some(subscription); | ||
} | ||
|
||
fn get_id(&self) -> String { | ||
self.node.lock().unwrap().id.clone() | ||
} | ||
} | ||
|
||
impl Node { | ||
fn new(id: String) -> Self { | ||
Self { id } | ||
} | ||
} | ||
|
||
fn main() { | ||
let chrono = Chrono::now(); | ||
let mut node = NodeWrapper::new("node1".to_string()); | ||
node.run(chrono); | ||
let mut sessions = Vec::new(); | ||
for i in 1..=16 { | ||
//sleep(Duration::from_secs(1)).await; | ||
println!("{chrono} Starting Session {i}"); | ||
println!("{chrono} N is {}", node.get_id()); | ||
let session = SyncResolve::res(zenoh::open(config::default())).unwrap(); | ||
|
||
for j in 0..100 { | ||
session.put("key/expression", 100 * i + j).res().unwrap(); | ||
} | ||
sessions.push(session); | ||
} | ||
println!("Main thread sleeping"); | ||
std::thread::sleep(Duration::from_secs(60)); | ||
} |
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