use super::*;
use crate::scopes::LaunchStanding;
use crate::supervise::RegistrationSpawn;
pub(crate) use crate::supervise::REGISTRATION_CHANNEL_CLOSED;
use subc_control::ClaimPurpose;
pub(crate) const CLAIM_OPERATION_NOT_ALLOWED: &str = "claim_operation_not_allowed";
pub(super) const REGISTRATION_OPERATIONS: &[&str] = &[
"config.declare",
"config.get",
"config.applied",
MODULE_TO_SUBC_OP_CATALOG_UPDATE,
MODULE_TO_SUBC_OP_CATALOG_UPDATE_ROLE_DECLARATIONS,
ops::ACTIVITY_RECORD,
"claim.mint",
"operator.confirm",
SCOPE_SYNC_OP,
SCOPE_APPLY_OP,
SCOPE_DESCRIBE_OP,
ops::SCOPE_WATCH,
subc_protocol::scope::ROUTE_AUTHORIZE_OP,
"supervisor.live_roots",
"supervisor.agent_commands",
subc_control::SCOPE_SERVED_BY_OP,
];
pub(super) enum ActivityReader {
Legacy,
Module {
module_id: String,
spawn: Arc<RegistrationSpawn>,
},
Refused,
}
impl ControlHandler {
pub(super) fn registration_operation_refusal(
&self,
ctx: &RouteCtx,
frame: &Frame,
op: Option<&str>,
) -> Result<Option<Frame>, RouterError> {
if !op.is_some_and(|op| REGISTRATION_OPERATIONS.contains(&op)) {
return Ok(None);
}
let closed = || {
control_error_frame(
frame,
REGISTRATION_CHANNEL_CLOSED,
"the spawn that owned this registration has exited",
)
.map(Some)
};
if let Some(binding) = self.claims.binding(ctx.connection_id) {
if !binding.registration.launch_ready() {
return closed();
}
if binding.prefix_module_id().is_some() {
return Ok(None);
}
return control_error_frame(
frame,
CLAIM_OPERATION_NOT_ALLOWED,
"a connection bound by a consumer claim has no registration operations",
)
.map(Some);
}
if self
.supervisor
.registration_spawn(ctx.connection_id)
.is_some_and(|spawn| !spawn.launch_ready())
{
return closed();
}
Ok(None)
}
pub(super) fn connection_is_current_launch(
&self,
owner: &str,
connection: ConnectionId,
current_nonce: Option<&str>,
) -> LaunchStanding {
if let Some(spawn) = self.supervisor.registration_spawn(connection) {
if self.supervisor.registration_snapshot_is_current_live(
&self.registry,
owner,
connection,
None,
) {
return LaunchStanding::Current;
}
let serving = spawn.module_id == owner
&& spawn.launch_ready()
&& (!self.supervisor.module_is_gated(owner) || spawn.has_program_binding())
&& self
.registry
.get_module(owner)
.ok()
.flatten()
.is_some_and(|registration| registration.connection_id == connection);
if !serving {
return LaunchStanding::NotCurrent;
}
if self.supervisor.get(owner).is_none() {
return LaunchStanding::NotCurrent;
}
return match self.supervisor.channel_launch_stopped(&spawn) {
Some(true) => LaunchStanding::NotCurrent,
_ => LaunchStanding::Unsettled,
};
}
self.hello_launch_nonces
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.presented(connection, current_nonce)
.into()
}
pub(super) fn confirm_channel_proven(
&self,
connection: ConnectionId,
module_id: &str,
) -> Option<bool> {
self.supervisor
.registration_spawn(connection)
.map(|spawn| spawn.launch_ready() && spawn.module_id == module_id)
}
pub(super) fn activity_reader(&self, connection: ConnectionId) -> ActivityReader {
if let Some(binding) = self.claims.binding(connection) {
if !binding.registration.launch_ready() {
return ActivityReader::Refused;
}
return match &binding.purpose {
ClaimPurpose::SelfConnection => ActivityReader::Module {
module_id: binding.registration.module_id.clone(),
spawn: binding.registration.clone(),
},
ClaimPurpose::Prefix { .. } => match self
.forwarding
.module_id_for_connection(connection)
.ok()
.flatten()
{
Some(module_id) => ActivityReader::Module {
module_id,
spawn: binding.registration.clone(),
},
None => ActivityReader::Refused,
},
_ => ActivityReader::Refused,
};
}
match self.supervisor.registration_spawn(connection) {
Some(spawn) if spawn.launch_ready() => ActivityReader::Module {
module_id: spawn.module_id.clone(),
spawn,
},
Some(_) => ActivityReader::Refused,
None => ActivityReader::Legacy,
}
}
}