use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::fmt;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use rustorb_giop::{Ior, TAG_INTERNET_IOP};
pub use rustorb_giop::{ServiceContext, TaggedComponent, TaggedProfile};
use crate::dii::{ArgMode, NamedValue};
use crate::iop::CodecFactory;
use crate::poa::{Policies, Policy};
use crate::policy::{PolicyType, PolicyValue};
use crate::typecode::TCKind;
use crate::{
Any, CompletionStatus, Exception, ObjectRef, Orb, Poa, Reply, ResolveError, Servant,
SystemException, SystemExceptionKind, TypeCode, Value, trace,
};
pub mod minor {
use crate::OMG_VMCID;
pub const INVALID_POINT: u32 = OMG_VMCID | 14;
pub const DUPLICATE_SERVICE_CONTEXT: u32 = OMG_VMCID | 15;
pub const NO_SERVICE_CONTEXT: u32 = OMG_VMCID | 26;
pub const NO_COMPONENT: u32 = OMG_VMCID | 28;
pub const UNKNOWN_PROFILE: u32 = OMG_VMCID | 29;
pub const NOT_AVAILABLE: u32 = OMG_VMCID | 1;
pub const INVALID_POLICY_TYPE: u32 = OMG_VMCID | 2;
pub const INIT_INFO_GONE: u32 = OMG_VMCID | 10;
}
pub mod reply_status {
pub const SUCCESSFUL: i16 = 0;
pub const SYSTEM_EXCEPTION: i16 = 1;
pub const USER_EXCEPTION: i16 = 2;
pub const LOCATION_FORWARD: i16 = 3;
pub const TRANSPORT_RETRY: i16 = 4;
pub const UNKNOWN: i16 = 5;
}
pub mod sync_scope {
pub const SYNC_NONE: i16 = 0;
pub const SYNC_WITH_TRANSPORT: i16 = 1;
pub const SYNC_WITH_SERVER: i16 = 2;
pub const SYNC_WITH_TARGET: i16 = 3;
}
pub type SlotId = u32;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InvalidSlot;
impl fmt::Display for InvalidSlot {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("PortableInterceptor::InvalidSlot")
}
}
impl std::error::Error for InvalidSlot {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DuplicateName(pub String);
impl fmt::Display for DuplicateName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "an interceptor named {:?} is registered already", self.0)
}
}
impl std::error::Error for DuplicateName {}
#[derive(Debug, Clone)]
pub enum InterceptError {
System(SystemException),
ForwardRequest(ObjectRef),
}
impl From<SystemException> for InterceptError {
fn from(e: SystemException) -> InterceptError {
InterceptError::System(e)
}
}
pub trait OrbInitializer: Send + Sync {
fn pre_init(&self, info: &mut OrbInitInfo<'_>) -> Result<(), SystemException> {
let _ = info;
Ok(())
}
fn post_init(&self, info: &mut OrbInitInfo<'_>) -> Result<(), SystemException> {
let _ = info;
Ok(())
}
}
impl fmt::Debug for dyn OrbInitializer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("OrbInitializer")
}
}
static INITIALIZERS: Mutex<Vec<Arc<dyn OrbInitializer>>> = Mutex::new(Vec::new());
pub fn register_orb_initializer(init: Arc<dyn OrbInitializer>) {
INITIALIZERS.lock().unwrap().push(init);
}
pub(crate) fn registered_initializers() -> Vec<Arc<dyn OrbInitializer>> {
INITIALIZERS.lock().unwrap().clone()
}
#[derive(Default)]
pub(crate) struct Additions {
client: Vec<Arc<dyn ClientRequestInterceptor>>,
server: Vec<Arc<dyn ServerRequestInterceptor>>,
ior: Vec<Arc<dyn IorInterceptor>>,
slots: u32,
}
impl Additions {
fn destroy(&self) {
for i in &self.client {
i.destroy();
}
for i in &self.server {
i.destroy();
}
for i in &self.ior {
i.destroy();
}
}
}
pub struct OrbInitInfo<'a> {
orb: &'a Orb,
post: bool,
additions: &'a mut Additions,
}
impl fmt::Debug for OrbInitInfo<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OrbInitInfo")
.field("post_init", &self.post)
.finish_non_exhaustive()
}
}
fn add_named<T: ?Sized + Interceptor>(
list: &mut Vec<Arc<T>>,
interceptor: Arc<T>,
) -> Result<(), DuplicateName> {
let name = interceptor.name();
if !name.is_empty() && list.iter().any(|i| i.name() == name) {
return Err(DuplicateName(name.to_owned()));
}
list.push(interceptor);
Ok(())
}
impl OrbInitInfo<'_> {
pub fn arguments(&self) -> &[String] {
&self.orb.inner.options.arguments
}
pub fn orb_id(&self) -> &str {
&self.orb.inner.options.orb_id
}
pub fn codec_factory(&self) -> CodecFactory {
CodecFactory::new(self.orb)
}
pub fn orb(&self) -> &Orb {
self.orb
}
pub fn register_initial_reference(
&self,
name: &str,
obj: Option<&ObjectRef>,
) -> Result<(), ResolveError> {
self.orb.register_initial_reference(name, obj)
}
pub fn resolve_initial_references(&self, name: &str) -> Result<ObjectRef, ResolveError> {
if !self.post {
return Err(ResolveError::System(invalid_point()));
}
self.orb.resolve_initial_references(name)
}
pub fn add_client_request_interceptor(
&mut self,
interceptor: Arc<dyn ClientRequestInterceptor>,
) -> Result<(), DuplicateName> {
add_named(&mut self.additions.client, interceptor)
}
pub fn add_server_request_interceptor(
&mut self,
interceptor: Arc<dyn ServerRequestInterceptor>,
) -> Result<(), DuplicateName> {
add_named(&mut self.additions.server, interceptor)
}
pub fn add_ior_interceptor(
&mut self,
interceptor: Arc<dyn IorInterceptor>,
) -> Result<(), DuplicateName> {
add_named(&mut self.additions.ior, interceptor)
}
pub fn allocate_slot_id(&mut self) -> SlotId {
self.additions.slots += 1;
self.additions.slots - 1
}
pub fn register_policy_factory(
&mut self,
policy_type: PolicyType,
) -> Result<(), SystemException> {
let _ = policy_type;
Err(SystemException::new(
SystemExceptionKind::NO_IMPLEMENT,
0,
CompletionStatus::No,
))
}
pub fn pi_current(&self) -> PiCurrent {
PiCurrent {
serial: self.orb.inner.serial,
slots: self.additions.slots,
}
}
}
pub(crate) fn initialize(
orb: &Orb,
initializers: &[Arc<dyn OrbInitializer>],
) -> Result<Option<Arc<Interceptors>>, SystemException> {
if initializers.is_empty() {
return Ok(None);
}
let mut additions = Additions::default();
for post in [false, true] {
for init in initializers {
let mut info = OrbInitInfo {
orb,
post,
additions: &mut additions,
};
let result = if post {
init.post_init(&mut info)
} else {
init.pre_init(&mut info)
};
if let Err(e) = result {
trace::warning!(
"ORBInitializer {} raised {e}: the ORB is not created",
if post { "post_init" } else { "pre_init" }
);
additions.destroy();
return Err(e);
}
}
}
if additions.client.is_empty()
&& additions.server.is_empty()
&& additions.ior.is_empty()
&& additions.slots == 0
{
return Ok(None);
}
Ok(Some(Arc::new(Interceptors {
serial: orb.inner.serial,
client: additions.client,
server: additions.server,
ior: additions.ior,
slots: additions.slots,
next_request_id: AtomicU32::new(0),
})))
}
pub(crate) struct Interceptors {
serial: u64,
pub(crate) client: Vec<Arc<dyn ClientRequestInterceptor>>,
pub(crate) server: Vec<Arc<dyn ServerRequestInterceptor>>,
pub(crate) ior: Vec<Arc<dyn IorInterceptor>>,
pub(crate) slots: u32,
next_request_id: AtomicU32,
}
impl Interceptors {
pub(crate) fn destroy(&self) {
for i in &self.client {
i.destroy();
}
for i in &self.server {
i.destroy();
}
for i in &self.ior {
i.destroy();
}
}
pub(crate) fn has_client(&self) -> bool {
!self.client.is_empty()
}
pub(crate) fn has_server(&self) -> bool {
!self.server.is_empty()
}
}
static NEXT_SERIAL: AtomicU64 = AtomicU64::new(1);
pub(crate) fn new_serial() -> u64 {
NEXT_SERIAL.fetch_add(1, Ordering::Relaxed)
}
pub trait Interceptor: Send + Sync {
fn name(&self) -> &str;
fn destroy(&self) {}
}
pub trait ClientRequestInterceptor: Interceptor {
fn send_request(&self, ri: &ClientRequestInfo<'_>) -> Result<(), InterceptError> {
let _ = ri;
Ok(())
}
fn send_poll(&self, ri: &ClientRequestInfo<'_>) -> Result<(), SystemException> {
let _ = ri;
Ok(())
}
fn receive_reply(&self, ri: &ClientRequestInfo<'_>) -> Result<(), SystemException> {
let _ = ri;
Ok(())
}
fn receive_exception(&self, ri: &ClientRequestInfo<'_>) -> Result<(), InterceptError> {
let _ = ri;
Ok(())
}
fn receive_other(&self, ri: &ClientRequestInfo<'_>) -> Result<(), InterceptError> {
let _ = ri;
Ok(())
}
}
pub trait ServerRequestInterceptor: Interceptor {
fn receive_request_service_contexts(
&self,
ri: &ServerRequestInfo<'_>,
) -> Result<(), InterceptError> {
let _ = ri;
Ok(())
}
fn receive_request(&self, ri: &ServerRequestInfo<'_>) -> Result<(), InterceptError> {
let _ = ri;
Ok(())
}
fn send_reply(&self, ri: &ServerRequestInfo<'_>) -> Result<(), SystemException> {
let _ = ri;
Ok(())
}
fn send_exception(&self, ri: &ServerRequestInfo<'_>) -> Result<(), InterceptError> {
let _ = ri;
Ok(())
}
fn send_other(&self, ri: &ServerRequestInfo<'_>) -> Result<(), InterceptError> {
let _ = ri;
Ok(())
}
}
pub trait IorInterceptor: Interceptor {
fn establish_components(&self, info: &IorInfo<'_>) -> Result<(), SystemException>;
}
thread_local! {
static THREAD_SLOTS: RefCell<HashMap<u64, Vec<Any>>> = RefCell::new(HashMap::new());
}
fn empty_slot() -> Any {
Any::new(TypeCode::null(), Value::Void)
}
#[derive(Debug, Clone)]
pub struct PiCurrent {
serial: u64,
slots: u32,
}
impl PiCurrent {
pub(crate) fn new(orb: &Orb) -> PiCurrent {
PiCurrent {
serial: orb.inner.serial,
slots: orb.interceptors().map_or(0, |pi| pi.slots),
}
}
pub fn get_slot(&self, id: SlotId) -> Result<Any, InvalidSlot> {
if id >= self.slots {
return Err(InvalidSlot);
}
Ok(THREAD_SLOTS.with(|t| {
t.borrow()
.get(&self.serial)
.and_then(|s| s.get(id as usize))
.cloned()
.unwrap_or_else(empty_slot)
}))
}
pub fn set_slot(&self, id: SlotId, data: Any) -> Result<(), InvalidSlot> {
if id >= self.slots {
return Err(InvalidSlot);
}
THREAD_SLOTS.with(|t| {
let mut t = t.borrow_mut();
let slots = t
.entry(self.serial)
.or_insert_with(|| vec![empty_slot(); self.slots as usize]);
if slots.len() < self.slots as usize {
slots.resize_with(self.slots as usize, empty_slot);
}
slots[id as usize] = data;
});
Ok(())
}
}
pub(crate) fn thread_slots(pi: &Interceptors) -> Vec<Any> {
THREAD_SLOTS.with(|t| {
let mut slots = t.borrow().get(&pi.serial).cloned().unwrap_or_default();
slots.resize_with(pi.slots as usize, empty_slot);
slots
})
}
pub(crate) struct ThreadSlotsGuard {
serial: u64,
old: Option<Vec<Any>>,
}
impl ThreadSlotsGuard {
fn enter(serial: u64, slots: Vec<Any>) -> ThreadSlotsGuard {
let old = THREAD_SLOTS.with(|t| t.borrow_mut().insert(serial, slots));
ThreadSlotsGuard { serial, old }
}
}
impl Drop for ThreadSlotsGuard {
fn drop(&mut self) {
let old = self.old.take();
let _ = THREAD_SLOTS.try_with(|t| match old {
Some(old) => {
t.borrow_mut().insert(self.serial, old);
}
None => {
t.borrow_mut().remove(&self.serial);
}
});
}
}
fn invalid_point() -> SystemException {
SystemException::new(
SystemExceptionKind::BAD_INV_ORDER,
minor::INVALID_POINT,
CompletionStatus::No,
)
}
fn not_available() -> SystemException {
SystemException::new(
SystemExceptionKind::NO_RESOURCES,
minor::NOT_AVAILABLE,
CompletionStatus::No,
)
}
fn no_context() -> SystemException {
SystemException::new(
SystemExceptionKind::BAD_PARAM,
minor::NO_SERVICE_CONTEXT,
CompletionStatus::No,
)
}
fn duplicate_context() -> SystemException {
SystemException::new(
SystemExceptionKind::BAD_INV_ORDER,
minor::DUPLICATE_SERVICE_CONTEXT,
CompletionStatus::No,
)
}
#[derive(Debug, Clone)]
pub struct Parameter {
pub argument: Any,
pub mode: ArgMode,
}
fn parameters(list: &[NamedValue]) -> Vec<Parameter> {
list.iter()
.map(|nv| Parameter {
argument: nv.value.clone(),
mode: nv.mode,
})
.collect()
}
fn is_core_context(id: u32) -> bool {
id == rustorb_giop::service_id::CODE_SETS || id == rustorb_giop::service_id::BI_DIR_IIOP
}
fn find_context(list: &[ServiceContext], id: u32) -> Result<ServiceContext, SystemException> {
list.iter()
.find(|c| c.context_id == id)
.cloned()
.ok_or_else(no_context)
}
fn add_context(
list: &mut Vec<ServiceContext>,
sc: ServiceContext,
replace: bool,
) -> Result<(), SystemException> {
match list.iter_mut().find(|c| c.context_id == sc.context_id) {
Some(_) if !replace => Err(duplicate_context()),
Some(old) => {
*old = sc;
Ok(())
}
None => {
list.push(sc);
Ok(())
}
}
}
fn system_exception_tc(id: &str) -> TypeCode {
let completion = TypeCode::enum_tc(
"IDL:omg.org/CORBA/CompletionStatus:1.0",
"CompletionStatus",
&["COMPLETED_YES", "COMPLETED_NO", "COMPLETED_MAYBE"],
)
.expect("a valid enum");
let name = id
.rsplit('/')
.next()
.and_then(|n| n.split(':').next())
.unwrap_or("UNKNOWN");
TypeCode::exception(
id,
name,
&[
("minor", TypeCode::simple(TCKind::ULong).expect("simple")),
("completed", completion),
],
)
.unwrap_or_else(|_| TypeCode::null())
}
pub fn system_exception_any(e: &SystemException) -> Any {
let completed = match e.completed {
CompletionStatus::Yes => 0,
CompletionStatus::No => 1,
CompletionStatus::Maybe => 2,
};
Any::new(
system_exception_tc(&e.repository_id),
Value::List(vec![Value::ULong(e.minor), Value::Enum(completed)]),
)
}
fn unknown_any() -> Any {
system_exception_any(&SystemException::new(
SystemExceptionKind::UNKNOWN,
crate::minor::UNLISTED_USER_EXCEPTION,
CompletionStatus::Yes,
))
}
#[derive(Debug, Clone)]
pub(crate) struct DiiParams {
pub(crate) arguments: Vec<NamedValue>,
pub(crate) result: TypeCode,
pub(crate) exceptions: Vec<TypeCode>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClientPoint {
SendRequest,
SendPoll,
ReceiveReply,
ReceiveException,
ReceiveOther,
}
pub struct ClientRequestInfo<'a> {
point: Cell<ClientPoint>,
request_id: u32,
operation: &'a str,
response_expected: bool,
target: &'a ObjectRef,
effective_target: ObjectRef,
effective_profile: TaggedProfile,
timeout: Option<Duration>,
slots: Vec<Any>,
dii: Option<&'a DiiParams>,
request_contexts: RefCell<Vec<ServiceContext>>,
reply_contexts: RefCell<Vec<ServiceContext>>,
reply_status: Cell<i16>,
exception: RefCell<Option<Exception>>,
forward: RefCell<Option<ObjectRef>>,
reply: RefCell<Option<Reply>>,
}
impl fmt::Debug for ClientRequestInfo<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClientRequestInfo")
.field("point", &self.point.get())
.field("request_id", &self.request_id)
.field("operation", &self.operation)
.finish_non_exhaustive()
}
}
pub(crate) struct ClientRequest<'a> {
pub(crate) operation: &'a str,
pub(crate) response_expected: bool,
pub(crate) target: &'a ObjectRef,
pub(crate) effective: Arc<Ior>,
pub(crate) profile_index: Option<usize>,
pub(crate) timeout: Option<Duration>,
pub(crate) slots: Vec<Any>,
pub(crate) dii: Option<&'a DiiParams>,
}
impl<'a> ClientRequestInfo<'a> {
pub(crate) fn new(pi: &Interceptors, r: ClientRequest<'a>) -> ClientRequestInfo<'a> {
let effective_profile = r
.profile_index
.and_then(|i| r.effective.profiles.get(i))
.cloned()
.unwrap_or(TaggedProfile {
tag: TAG_INTERNET_IOP,
profile_data: Vec::new(),
});
let effective_target = if Arc::ptr_eq(&r.effective, &r.target.ior_arc()) {
r.target.clone()
} else {
ObjectRef::new(r.target.orb().clone(), (*r.effective).clone())
};
ClientRequestInfo {
point: Cell::new(ClientPoint::SendRequest),
request_id: pi.next_request_id.fetch_add(1, Ordering::Relaxed),
operation: r.operation,
response_expected: r.response_expected,
target: r.target,
effective_target,
effective_profile,
timeout: r.timeout,
slots: r.slots,
dii: r.dii,
request_contexts: RefCell::new(Vec::new()),
reply_contexts: RefCell::new(Vec::new()),
reply_status: Cell::new(reply_status::SUCCESSFUL),
exception: RefCell::new(None),
forward: RefCell::new(None),
reply: RefCell::new(None),
}
}
fn check(&self, allowed: &[ClientPoint]) -> Result<(), SystemException> {
if allowed.contains(&self.point.get()) {
Ok(())
} else {
Err(invalid_point())
}
}
fn after_send(&self) -> Result<(), SystemException> {
use ClientPoint::*;
self.check(&[ReceiveReply, ReceiveException, ReceiveOther])
}
pub fn point(&self) -> ClientPoint {
self.point.get()
}
pub fn request_id(&self) -> u32 {
self.request_id
}
pub fn operation(&self) -> &str {
self.operation
}
pub fn arguments(&self) -> Result<Vec<Parameter>, SystemException> {
use ClientPoint::*;
self.check(&[SendRequest, ReceiveReply])?;
let dii = self.dii.ok_or_else(not_available)?;
if self.point.get() == SendRequest {
return Ok(parameters(&dii.arguments));
}
let (_, out) = self.decode_reply(dii)?;
let mut out = out.into_iter();
Ok(dii
.arguments
.iter()
.map(|nv| Parameter {
argument: if nv.mode.is_out() {
out.next().unwrap_or_else(|| nv.value.clone())
} else {
nv.value.clone()
},
mode: nv.mode,
})
.collect())
}
fn decode_reply(&self, dii: &DiiParams) -> Result<(Any, Vec<Any>), SystemException> {
let reply = self.reply.borrow();
let reply = reply.as_ref().ok_or_else(not_available)?;
let mut input = reply.input();
let result = Any::new(dii.result.clone(), input.read_value(&dii.result)?);
let mut out = Vec::new();
for nv in dii.arguments.iter().filter(|nv| nv.mode.is_out()) {
let tc = nv.value.typecode().clone();
let value = input.read_value(&tc)?;
out.push(Any::new(tc, value));
}
Ok((result, out))
}
pub fn exceptions(&self) -> Result<Vec<TypeCode>, SystemException> {
self.check_not_poll()?;
Ok(self.dii.ok_or_else(not_available)?.exceptions.clone())
}
fn check_not_poll(&self) -> Result<(), SystemException> {
if self.point.get() == ClientPoint::SendPoll {
Err(invalid_point())
} else {
Ok(())
}
}
pub fn contexts(&self) -> Result<Vec<String>, SystemException> {
self.check_not_poll()?;
Err(not_available())
}
pub fn operation_context(&self) -> Result<Vec<String>, SystemException> {
self.check_not_poll()?;
Err(not_available())
}
pub fn result(&self) -> Result<Any, SystemException> {
self.check(&[ClientPoint::ReceiveReply])?;
let dii = self.dii.ok_or_else(not_available)?;
Ok(self.decode_reply(dii)?.0)
}
pub fn response_expected(&self) -> bool {
self.response_expected
}
pub fn sync_scope(&self) -> i16 {
if self.response_expected {
sync_scope::SYNC_WITH_TARGET
} else {
sync_scope::SYNC_NONE
}
}
pub fn reply_status(&self) -> Result<i16, SystemException> {
self.after_send()?;
Ok(self.reply_status.get())
}
pub fn forward_reference(&self) -> Result<ObjectRef, SystemException> {
self.check(&[ClientPoint::ReceiveOther])?;
self.forward.borrow().clone().ok_or_else(invalid_point)
}
pub fn get_slot(&self, id: SlotId) -> Result<Any, InvalidSlot> {
self.slots.get(id as usize).cloned().ok_or(InvalidSlot)
}
pub fn get_request_service_context(&self, id: u32) -> Result<ServiceContext, SystemException> {
self.check_not_poll()?;
find_context(&self.request_contexts.borrow(), id)
}
pub fn get_reply_service_context(&self, id: u32) -> Result<ServiceContext, SystemException> {
self.after_send()?;
find_context(&self.reply_contexts.borrow(), id)
}
pub fn target(&self) -> &ObjectRef {
self.target
}
pub fn effective_target(&self) -> &ObjectRef {
&self.effective_target
}
pub fn effective_profile(&self) -> &TaggedProfile {
&self.effective_profile
}
pub fn received_exception(&self) -> Result<Any, SystemException> {
self.check(&[ClientPoint::ReceiveException])?;
match &*self.exception.borrow() {
Some(Exception::System(e)) => Ok(system_exception_any(e)),
Some(Exception::User(e)) => Ok(self
.dii
.and_then(|dii| crate::dii::exception_any(&dii.exceptions, e).ok())
.unwrap_or_else(unknown_any)),
None => Err(invalid_point()),
}
}
pub fn received_exception_id(&self) -> Result<String, SystemException> {
self.check(&[ClientPoint::ReceiveException])?;
match &*self.exception.borrow() {
Some(e) => Ok(e.repository_id().to_owned()),
None => Err(invalid_point()),
}
}
pub fn exception(&self) -> Option<Exception> {
self.exception.borrow().clone()
}
fn components(&self) -> Vec<TaggedComponent> {
self.effective_profile
.iiop()
.map(|p| p.components)
.unwrap_or_default()
}
pub fn get_effective_component(&self, tag: u32) -> Result<TaggedComponent, SystemException> {
self.components()
.into_iter()
.find(|c| c.tag == tag)
.ok_or_else(no_component)
}
pub fn get_effective_components(
&self,
tag: u32,
) -> Result<Vec<TaggedComponent>, SystemException> {
let found: Vec<_> = self
.components()
.into_iter()
.filter(|c| c.tag == tag)
.collect();
if found.is_empty() {
Err(no_component())
} else {
Ok(found)
}
}
pub fn get_request_policy(
&self,
policy_type: PolicyType,
) -> Result<Option<PolicyValue>, SystemException> {
if policy_type != crate::policy::RELATIVE_RT_TIMEOUT_POLICY_TYPE {
return Err(SystemException::new(
SystemExceptionKind::INV_POLICY,
minor::INVALID_POLICY_TYPE,
CompletionStatus::No,
));
}
Ok(self
.timeout
.map(|t| PolicyValue::RelativeRoundtripTimeout(crate::timeout::duration_to_time_t(t))))
}
pub fn add_request_service_context(
&self,
sc: ServiceContext,
replace: bool,
) -> Result<(), SystemException> {
self.check(&[ClientPoint::SendRequest])?;
if is_core_context(sc.context_id) {
return Err(duplicate_context());
}
add_context(&mut self.request_contexts.borrow_mut(), sc, replace)
}
pub(crate) fn take_request_contexts(&self) -> Vec<ServiceContext> {
self.request_contexts.borrow().clone()
}
fn enter(&self, ending: &ClientEnding) {
*self.exception.borrow_mut() = None;
*self.forward.borrow_mut() = None;
let (point, status) = match ending {
ClientEnding::Reply(_) => (ClientPoint::ReceiveReply, reply_status::SUCCESSFUL),
ClientEnding::Oneway => (ClientPoint::ReceiveOther, reply_status::SUCCESSFUL),
ClientEnding::Exception(e) => {
*self.exception.borrow_mut() = Some(e.clone());
let status = match e {
Exception::System(_) => reply_status::SYSTEM_EXCEPTION,
Exception::User(_) => reply_status::USER_EXCEPTION,
};
(ClientPoint::ReceiveException, status)
}
ClientEnding::Forward(to) => {
*self.forward.borrow_mut() = Some(to.clone());
(ClientPoint::ReceiveOther, reply_status::LOCATION_FORWARD)
}
ClientEnding::Retry => (ClientPoint::ReceiveOther, reply_status::TRANSPORT_RETRY),
};
if let ClientEnding::Reply(reply) = ending {
*self.reply.borrow_mut() = Some(reply.clone());
}
self.point.set(point);
self.reply_status.set(status);
}
pub(crate) fn set_reply_contexts(&self, contexts: Vec<ServiceContext>) {
*self.reply_contexts.borrow_mut() = contexts;
}
}
fn no_component() -> SystemException {
SystemException::new(
SystemExceptionKind::BAD_PARAM,
minor::NO_COMPONENT,
CompletionStatus::No,
)
}
#[derive(Debug, Clone)]
pub(crate) enum ClientEnding {
Reply(Reply),
Oneway,
Exception(Exception),
Forward(ObjectRef),
Retry,
}
impl ClientEnding {
fn from_error(e: InterceptError) -> ClientEnding {
match e {
InterceptError::System(e) => ClientEnding::Exception(Exception::System(e)),
InterceptError::ForwardRequest(to) => ClientEnding::Forward(to),
}
}
}
pub(crate) fn send_request(
pi: &Interceptors,
info: &ClientRequestInfo<'_>,
) -> Result<(), ClientEnding> {
info.point.set(ClientPoint::SendRequest);
for (k, i) in pi.client.iter().enumerate() {
if let Err(e) = catch_client(|| i.send_request(info)) {
return Err(end_client(pi, info, ClientEnding::from_error(e), k));
}
}
Ok(())
}
pub(crate) fn receive(
pi: &Interceptors,
info: &ClientRequestInfo<'_>,
ending: ClientEnding,
) -> ClientEnding {
end_client(pi, info, ending, pi.client.len())
}
fn end_client(
pi: &Interceptors,
info: &ClientRequestInfo<'_>,
mut ending: ClientEnding,
started: usize,
) -> ClientEnding {
for i in pi.client[..started].iter().rev() {
info.enter(&ending);
let result = match &ending {
ClientEnding::Reply(_) => {
catch_client(|| i.receive_reply(info).map_err(InterceptError::System))
}
ClientEnding::Exception(_) => catch_client(|| i.receive_exception(info)),
ClientEnding::Oneway | ClientEnding::Forward(_) | ClientEnding::Retry => {
catch_client(|| i.receive_other(info))
}
};
if let Err(e) = result {
ending = ClientEnding::from_error(e);
}
}
ending
}
fn catch_client(f: impl FnOnce() -> Result<(), InterceptError>) -> Result<(), InterceptError> {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).unwrap_or_else(|_| {
trace::warning!("client request interceptor panicked");
Err(InterceptError::System(SystemException::new(
SystemExceptionKind::UNKNOWN,
0,
CompletionStatus::Maybe,
)))
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ServerPoint {
ReceiveRequestServiceContexts,
ReceiveRequest,
SendReply,
SendException,
SendOther,
}
#[derive(Debug, Clone, Default)]
pub(crate) struct DsiParams {
pub(crate) arguments: Option<Vec<NamedValue>>,
pub(crate) result: Option<Any>,
pub(crate) exception: Option<Any>,
}
thread_local! {
static DSI: RefCell<Option<DsiParams>> = const { RefCell::new(None) };
}
pub(crate) fn wants_dsi_params() -> bool {
DSI.with(|d| d.borrow().is_some())
}
pub(crate) fn leave_dsi_params(params: DsiParams) {
DSI.with(|d| {
let mut d = d.borrow_mut();
if d.is_some() {
*d = Some(params);
}
});
}
pub(crate) struct DsiGuard {
old: Option<DsiParams>,
}
impl DsiGuard {
pub(crate) fn enter() -> DsiGuard {
DsiGuard {
old: DSI.with(|d| d.borrow_mut().replace(DsiParams::default())),
}
}
pub(crate) fn take(self) -> DsiParams {
DSI.with(|d| d.borrow_mut().take()).unwrap_or_default()
}
}
impl Drop for DsiGuard {
fn drop(&mut self) {
let old = self.old.take();
let _ = DSI.try_with(|d| *d.borrow_mut() = old);
}
}
pub(crate) struct ServerTarget {
pub(crate) poa: Poa,
pub(crate) id: Vec<u8>,
}
pub struct ServerRequestInfo<'a> {
point: Cell<ServerPoint>,
request_id: u32,
operation: &'a str,
response_expected: bool,
orb: &'a Orb,
target: Option<&'a ServerTarget>,
servant: RefCell<Option<Arc<dyn Servant>>>,
request_contexts: &'a [ServiceContext],
reply_contexts: RefCell<Vec<ServiceContext>>,
slots: RefCell<Vec<Any>>,
reply_status: Cell<i16>,
exception: RefCell<Option<ServerException>>,
forward: RefCell<Option<ObjectRef>>,
dsi: RefCell<Option<DsiParams>>,
}
impl fmt::Debug for ServerRequestInfo<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ServerRequestInfo")
.field("point", &self.point.get())
.field("request_id", &self.request_id)
.field("operation", &self.operation)
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone)]
pub(crate) enum ServerException {
System(SystemException),
User(String),
}
#[derive(Debug, Clone)]
pub(crate) enum ServerEnding {
Reply,
Exception(ServerException),
Forward(ObjectRef),
}
impl ServerEnding {
fn from_error(e: InterceptError) -> ServerEnding {
match e {
InterceptError::System(e) => ServerEnding::Exception(ServerException::System(e)),
InterceptError::ForwardRequest(to) => ServerEnding::Forward(to),
}
}
}
pub(crate) struct ServerRequest<'a> {
pub(crate) request_id: u32,
pub(crate) operation: &'a str,
pub(crate) response_expected: bool,
pub(crate) orb: &'a Orb,
pub(crate) target: Option<&'a ServerTarget>,
pub(crate) contexts: &'a [ServiceContext],
}
impl<'a> ServerRequestInfo<'a> {
pub(crate) fn new(pi: &Interceptors, r: ServerRequest<'a>) -> ServerRequestInfo<'a> {
ServerRequestInfo {
point: Cell::new(ServerPoint::ReceiveRequestServiceContexts),
request_id: r.request_id,
operation: r.operation,
response_expected: r.response_expected,
orb: r.orb,
target: r.target,
servant: RefCell::new(None),
request_contexts: r.contexts,
reply_contexts: RefCell::new(Vec::new()),
slots: RefCell::new(vec![empty_slot(); pi.slots as usize]),
reply_status: Cell::new(reply_status::SUCCESSFUL),
exception: RefCell::new(None),
forward: RefCell::new(None),
dsi: RefCell::new(None),
}
}
fn check(&self, allowed: &[ServerPoint]) -> Result<(), SystemException> {
if allowed.contains(&self.point.get()) {
Ok(())
} else {
Err(invalid_point())
}
}
fn sending(&self) -> Result<(), SystemException> {
use ServerPoint::*;
self.check(&[SendReply, SendException, SendOther])
}
fn not_first(&self) -> Result<(), SystemException> {
if self.point.get() == ServerPoint::ReceiveRequestServiceContexts {
Err(invalid_point())
} else {
Ok(())
}
}
pub fn point(&self) -> ServerPoint {
self.point.get()
}
pub fn request_id(&self) -> u32 {
self.request_id
}
pub fn operation(&self) -> &str {
self.operation
}
pub fn arguments(&self) -> Result<Vec<Parameter>, SystemException> {
use ServerPoint::*;
self.check(&[ReceiveRequest, SendReply])?;
self.dsi
.borrow()
.as_ref()
.and_then(|d| d.arguments.as_deref().map(parameters))
.ok_or_else(not_available)
}
pub fn exceptions(&self) -> Result<Vec<TypeCode>, SystemException> {
self.not_first()?;
Err(not_available())
}
pub fn contexts(&self) -> Result<Vec<String>, SystemException> {
self.not_first()?;
Err(not_available())
}
pub fn operation_context(&self) -> Result<Vec<String>, SystemException> {
self.not_first()?;
Err(not_available())
}
pub fn result(&self) -> Result<Any, SystemException> {
self.check(&[ServerPoint::SendReply])?;
self.dsi
.borrow()
.as_ref()
.filter(|d| d.arguments.is_some())
.map(|d| {
d.result
.clone()
.unwrap_or_else(|| Any::new(TypeCode::void(), Value::Void))
})
.ok_or_else(not_available)
}
pub fn response_expected(&self) -> bool {
self.response_expected
}
pub fn sync_scope(&self) -> i16 {
if self.response_expected {
sync_scope::SYNC_WITH_TARGET
} else {
sync_scope::SYNC_NONE
}
}
pub fn reply_status(&self) -> Result<i16, SystemException> {
self.sending()?;
Ok(self.reply_status.get())
}
pub fn forward_reference(&self) -> Result<ObjectRef, SystemException> {
self.check(&[ServerPoint::SendOther])?;
self.forward.borrow().clone().ok_or_else(invalid_point)
}
pub fn get_slot(&self, id: SlotId) -> Result<Any, InvalidSlot> {
self.slots
.borrow()
.get(id as usize)
.cloned()
.ok_or(InvalidSlot)
}
pub fn set_slot(&self, id: SlotId, data: Any) -> Result<(), InvalidSlot> {
match self.slots.borrow_mut().get_mut(id as usize) {
Some(slot) => {
*slot = data;
Ok(())
}
None => Err(InvalidSlot),
}
}
pub fn get_request_service_context(&self, id: u32) -> Result<ServiceContext, SystemException> {
find_context(self.request_contexts, id)
}
pub fn request_service_contexts(&self) -> &[ServiceContext] {
self.request_contexts
}
pub fn get_reply_service_context(&self, id: u32) -> Result<ServiceContext, SystemException> {
self.sending()?;
find_context(&self.reply_contexts.borrow(), id)
}
pub fn add_reply_service_context(
&self,
sc: ServiceContext,
replace: bool,
) -> Result<(), SystemException> {
if is_core_context(sc.context_id) {
return Err(duplicate_context());
}
add_context(&mut self.reply_contexts.borrow_mut(), sc, replace)
}
pub fn sending_exception(&self) -> Result<Any, SystemException> {
self.check(&[ServerPoint::SendException])?;
match &*self.exception.borrow() {
Some(ServerException::System(e)) => Ok(system_exception_any(e)),
Some(ServerException::User(_)) => Ok(self
.dsi
.borrow()
.as_ref()
.and_then(|d| d.exception.clone())
.unwrap_or_else(unknown_any)),
None => Err(invalid_point()),
}
}
pub fn sending_exception_id(&self) -> Result<String, SystemException> {
self.check(&[ServerPoint::SendException])?;
match &*self.exception.borrow() {
Some(ServerException::System(e)) => Ok(e.repository_id.clone()),
Some(ServerException::User(id)) => Ok(id.clone()),
None => Err(invalid_point()),
}
}
fn target(&self) -> Result<&ServerTarget, SystemException> {
self.not_first()?;
self.target.ok_or_else(not_available)
}
pub fn object_id(&self) -> Result<Vec<u8>, SystemException> {
Ok(self.target()?.id.clone())
}
pub fn adapter_id(&self) -> Result<Vec<u8>, SystemException> {
Ok(self.target()?.poa.inner.key(&[]))
}
pub fn adapter_name(&self) -> Result<Vec<String>, SystemException> {
let t = self.target()?;
let mut names = vec!["RootPOA".to_owned()];
names.extend(t.poa.inner.path().iter().cloned());
Ok(names)
}
pub fn orb_id(&self) -> &str {
&self.orb.inner.options.orb_id
}
pub fn server_id(&self) -> &str {
&self.orb.inner.options.server_id
}
fn servant(&self) -> Result<Arc<dyn Servant>, SystemException> {
self.not_first()?;
self.servant.borrow().clone().ok_or_else(not_available)
}
pub fn target_most_derived_interface(&self) -> Result<String, SystemException> {
Ok(self.servant()?.repository_id().to_owned())
}
pub fn target_is_a(&self, id: &str) -> Result<bool, SystemException> {
let servant = self.servant()?;
Ok(id == "IDL:omg.org/CORBA/Object:1.0" || servant.is_a(id))
}
pub fn get_server_policy(
&self,
policy_type: PolicyType,
) -> Result<PolicyValue, SystemException> {
let t = self.target.ok_or_else(not_available)?;
policy_of(t.poa.inner.policies(), policy_type)
}
pub(crate) fn set_servant(&self, servant: &Arc<dyn Servant>) {
*self.servant.borrow_mut() = Some(Arc::clone(servant));
}
pub(crate) fn set_dsi(&self, dsi: DsiParams) {
*self.dsi.borrow_mut() = Some(dsi);
}
pub(crate) fn reply_contexts(&self) -> Vec<ServiceContext> {
self.reply_contexts.borrow().clone()
}
pub(crate) fn enter_thread_scope(&self, pi: &Interceptors) -> ThreadSlotsGuard {
ThreadSlotsGuard::enter(pi.serial, self.slots.borrow().clone())
}
fn enter(&self, ending: &ServerEnding) {
*self.exception.borrow_mut() = None;
*self.forward.borrow_mut() = None;
let (point, status) = match ending {
ServerEnding::Reply => (ServerPoint::SendReply, reply_status::SUCCESSFUL),
ServerEnding::Exception(e) => {
*self.exception.borrow_mut() = Some(e.clone());
let status = match e {
ServerException::System(_) => reply_status::SYSTEM_EXCEPTION,
ServerException::User(_) => reply_status::USER_EXCEPTION,
};
(ServerPoint::SendException, status)
}
ServerEnding::Forward(to) => {
*self.forward.borrow_mut() = Some(to.clone());
(ServerPoint::SendOther, reply_status::LOCATION_FORWARD)
}
};
self.point.set(point);
self.reply_status.set(status);
}
}
fn policy_of(p: &Policies, policy_type: PolicyType) -> Result<PolicyValue, SystemException> {
use crate::policy::*;
let policy = match policy_type {
THREAD_POLICY_ID => Policy::Thread(p.thread),
BIDIRECTIONAL_POLICY_TYPE => Policy::Bidirectional(p.bidirectional),
LIFESPAN_POLICY_ID => Policy::Lifespan(p.lifespan),
ID_UNIQUENESS_POLICY_ID => Policy::IdUniqueness(p.id_uniqueness),
ID_ASSIGNMENT_POLICY_ID => Policy::IdAssignment(p.id_assignment),
IMPLICIT_ACTIVATION_POLICY_ID => Policy::ImplicitActivation(p.implicit_activation),
SERVANT_RETENTION_POLICY_ID => Policy::ServantRetention(p.servant_retention),
REQUEST_PROCESSING_POLICY_ID => Policy::RequestProcessing(p.request_processing),
_ => {
return Err(SystemException::new(
SystemExceptionKind::INV_POLICY,
minor::INVALID_POLICY_TYPE,
CompletionStatus::No,
));
}
};
Ok(PolicyValue::Poa(policy))
}
pub(crate) fn receive_request_service_contexts(
pi: &Interceptors,
info: &ServerRequestInfo<'_>,
) -> Result<(), ServerEnding> {
info.point.set(ServerPoint::ReceiveRequestServiceContexts);
for (k, i) in pi.server.iter().enumerate() {
if let Err(e) = catch_server(|| i.receive_request_service_contexts(info)) {
return Err(end_server(pi, info, ServerEnding::from_error(e), k));
}
}
Ok(())
}
pub(crate) fn receive_request(
pi: &Interceptors,
info: &ServerRequestInfo<'_>,
) -> Result<(), ServerEnding> {
info.point.set(ServerPoint::ReceiveRequest);
for i in &pi.server {
if let Err(e) = catch_server(|| i.receive_request(info)) {
return Err(send_reply(pi, info, ServerEnding::from_error(e)));
}
}
Ok(())
}
pub(crate) fn send_reply(
pi: &Interceptors,
info: &ServerRequestInfo<'_>,
ending: ServerEnding,
) -> ServerEnding {
end_server(pi, info, ending, pi.server.len())
}
fn end_server(
pi: &Interceptors,
info: &ServerRequestInfo<'_>,
mut ending: ServerEnding,
started: usize,
) -> ServerEnding {
for i in pi.server[..started].iter().rev() {
info.enter(&ending);
let result = match &ending {
ServerEnding::Reply => {
catch_server(|| i.send_reply(info).map_err(InterceptError::System))
}
ServerEnding::Exception(_) => catch_server(|| i.send_exception(info)),
ServerEnding::Forward(_) => catch_server(|| i.send_other(info)),
};
if let Err(e) = result {
ending = ServerEnding::from_error(e);
}
}
ending
}
fn catch_server(f: impl FnOnce() -> Result<(), InterceptError>) -> Result<(), InterceptError> {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).unwrap_or_else(|_| {
trace::warning!("server request interceptor panicked");
Err(InterceptError::System(SystemException::new(
SystemExceptionKind::UNKNOWN,
0,
CompletionStatus::Maybe,
)))
})
}
pub struct IorInfo<'a> {
policies: &'a Policies,
adapter_name: Vec<String>,
components: RefCell<Vec<TaggedComponent>>,
}
impl fmt::Debug for IorInfo<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("IorInfo")
.field("adapter_name", &self.adapter_name)
.finish_non_exhaustive()
}
}
impl IorInfo<'_> {
pub fn get_effective_policy(
&self,
policy_type: PolicyType,
) -> Result<PolicyValue, SystemException> {
policy_of(self.policies, policy_type)
}
pub fn adapter_name(&self) -> &[String] {
&self.adapter_name
}
pub fn add_ior_component(&self, component: TaggedComponent) {
self.components.borrow_mut().push(component);
}
pub fn add_ior_component_to_profile(
&self,
component: TaggedComponent,
profile_id: u32,
) -> Result<(), SystemException> {
if profile_id != TAG_INTERNET_IOP {
return Err(SystemException::new(
SystemExceptionKind::BAD_PARAM,
minor::UNKNOWN_PROFILE,
CompletionStatus::No,
));
}
self.add_ior_component(component);
Ok(())
}
}
pub(crate) fn establish_components(
orb: &Orb,
policies: &Policies,
path: &[String],
) -> Vec<TaggedComponent> {
let Some(pi) = orb.interceptors() else {
return Vec::new();
};
if pi.ior.is_empty() {
return Vec::new();
}
let mut adapter_name = vec!["RootPOA".to_owned()];
adapter_name.extend(path.iter().cloned());
let info = IorInfo {
policies,
adapter_name,
components: RefCell::new(Vec::new()),
};
for i in &pi.ior {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
i.establish_components(&info)
}));
match result {
Ok(Ok(())) => {}
Ok(Err(e)) => trace::warning!(
"IORInterceptor {:?} raised {e} in establish_components (ignored)",
i.name()
),
Err(_) => trace::warning!(
"IORInterceptor {:?} panicked in establish_components (ignored)",
i.name()
),
}
}
info.components.into_inner()
}
#[allow(dead_code)]
fn assert_send() {
fn is_send<T: Send>() {}
is_send::<ClientRequestInfo<'static>>();
}