Skip to main content

rustorb_giop/
iiop.rs

1use rustorb_cdr::{ByteOrder, CdrDecode, CdrEncode, Decoder, Encoder, Encoding};
2
3use crate::{
4    AlternateIiopAddress, CodeSetComponentInfo, Error, Result, TAG_INTERNET_IOP, TaggedComponent,
5    TaggedProfile, Version, component_tag,
6};
7
8/// `IIOP::ProfileBody`, the body of a [`TAG_INTERNET_IOP`] profile.
9///
10/// IIOP 1.0 profiles have no components; 1.1 and later carry a component
11/// list. Minor versions above 2 are read like 1.2, and bytes after the
12/// components are ignored, as the specification asks for forward
13/// compatibility.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct IiopProfile {
16    /// IIOP version, which bounds the GIOP version a client may use.
17    pub version: Version,
18    /// Host name or IP address.
19    pub host: String,
20    /// TCP port.
21    pub port: u16,
22    /// Object key sent in requests.
23    pub object_key: Vec<u8>,
24    /// Tagged components (IIOP 1.1 and later).
25    pub components: Vec<TaggedComponent>,
26}
27
28impl IiopProfile {
29    /// Reads the profile from a [`TAG_INTERNET_IOP`] tagged profile.
30    pub fn from_tagged_profile(profile: &TaggedProfile) -> Result<IiopProfile> {
31        if profile.tag != TAG_INTERNET_IOP {
32            return Err(Error::UnexpectedProfileTag(profile.tag));
33        }
34        IiopProfile::from_encapsulation(&profile.profile_data)
35    }
36
37    /// Reads the profile from the encapsulated profile body.
38    pub fn from_encapsulation(data: &[u8]) -> Result<IiopProfile> {
39        let mut dec = Decoder::encapsulation(data, Encoding::Giop1_2)?;
40        let version = Version {
41            major: dec.read_octet()?,
42            minor: dec.read_octet()?,
43        };
44        if version.major != 1 {
45            return Err(Error::UnsupportedIiopVersion(version));
46        }
47        let host = dec.read_string()?;
48        let port = dec.read_u16()?;
49        let object_key = dec.read_octet_seq()?.to_vec();
50        let components = if version.minor == 0 {
51            Vec::new()
52        } else {
53            Vec::decode(&mut dec)?
54        };
55        Ok(IiopProfile {
56            version,
57            host,
58            port,
59            object_key,
60            components,
61        })
62    }
63
64    /// Writes the profile as a [`TAG_INTERNET_IOP`] tagged profile whose
65    /// encapsulation uses `order`.
66    pub fn to_tagged_profile(&self, order: ByteOrder) -> Result<TaggedProfile> {
67        Ok(TaggedProfile {
68            tag: TAG_INTERNET_IOP,
69            profile_data: self.to_encapsulation(order)?,
70        })
71    }
72
73    /// Writes the encapsulated profile body.
74    ///
75    /// Fails with [`Error::ComponentsInIiop1_0`] if an IIOP 1.0 profile has
76    /// components, since 1.0 cannot carry them.
77    pub fn to_encapsulation(&self, order: ByteOrder) -> Result<Vec<u8>> {
78        if self.version.major != 1 {
79            return Err(Error::UnsupportedIiopVersion(self.version));
80        }
81        if self.version.minor == 0 && !self.components.is_empty() {
82            return Err(Error::ComponentsInIiop1_0);
83        }
84        let mut enc = Encoder::encapsulation(order, Encoding::Giop1_2);
85        enc.write_octet(self.version.major);
86        enc.write_octet(self.version.minor);
87        enc.write_string(&self.host)?;
88        enc.write_u16(self.port);
89        enc.write_octet_seq(&self.object_key)?;
90        if self.version.minor != 0 {
91            self.components.encode(&mut enc)?;
92        }
93        Ok(enc.into_bytes())
94    }
95
96    /// The components with tag `tag`, in order.
97    pub fn components_with_tag(&self, tag: u32) -> impl Iterator<Item = &TaggedComponent> {
98        self.components.iter().filter(move |c| c.tag == tag)
99    }
100
101    /// The ORB type from the first [`component_tag::ORB_TYPE`] component,
102    /// such as [`orb_type::OMNIORB`](crate::orb_type::OMNIORB).
103    pub fn orb_type(&self) -> Option<Result<u32>> {
104        self.components_with_tag(component_tag::ORB_TYPE)
105            .next()
106            .map(|c| Ok(c.decoder()?.read_u32()?))
107    }
108
109    /// The code sets from the first [`component_tag::CODE_SETS`] component.
110    pub fn code_sets(&self) -> Option<Result<CodeSetComponentInfo>> {
111        self.components_with_tag(component_tag::CODE_SETS)
112            .next()
113            .map(CodeSetComponentInfo::from_component)
114    }
115
116    /// The addresses from every [`component_tag::ALTERNATE_IIOP_ADDRESS`]
117    /// component.
118    pub fn alternate_addresses(&self) -> Result<Vec<AlternateIiopAddress>> {
119        self.components_with_tag(component_tag::ALTERNATE_IIOP_ADDRESS)
120            .map(AlternateIiopAddress::from_component)
121            .collect()
122    }
123}
124
125impl TaggedProfile {
126    /// Reads this profile as an IIOP profile.
127    pub fn iiop(&self) -> Result<IiopProfile> {
128        IiopProfile::from_tagged_profile(self)
129    }
130
131    /// Reads the components of a [`TAG_MULTIPLE_COMPONENTS`] profile.
132    ///
133    /// [`TAG_MULTIPLE_COMPONENTS`]: crate::TAG_MULTIPLE_COMPONENTS
134    pub fn multiple_components(&self) -> Result<Vec<TaggedComponent>> {
135        if self.tag != crate::TAG_MULTIPLE_COMPONENTS {
136            return Err(Error::UnexpectedProfileTag(self.tag));
137        }
138        let mut dec = Decoder::encapsulation(&self.profile_data, Encoding::Giop1_2)?;
139        Ok(Vec::decode(&mut dec)?)
140    }
141
142    /// Builds a [`TAG_MULTIPLE_COMPONENTS`] profile.
143    ///
144    /// [`TAG_MULTIPLE_COMPONENTS`]: crate::TAG_MULTIPLE_COMPONENTS
145    pub fn multiple_components_profile(
146        components: &[TaggedComponent],
147        order: ByteOrder,
148    ) -> Result<TaggedProfile> {
149        let mut enc = Encoder::encapsulation(order, Encoding::Giop1_2);
150        components.encode(&mut enc)?;
151        Ok(TaggedProfile {
152            tag: crate::TAG_MULTIPLE_COMPONENTS,
153            profile_data: enc.into_bytes(),
154        })
155    }
156}