Struct Config

Source
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

Source

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§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V