use std::time::Duration;
use transport_core::Endpoint;
use super::SimpleDiscovery;
use super::types::DiscoveryState;
impl SimpleDiscovery {
pub fn register_topic(&mut self, name: impl Into<String>) {
let name = name.into();
self.topics.insert(name.clone());
self.topic_state.insert(name, DiscoveryState::default());
}
pub fn register_service(&mut self, name: impl Into<String>) {
let name = name.into();
self.services.insert(name.clone());
self.service_state.insert(name, DiscoveryState::default());
}
pub fn register_mission(&mut self, name: impl Into<String>) {
let name = name.into();
self.missions.insert(name.clone());
self.mission_state.insert(name, DiscoveryState::default());
}
pub fn register_topic_with_ttl(&mut self, name: impl Into<String>, ttl: Duration) {
let name = name.into();
self.topics.insert(name.clone());
self.topic_state.insert(name, DiscoveryState::with_ttl(ttl));
}
pub fn register_service_with_ttl(&mut self, name: impl Into<String>, ttl: Duration) {
let name = name.into();
self.services.insert(name.clone());
self.service_state
.insert(name, DiscoveryState::with_ttl(ttl));
}
pub fn register_mission_with_ttl(&mut self, name: impl Into<String>, ttl: Duration) {
let name = name.into();
self.missions.insert(name.clone());
self.mission_state
.insert(name, DiscoveryState::with_ttl(ttl));
}
pub fn add_labels(&mut self, key: impl Into<String>, labels: Vec<String>) {
self.labels.insert(key.into(), labels);
}
pub fn register_endpoint(&mut self, name: impl Into<String>, endpoint: Endpoint) {
let name = name.into();
self.endpoints.insert(name.clone(), endpoint);
self.endpoint_state.insert(name, DiscoveryState::default());
}
pub fn register_endpoint_with_ttl(
&mut self,
name: impl Into<String>,
endpoint: Endpoint,
ttl: Duration,
) {
let name = name.into();
self.endpoints.insert(name.clone(), endpoint);
self.endpoint_state
.insert(name, DiscoveryState::with_ttl(ttl));
}
pub fn update_endpoint_labels(&mut self, name: &str, labels: Vec<String>) -> bool {
let Some(endpoint) = self.endpoints.get_mut(name) else {
return false;
};
endpoint.labels = labels;
true
}
pub fn unregister_endpoint(&mut self, name: &str) -> bool {
let removed_endpoint = self.endpoints.remove(name).is_some();
let removed_state = self.endpoint_state.remove(name).is_some();
if removed_endpoint || removed_state {
self.remove_labels_if_unused(name);
}
removed_endpoint || removed_state
}
}