//! K2101–K2105 PID1 service readiness. A live PID is never readiness: only a //! reply from the exact launched process generation, echoing the issued nonce //! before the deadline and naming a registry endpoint owned by that same //! process, publishes Ready. A missed deadline becomes an explicit Degraded //! state that keeps the last refusal visible; it is never converted into Ready. //! Reply sender and endpoint owner must come from the trusted process/registry //! boundary, never from request bytes. pub const VERSION: u64 = 1; #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error { Configuration=1, State, Stale, Nonce, Version, NoEndpoint, EndpointOwner, Late, Clock, NotReady } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Phase { Empty=0, Launching, Ready, Degraded, Stopped } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Identity { pub pid:u64, pub generation:u64 } /// `handle == 0` is the registry answer "no endpoint published". #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Endpoint { pub handle:u64, pub owner:Identity } /// One service reply. Not Clone: a refused reply is returned to its caller. #[repr(C)] #[derive(Debug,PartialEq,Eq)] pub struct Reply { pub sender:Identity, pub nonce:u64, pub version:u64, pub endpoint:Endpoint } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Status { pub phase:Phase, pub identity:Identity, pub endpoint:Endpoint, pub deadline:u64, pub ready_at:u64, pub degraded_at:u64, pub refusals:u64, pub last_refusal:u64 } #[repr(C)] #[derive(Debug)] pub struct Slot { service:u64, phase:Phase, identity:Identity, nonce:u64, deadline:u64, last:u64, endpoint:Endpoint, ready_at:u64, degraded_at:u64, refusals:u64, last_refusal:u64 } const NONE:Identity=Identity{pid:0,generation:0}; const NO_ENDPOINT:Endpoint=Endpoint{handle:0,owner:NONE}; impl Slot { pub const fn new(service:u64)->Result{ if service==0{return Err(Error::Configuration)} Ok(Self{service,phase:Phase::Empty,identity:NONE,nonce:0,deadline:0,last:0,endpoint:NO_ENDPOINT,ready_at:0,degraded_at:0,refusals:0,last_refusal:0}) } pub const fn service(&self)->u64 { self.service } pub fn status(&self)->Status { Status{phase:self.phase,identity:self.identity,endpoint:self.endpoint,deadline:self.deadline, ready_at:self.ready_at,degraded_at:self.degraded_at,refusals:self.refusals,last_refusal:self.last_refusal} } /// `identity` is the process-table record of the started service. Starting /// it publishes nothing to clients; only `handshake` can. pub fn launch(&mut self,identity:Identity,nonce:u64,now:u64,timeout:u64)->Result{ if self.phase!=Phase::Empty&&self.phase!=Phase::Stopped{return Err(Error::State)} if nowResult{ let fail=if self.phase!=Phase::Launching{Some(Error::State)}else if now=self.deadline{Some(Error::Late)} else if reply.endpoint.handle==0{Some(Error::NoEndpoint)}else if reply.endpoint.owner!=self.identity{Some(Error::EndpointOwner)}else{None}; if let Some(error)=fail{ if self.phase==Phase::Launching{self.refusals=self.refusals.saturating_add(1);self.last_refusal=error as u64} return Err((error,reply)) } self.last=now;self.phase=Phase::Ready;self.endpoint=reply.endpoint;self.ready_at=now;Ok(self.status()) } /// A deadline turns an unanswered launch into visible degradation. pub fn poll(&mut self,now:u64)->Result{ if now=self.deadline{self.phase=Phase::Degraded;self.degraded_at=now} Ok(self.status()) } /// Clients receive an endpoint only from a Ready slot. pub fn require_ready(&self)->Result{ if self.phase==Phase::Ready{Ok(self.endpoint)}else{Err(Error::NotReady)} } /// Only the launched generation is stopped; the endpoint is withdrawn. pub fn stop(&mut self,identity:Identity)->Result{ if self.phase==Phase::Empty||self.phase==Phase::Stopped{return Err(Error::State)} if identity!=self.identity{return Err(Error::Stale)} self.phase=Phase::Stopped;self.endpoint=NO_ENDPOINT;Ok(self.status()) } }