use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt;
use std::panic::{self, AssertUnwindSafe};
use std::sync::{Arc, Condvar, Mutex, MutexGuard, Weak};
use std::thread::{self, ThreadId};
use rustorb_giop::{IiopProfile, TaggedComponent};
use crate::pool::Job;
use crate::servant::{Cookie, ServantLocator};
use crate::server::Server;
use crate::trace;
use crate::{
AdapterActivator, CompletionStatus, ObjectRef, Orb, PolicyError, PolicyValue, Servant,
ServantActivator, ServantManager, ServantManagerError, SystemException, SystemExceptionKind,
minor,
};
pub type ObjectId = Vec<u8>;
const PATH_START: u8 = 0xff;
const PATH_SEPARATOR: u8 = 0xff;
const PERSISTENT_END: u8 = 0x00;
const TRANSIENT_END: u8 = 0xfe;
pub(crate) const BOOT_ID_LEN: usize = 8;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ThreadPolicy {
OrbCtrl,
SingleThread,
MainThread,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Lifespan {
Transient,
Persistent,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IdUniqueness {
Unique,
Multiple,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IdAssignment {
User,
System,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ImplicitActivation {
Implicit,
NoImplicit,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ServantRetention {
Retain,
NonRetain,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RequestProcessing {
ActiveObjectMapOnly,
UseDefaultServant,
UseServantManager,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Bidirectional {
Normal,
Both,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Policy {
Thread(ThreadPolicy),
Lifespan(Lifespan),
IdUniqueness(IdUniqueness),
IdAssignment(IdAssignment),
ImplicitActivation(ImplicitActivation),
ServantRetention(ServantRetention),
RequestProcessing(RequestProcessing),
Bidirectional(Bidirectional),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Policies {
pub thread: ThreadPolicy,
pub lifespan: Lifespan,
pub id_uniqueness: IdUniqueness,
pub id_assignment: IdAssignment,
pub implicit_activation: ImplicitActivation,
pub servant_retention: ServantRetention,
pub request_processing: RequestProcessing,
pub bidirectional: Bidirectional,
}
impl Default for Policies {
fn default() -> Policies {
Policies {
thread: ThreadPolicy::OrbCtrl,
lifespan: Lifespan::Transient,
id_uniqueness: IdUniqueness::Unique,
id_assignment: IdAssignment::System,
implicit_activation: ImplicitActivation::NoImplicit,
servant_retention: ServantRetention::Retain,
request_processing: RequestProcessing::ActiveObjectMapOnly,
bidirectional: Bidirectional::Normal,
}
}
}
impl Policies {
pub(crate) fn root() -> Policies {
Policies {
implicit_activation: ImplicitActivation::Implicit,
..Policies::default()
}
}
fn from_list(list: &[Policy]) -> Result<Policies, u16> {
let mut p = Policies::default();
let mut seen = Vec::new();
let index = |i: usize| u16::try_from(i).unwrap_or(u16::MAX);
for (i, policy) in list.iter().enumerate() {
let kind = std::mem::discriminant(policy);
if seen.contains(&kind) {
return Err(index(i));
}
seen.push(kind);
match *policy {
Policy::Thread(v) => p.thread = v,
Policy::Lifespan(v) => p.lifespan = v,
Policy::IdUniqueness(v) => p.id_uniqueness = v,
Policy::IdAssignment(v) => p.id_assignment = v,
Policy::ImplicitActivation(v) => p.implicit_activation = v,
Policy::ServantRetention(v) => p.servant_retention = v,
Policy::RequestProcessing(v) => p.request_processing = v,
Policy::Bidirectional(v) => p.bidirectional = v,
}
}
let position = |f: fn(&Policy) -> bool| index(list.iter().rposition(f).unwrap_or(0));
if p.implicit_activation == ImplicitActivation::Implicit
&& (p.id_assignment != IdAssignment::System
|| p.servant_retention != ServantRetention::Retain)
{
return Err(position(|q| matches!(q, Policy::ImplicitActivation(_))));
}
if p.servant_retention == ServantRetention::NonRetain
&& p.request_processing == RequestProcessing::ActiveObjectMapOnly
{
return Err(position(|q| {
matches!(
q,
Policy::ServantRetention(_) | Policy::RequestProcessing(_)
)
}));
}
if p.request_processing == RequestProcessing::UseDefaultServant
&& p.id_uniqueness != IdUniqueness::Multiple
{
return Err(position(|q| {
matches!(q, Policy::RequestProcessing(_) | Policy::IdUniqueness(_))
}));
}
Ok(p)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PoaError {
AdapterAlreadyExists,
AdapterNonExistent,
InvalidPolicy(u16),
ServantAlreadyActive,
ObjectAlreadyActive,
ServantNotActive,
ObjectNotActive,
WrongPolicy,
WrongAdapter,
AdapterInactive,
ManagerAlreadyExists,
Policy(PolicyError),
NoServant,
NoContext,
System(SystemException),
}
impl fmt::Display for PoaError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PoaError::AdapterAlreadyExists => {
f.write_str("PortableServer::POA::AdapterAlreadyExists")
}
PoaError::AdapterNonExistent => f.write_str("PortableServer::POA::AdapterNonExistent"),
PoaError::InvalidPolicy(i) => {
write!(f, "PortableServer::POA::InvalidPolicy (index {i})")
}
PoaError::ServantAlreadyActive => {
f.write_str("PortableServer::POA::ServantAlreadyActive")
}
PoaError::ObjectAlreadyActive => {
f.write_str("PortableServer::POA::ObjectAlreadyActive")
}
PoaError::ServantNotActive => f.write_str("PortableServer::POA::ServantNotActive"),
PoaError::ObjectNotActive => f.write_str("PortableServer::POA::ObjectNotActive"),
PoaError::WrongPolicy => f.write_str("PortableServer::POA::WrongPolicy"),
PoaError::WrongAdapter => f.write_str("PortableServer::POA::WrongAdapter"),
PoaError::AdapterInactive => f.write_str("PortableServer::POAManager::AdapterInactive"),
PoaError::ManagerAlreadyExists => {
f.write_str("PortableServer::POAManagerFactory::ManagerAlreadyExists")
}
PoaError::Policy(e) => e.fmt(f),
PoaError::NoServant => f.write_str("PortableServer::POA::NoServant"),
PoaError::NoContext => f.write_str("PortableServer::Current::NoContext"),
PoaError::System(e) => e.fmt(f),
}
}
}
impl std::error::Error for PoaError {}
impl From<SystemException> for PoaError {
fn from(e: SystemException) -> Self {
PoaError::System(e)
}
}
fn sys(kind: SystemExceptionKind, minor: u32) -> SystemException {
SystemException::new(kind, minor, CompletionStatus::No)
}
pub(crate) fn no_such_object() -> SystemException {
sys(SystemExceptionKind::OBJECT_NOT_EXIST, minor::NO_SUCH_OBJECT)
}
thread_local! {
static UPCALL_DEPTH: Cell<usize> = const { Cell::new(0) };
static MAIN_DEPTH: Cell<usize> = const { Cell::new(0) };
static CURRENT: RefCell<Vec<CurrentEntry>> = const { RefCell::new(Vec::new()) };
}
#[derive(Clone)]
struct CurrentEntry {
poa: Poa,
id: ObjectId,
servant: Arc<dyn Servant>,
}
pub(crate) struct CurrentGuard(());
impl CurrentGuard {
pub(crate) fn enter(poa: &Poa, id: &[u8], servant: &Arc<dyn Servant>) -> CurrentGuard {
CURRENT.with(|c| {
c.borrow_mut().push(CurrentEntry {
poa: poa.clone(),
id: id.to_vec(),
servant: Arc::clone(servant),
});
});
CurrentGuard(())
}
}
impl Drop for CurrentGuard {
fn drop(&mut self) {
CURRENT.with(|c| c.borrow_mut().pop());
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct PoaCurrent;
impl PoaCurrent {
fn with<T>(f: impl FnOnce(&CurrentEntry) -> T) -> Result<T, PoaError> {
CURRENT.with(|c| c.borrow().last().map(f).ok_or(PoaError::NoContext))
}
pub fn get_poa() -> Result<Poa, PoaError> {
PoaCurrent::with(|e| e.poa.clone())
}
pub fn get_object_id() -> Result<ObjectId, PoaError> {
PoaCurrent::with(|e| e.id.clone())
}
pub fn get_reference() -> Result<ObjectRef, PoaError> {
let (poa, id, servant) =
PoaCurrent::with(|e| (e.poa.clone(), e.id.clone(), Arc::clone(&e.servant)))?;
poa.reference(&id, servant.repository_id())
}
pub fn get_servant() -> Result<Arc<dyn Servant>, PoaError> {
PoaCurrent::with(|e| Arc::clone(&e.servant))
}
}
pub(crate) struct MainThreadGuard(());
impl MainThreadGuard {
pub(crate) fn enter() -> MainThreadGuard {
MAIN_DEPTH.with(|d| d.set(d.get() + 1));
MainThreadGuard(())
}
}
impl Drop for MainThreadGuard {
fn drop(&mut self) {
MAIN_DEPTH.with(|d| d.set(d.get() - 1));
}
}
pub(crate) fn is_main_thread() -> bool {
MAIN_DEPTH.with(Cell::get) > 0
}
pub(crate) fn in_upcall() -> bool {
UPCALL_DEPTH.with(Cell::get) > 0
}
pub(crate) struct UpcallGuard(());
impl UpcallGuard {
pub(crate) fn enter() -> UpcallGuard {
UPCALL_DEPTH.with(|d| d.set(d.get() + 1));
UpcallGuard(())
}
}
impl Drop for UpcallGuard {
fn drop(&mut self) {
UPCALL_DEPTH.with(|d| d.set(d.get() - 1));
}
}
#[derive(Debug, Default)]
pub(crate) struct Activity {
count: Mutex<usize>,
idle: Condvar,
}
impl Activity {
pub(crate) fn enter(self: &Arc<Self>) -> ActivityGuard {
*self.count.lock().unwrap() += 1;
ActivityGuard(Arc::clone(self))
}
pub(crate) fn wait_idle(&self) {
let mut count = self.count.lock().unwrap();
while *count > 0 {
count = self.idle.wait(count).unwrap();
}
}
}
pub(crate) struct ActivityGuard(Arc<Activity>);
impl Drop for ActivityGuard {
fn drop(&mut self) {
let mut count = self.0.count.lock().unwrap();
*count -= 1;
if *count == 0 {
self.0.idle.notify_all();
}
}
}
fn check_wait(wait_for_completion: bool) -> Result<(), SystemException> {
if wait_for_completion && in_upcall() {
return Err(sys(
SystemExceptionKind::BAD_INV_ORDER,
minor::WOULD_DEADLOCK,
));
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PoaManagerState {
Holding,
Active,
Discarding,
Inactive,
}
pub(crate) enum Disposition {
Run,
Reject(SystemException),
}
pub(crate) type Dispatch = Box<dyn FnOnce(Disposition) + Send>;
const MAX_HELD_REQUESTS: usize = 4096;
#[derive(Clone)]
pub struct PoaManager {
pub(crate) inner: Arc<ManagerInner>,
}
pub(crate) struct ManagerInner {
id: String,
state: Mutex<ManagerState>,
activity: Arc<Activity>,
server: Weak<Server>,
poas: Mutex<ManagedPoas>,
}
#[derive(Default)]
struct ManagedPoas {
list: Vec<Weak<PoaInner>>,
live: usize,
destroyed: bool,
}
struct ManagerState {
state: PoaManagerState,
held: VecDeque<Dispatch>,
}
impl fmt::Debug for PoaManager {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PoaManager")
.field("id", &self.inner.id)
.field("state", &self.state())
.finish_non_exhaustive()
}
}
impl PartialEq for PoaManager {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}
impl Eq for PoaManager {}
impl PoaManager {
pub(crate) fn new(server: &Arc<Server>, id: String) -> PoaManager {
let inner = Arc::new(ManagerInner {
id,
state: Mutex::new(ManagerState {
state: PoaManagerState::Holding,
held: VecDeque::new(),
}),
activity: Arc::default(),
server: Arc::downgrade(server),
poas: Mutex::default(),
});
server.register_manager(&inner);
PoaManager { inner }
}
pub fn get_id(&self) -> &str {
&self.inner.id
}
pub fn state(&self) -> PoaManagerState {
self.inner.state.lock().unwrap().state
}
pub fn activate(&self) -> Result<(), PoaError> {
let held = {
let mut st = self.inner.state.lock().unwrap();
if st.state == PoaManagerState::Inactive {
return Err(PoaError::AdapterInactive);
}
st.state = PoaManagerState::Active;
trace::info!("POA manager active");
std::mem::take(&mut st.held)
};
for dispatch in held {
dispatch(Disposition::Run);
}
Ok(())
}
pub fn hold_requests(&self, wait_for_completion: bool) -> Result<(), PoaError> {
check_wait(wait_for_completion)?;
self.set(PoaManagerState::Holding)?;
if wait_for_completion {
self.inner.activity.wait_idle();
}
Ok(())
}
pub fn discard_requests(&self, wait_for_completion: bool) -> Result<(), PoaError> {
check_wait(wait_for_completion)?;
self.set(PoaManagerState::Discarding)?;
if wait_for_completion {
self.inner.activity.wait_idle();
}
Ok(())
}
pub fn deactivate(&self, etherealize: bool, wait_for_completion: bool) -> Result<(), PoaError> {
check_wait(wait_for_completion)?;
self.set(PoaManagerState::Inactive)?;
if etherealize {
let poas: Vec<_> = self
.inner
.poas
.lock()
.unwrap()
.list
.iter()
.filter_map(Weak::upgrade)
.collect();
let mut work = Vec::new();
for poa in poas {
work.extend(poa.take_for_etherealize());
}
if let Some(server) = self.inner.server.upgrade() {
server.etherealize(work, wait_for_completion);
}
}
if wait_for_completion {
self.inner.activity.wait_idle();
}
Ok(())
}
fn set(&self, state: PoaManagerState) -> Result<(), PoaError> {
let held = {
let mut st = self.inner.state.lock().unwrap();
if st.state == PoaManagerState::Inactive {
return Err(PoaError::AdapterInactive);
}
st.state = state;
trace::info!("POA manager {state:?}");
if state == PoaManagerState::Holding {
return Ok(());
}
std::mem::take(&mut st.held)
};
let e = refusal(state);
for dispatch in held {
dispatch(Disposition::Reject(e.clone()));
}
Ok(())
}
}
#[derive(Clone)]
pub struct PoaManagerFactory {
orb: Orb,
}
impl fmt::Debug for PoaManagerFactory {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PoaManagerFactory").finish_non_exhaustive()
}
}
impl PoaManagerFactory {
fn server(&self) -> Result<Arc<Server>, PoaError> {
self.orb
.server()
.ok_or_else(|| sys(SystemExceptionKind::OBJECT_NOT_EXIST, 0).into())
}
pub fn create_poa_manager(
&self,
id: &str,
policies: &[PolicyValue],
) -> Result<PoaManager, PoaError> {
if !policies.is_empty() {
return Err(PoaError::Policy(PolicyError {
reason: PolicyError::UNSUPPORTED_POLICY,
}));
}
self.server()?.create_manager(id)
}
pub fn list(&self) -> Vec<PoaManager> {
self.server().map(|s| s.list_managers()).unwrap_or_default()
}
pub fn find(&self, id: &str) -> Option<PoaManager> {
self.server().ok()?.find_manager(id)
}
}
impl ManagerInner {
pub(crate) fn id(&self) -> &str {
&self.id
}
}
fn refusal(state: PoaManagerState) -> SystemException {
match state {
PoaManagerState::Inactive => sys(SystemExceptionKind::OBJ_ADAPTER, minor::POA_INACTIVE),
_ => sys(SystemExceptionKind::TRANSIENT, minor::POA_DISCARDING),
}
}
impl ManagerInner {
pub(crate) fn dispatch(&self, dispatch: Dispatch) {
let mut st = self.state.lock().unwrap();
match st.state {
PoaManagerState::Active => {
drop(st);
dispatch(Disposition::Run);
}
PoaManagerState::Holding if st.held.len() < MAX_HELD_REQUESTS => {
st.held.push_back(dispatch);
}
PoaManagerState::Holding => {
drop(st);
dispatch(Disposition::Reject(sys(SystemExceptionKind::TRANSIENT, 0)));
}
state => {
drop(st);
dispatch(Disposition::Reject(refusal(state)));
}
}
}
pub(crate) fn is_inactive(&self) -> bool {
self.state.lock().unwrap().state == PoaManagerState::Inactive
}
pub(crate) fn activity(&self) -> &Arc<Activity> {
&self.activity
}
fn add_poa(&self, poa: &Arc<PoaInner>) -> Result<(), PoaError> {
let mut poas = self.poas.lock().unwrap();
if poas.destroyed {
return Err(sys(SystemExceptionKind::OBJECT_NOT_EXIST, 0).into());
}
poas.list.retain(|p| p.strong_count() > 0);
poas.list.push(Arc::downgrade(poa));
poas.live += 1;
Ok(())
}
fn poa_destroyed(&self) {
let mut poas = self.poas.lock().unwrap();
poas.live = poas.live.saturating_sub(1);
if poas.live > 0 || poas.destroyed {
return;
}
poas.destroyed = true;
trace::info!("POA manager {:?} destroyed", self.id);
if let Some(server) = self.server.upgrade() {
server.factory_remove(self);
}
}
pub(crate) fn shut_down(&self) {
let held = {
let mut st = self.state.lock().unwrap();
st.state = PoaManagerState::Inactive;
std::mem::take(&mut st.held)
};
drop(held);
}
}
#[derive(Clone)]
pub struct Poa {
pub(crate) orb: Orb,
pub(crate) inner: Arc<PoaInner>,
}
pub(crate) struct PoaInner {
name: String,
path: Vec<String>,
policies: Policies,
pub(crate) manager: PoaManager,
parent: Option<Weak<PoaInner>>,
key_prefix: Vec<u8>,
boot_id: [u8; BOOT_ID_LEN],
activity: Arc<Activity>,
state: Mutex<PoaState>,
changed: Condvar,
serial: Mutex<Serial>,
pub(crate) components: Vec<TaggedComponent>,
}
#[derive(Default)]
struct PoaState {
destroyed: bool,
children: HashMap<String, Arc<PoaInner>>,
active: HashMap<ObjectId, Arc<dyn Servant>>,
ids: HashMap<usize, ObjectId>,
next_id: u32,
default_servant: Option<Arc<dyn Servant>>,
servant_manager: Option<ServantManager>,
activator: Option<Arc<dyn AdapterActivator>>,
incarnating: HashSet<ObjectId>,
activating: HashSet<String>,
running: HashMap<ObjectId, usize>,
deactivated: HashMap<ObjectId, Arc<dyn Servant>>,
}
#[derive(Default)]
struct Serial {
queue: VecDeque<Job>,
busy: bool,
owner: Option<ThreadId>,
}
pub(crate) enum SerialEntry {
Reentrant,
Acquired(SerialGuard),
Busy,
}
pub(crate) struct SerialGuard {
poa: Arc<PoaInner>,
server: Arc<Server>,
}
impl Drop for SerialGuard {
fn drop(&mut self) {
self.poa.release_serial(&self.server);
}
}
pub(crate) struct Etherealization {
poa: Arc<PoaInner>,
activator: Arc<dyn ServantActivator>,
id: ObjectId,
servant: Arc<dyn Servant>,
cleanup_in_progress: bool,
}
impl Etherealization {
pub(crate) fn poa(&self) -> &Arc<PoaInner> {
&self.poa
}
pub(crate) fn run(self, orb: &Orb, remaining: Option<bool>) {
let remaining = remaining.unwrap_or_else(|| {
let st = self.poa.state.lock().unwrap();
st.active.values().any(|s| same_servant(s, &self.servant))
});
let adapter = Poa {
orb: orb.clone(),
inner: self.poa,
};
let _upcall = UpcallGuard::enter();
let result = panic::catch_unwind(AssertUnwindSafe(|| {
self.activator.etherealize(
&self.id,
&adapter,
self.servant,
self.cleanup_in_progress,
remaining,
);
}));
if result.is_err() {
trace::warning!("servant activator panicked in etherealize");
}
}
}
pub(crate) fn etherealize_all(orb: &Orb, etherealizations: Vec<Etherealization>) {
let mut rest = etherealizations;
while !rest.is_empty() {
let e = rest.remove(0);
let remaining = rest.iter().any(|r| same_servant(&r.servant, &e.servant));
e.run(orb, Some(remaining));
}
}
pub(crate) struct Resolved {
pub(crate) servant: Arc<dyn Servant>,
_object: Option<ObjectGuard>,
locator: Option<(Arc<dyn ServantLocator>, Cookie)>,
}
impl Resolved {
pub(crate) fn finish(self, adapter: &Poa, id: &[u8], operation: &str) {
if let Some((locator, cookie)) = self.locator {
let servant = self.servant;
let result = panic::catch_unwind(AssertUnwindSafe(|| {
locator.postinvoke(id, adapter, operation, cookie, servant);
}));
if result.is_err() {
trace::warning!("servant locator panicked in postinvoke");
}
}
}
}
pub(crate) struct ObjectGuard {
poa: Arc<PoaInner>,
server: Weak<Server>,
id: ObjectId,
}
impl Drop for ObjectGuard {
fn drop(&mut self) {
let pending = {
let mut st = self.poa.state.lock().unwrap();
let Some(count) = st.running.get_mut(&self.id) else {
return;
};
*count -= 1;
if *count > 0 {
return;
}
st.running.remove(&self.id);
if st.destroyed {
return;
}
let Some(servant) = st.deactivated.remove(&self.id) else {
return;
};
let Some(ServantManager::Activator(activator)) = st.servant_manager.clone() else {
return;
};
Etherealization {
poa: Arc::clone(&self.poa),
activator,
id: std::mem::take(&mut self.id),
servant,
cleanup_in_progress: false,
}
};
if let Some(server) = self.server.upgrade() {
server.etherealize_later(pending);
}
}
}
impl fmt::Debug for Poa {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Poa")
.field("name", &self.inner.name)
.field("path", &self.inner.path)
.field("policies", &self.inner.policies)
.finish_non_exhaustive()
}
}
impl PartialEq for Poa {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}
impl Eq for Poa {}
fn servant_addr(servant: &Arc<dyn Servant>) -> usize {
Arc::as_ptr(servant).cast::<()>() as usize
}
fn same_servant(a: &Arc<dyn Servant>, b: &Arc<dyn Servant>) -> bool {
servant_addr(a) == servant_addr(b)
}
type Drained = (
Vec<(ObjectId, Arc<dyn Servant>)>,
Option<Arc<dyn ServantActivator>>,
);
fn drain(st: &mut PoaState) -> Drained {
st.ids.clear();
let mut entries: Vec<_> = std::mem::take(&mut st.active)
.into_iter()
.chain(std::mem::take(&mut st.deactivated))
.collect();
entries.sort_by(|a, b| a.0.cmp(&b.0));
let activator = match &st.servant_manager {
Some(ServantManager::Activator(a)) => Some(Arc::clone(a)),
_ => None,
};
(entries, activator)
}
fn catch<T>(
what: &str,
f: impl FnOnce() -> Result<T, ServantManagerError>,
) -> Result<T, ServantManagerError> {
let _upcall = UpcallGuard::enter();
panic::catch_unwind(AssertUnwindSafe(f)).unwrap_or_else(|_| {
trace::warning!("servant manager panicked in {what}");
Err(SystemException::new(SystemExceptionKind::UNKNOWN, 0, CompletionStatus::Maybe).into())
})
}
impl PoaInner {
#[allow(clippy::too_many_arguments)]
fn new(
name: String,
path: Vec<String>,
policies: Policies,
manager: PoaManager,
parent: Option<Weak<PoaInner>>,
key_prefix: Vec<u8>,
boot_id: [u8; BOOT_ID_LEN],
components: Vec<TaggedComponent>,
) -> Result<Arc<PoaInner>, PoaError> {
let poa = Arc::new(PoaInner {
components,
name,
path,
policies,
manager,
parent,
key_prefix,
boot_id,
activity: Arc::default(),
state: Mutex::default(),
changed: Condvar::new(),
serial: Mutex::default(),
});
poa.manager.inner.add_poa(&poa)?;
Ok(poa)
}
pub(crate) fn new_root(
manager: PoaManager,
boot_id: [u8; BOOT_ID_LEN],
components: Vec<TaggedComponent>,
) -> Result<Arc<PoaInner>, PoaError> {
let mut key_prefix = vec![TRANSIENT_END];
key_prefix.extend_from_slice(&boot_id);
PoaInner::new(
"RootPOA".into(),
Vec::new(),
Policies::root(),
manager,
None,
key_prefix,
boot_id,
components,
)
}
fn new_child(
parent: &Arc<PoaInner>,
name: &str,
manager: PoaManager,
policies: Policies,
components: Vec<TaggedComponent>,
) -> Result<Arc<PoaInner>, PoaError> {
let mut path = parent.path.clone();
path.push(name.to_owned());
let mut key_prefix = vec![PATH_START];
for (i, n) in path.iter().enumerate() {
if i > 0 {
key_prefix.push(PATH_SEPARATOR);
}
key_prefix.extend_from_slice(n.as_bytes());
}
match policies.lifespan {
Lifespan::Persistent => key_prefix.push(PERSISTENT_END),
Lifespan::Transient => {
key_prefix.push(TRANSIENT_END);
key_prefix.extend_from_slice(&parent.boot_id);
}
}
PoaInner::new(
name.to_owned(),
path,
policies,
manager,
Some(Arc::downgrade(parent)),
key_prefix,
parent.boot_id,
components,
)
}
pub(crate) fn policies(&self) -> &Policies {
&self.policies
}
pub(crate) fn path(&self) -> &[String] {
&self.path
}
pub(crate) fn activity(&self) -> &Arc<Activity> {
&self.activity
}
pub(crate) fn key(&self, id: &[u8]) -> Vec<u8> {
let mut key = self.key_prefix.clone();
key.extend_from_slice(id);
key
}
pub(crate) fn child(&self, name: &str) -> Result<Arc<PoaInner>, bool> {
let st = self.state.lock().unwrap();
if st.destroyed {
return Err(false);
}
st.children.get(name).cloned().ok_or(st.activator.is_some())
}
pub(crate) fn find_or_activate_child(
self: &Arc<Self>,
orb: &Orb,
name: &str,
) -> Option<Arc<PoaInner>> {
let mut st = self.state.lock().unwrap();
loop {
if st.destroyed {
return None;
}
if let Some(child) = st.children.get(name) {
return Some(Arc::clone(child));
}
let activator = st.activator.clone()?;
if st.activating.contains(name) {
st = self.changed.wait(st).unwrap();
continue;
}
st.activating.insert(name.to_owned());
drop(st);
let parent = Poa {
orb: orb.clone(),
inner: Arc::clone(self),
};
let created = {
let _upcall = UpcallGuard::enter();
panic::catch_unwind(AssertUnwindSafe(|| {
activator.unknown_adapter(&parent, name)
}))
.unwrap_or_else(|_| {
trace::warning!("adapter activator panicked in unknown_adapter");
false
})
};
st = self.state.lock().unwrap();
st.activating.remove(name);
self.changed.notify_all();
if !created {
return None;
}
return st.children.get(name).cloned();
}
}
pub(crate) fn has_object(&self, id: &[u8]) -> bool {
let st = self.state.lock().unwrap();
!st.destroyed
&& (st.active.contains_key(id)
|| self.policies.request_processing != RequestProcessing::ActiveObjectMapOnly)
}
pub(crate) fn is_destroyed(&self) -> bool {
self.state.lock().unwrap().destroyed
}
pub(crate) fn resolve(
self: &Arc<Self>,
adapter: &Poa,
server: &Arc<Server>,
id: &[u8],
operation: &str,
) -> Result<Resolved, ServantManagerError> {
let retain = self.policies.servant_retention == ServantRetention::Retain;
let mut st = self.state.lock().unwrap();
loop {
if st.destroyed {
return Err(no_such_object().into());
}
if retain {
if let Some(servant) = st.active.get(id).cloned() {
return Ok(self.resolved(&mut st, server, id, servant));
}
}
match self.policies.request_processing {
RequestProcessing::ActiveObjectMapOnly => return Err(no_such_object().into()),
RequestProcessing::UseDefaultServant => {
let servant = st.default_servant.clone().ok_or_else(|| {
sys(SystemExceptionKind::OBJ_ADAPTER, minor::NO_DEFAULT_SERVANT)
})?;
return Ok(Resolved {
servant,
_object: None,
locator: None,
});
}
RequestProcessing::UseServantManager => {}
}
match st.servant_manager.clone() {
None => {
return Err(
sys(SystemExceptionKind::OBJ_ADAPTER, minor::NO_SERVANT_MANAGER).into(),
);
}
Some(ServantManager::Locator(locator)) => {
drop(st);
let (servant, cookie) =
catch("preinvoke", || locator.preinvoke(id, adapter, operation))?;
return Ok(Resolved {
servant,
_object: None,
locator: Some((locator, cookie)),
});
}
Some(ServantManager::Activator(activator)) => {
if st.incarnating.contains(id) {
st = self.changed.wait(st).unwrap();
continue;
}
st.incarnating.insert(id.to_vec());
drop(st);
let result = catch("incarnate", || activator.incarnate(id, adapter));
st = self.state.lock().unwrap();
st.incarnating.remove(id);
self.changed.notify_all();
let servant = result?;
if st.destroyed {
return Err(no_such_object().into());
}
match self.activate(&mut st, id.to_vec(), Arc::clone(&servant)) {
Ok(()) => return Ok(self.resolved(&mut st, server, id, servant)),
Err(PoaError::ObjectAlreadyActive) => continue,
Err(_) => {
return Err(sys(
SystemExceptionKind::OBJ_ADAPTER,
minor::SERVANT_ALREADY_ACTIVE,
)
.into());
}
}
}
}
}
}
fn resolved(
self: &Arc<Self>,
st: &mut PoaState,
server: &Arc<Server>,
id: &[u8],
servant: Arc<dyn Servant>,
) -> Resolved {
*st.running.entry(id.to_vec()).or_insert(0) += 1;
Resolved {
servant,
_object: Some(ObjectGuard {
poa: Arc::clone(self),
server: Arc::downgrade(server),
id: id.to_vec(),
}),
locator: None,
}
}
pub(crate) fn destroy_tree(self: &Arc<Self>, etherealize: bool) -> Vec<Etherealization> {
let (children, drained, rest) = {
let mut st = self.state.lock().unwrap();
if st.destroyed {
return Vec::new();
}
st.destroyed = true;
let children = std::mem::take(&mut st.children);
let drained = drain(&mut st);
let rest = (
st.default_servant.take(),
st.servant_manager.take(),
st.activator.take(),
);
(children, drained, rest)
};
drop(rest);
let mut all = Vec::new();
for child in children.into_values() {
all.extend(child.destroy_tree(etherealize));
}
self.manager.inner.poa_destroyed();
all.extend(self.etherealizations(drained, etherealize));
all
}
fn take_for_etherealize(self: &Arc<Self>) -> Vec<Etherealization> {
let drained = {
let mut st = self.state.lock().unwrap();
if st.destroyed {
return Vec::new();
}
drain(&mut st)
};
self.etherealizations(drained, true)
}
fn etherealizations(
self: &Arc<Self>,
(entries, activator): Drained,
etherealize: bool,
) -> Vec<Etherealization> {
let Some(activator) = activator.filter(|_| etherealize) else {
return Vec::new();
};
entries
.into_iter()
.map(|(id, servant)| Etherealization {
poa: Arc::clone(self),
activator: Arc::clone(&activator),
id,
servant,
cleanup_in_progress: true,
})
.collect()
}
fn generate_id(&self, st: &mut PoaState) -> ObjectId {
let serial = st.next_id;
st.next_id = st.next_id.wrapping_add(1);
let mut id = self.boot_id.to_vec();
id.extend_from_slice(&serial.to_be_bytes());
id
}
fn activate(
&self,
st: &mut PoaState,
id: ObjectId,
servant: Arc<dyn Servant>,
) -> Result<(), PoaError> {
if st.active.contains_key(&id) {
return Err(PoaError::ObjectAlreadyActive);
}
if self.policies.id_uniqueness == IdUniqueness::Unique {
let addr = servant_addr(&servant);
if st.ids.contains_key(&addr) {
return Err(PoaError::ServantAlreadyActive);
}
st.ids.insert(addr, id.clone());
}
trace::debug!(
"activated object {} in POA {:?}",
trace::IdSummary(&id),
self.name
);
st.active.insert(id, servant);
Ok(())
}
pub(crate) fn run_serially(self: &Arc<Self>, server: &Arc<Server>, job: Job) {
let mut serial = self.serial.lock().unwrap();
serial.queue.push_back(job);
if !serial.busy {
serial.busy = true;
drop(serial);
self.submit_serial(server);
}
}
fn submit_serial(self: &Arc<Self>, server: &Arc<Server>) {
let poa = Arc::clone(self);
let srv = Arc::clone(server);
server.submit(Box::new(move || poa.run_one_serially(&srv)));
}
fn run_one_serially(self: &Arc<Self>, server: &Arc<Server>) {
let job = {
let mut serial = self.serial.lock().unwrap();
let Some(job) = serial.queue.pop_front() else {
serial.busy = false;
return;
};
serial.owner = Some(thread::current().id());
job
};
let _ = panic::catch_unwind(AssertUnwindSafe(job));
self.release_serial(server);
}
fn release_serial(self: &Arc<Self>, server: &Arc<Server>) {
let mut serial = self.serial.lock().unwrap();
serial.owner = None;
if serial.queue.is_empty() {
serial.busy = false;
} else {
drop(serial);
self.submit_serial(server);
}
}
pub(crate) fn enter_serially(self: &Arc<Self>, server: &Arc<Server>) -> SerialEntry {
let me = thread::current().id();
let mut serial = self.serial.lock().unwrap();
if serial.owner == Some(me) {
SerialEntry::Reentrant
} else if serial.busy {
SerialEntry::Busy
} else {
serial.busy = true;
serial.owner = Some(me);
SerialEntry::Acquired(SerialGuard {
poa: Arc::clone(self),
server: Arc::clone(server),
})
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum ParsedKey<'a> {
Poa {
path: Vec<&'a str>,
boot_id: Option<&'a [u8]>,
id: &'a [u8],
},
Simple(&'a [u8]),
Invalid,
}
pub(crate) fn parse_key(key: &[u8]) -> ParsedKey<'_> {
fn transient<'a>(rest: &'a [u8], path: Vec<&'a str>) -> ParsedKey<'a> {
if rest.len() < BOOT_ID_LEN {
return ParsedKey::Invalid;
}
let (boot_id, id) = rest.split_at(BOOT_ID_LEN);
ParsedKey::Poa {
path,
boot_id: Some(boot_id),
id,
}
}
match key.first() {
Some(&TRANSIENT_END) => transient(&key[1..], Vec::new()),
Some(&PATH_START) => {
let rest = &key[1..];
let Some(end) = rest
.iter()
.position(|&b| b == PERSISTENT_END || b == TRANSIENT_END)
else {
return ParsedKey::Invalid;
};
let mut path = Vec::new();
for name in rest[..end].split(|&b| b == PATH_SEPARATOR) {
match std::str::from_utf8(name) {
Ok(name) if !name.is_empty() => path.push(name),
_ => return ParsedKey::Invalid,
}
}
if rest[end] == PERSISTENT_END {
ParsedKey::Poa {
path,
boot_id: None,
id: &rest[end + 1..],
}
} else {
transient(&rest[end + 1..], path)
}
}
_ => ParsedKey::Simple(key),
}
}
impl Poa {
fn state(&self) -> Result<MutexGuard<'_, PoaState>, PoaError> {
let st = self.inner.state.lock().unwrap();
if st.destroyed {
return Err(sys(SystemExceptionKind::OBJECT_NOT_EXIST, 0).into());
}
Ok(st)
}
fn server(&self) -> Result<Arc<Server>, PoaError> {
self.orb
.server()
.ok_or_else(|| sys(SystemExceptionKind::OBJECT_NOT_EXIST, 0).into())
}
fn handle(&self, inner: Arc<PoaInner>) -> Poa {
Poa {
orb: self.orb.clone(),
inner,
}
}
pub fn the_name(&self) -> &str {
&self.inner.name
}
pub fn the_parent(&self) -> Option<Poa> {
let parent = self.inner.parent.as_ref()?.upgrade()?;
Some(self.handle(parent))
}
pub fn the_children(&self) -> Vec<Poa> {
let st = self.inner.state.lock().unwrap();
st.children
.values()
.map(|c| self.handle(Arc::clone(c)))
.collect()
}
pub fn the_poa_manager(&self) -> PoaManager {
self.inner.manager.clone()
}
pub fn the_poa_manager_factory(&self) -> PoaManagerFactory {
PoaManagerFactory {
orb: self.orb.clone(),
}
}
pub fn policies(&self) -> Policies {
self.inner.policies
}
pub fn create_poa(
&self,
name: &str,
manager: Option<&PoaManager>,
policies: &[Policy],
) -> Result<Poa, PoaError> {
if name.is_empty() || name.contains('\0') {
return Err(sys(SystemExceptionKind::BAD_PARAM, 0).into());
}
let policies = Policies::from_list(policies).map_err(PoaError::InvalidPolicy)?;
let server = self.server()?;
let components = if self.state()?.children.contains_key(name) {
Vec::new()
} else {
let mut path = self.inner.path.clone();
path.push(name.to_owned());
crate::pi::establish_components(&self.orb, &policies, &path)
};
let mut st = self.state()?;
if st.children.contains_key(name) {
return Err(PoaError::AdapterAlreadyExists);
}
let manager = match manager {
Some(m) => m.clone(),
None => server.create_implicit_manager(),
};
if policies.lifespan == Lifespan::Persistent && !server.has_fixed_port() {
trace::warning!(
"PERSISTENT POA {name:?} on an endpoint without a fixed port: \
its references will not survive a restart"
);
}
let child = PoaInner::new_child(&self.inner, name, manager, policies, components)?;
trace::info!("created POA {name:?} under {:?}", self.inner.name);
st.children.insert(name.to_owned(), Arc::clone(&child));
Ok(self.handle(child))
}
pub fn find_poa(&self, name: &str, activate_it: bool) -> Result<Poa, PoaError> {
drop(self.state()?);
let child = if activate_it {
self.inner.find_or_activate_child(&self.orb, name)
} else {
self.inner.child(name).ok()
};
child
.map(|c| self.handle(c))
.ok_or(PoaError::AdapterNonExistent)
}
pub fn the_activator(&self) -> Option<Arc<dyn AdapterActivator>> {
self.inner.state.lock().unwrap().activator.clone()
}
pub fn set_the_activator(
&self,
activator: Option<Arc<dyn AdapterActivator>>,
) -> Result<(), PoaError> {
let old = std::mem::replace(&mut self.state()?.activator, activator);
drop(old);
Ok(())
}
pub fn get_servant_manager(&self) -> Result<Option<ServantManager>, PoaError> {
if self.inner.policies.request_processing != RequestProcessing::UseServantManager {
return Err(PoaError::WrongPolicy);
}
Ok(self.state()?.servant_manager.clone())
}
pub fn set_servant_manager(&self, manager: ServantManager) -> Result<(), PoaError> {
let p = self.inner.policies;
if p.request_processing != RequestProcessing::UseServantManager {
return Err(PoaError::WrongPolicy);
}
let fits = matches!(
(&manager, p.servant_retention),
(ServantManager::Activator(_), ServantRetention::Retain)
| (ServantManager::Locator(_), ServantRetention::NonRetain)
);
if !fits {
return Err(sys(SystemExceptionKind::OBJ_ADAPTER, minor::NO_SERVANT_MANAGER).into());
}
let mut st = self.state()?;
if st.servant_manager.is_some() {
return Err(sys(
SystemExceptionKind::BAD_INV_ORDER,
minor::SERVANT_MANAGER_ALREADY_SET,
)
.into());
}
st.servant_manager = Some(manager);
Ok(())
}
pub fn get_servant(&self) -> Result<Arc<dyn Servant>, PoaError> {
if self.inner.policies.request_processing != RequestProcessing::UseDefaultServant {
return Err(PoaError::WrongPolicy);
}
self.state()?
.default_servant
.clone()
.ok_or(PoaError::NoServant)
}
pub fn set_servant(&self, servant: Arc<dyn Servant>) -> Result<(), PoaError> {
if self.inner.policies.request_processing != RequestProcessing::UseDefaultServant {
return Err(PoaError::WrongPolicy);
}
let old = self.state()?.default_servant.replace(servant);
drop(old);
Ok(())
}
pub fn destroy(&self, etherealize: bool, wait_for_completion: bool) -> Result<(), PoaError> {
check_wait(wait_for_completion)?;
trace::info!("destroying POA {:?}", self.inner.name);
if let Some(parent) = self.inner.parent.as_ref().and_then(Weak::upgrade) {
let mut st = parent.state.lock().unwrap();
if st
.children
.get(&self.inner.name)
.is_some_and(|c| Arc::ptr_eq(c, &self.inner))
{
st.children.remove(&self.inner.name);
}
}
let work = self.inner.destroy_tree(etherealize);
if wait_for_completion {
self.inner.activity.wait_idle();
}
if let Some(server) = self.orb.server() {
server.etherealize(work, wait_for_completion);
}
Ok(())
}
fn check_retain(&self) -> Result<(), PoaError> {
if self.inner.policies.servant_retention != ServantRetention::Retain {
return Err(PoaError::WrongPolicy);
}
Ok(())
}
pub fn activate_object(&self, servant: Arc<dyn Servant>) -> Result<ObjectId, PoaError> {
self.check_retain()?;
if self.inner.policies.id_assignment != IdAssignment::System {
return Err(PoaError::WrongPolicy);
}
let mut st = self.state()?;
let id = self.inner.generate_id(&mut st);
self.inner.activate(&mut st, id.clone(), servant)?;
Ok(id)
}
pub fn activate_object_with_id(
&self,
id: &[u8],
servant: Arc<dyn Servant>,
) -> Result<(), PoaError> {
self.check_retain()?;
let mut st = self.state()?;
self.inner.activate(&mut st, id.to_vec(), servant)
}
pub fn deactivate_object(&self, id: &[u8]) -> Result<(), PoaError> {
self.check_retain()?;
let (servant, now) = {
let mut st = self.state()?;
let servant = st.active.remove(id).ok_or(PoaError::ObjectNotActive)?;
trace::debug!(
"deactivated object {} in POA {:?}",
trace::IdSummary(id),
self.inner.name
);
let addr = servant_addr(&servant);
if st.ids.get(&addr).is_some_and(|i| i == id) {
st.ids.remove(&addr);
}
match st.servant_manager.clone() {
Some(ServantManager::Activator(activator)) => {
if st.running.contains_key(id) {
st.deactivated.insert(id.to_vec(), servant);
(None, None)
} else {
(None, Some((activator, servant)))
}
}
_ => (Some(servant), None),
}
};
drop(servant);
if let Some((activator, servant)) = now {
let work = Etherealization {
poa: Arc::clone(&self.inner),
activator,
id: id.to_vec(),
servant,
cleanup_in_progress: false,
};
if let Some(server) = self.orb.server() {
server.etherealize_later(work);
}
}
Ok(())
}
pub fn servant_to_id(&self, servant: &Arc<dyn Servant>) -> Result<ObjectId, PoaError> {
let p = self.inner.policies;
let retain = p.servant_retention == ServantRetention::Retain;
let default_servant = p.request_processing == RequestProcessing::UseDefaultServant;
if !default_servant
&& !(retain
&& (p.id_uniqueness == IdUniqueness::Unique
|| p.implicit_activation == ImplicitActivation::Implicit))
{
return Err(PoaError::WrongPolicy);
}
let mut st = self.state()?;
if retain && p.id_uniqueness == IdUniqueness::Unique {
if let Some(id) = st.ids.get(&servant_addr(servant)) {
return Ok(id.clone());
}
}
if retain && p.implicit_activation == ImplicitActivation::Implicit {
let id = self.inner.generate_id(&mut st);
self.inner
.activate(&mut st, id.clone(), Arc::clone(servant))?;
return Ok(id);
}
let is_default = st
.default_servant
.as_ref()
.is_some_and(|d| same_servant(d, servant));
drop(st);
if is_default {
let current = CURRENT.with(|c| {
c.borrow()
.last()
.filter(|e| e.poa == *self && same_servant(&e.servant, servant))
.map(|e| e.id.clone())
});
if let Some(id) = current {
return Ok(id);
}
}
Err(PoaError::ServantNotActive)
}
pub fn servant_to_reference(&self, servant: &Arc<dyn Servant>) -> Result<ObjectRef, PoaError> {
let id = self.servant_to_id(servant)?;
self.reference(&id, servant.repository_id())
}
pub fn id_to_servant(&self, id: &[u8]) -> Result<Arc<dyn Servant>, PoaError> {
let p = self.inner.policies;
let retain = p.servant_retention == ServantRetention::Retain;
let default_servant = p.request_processing == RequestProcessing::UseDefaultServant;
if !retain && !default_servant {
return Err(PoaError::WrongPolicy);
}
let st = self.state()?;
let active = retain.then(|| st.active.get(id).cloned()).flatten();
active
.or_else(|| st.default_servant.clone().filter(|_| default_servant))
.ok_or(PoaError::ObjectNotActive)
}
pub fn id_to_reference(&self, id: &[u8]) -> Result<ObjectRef, PoaError> {
self.check_retain()?;
let servant = self
.state()?
.active
.get(id)
.cloned()
.ok_or(PoaError::ObjectNotActive)?;
self.reference(id, servant.repository_id())
}
pub fn create_reference(&self, intf: &str) -> Result<ObjectRef, PoaError> {
if self.inner.policies.id_assignment != IdAssignment::System {
return Err(PoaError::WrongPolicy);
}
let id = {
let mut st = self.state()?;
self.inner.generate_id(&mut st)
};
self.reference(&id, intf)
}
pub fn create_reference_with_id(&self, id: &[u8], intf: &str) -> Result<ObjectRef, PoaError> {
drop(self.state()?);
self.reference(id, intf)
}
pub fn reference_to_id(&self, reference: &ObjectRef) -> Result<ObjectId, PoaError> {
drop(self.state()?);
let prefix = &self.inner.key_prefix;
reference
.ior()
.iiop_profiles()
.filter_map(Result::ok)
.map(|p: IiopProfile| p.object_key)
.find(|key| key.starts_with(prefix))
.map(|key| key[prefix.len()..].to_vec())
.ok_or(PoaError::WrongAdapter)
}
pub fn reference_to_servant(
&self,
reference: &ObjectRef,
) -> Result<Arc<dyn Servant>, PoaError> {
let id = self.reference_to_id(reference)?;
self.id_to_servant(&id)
}
pub(crate) fn reference(&self, id: &[u8], type_id: &str) -> Result<ObjectRef, PoaError> {
let server = self.server()?;
let ior = server.ior(type_id, self.inner.key(id), &self.inner.components)?;
Ok(ObjectRef::new(self.orb.clone(), ior))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn keys_parse_back() {
let boot = [1, 2, 3, 4, 5, 6, 7, 8];
assert_eq!(
parse_key(b"\xffpersistent_user\x00obj"),
ParsedKey::Poa {
path: vec!["persistent_user"],
boot_id: None,
id: b"obj"
}
);
assert_eq!(
parse_key(b"\xffa\xffnested\x00"),
ParsedKey::Poa {
path: vec!["a", "nested"],
boot_id: None,
id: b""
}
);
let mut transient = b"\xffchild\xfe".to_vec();
transient.extend_from_slice(&boot);
transient.extend_from_slice(b"id");
assert_eq!(
parse_key(&transient),
ParsedKey::Poa {
path: vec!["child"],
boot_id: Some(&boot),
id: b"id"
}
);
let mut root = vec![0xfe];
root.extend_from_slice(&boot);
assert_eq!(
parse_key(&root),
ParsedKey::Poa {
path: vec![],
boot_id: Some(&boot),
id: b""
}
);
assert_eq!(parse_key(b"NameService"), ParsedKey::Simple(b"NameService"));
assert_eq!(parse_key(b""), ParsedKey::Simple(b""));
assert_eq!(parse_key(b"\xffno-end"), ParsedKey::Invalid);
assert_eq!(parse_key(b"\xfe\x01\x02"), ParsedKey::Invalid);
assert_eq!(parse_key(b"\xff\xff\x00x"), ParsedKey::Invalid);
}
#[test]
fn policy_lists() {
assert_eq!(Policies::from_list(&[]), Ok(Policies::default()));
let p = Policies::from_list(&[
Policy::Lifespan(Lifespan::Persistent),
Policy::IdAssignment(IdAssignment::User),
])
.unwrap();
assert_eq!(p.lifespan, Lifespan::Persistent);
assert_eq!(p.id_assignment, IdAssignment::User);
assert_eq!(
Policies::from_list(&[
Policy::Lifespan(Lifespan::Persistent),
Policy::Lifespan(Lifespan::Transient),
]),
Err(1)
);
assert_eq!(
Policies::from_list(&[
Policy::IdAssignment(IdAssignment::User),
Policy::ImplicitActivation(ImplicitActivation::Implicit),
]),
Err(1)
);
assert_eq!(
Policies::from_list(&[Policy::Thread(ThreadPolicy::MainThread)])
.unwrap()
.thread,
ThreadPolicy::MainThread
);
assert_eq!(
Policies::from_list(&[Policy::ServantRetention(ServantRetention::NonRetain)]),
Err(0)
);
assert!(
Policies::from_list(&[
Policy::ServantRetention(ServantRetention::NonRetain),
Policy::RequestProcessing(RequestProcessing::UseServantManager),
])
.is_ok()
);
assert_eq!(
Policies::from_list(&[Policy::RequestProcessing(
RequestProcessing::UseDefaultServant
)]),
Err(0)
);
assert!(
Policies::from_list(&[
Policy::RequestProcessing(RequestProcessing::UseDefaultServant),
Policy::IdUniqueness(IdUniqueness::Multiple),
])
.is_ok()
);
assert_eq!(
Policies::from_list(&[
Policy::ImplicitActivation(ImplicitActivation::Implicit),
Policy::ServantRetention(ServantRetention::NonRetain),
Policy::RequestProcessing(RequestProcessing::UseServantManager),
]),
Err(0)
);
}
}