1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::{
collections::HashMap,
fmt::Debug,
str::FromStr,
sync::{Arc, RwLock},
};
use crate::{
common::url::Url,
invocation::{Invocation, RpcInvocation},
registry::{memory_registry::MemoryNotifyListener, BoxRegistry, RegistryWrapper},
};
pub trait Directory: Debug + DirectoryClone {
fn list(&self, invocation: Arc<RpcInvocation>) -> Vec<Url>;
}
pub trait DirectoryClone {
fn clone_box(&self) -> Box<dyn Directory>;
}
impl<T> DirectoryClone for T
where
T: 'static + Directory + Clone,
{
fn clone_box(&self) -> Box<dyn Directory> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn Directory> {
fn clone(&self) -> Box<dyn Directory> {
self.clone_box()
}
}
#[derive(Debug)]
pub struct StaticDirectory {
uri: http::Uri,
}
impl StaticDirectory {
pub fn new(host: &str) -> StaticDirectory {
let uri = match http::Uri::from_str(host) {
Ok(v) => v,
Err(err) => {
tracing::error!("http uri parse error: {}, host: {}", err, host);
panic!("http uri parse error: {}, host: {}", err, host)
}
};
StaticDirectory { uri: uri }
}
pub fn from_uri(uri: &http::Uri) -> StaticDirectory {
StaticDirectory { uri: uri.clone() }
}
}
impl Directory for StaticDirectory {
fn list(&self, invocation: Arc<RpcInvocation>) -> Vec<Url> {
let url = Url::from_url(&format!(
"tri://{}:{}/{}",
self.uri.host().unwrap(),
self.uri.port().unwrap(),
invocation.get_target_service_unique_name(),
))
.unwrap();
vec![url]
}
}
impl DirectoryClone for StaticDirectory {
fn clone_box(&self) -> Box<dyn Directory> {
Box::new(StaticDirectory {
uri: self.uri.clone(),
})
}
}
#[derive(Debug)]
pub struct RegistryDirectory {
registry: RegistryWrapper,
service_instances: Arc<RwLock<HashMap<String, Vec<Url>>>>,
}
impl RegistryDirectory {
pub fn new(registry: BoxRegistry) -> RegistryDirectory {
RegistryDirectory {
registry: RegistryWrapper {
registry: Some(registry),
},
service_instances: Arc::new(RwLock::new(HashMap::new())),
}
}
}
impl DirectoryClone for RegistryDirectory {
fn clone_box(&self) -> Box<dyn Directory> {
todo!()
}
}
impl Directory for RegistryDirectory {
fn list(&self, invocation: Arc<RpcInvocation>) -> Vec<Url> {
let service_name = invocation.get_target_service_unique_name();
let url = Url::from_url(&format!(
"triple://{}:{}/{}",
"127.0.0.1", "8888", service_name
))
.unwrap();
self.registry
.registry
.as_ref()
.expect("msg")
.subscribe(
url,
MemoryNotifyListener {
service_instances: Arc::clone(&self.service_instances),
},
)
.expect("subscribe");
let map = self
.service_instances
.read()
.expect("service_instances.read");
let binding = Vec::new();
let url_vec = map.get(&service_name).unwrap_or(&binding);
url_vec.to_vec()
}
}