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 values: None,
235 }
236 }
237
238 pub fn user_exception(
241 &mut self,
242 repository_id: &str,
243 ) -> Result<OutStream<'_>, SystemException> {
244 self.msg = start(
245 self.version,
246 self.order,
247 self.request_id,
248 ReplyStatus::UserException,
249 &self.contexts,
250 )?;
251 self.status = ReplyStatus::UserException;
252 self.exception_id = Some(repository_id.to_owned());
253 let mut out = self.output();
254 out.write_string(repository_id)?;
255 Ok(out)
256 }
257
258 pub fn status(&self) -> ReplyStatus {
260 self.status
261 }
262
263 pub(crate) fn exception_id(&self) -> Option<&str> {
265 self.exception_id.as_deref()
266 }
267
268 pub(crate) fn finish(self) -> Result<OutMessage, SystemException> {
271 self.msg.finish_shared().map_err(|_| internal())
272 }
273
274 pub(crate) fn finish_with(
279 self,
280 contexts: Vec<ServiceContext>,
281 ) -> Result<OutMessage, SystemException> {
282 if contexts == self.contexts {
283 return self.finish();
284 }
285 let header = ReplyHeader {
286 request_id: self.request_id,
287 reply_status: self.status,
288 service_context: contexts,
289 };
290 let msg = self.msg.finish_shared().map_err(|_| internal())?;
291 Ok(match msg.with_reply_header(&header) {
292 Ok(msg) => msg,
293 Err(msg) => {
294 crate::trace::warning!(
295 "reply to request id {}: the service contexts added after the servant ran \
296 would misalign the GIOP 1.0/1.1 body, and are not sent",
297 header.request_id
298 );
299 msg
300 }
301 })
302 }
303}
304
305fn start(
306 version: Version,
307 order: ByteOrder,
308 request_id: u32,
309 reply_status: ReplyStatus,
310 contexts: &[ServiceContext],
311) -> Result<MessageEncoder, SystemException> {
312 let header = ReplyHeader {
313 request_id,
314 reply_status,
315 service_context: contexts.to_vec(),
316 };
317 MessageEncoder::reply(version, order, &header).map_err(|_| internal())
318}
319
320fn internal() -> SystemException {
321 SystemException::new(SystemExceptionKind::INTERNAL, 0, CompletionStatus::Maybe)
322}
323
324pub(crate) fn location_forward_reply(
326 version: Version,
327 order: ByteOrder,
328 request_id: u32,
329 ior: &Ior,
330 contexts: &[ServiceContext],
331) -> Option<Vec<u8>> {
332 let mut m = start(
333 version,
334 order,
335 request_id,
336 ReplyStatus::LocationForward,
337 contexts,
338 )
339 .ok()?;
340 ior.encode(m.body()).ok()?;
341 m.finish().ok()
342}
343
344pub(crate) fn system_exception_reply(
346 version: Version,
347 order: ByteOrder,
348 request_id: u32,
349 e: &SystemException,
350 contexts: &[ServiceContext],
351) -> Option<Vec<u8>> {
352 let mut m = start(
353 version,
354 order,
355 request_id,
356 ReplyStatus::SystemException,
357 contexts,
358 )
359 .ok()?;
360 SystemExceptionBody::from(e).encode(m.body()).ok()?;
361 m.finish().ok()
362}