//! Request dispatch and daemon state. use tokio::sync::broadcast; use varde_proto::{Envelope, ErrorCode, Event, GlobalStatus, Request, ResponseData}; use crate::config::Config; /// Shared daemon state. Handlers grow with the milestones; anything not /// yet wired returns [`ErrorCode::Unimplemented`] rather than pretending. pub struct Daemon { config: Config, events: broadcast::Sender, } impl Daemon { pub fn new(config: Config) -> Self { // Capacity bounds memory if a subscriber stalls; laggards get a // Lagged error, not unbounded buffering. let (events, _) = broadcast::channel(1024); Daemon { config, events } } pub fn config(&self) -> &Config { &self.config } /// New receiver for the daemon event stream. pub fn subscribe_events(&self) -> broadcast::Receiver { self.events.subscribe() } /// Handle one API request. Infallible at this level: all failures are /// mapped to structured error envelopes. pub async fn handle(&self, request: Request) -> Envelope { match request { Request::Status { hash: None } => Envelope::ok(ResponseData::Status(GlobalStatus { version: env!("CARGO_PKG_VERSION").to_string(), protocol: varde_proto::PROTOCOL_VERSION, node_id: None, store_path: self.config.store_dir.display().to_string(), pins: 0, connected_peers: 0, })), Request::List {} => Envelope::ok(ResponseData::Pins { pins: Vec::new() }), Request::Subscribe {} => Envelope::ok(ResponseData::Done {}), other => Envelope::err( ErrorCode::Unimplemented, format!("not implemented yet: {other:?}"), ), } } }