disconnect_watcher/
disconnect_watcher.rs1use std::sync::Arc;
2use std::time::Duration;
3
4use netconf_rust::{Config, Session};
5
6#[tokio::main]
7async fn main() -> netconf_rust::Result<()> {
8 let config = Config::builder()
9 .keepalive_interval(Duration::from_secs(10))
10 .keepalive_max(3)
11 .rpc_timeout(Duration::from_secs(30))
12 .connect_timeout(Duration::from_secs(10))
13 .nodelay(true)
14 .build();
15
16 let session = Arc::new(
17 Session::connect_with_config("localhost", 830, "netconf", "netconf", config).await?,
18 );
19
20 println!("Connected (session {})", session.session_id());
21
22 let watcher_session = Arc::clone(&session);
26 let watcher = tokio::spawn(async move {
27 let reason = watcher_session.disconnected().await;
28 println!("Disconnect detected: {reason}");
29 });
32
33 let config = session
35 .get_config(netconf_rust::Datastore::Running, None)
36 .await?;
37 println!("Got config ({} bytes)", config.len());
38
39 tokio::select! {
41 result = session.get_config(netconf_rust::Datastore::Running, None) => {
42 match result {
43 Ok(data) => println!("Got config again ({} bytes)", data.len()),
44 Err(e) => eprintln!("RPC failed: {e}"),
45 }
46 }
47 reason = session.disconnected() => {
48 eprintln!("Connection lost while waiting for RPC: {reason}");
49 }
50 }
51
52 session.close_session().await?;
55 println!("Session closed");
56
57 let _ = watcher.await;
59
60 Ok(())
61}