use crate::logger::tracing::debug;
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
use crate::Url;
use super::{NotifyListener, Registry, RegistryNotifyListener};
pub const REGISTRY_GROUP_KEY: &str = "registry.group";
#[derive(Debug, Default, Clone)]
pub struct MemoryRegistry {
registries: Arc<RwLock<HashMap<String, String>>>,
}
impl MemoryRegistry {
pub fn new() -> MemoryRegistry {
MemoryRegistry {
registries: Arc::new(RwLock::new(HashMap::new())),
}
}
}
impl Registry for MemoryRegistry {
fn register(&mut self, mut url: Url) -> Result<(), crate::StdError> {
let registry_group = match url.get_param(REGISTRY_GROUP_KEY) {
Some(key) => key,
None => "dubbo".to_string(),
};
let dubbo_path = format!(
"/{}/{}/{}",
registry_group,
url.get_service_name(),
"provider",
);
url.params.insert("anyhost".to_string(), "true".to_string());
let raw_url = url.raw_url_string();
self.registries.write().unwrap().insert(dubbo_path, raw_url);
Ok(())
}
fn unregister(&mut self, url: crate::Url) -> Result<(), crate::StdError> {
let registry_group = match url.get_param(REGISTRY_GROUP_KEY) {
Some(key) => key,
None => "dubbo".to_string(),
};
let dubbo_path = format!(
"/{}/{}/{}",
registry_group,
url.get_service_name(),
"provider",
);
self.registries.write().unwrap().remove(&dubbo_path);
Ok(())
}
fn subscribe(
&self,
url: crate::Url,
listener: RegistryNotifyListener,
) -> Result<(), crate::StdError> {
todo!()
}
fn unsubscribe(
&self,
url: crate::Url,
listener: RegistryNotifyListener,
) -> Result<(), crate::StdError> {
todo!()
}
}
pub struct MemoryNotifyListener {
pub service_instances: Arc<RwLock<HashMap<String, Vec<Url>>>>,
}
impl NotifyListener for MemoryNotifyListener {
fn notify(&self, event: super::ServiceEvent) {
debug!("notify {:?}", event);
let mut map = self.service_instances.write().expect("msg");
match event.action.as_str() {
"ADD" => map.insert(event.key, event.service),
&_ => todo!(),
};
}
fn notify_all(&self, event: super::ServiceEvent) {
todo!()
}
}