pub struct Config {
pub network: String,
pub address: String,
pub sphinx_geometry: Geometry,
pub on_connection_status: Option<Arc<dyn Fn(&BTreeMap<Value, Value>) + Send + Sync>>,
pub on_new_pki_document: Option<Arc<dyn Fn(&BTreeMap<Value, Value>) + Send + Sync>>,
pub on_message_sent: Option<Arc<dyn Fn(&BTreeMap<Value, Value>) + Send + Sync>>,
pub on_message_reply: Option<Arc<dyn Fn(&BTreeMap<Value, Value>) + Send + Sync>>,
}Expand description
Our configuration defines some callbacks which the thin client will envoke when it receives the corresponding event from the client daemon.
Fields§
§network: String§address: String§sphinx_geometry: Geometry§on_connection_status: Option<Arc<dyn Fn(&BTreeMap<Value, Value>) + Send + Sync>>§on_new_pki_document: Option<Arc<dyn Fn(&BTreeMap<Value, Value>) + Send + Sync>>§on_message_sent: Option<Arc<dyn Fn(&BTreeMap<Value, Value>) + Send + Sync>>§on_message_reply: Option<Arc<dyn Fn(&BTreeMap<Value, Value>) + Send + Sync>>Implementations§
Source§impl Config
impl Config
Sourcepub fn new(filepath: &str) -> Result<Self, Box<dyn Error>>
pub fn new(filepath: &str) -> Result<Self, Box<dyn Error>>
Examples found in repository?
examples/echo_ping.rs (line 66)
61async fn run_client(config_path: &str) -> Result<(), Box<dyn std::error::Error>> {
62 let state = Arc::new(ClientState::new());
63 let state_for_reply = Arc::clone(&state);
64 let state_for_pki = Arc::clone(&state);
65
66 let mut cfg = Config::new(config_path)?;
67 cfg.on_new_pki_document = Some(Arc::new(move |_pki_doc| {
68 println!("✅ PKI document received.");
69 state_for_pki.set_pki_received();
70 }));
71 cfg.on_message_reply = Some(Arc::new(move |reply| {
72 println!("📩 Received a reply!");
73 state_for_reply.save_reply(reply);
74 }));
75
76 println!("🚀 Initializing ThinClient...");
77 let client = ThinClient::new(cfg).await?;
78
79 println!("⏳ Waiting for PKI document...");
80 let result = timeout(Duration::from_secs(5), async {
81 loop {
82 if state.is_pki_received() {
83 break;
84 }
85 tokio::task::yield_now().await;
86 }
87 })
88 .await;
89
90 if result.is_err() {
91 return Err("❌ PKI document not received in time.".into());
92 }
93
94 println!("✅ Pretty printing PKI document:");
95 let doc = client.pki_document().await;
96 pretty_print_pki_doc(&doc);
97 println!("AFTER Pretty printing PKI document");
98
99
100 let service_desc = client.get_service("echo").await?;
101 println!("got service descriptor for echo service");
102
103 let surb_id = ThinClient::new_surb_id();
104 let payload = b"hello".to_vec();
105 let (dest_node, dest_queue) = service_desc.to_destination();
106
107 println!("before calling send_message");
108 client.send_message(surb_id, &payload, dest_node, dest_queue).await?;
109 println!("after calling send_message");
110
111 println!("⏳ Waiting for message reply...");
112 let state_for_reply_wait = Arc::clone(&state);
113
114 let result = timeout(Duration::from_secs(5), async move {
115 loop {
116 if let Some(reply) = state_for_reply_wait.await_message_reply() {
117 if let Some(Value::Bytes(payload2)) = reply.get(&Value::Text("payload".to_string())) {
118 let payload2 = &payload2[..payload.len()];
119 assert_eq!(payload, payload2, "Reply does not match payload!");
120 println!("✅ Received valid reply, stopping client.");
121 return Ok::<(), Box<dyn std::error::Error>>(());
122 }
123 }
124 tokio::task::yield_now().await;
125 }
126 }).await;
127
128 result.map_err(|e| Box::new(e))??;
129 client.stop().await;
130 println!("✅ Client stopped successfully.");
131 Ok(())
132}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Config
impl !RefUnwindSafe for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl !UnwindSafe for Config
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more