use std::collections::HashMap;
use super::{
triple_exporter::TripleExporter, triple_invoker::TripleInvoker, triple_server::TripleServer,
};
use crate::{
params::registry_param::InterfaceName,
protocol::{BoxExporter, Protocol},
url::UrlParam,
Url,
};
use async_trait::async_trait;
#[derive(Clone)]
pub struct TripleProtocol {
servers: HashMap<String, TripleServer>,
}
impl Default for TripleProtocol {
fn default() -> Self {
Self::new()
}
}
impl TripleProtocol {
pub fn new() -> Self {
TripleProtocol {
servers: HashMap::new(),
}
}
pub fn get_server(&self, url: Url) -> Option<TripleServer> {
let interface_name = url.query::<InterfaceName>().unwrap();
self.servers
.get(interface_name.value().as_str())
.map(|data| data.to_owned())
}
}
#[async_trait]
impl Protocol for TripleProtocol {
type Invoker = TripleInvoker;
fn destroy(&self) {
todo!()
}
async fn export(mut self, url: Url) -> BoxExporter {
let server = TripleServer::new();
let interface_name = url.query::<InterfaceName>().unwrap();
let interface_name = interface_name.value();
self.servers.insert(interface_name, server.clone());
server.serve(url).await;
Box::new(TripleExporter::new())
}
async fn refer(self, _url: Url) -> Self::Invoker {
todo!()
}
}