rustorb_giop/
service_context.rs1use rustorb_cdr::{ByteOrder, CdrDecode, CdrEncode, Decoder, Encoder, Encoding};
2
3#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct ServiceContext {
6 pub context_id: u32,
8 pub context_data: Vec<u8>,
10}
11
12impl CdrEncode for ServiceContext {
13 fn encode(&self, enc: &mut Encoder) -> rustorb_cdr::Result<()> {
14 enc.write_u32(self.context_id);
15 enc.write_octet_seq(&self.context_data)
16 }
17}
18
19impl CdrDecode for ServiceContext {
20 fn decode(dec: &mut Decoder<'_>) -> rustorb_cdr::Result<Self> {
21 Ok(ServiceContext {
22 context_id: dec.read_u32()?,
23 context_data: dec.read_octet_seq()?.to_vec(),
24 })
25 }
26}
27
28pub mod service_id {
30 pub const TRANSACTION_SERVICE: u32 = 0;
32 pub const CODE_SETS: u32 = 1;
34 pub const CHAIN_BYPASS_CHECK: u32 = 2;
36 pub const CHAIN_BYPASS_INFO: u32 = 3;
38 pub const LOGICAL_THREAD_ID: u32 = 4;
40 pub const BI_DIR_IIOP: u32 = 5;
42 pub const SENDING_CONTEXT_RUN_TIME: u32 = 6;
44 pub const INVOCATION_POLICIES: u32 = 7;
46 pub const FORWARDED_IDENTITY: u32 = 8;
48 pub const UNKNOWN_EXCEPTION_INFO: u32 = 9;
50 pub const RT_CORBA_PRIORITY: u32 = 10;
52 pub const RT_CORBA_PRIORITY_RANGE: u32 = 11;
54 pub const FT_GROUP_VERSION: u32 = 12;
56 pub const FT_REQUEST: u32 = 13;
58 pub const EXCEPTION_DETAIL_MESSAGE: u32 = 14;
60 pub const SECURITY_ATTRIBUTE_SERVICE: u32 = 15;
62 pub const ACTIVITY_SERVICE: u32 = 16;
64}
65
66pub mod code_set {
68 pub const ISO_8859_1: u32 = 0x0001_0001;
70 pub const UCS_2_LEVEL_1: u32 = 0x0001_0100;
72 pub const UTF_16: u32 = 0x0001_0109;
74 pub const UTF_8: u32 = 0x0501_0001;
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub struct CodeSetContext {
82 pub char_data: u32,
84 pub wchar_data: u32,
86}
87
88impl CodeSetContext {
89 pub fn to_service_context(&self, order: ByteOrder) -> ServiceContext {
91 let mut enc = Encoder::encapsulation(order, Encoding::Giop1_2);
92 enc.write_u32(self.char_data);
93 enc.write_u32(self.wchar_data);
94 ServiceContext {
95 context_id: service_id::CODE_SETS,
96 context_data: enc.into_bytes(),
97 }
98 }
99
100 pub fn from_service_context(context: &ServiceContext) -> rustorb_cdr::Result<CodeSetContext> {
102 let mut dec = Decoder::encapsulation(&context.context_data, Encoding::Giop1_2)?;
103 Ok(CodeSetContext {
104 char_data: dec.read_u32()?,
105 wchar_data: dec.read_u32()?,
106 })
107 }
108
109 pub fn find(contexts: &[ServiceContext]) -> Option<rustorb_cdr::Result<CodeSetContext>> {
111 contexts
112 .iter()
113 .find(|c| c.context_id == service_id::CODE_SETS)
114 .map(CodeSetContext::from_service_context)
115 }
116}
117
118#[derive(Debug, Clone, PartialEq, Eq, Hash)]
120pub struct ListenPoint {
121 pub host: String,
123 pub port: u16,
125}
126
127#[derive(Debug, Clone, PartialEq, Eq, Default)]
131pub struct BiDirIiopServiceContext {
132 pub listen_points: Vec<ListenPoint>,
134}
135
136impl BiDirIiopServiceContext {
137 pub fn to_service_context(&self, order: ByteOrder) -> rustorb_cdr::Result<ServiceContext> {
139 let mut enc = Encoder::encapsulation(order, Encoding::Giop1_2);
140 enc.write_len(self.listen_points.len())?;
141 for lp in &self.listen_points {
142 enc.write_string(&lp.host)?;
143 enc.write_u16(lp.port);
144 }
145 Ok(ServiceContext {
146 context_id: service_id::BI_DIR_IIOP,
147 context_data: enc.into_bytes(),
148 })
149 }
150
151 pub fn from_service_context(
153 context: &ServiceContext,
154 ) -> rustorb_cdr::Result<BiDirIiopServiceContext> {
155 let mut dec = Decoder::encapsulation(&context.context_data, Encoding::Giop1_2)?;
156 let n = dec.read_len(7)?;
159 let mut listen_points = Vec::with_capacity(n);
160 for _ in 0..n {
161 listen_points.push(ListenPoint {
162 host: dec.read_string()?,
163 port: dec.read_u16()?,
164 });
165 }
166 Ok(BiDirIiopServiceContext { listen_points })
167 }
168
169 pub fn find(
171 contexts: &[ServiceContext],
172 ) -> Option<rustorb_cdr::Result<BiDirIiopServiceContext>> {
173 contexts
174 .iter()
175 .find(|c| c.context_id == service_id::BI_DIR_IIOP)
176 .map(BiDirIiopServiceContext::from_service_context)
177 }
178}
179
180#[cfg(test)]
181mod bidir_tests {
182 use super::*;
183
184 #[test]
185 fn bidir_context_round_trips() {
186 let ctx = BiDirIiopServiceContext {
187 listen_points: vec![
188 ListenPoint {
189 host: "client.example".into(),
190 port: 2809,
191 },
192 ListenPoint {
193 host: "10.0.0.1".into(),
194 port: 65535,
195 },
196 ],
197 };
198 for order in [ByteOrder::Big, ByteOrder::Little] {
199 let sc = ctx.to_service_context(order).unwrap();
200 assert_eq!(sc.context_id, service_id::BI_DIR_IIOP);
201 assert_eq!(BiDirIiopServiceContext::find(&[sc]).unwrap().unwrap(), ctx);
202 }
203 }
204
205 #[test]
206 fn bidir_context_wire_form() {
207 let ctx = BiDirIiopServiceContext {
208 listen_points: vec![ListenPoint {
209 host: "h".into(),
210 port: 0x1234,
211 }],
212 };
213 let sc = ctx.to_service_context(ByteOrder::Big).unwrap();
214 assert_eq!(
216 sc.context_data,
217 [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, b'h', 0, 0x12, 0x34]
218 );
219 }
220
221 #[test]
222 fn bidir_context_rejects_a_bad_count() {
223 let sc = ServiceContext {
224 context_id: service_id::BI_DIR_IIOP,
225 context_data: vec![0, 0, 0, 0, 0x7f, 0xff, 0xff, 0xff],
226 };
227 assert!(BiDirIiopServiceContext::find(&[sc]).unwrap().is_err());
228 }
229}