hive-c0re: in-memory broker + per-agent sockets + coordinator state
This commit is contained in:
parent
4545c08908
commit
d79b5a39a1
6 changed files with 220 additions and 9 deletions
56
hive-c0re/src/coordinator.rs
Normal file
56
hive-c0re/src/coordinator.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
//! Runtime state shared between the host admin socket and the per-agent
|
||||
//! sockets: the broker plus a map of `name -> AgentSocket`.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use crate::agent_server::{self, AgentSocket};
|
||||
use crate::broker::Broker;
|
||||
|
||||
const AGENT_RUNTIME_ROOT: &str = "/run/hyperhive/agents";
|
||||
|
||||
pub struct Coordinator {
|
||||
pub broker: Arc<Broker>,
|
||||
agents: Mutex<HashMap<String, AgentSocket>>,
|
||||
}
|
||||
|
||||
impl Coordinator {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
broker: Arc::new(Broker::new()),
|
||||
agents: Mutex::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn register_agent(&self, name: &str) -> Result<PathBuf> {
|
||||
let agent_dir = Self::agent_dir(name);
|
||||
std::fs::create_dir_all(&agent_dir)
|
||||
.with_context(|| format!("create agent dir {}", agent_dir.display()))?;
|
||||
let socket_path = Self::socket_path(name);
|
||||
let socket =
|
||||
agent_server::start(name.to_owned(), socket_path, self.broker.clone()).await?;
|
||||
self.agents
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(name.to_owned(), socket);
|
||||
Ok(agent_dir)
|
||||
}
|
||||
|
||||
pub fn unregister_agent(&self, name: &str) {
|
||||
if let Some(socket) = self.agents.lock().unwrap().remove(name) {
|
||||
socket.handle.abort();
|
||||
let _ = std::fs::remove_file(&socket.path);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn agent_dir(name: &str) -> PathBuf {
|
||||
PathBuf::from(format!("{AGENT_RUNTIME_ROOT}/{name}"))
|
||||
}
|
||||
|
||||
pub fn socket_path(name: &str) -> PathBuf {
|
||||
Self::agent_dir(name).join("mcp.sock")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue