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
use std::collections::HashMap;


use crate::{errors::ProtocolError, properties::Properties, protocol::{Encapsulation, Identity, LocatorResult, RequestData}};
use crate::encoding::{ToBytes,FromBytes};
use crate::proxy::Proxy;

pub struct Locator {
    proxy: Proxy,
    request_id: i32
}

impl Locator {
    pub fn new(proxy_string: &str, properties: &Properties) -> Result<Locator, Box<dyn std::error::Error>> {
        Ok(Locator {
            proxy: Proxy::new(proxy_string, properties)?,
            request_id: 0
        })
    }

    pub fn find_object_by_id(&mut self, req: &str) -> Result<LocatorResult, Box<dyn std::error::Error>> {
        self.request_id = self.request_id + 1;
        let mut bytes = req.to_bytes()?;
        bytes.push(0);
        let req_data = RequestData {
            request_id: self.request_id,
            id: Identity::new(&self.proxy.ident),
            facet: vec![],
            operation: String::from("findObjectById"),
            mode: 1,
            context: HashMap::new(),
            params: Encapsulation::from(bytes)
        };
        let reply = self.proxy.make_request::<ProtocolError>(&req_data)?;

        let mut read = 0;
        LocatorResult::from_bytes(&reply.body.data[read as usize..reply.body.data.len()], &mut read)
    }

    pub fn find_adapter_by_id(&mut self, req: &str) -> Result<LocatorResult, Box<dyn std::error::Error>> {
        self.request_id = self.request_id + 1;
        let bytes = req.to_bytes()?;
        let req_data = RequestData {
            request_id: self.request_id,
            id: Identity::new(&self.proxy.ident),
            facet: vec![],
            operation: String::from("findAdapterById"),
            mode: 1,
            context: HashMap::new(),
            params: Encapsulation::from(bytes)
        };
        let reply = self.proxy.make_request::<ProtocolError>(&req_data)?;
        
        let mut read = 0;
        LocatorResult::from_bytes(&reply.body.data[read as usize..reply.body.data.len()], &mut read)
    }
}