use crate::restream_session::{NodeSession, NodeUplink, UpstreamKeys};
use hatchery_core::ChannelKind;
use hatchery_enroll::NodeIdentity;
use serde_json::Value;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
pub const NODE_CALL_CHANNEL: u32 = 2;
const HANDSHAKE: Duration = Duration::from_secs(8);
const CALL_TIMEOUT: Duration = Duration::from_secs(15);
const OPEN_BACKOFF: Duration = Duration::from_secs(60);
#[derive(Default)]
struct Link {
uplink: Option<Arc<NodeUplink>>,
session: Option<NodeSession>,
open_not_before: Option<Instant>,
}
#[derive(Clone)]
pub struct NodeCalls {
link: Arc<Mutex<Link>>,
node_id: String,
keys: Option<UpstreamKeys>,
legacy: Option<(SocketAddr, Arc<NodeIdentity>)>,
}
#[derive(Debug)]
pub enum CallError {
Refused,
Unavailable(String),
}
impl NodeCalls {
pub fn new(node_id: String, keys: Option<UpstreamKeys>, legacy: Option<(SocketAddr, Arc<NodeIdentity>)>) -> Self {
Self { link: Arc::default(), node_id, keys, legacy }
}
pub async fn set_uplink(&self, uplink: Option<Arc<NodeUplink>>) {
let mut l = self.link.lock().await;
l.uplink = uplink;
l.session = None;
l.open_not_before = None;
}
pub async fn call(&self, op: &str, payload: &Value) -> Result<Value, CallError> {
if let Some(keys) = self.keys {
let mut l = self.link.lock().await;
if let Some(uplink) = l.uplink.clone() {
if l.session.is_none() {
let gated = l.open_not_before.is_some_and(|t| Instant::now() < t);
if gated {
} else {
match uplink.open(&self.node_id, NODE_CALL_CHANNEL, ChannelKind::M4a, keys, HANDSHAKE).await {
Ok(s) => {
tracing::info!("node calls over the uplink: channel open");
l.session = Some(s);
l.open_not_before = None;
}
Err(e) => {
tracing::warn!(error = %e, "node-call channel open failed; backing off {}s", OPEN_BACKOFF.as_secs());
l.open_not_before = Some(Instant::now() + OPEN_BACKOFF);
}
}
}
}
if let Some(session) = l.session.as_mut() {
return match session.call(op, payload, CALL_TIMEOUT).await {
Ok(v) => Ok(v),
Err(e) if e.starts_with("403") => Err(CallError::Refused),
Err(e) if e.chars().take(3).all(|c| c.is_ascii_digit()) && e.contains(':') => {
Err(CallError::Unavailable(e))
}
Err(e) => {
l.session = None;
l.open_not_before = Some(Instant::now() + OPEN_BACKOFF);
Err(CallError::Unavailable(e))
}
};
}
}
}
match &self.legacy {
Some((c2, identity)) => match crate::enroll_client::node_call(*c2, identity, &self.node_id, op, payload).await {
Ok(v) => Ok(v),
Err(crate::enroll_client::EnrollClientError::Refused) => Err(CallError::Refused),
Err(e) => Err(CallError::Unavailable(format!("{e:?}"))),
},
None => Err(CallError::Unavailable("no uplink".into())),
}
}
}