1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::sync::Mutex;

use crate::{proxy::Proxy, proxy_factory::ProxyFactory};
use crate::initdata::InitializationData;
use crate::errors::PropertyError;
use crate::adapter::*;
use lazy_static::lazy_static;

lazy_static! {
    pub static ref INITDATA: Mutex<InitializationData> = Mutex::new(InitializationData::new());
}

/// The Communicator is a basic object in ZeroC Ice. Currently
/// this is more a stub that does dummy initialization.
pub struct Communicator {
    proxy_factory: ProxyFactory
}

impl Communicator {
    pub async fn new() -> Result<Communicator, Box<dyn std::error::Error + Sync + Send>> {
        let init_data = INITDATA.lock().unwrap();
        let proxy_factory = ProxyFactory::new(init_data.properties()).await?;
        Ok(Communicator {
            proxy_factory
        })
    }

    pub async fn string_to_proxy(&mut self, proxy_string: &str) -> Result<Proxy, Box<dyn std::error::Error + Sync + Send>> {
        let init_data = INITDATA.lock().unwrap();
        self.proxy_factory.create(proxy_string, init_data.properties()).await
    }

    pub async fn property_to_proxy(&mut self, property: &str) -> Result<Proxy, Box<dyn std::error::Error + Sync + Send>> {
        let init_data = INITDATA.lock().unwrap();
        let properties = init_data.properties();
        match properties.get(property) {
            Some(value) => {
                self.proxy_factory.create(value, &properties).await
            }
            None => {
                Err(Box::new(PropertyError::new(property)))
            }
        }
    }

    pub async fn create_object_adapter_with_endpoint(&self, name: &str, endpoint: &str) -> Result<Adapter, Box<dyn std::error::Error + Sync + Send>> {
        Adapter::with_endpoint(name, endpoint)
    }
}

pub async fn initialize(config_file: &str) -> Result<Communicator, Box<dyn std::error::Error + Sync + Send>> {
    let mut init_data = INITDATA.lock().unwrap();
    let properties = init_data.properties_as_mut();
    properties.load(config_file).unwrap();
    let proxy_factory = ProxyFactory::new(&properties).await?;
    Ok(Communicator {
        proxy_factory
    })
}