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, SystemExceptionBody, Version,
11};
12
13use crate::stream::{InStream, OutStream};
14use crate::{CodeSets, CompletionStatus, ObjectRef, Poa, SystemException, SystemExceptionKind};
15
16pub(crate) const OBJECT_ID: &str = "IDL:omg.org/CORBA/Object:1.0";
18
19pub trait Servant: Send + Sync {
25 fn repository_id(&self) -> &str;
28
29 fn is_a(&self, repository_id: &str) -> bool {
33 repository_id == self.repository_id()
34 }
35
36 fn dispatch(
45 &self,
46 operation: &str,
47 input: &mut InStream<'_>,
48 reply: &mut ServerReply,
49 ) -> Result<(), SystemException>;
50}
51
52#[derive(Debug, Clone)]
54pub enum ServantManagerError {
55 ForwardRequest(ObjectRef),
58 System(SystemException),
60}
61
62impl From<SystemException> for ServantManagerError {
63 fn from(e: SystemException) -> Self {
64 ServantManagerError::System(e)
65 }
66}
67
68impl fmt::Display for ServantManagerError {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 match self {
71 ServantManagerError::ForwardRequest(_) => f.write_str("PortableServer::ForwardRequest"),
72 ServantManagerError::System(e) => e.fmt(f),
73 }
74 }
75}
76
77impl std::error::Error for ServantManagerError {}
78
79pub trait ServantActivator: Send + Sync {
87 fn incarnate(&self, id: &[u8], adapter: &Poa) -> Result<Arc<dyn Servant>, ServantManagerError>;
91
92 fn etherealize(
97 &self,
98 id: &[u8],
99 adapter: &Poa,
100 servant: Arc<dyn Servant>,
101 cleanup_in_progress: bool,
102 remaining_activations: bool,
103 ) {
104 let _ = (
105 id,
106 adapter,
107 servant,
108 cleanup_in_progress,
109 remaining_activations,
110 );
111 }
112}
113
114pub type Cookie = Box<dyn Any + Send>;
116
117pub trait ServantLocator: Send + Sync {
123 fn preinvoke(
126 &self,
127 id: &[u8],
128 adapter: &Poa,
129 operation: &str,
130 ) -> Result<(Arc<dyn Servant>, Cookie), ServantManagerError>;
131
132 fn postinvoke(
135 &self,
136 id: &[u8],
137 adapter: &Poa,
138 operation: &str,
139 cookie: Cookie,
140 servant: Arc<dyn Servant>,
141 ) {
142 let _ = (id, adapter, operation, cookie, servant);
143 }
144}
145
146#[derive(Clone)]
148pub enum ServantManager {
149 Activator(Arc<dyn ServantActivator>),
151 Locator(Arc<dyn ServantLocator>),
153}
154
155impl fmt::Debug for ServantManager {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 match self {
158 ServantManager::Activator(_) => f.write_str("ServantManager::Activator"),
159 ServantManager::Locator(_) => f.write_str("ServantManager::Locator"),
160 }
161 }
162}
163
164pub trait AdapterActivator: Send + Sync {
167 fn unknown_adapter(&self, parent: &Poa, name: &str) -> bool;
172}
173
174#[derive(Debug)]
177pub struct ServerReply {
178 version: Version,
179 order: ByteOrder,
180 request_id: u32,
181 code_sets: CodeSets,
182 msg: MessageEncoder,
183 status: ReplyStatus,
184}
185
186impl ServerReply {
187 pub(crate) fn new(
188 version: Version,
189 order: ByteOrder,
190 request_id: u32,
191 code_sets: CodeSets,
192 ) -> Result<ServerReply, SystemException> {
193 Ok(ServerReply {
194 version,
195 order,
196 request_id,
197 code_sets,
198 msg: start(version, order, request_id, ReplyStatus::NoException)?,
199 status: ReplyStatus::NoException,
200 })
201 }
202
203 pub fn output(&mut self) -> OutStream<'_> {
206 OutStream {
207 enc: self.msg.body(),
208 code_sets: self.code_sets,
209 orb: None,
210 }
211 }
212
213 pub fn user_exception(
216 &mut self,
217 repository_id: &str,
218 ) -> Result<OutStream<'_>, SystemException> {
219 self.msg = start(
220 self.version,
221 self.order,
222 self.request_id,
223 ReplyStatus::UserException,
224 )?;
225 self.status = ReplyStatus::UserException;
226 let mut out = self.output();
227 out.write_string(repository_id)?;
228 Ok(out)
229 }
230
231 pub fn status(&self) -> ReplyStatus {
233 self.status
234 }
235
236 pub(crate) fn finish(self) -> Result<OutMessage, SystemException> {
239 self.msg.finish_shared().map_err(|_| internal())
240 }
241}
242
243fn start(
244 version: Version,
245 order: ByteOrder,
246 request_id: u32,
247 reply_status: ReplyStatus,
248) -> Result<MessageEncoder, SystemException> {
249 let header = ReplyHeader {
250 request_id,
251 reply_status,
252 service_context: Vec::new(),
253 };
254 MessageEncoder::reply(version, order, &header).map_err(|_| internal())
255}
256
257fn internal() -> SystemException {
258 SystemException::new(SystemExceptionKind::INTERNAL, 0, CompletionStatus::Maybe)
259}
260
261pub(crate) fn location_forward_reply(
263 version: Version,
264 order: ByteOrder,
265 request_id: u32,
266 ior: &Ior,
267) -> Option<Vec<u8>> {
268 let mut m = start(version, order, request_id, ReplyStatus::LocationForward).ok()?;
269 ior.encode(m.body()).ok()?;
270 m.finish().ok()
271}
272
273pub(crate) fn system_exception_reply(
275 version: Version,
276 order: ByteOrder,
277 request_id: u32,
278 e: &SystemException,
279) -> Option<Vec<u8>> {
280 let mut m = start(version, order, request_id, ReplyStatus::SystemException).ok()?;
281 SystemExceptionBody::from(e).encode(m.body()).ok()?;
282 m.finish().ok()
283}