ice_rs/
communicator.rs

1use std::sync::Mutex;
2
3use crate::{proxy::Proxy, proxy_factory::ProxyFactory};
4use crate::initdata::InitializationData;
5use crate::errors::PropertyError;
6use crate::adapter::*;
7use lazy_static::lazy_static;
8
9lazy_static! {
10    pub static ref INITDATA: Mutex<InitializationData> = Mutex::new(InitializationData::new());
11}
12
13/// The Communicator is a basic object in ZeroC Ice. Currently
14/// this is more a stub that does dummy initialization.
15pub struct Communicator {
16    proxy_factory: ProxyFactory
17}
18
19impl Communicator {
20    pub async fn new() -> Result<Communicator, Box<dyn std::error::Error + Sync + Send>> {
21        let init_data = INITDATA.lock().unwrap();
22        let proxy_factory = ProxyFactory::new(init_data.properties()).await?;
23        Ok(Communicator {
24            proxy_factory
25        })
26    }
27
28    pub async fn string_to_proxy(&mut self, proxy_string: &str) -> Result<Proxy, Box<dyn std::error::Error + Sync + Send>> {
29        let init_data = INITDATA.lock().unwrap();
30        self.proxy_factory.create(proxy_string, init_data.properties()).await
31    }
32
33    pub async fn property_to_proxy(&mut self, property: &str) -> Result<Proxy, Box<dyn std::error::Error + Sync + Send>> {
34        let init_data = INITDATA.lock().unwrap();
35        let properties = init_data.properties();
36        match properties.get(property) {
37            Some(value) => {
38                self.proxy_factory.create(value, &properties).await
39            }
40            None => {
41                Err(Box::new(PropertyError::new(property)))
42            }
43        }
44    }
45
46    pub async fn create_object_adapter_with_endpoint(&self, name: &str, endpoint: &str) -> Result<Adapter, Box<dyn std::error::Error + Sync + Send>> {
47        Adapter::with_endpoint(name, endpoint)
48    }
49}
50
51pub async fn initialize(config_file: &str) -> Result<Communicator, Box<dyn std::error::Error + Sync + Send>> {
52    let mut init_data = INITDATA.lock().unwrap();
53    let properties = init_data.properties_as_mut();
54    properties.load(config_file).unwrap();
55    let proxy_factory = ProxyFactory::new(&properties).await?;
56    Ok(Communicator {
57        proxy_factory
58    })
59}