use super::*;
use crate::Reason;
impl Host {
pub fn begin_root(&self, principal: impl Into<String>) -> Result<Id> {
let mut inner = self.inner.borrow_mut();
inner.open()?;
let version = inner.version;
inner.root(version, principal.into(), false)
}
pub fn cleanup_root(&self, principal: impl Into<String>) -> Result<Id> {
let mut inner = self.inner.borrow_mut();
if inner.closed {
return Err(Error::new(Code::Closed, Reason::Host, "host is closed"));
}
let version = inner.version;
inner.root(version, principal.into(), true)
}
pub fn check_root(&self, root: Id) -> Result<()> {
self.inner.borrow().check_root(root).map(|_| ())
}
pub fn end_root(&self, root: Id) -> Result<()> {
let mut inner = self.inner.borrow_mut();
let record = inner
.roots
.get_mut(&root)
.ok_or_else(|| gone(Reason::Root))?;
record.active = false;
let leases = if record.calls == 0 {
inner.remove_root(root)
} else {
Vec::new()
};
drop(inner);
drop(leases);
Ok(())
}
pub fn cancel_root(&self, root: Id) -> Result<()> {
let mut inner = self.inner.borrow_mut();
let record = inner
.roots
.get_mut(&root)
.ok_or_else(|| gone(Reason::Root))?;
if !record.cancelled {
record.cancelled = true;
inner.actions.push_back(Action::CancelRoot(root));
}
Ok(())
}
pub fn root_principal(&self, root: Id) -> Result<String> {
Ok(self.inner.borrow().check_root(root)?.principal.clone())
}
pub fn begin_call(&self, reference: Id, root: Id, method: &str) -> Result<Invocation> {
self.begin_call_with_context(reference, root, method, 0, None)
}
pub fn begin_call_with_context(
&self,
reference: Id,
root: Id,
method: &str,
frame: Id,
parent: Option<Id>,
) -> Result<Invocation> {
let mut inner = self.inner.borrow_mut();
inner.check_frame(root, frame)?;
inner.check_parent(root, parent)?;
let context = inner.check_root(root)?;
if inner.closing && !context.cleanup {
return Err(Error::new(Code::Closed, Reason::Host, "host is closing"));
}
let version = context.version;
let reference = inner
.references
.get(&reference)
.ok_or_else(|| gone(Reason::Reference))?;
if reference.released {
return Err(Error::new(
Code::Released,
Reason::Reference,
"reference is released",
));
}
let object = reference.object;
#[cfg(feature = "views")]
if reference
.allowed
.as_ref()
.is_some_and(|allowed| !allowed.contains(method))
{
return Err(Error::new(
Code::Forbidden,
Reason::Method,
"method is not exposed",
));
}
if !reference.methods.is_empty() && !reference.methods.contains(method) {
return Err(Error::new(
Code::Forbidden,
Reason::Method,
"method was not required",
));
}
let record = inner
.objects
.get(&object)
.ok_or_else(|| gone(Reason::Object))?;
if record.phase != Status::Live || record.disposing {
return Err(Error::new(
Code::Unavailable,
Reason::NotReady,
"object is not live",
));
}
if version < record.min_version {
return Err(conflict(Reason::UpgradeConflict));
}
if record
.gate
.as_ref()
.is_some_and(|allowed| !allowed.contains(&root))
{
return Err(conflict(Reason::UpgradeConflict));
}
let (orb, service) = inner.find_service(&record.address.service, version)?;
if !service.methods.contains(method) {
return Err(Error::invalid(Reason::Method, "method is not declared"));
}
let orb_owner = inner.check_orb(orb, root, version)?;
let id = inner.id()?;
let call = Invocation {
context: frame,
id,
root,
object,
orb,
code_version: version,
method: method.into(),
};
let call = inner.retain_call(call, orb_owner)?;
inner.link_call(call.id, parent);
Ok(call)
}
pub fn cancel_call(&self, call: Id) -> Result<Vec<Id>> {
let mut inner = self.inner.borrow_mut();
if !inner.calls.contains_key(&call) {
return Err(gone(Reason::Call));
}
let mut cancelled = Vec::new();
let mut next = vec![call];
while let Some(call) = next.pop() {
if !inner.cancelled_calls.insert(call) {
continue;
}
inner.actions.push_back(Action::CancelCall(call));
cancelled.push(call);
next.extend(inner.call_children.members(&call));
}
Ok(cancelled)
}
pub fn check_call(&self, call: Id) -> Result<()> {
let inner = self.inner.borrow();
if !inner.calls.contains_key(&call) {
return Err(gone(Reason::Call));
}
inner.check_uncancelled(call)
}
pub fn call_cancelled(&self, call: Id) -> bool {
self.inner.borrow().cancelled_calls.contains(&call)
}
pub fn finish_call(&self, call: Id) -> Result<()> {
let mut inner = self.inner.borrow_mut();
let call = inner
.calls
.remove(&call)
.ok_or_else(|| gone(Reason::Call))?;
inner.unlink_call(call.id);
#[cfg(feature = "hooks")]
inner.release_observation_call(call.id);
let mut leases = Vec::new();
for frame in inner.call_contexts.remove(&call.id).unwrap_or_default() {
inner.release_frame(frame, &mut leases);
}
inner.release_frame(call.context, &mut leases);
let orb_owner = inner.orbs[&call.orb].owner;
for owner in [
Owner::Host,
Owner::Orb(orb_owner),
Owner::Object(call.object),
] {
inner.owner_calls.remove(&owner, call.id);
inner.settled_owner(owner);
}
let root = inner.roots.get_mut(&call.root).expect("call retains root");
root.calls -= 1;
if root.calls == 0 && !root.active {
leases.extend(inner.remove_root(call.root));
}
if call.object != 0 {
inner
.objects
.get_mut(&call.object)
.expect("call retains object")
.calls -= 1;
inner.maybe_dispose(call.object);
}
drop(inner);
drop(leases);
Ok(())
}
pub fn invocation(&self, call: Id) -> Result<Invocation> {
self.inner
.borrow()
.calls
.get(&call)
.cloned()
.ok_or_else(|| gone(Reason::Call))
}
pub fn begin_operation(
&self,
orb: Id,
root: Id,
operation: Id,
object: Option<Id>,
) -> Result<Invocation> {
self.begin_operation_with_context(orb, root, operation, object, 0, None)
}
pub fn begin_operation_with_context(
&self,
orb: Id,
root: Id,
operation: Id,
object: Option<Id>,
frame: Id,
parent: Option<Id>,
) -> Result<Invocation> {
let mut inner = self.inner.borrow_mut();
inner.check_frame(root, frame)?;
inner.check_parent(root, parent)?;
let context = inner.check_root(root)?;
let version = context.version;
let snapshot = inner
.versions
.get(&version)
.ok_or_else(|| conflict(Reason::Superseded))?;
if inner
.orbs
.get(&orb)
.is_none_or(|record| snapshot.get(&record.declaration.name) != Some(&orb))
|| operation == 0
{
return Err(gone(Reason::Operation));
}
let orb_owner = inner.check_orb(orb, root, version)?;
if let Some(object) = object {
let record = inner
.objects
.get(&object)
.ok_or_else(|| gone(Reason::Object))?;
if record.phase != Status::Live || record.disposing {
return Err(gone(Reason::Object));
}
if version < record.min_version
|| record
.gate
.as_ref()
.is_some_and(|roots| !roots.contains(&root))
{
return Err(conflict(Reason::UpgradeConflict));
}
let (expected, _) = inner.find_service(&record.address.service, version)?;
if expected != orb {
return Err(conflict(Reason::OperationOwner));
}
}
let id = inner.id()?;
let invocation = Invocation {
context: frame,
id,
root,
object: object.unwrap_or(0),
orb,
code_version: version,
method: operation.to_string(),
};
let invocation = inner.retain_call(invocation, orb_owner)?;
inner.link_call(invocation.id, parent);
Ok(invocation)
}
}
impl Inner {
fn check_parent(&self, root: Id, parent: Option<Id>) -> Result<()> {
let Some(parent) = parent else {
return Ok(());
};
match self.calls.get(&parent) {
None => Err(gone(Reason::Call)),
Some(call) if call.root != root => Err(Error::invalid(
Reason::Context,
"the issuing invocation belongs to another root",
)),
Some(_) => self.check_uncancelled(parent),
}
}
pub(crate) fn check_uncancelled(&self, call: Id) -> Result<()> {
if self.cancelled_calls.contains(&call) {
return Err(Error::new(
Code::Cancelled,
Reason::Call,
"invocation was cancelled",
));
}
Ok(())
}
fn link_call(&mut self, call: Id, parent: Option<Id>) {
if let Some(parent) = parent {
self.call_parents.insert(call, parent);
self.call_children.insert(parent, call);
}
}
fn unlink_call(&mut self, call: Id) {
self.cancelled_calls.remove(&call);
let parent = self.call_parents.remove(&call);
if let Some(parent) = parent {
self.call_children.remove(&parent, call);
}
for child in self.call_children.take(&call) {
match parent {
Some(parent) => {
self.call_parents.insert(child, parent);
self.call_children.insert(parent, child);
}
None => {
self.call_parents.remove(&child);
}
}
}
}
fn retain_call(&mut self, call: Invocation, orb_owner: Id) -> Result<Invocation> {
#[cfg(feature = "hooks")]
self.claim_observation_call(&call)?;
self.orb_roots.insert(orb_owner, call.root);
self.owner_calls.insert(Owner::Host, call.id);
self.owner_calls.insert(Owner::Orb(orb_owner), call.id);
if call.object != 0 {
self.owner_calls.insert(Owner::Object(call.object), call.id);
self.object_roots.insert(call.object, call.root);
self.objects
.get_mut(&call.object)
.expect("checked object")
.calls += 1;
}
let root = self.roots.get_mut(&call.root).expect("checked root");
root.calls += 1;
root.orbs.insert(orb_owner);
if call.object != 0 {
root.touched.insert(call.object);
}
self.retain_frame(call.context);
self.calls.insert(call.id, call.clone());
Ok(call)
}
}