1use std::any::Any;
5use std::fmt;
6use std::sync::Arc;
7
8use rustorb_cdr::{ByteOrder, CdrEncode};
9use rustorb_giop::{
10 Ior, MessageEncoder, OutMessage, ReplyHeader, ReplyStatus, ServiceContext, SystemExceptionBody,
11 Version,
12};
13
14use crate::stream::{InStream, OutStream};
15use crate::{CodeSets, CompletionStatus, ObjectRef, Poa, SystemException, SystemExceptionKind};
16
17pub(crate) const OBJECT_ID: &str = "IDL:omg.org/CORBA/Object:1.0";
19
20pub trait Servant: Send + Sync {
26 fn repository_id(&self) -> &str;
29
30 fn is_a(&self, repository_id: &str) -> bool {
34 repository_id == self.repository_id()
35 }
36
37 fn dispatch(
46 &self,
47 operation: &str,
48 input: &mut InStream<'_>,
49 reply: &mut ServerReply,
50 ) -> Result<(), SystemException>;
51}
52
53#[derive(Debug, Clone)]
55pub enum ServantManagerError {
56 ForwardRequest(ObjectRef),
59 System(SystemException),
61}
62
63impl From<SystemException> for ServantManagerError {
64 fn from(e: SystemException) -> Self {
65 ServantManagerError::System(e)
66 }
67}
68
69impl fmt::Display for ServantManagerError {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 match self {
72 ServantManagerError::ForwardRequest(_) => f.write_str("PortableServer::ForwardRequest"),
73 ServantManagerError::System(e) => e.fmt(f),
74 }
75 }
76}
77
78impl std::error::Error for ServantManagerError {}
79
80pub trait ServantActivator: Send + Sync {
88 fn incarnate(&self, id: &[u8], adapter: &Poa) -> Result<Arc<dyn Servant>, ServantManagerError>;
92
93 fn etherealize(
98 &self,
99 id: &[u8],
100 adapter: &Poa,
101 servant: Arc<dyn Servant>,
102 cleanup_in_progress: bool,
103 remaining_activations: bool,
104 ) {
105 let _ = (
106 id,
107 adapter,
108 servant,
109 cleanup_in_progress,
110 remaining_activations,
111 );
112 }
113}
114
115pub type Cookie = Box<dyn Any + Send>;
117
118pub trait ServantLocator: Send + Sync {
124 fn preinvoke(
127 &self,
128 id: &[u8],
129 adapter: &Poa,
130 operation: &str,
131 ) -> Result<(Arc<dyn Servant>, Cookie), ServantManagerError>;
132
133 fn postinvoke(
136 &self,
137 id: &[u8],
138 adapter: &Poa,
139 operation: &str,
140 cookie: Cookie,
141 servant: Arc<dyn Servant>,
142 ) {
143 let _ = (id, adapter, operation, cookie, servant);
144 }
145}
146
147#[derive(Clone)]
149pub enum ServantManager {
150 Activator(Arc<dyn ServantActivator>),
152 Locator(Arc<dyn ServantLocator>),
154}
155
156impl fmt::Debug for ServantManager {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 match self {
159 ServantManager::Activator(_) => f.write_str("ServantManager::Activator"),
160 ServantManager::Locator(_) => f.write_str("ServantManager::Locator"),
161 }
162 }
163}
164
165pub trait AdapterActivator: Send + Sync {
168 fn unknown_adapter(&self, parent: &Poa, name: &str) -> bool;
173}
174
175#[derive(Debug)]
178pub struct ServerReply {
179 version: Version,
180 order: ByteOrder,
181 request_id: u32,
182 code_sets: CodeSets,
183 msg: MessageEncoder,
184 status: ReplyStatus,
185 contexts: Vec<ServiceContext>,
187 exception_id: Option<String>,
189}
190
191impl ServerReply {
192 pub(crate) fn new(
193 version: Version,
194 order: ByteOrder,
195 request_id: u32,
196 code_sets: CodeSets,
197 ) -> Result<ServerReply, SystemException> {
198 ServerReply::with_contexts(version, order, request_id, code_sets, Vec::new())
199 }
200
201 pub(crate) fn with_contexts(
203 version: Version,
204 order: ByteOrder,
205 request_id: u32,
206 code_sets: CodeSets,
207 contexts: Vec<ServiceContext>,
208 ) -> Result<ServerReply, SystemException> {
209 Ok(ServerReply {
210 version,
211 order,
212 request_id,
213 code_sets,
214 msg: start(
215 version,
216 order,
217 request_id,
218 ReplyStatus::NoException,
219 &contexts,
220 )?,
221 status: ReplyStatus::NoException,
222 contexts,
223 exception_id: None,
224 })
225 }
226
227 pub fn output(&mut self) -> OutStream<'_> {
230 OutStream {
231 enc: self.msg.body(),
232 code_sets: self.code_sets,
233 orb: None,
234 }
235 }
236
237 pub fn user_exception(
240 &mut self,
241 repository_id: &str,
242 ) -> Result<OutStream<'_>, SystemException> {
243 self.msg = start(
244 self.version,
245 self.order,
246 self.request_id,
247 ReplyStatus::UserException,
248 &self.contexts,
249 )?;
250 self.status = ReplyStatus::UserException;
251 self.exception_id = Some(repository_id.to_owned());
252 let mut out = self.output();
253 out.write_string(repository_id)?;
254 Ok(out)
255 }
256
257 pub fn status(&self) -> ReplyStatus {
259 self.status
260 }
261
262 pub(crate) fn exception_id(&self) -> Option<&str> {
264 self.exception_id.as_deref()
265 }
266
267 pub(crate) fn finish(self) -> Result<OutMessage, SystemException> {
270 self.msg.finish_shared().map_err(|_| internal())
271 }
272
273 pub(crate) fn finish_with(
278 self,
279 contexts: Vec<ServiceContext>,
280 ) -> Result<OutMessage, SystemException> {
281 if contexts == self.contexts {
282 return self.finish();
283 }
284 let header = ReplyHeader {
285 request_id: self.request_id,
286 reply_status: self.status,
287 service_context: contexts,
288 };
289 let msg = self.msg.finish_shared().map_err(|_| internal())?;
290 Ok(match msg.with_reply_header(&header) {
291 Ok(msg) => msg,
292 Err(msg) => {
293 crate::trace::warning!(
294 "reply to request id {}: the service contexts added after the servant ran \
295 would misalign the GIOP 1.0/1.1 body, and are not sent",
296 header.request_id
297 );
298 msg
299 }
300 })
301 }
302}
303
304fn start(
305 version: Version,
306 order: ByteOrder,
307 request_id: u32,
308 reply_status: ReplyStatus,
309 contexts: &[ServiceContext],
310) -> Result<MessageEncoder, SystemException> {
311 let header = ReplyHeader {
312 request_id,
313 reply_status,
314 service_context: contexts.to_vec(),
315 };
316 MessageEncoder::reply(version, order, &header).map_err(|_| internal())
317}
318
319fn internal() -> SystemException {
320 SystemException::new(SystemExceptionKind::INTERNAL, 0, CompletionStatus::Maybe)
321}
322
323pub(crate) fn location_forward_reply(
325 version: Version,
326 order: ByteOrder,
327 request_id: u32,
328 ior: &Ior,
329 contexts: &[ServiceContext],
330) -> Option<Vec<u8>> {
331 let mut m = start(
332 version,
333 order,
334 request_id,
335 ReplyStatus::LocationForward,
336 contexts,
337 )
338 .ok()?;
339 ior.encode(m.body()).ok()?;
340 m.finish().ok()
341}
342
343pub(crate) fn system_exception_reply(
345 version: Version,
346 order: ByteOrder,
347 request_id: u32,
348 e: &SystemException,
349 contexts: &[ServiceContext],
350) -> Option<Vec<u8>> {
351 let mut m = start(
352 version,
353 order,
354 request_id,
355 ReplyStatus::SystemException,
356 contexts,
357 )
358 .ok()?;
359 SystemExceptionBody::from(e).encode(m.body()).ok()?;
360 m.finish().ok()
361}