1use rustorb_cdr::{ByteOrder, CdrDecode, CdrEncode, CdrVersion, Decoder, Encoder};
2
3use crate::{
4 AlternateIiopAddress, CodeSetComponentInfo, Error, Result, TAG_INTERNET_IOP, TaggedComponent,
5 TaggedProfile, Version, component_tag,
6};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct IiopProfile {
16 pub version: Version,
18 pub host: String,
20 pub port: u16,
22 pub object_key: Vec<u8>,
24 pub components: Vec<TaggedComponent>,
26}
27
28impl IiopProfile {
29 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 pub fn from_encapsulation(data: &[u8]) -> Result<IiopProfile> {
39 let mut dec = Decoder::encapsulation(data, CdrVersion::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 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 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, CdrVersion::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 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 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 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 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 pub fn iiop(&self) -> Result<IiopProfile> {
128 IiopProfile::from_tagged_profile(self)
129 }
130
131 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, CdrVersion::Giop1_2)?;
139 Ok(Vec::decode(&mut dec)?)
140 }
141
142 pub fn multiple_components_profile(
146 components: &[TaggedComponent],
147 order: ByteOrder,
148 ) -> Result<TaggedProfile> {
149 let mut enc = Encoder::encapsulation(order, CdrVersion::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}