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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use {
fidl::endpoints::{DiscoverableProtocolMarker, ProtocolMarker, Proxy},
fuchsia_component::client::connect_to_protocol_at_path,
parking_lot::RwLock,
std::sync::Arc,
};
const SVC_DIR: &str = "/svc";
pub trait Connect {
type Proxy: Proxy;
fn connect(&self) -> Result<Self::Proxy, anyhow::Error>;
}
#[derive(Clone)]
pub struct ServiceReconnector<P>
where
P: DiscoverableProtocolMarker,
<P as ProtocolMarker>::Proxy: Clone,
{
inner: Arc<ServiceReconnectorInner<P>>,
}
impl<P> ServiceReconnector<P>
where
P: DiscoverableProtocolMarker,
<P as ProtocolMarker>::Proxy: Clone,
{
pub fn new() -> Self {
Self::with_service_at(SVC_DIR)
}
pub fn with_service_at(service_directory_path: &str) -> Self {
let service_path = format!("{}/{}", service_directory_path, P::PROTOCOL_NAME);
Self::with_service_at_path(service_path)
}
pub fn with_service_at_path<S: Into<String>>(service_path: S) -> Self {
let service_path = service_path.into();
Self { inner: Arc::new(ServiceReconnectorInner { proxy: RwLock::new(None), service_path }) }
}
}
impl<P> Connect for ServiceReconnector<P>
where
P: DiscoverableProtocolMarker,
<P as ProtocolMarker>::Proxy: Clone,
{
type Proxy = P::Proxy;
fn connect(&self) -> Result<Self::Proxy, anyhow::Error> {
self.inner.connect()
}
}
struct ServiceReconnectorInner<P>
where
P: ProtocolMarker,
<P as ProtocolMarker>::Proxy: Clone,
{
proxy: RwLock<Option<<P as ProtocolMarker>::Proxy>>,
service_path: String,
}
impl<P> Connect for ServiceReconnectorInner<P>
where
P: DiscoverableProtocolMarker,
<P as ProtocolMarker>::Proxy: Clone,
{
type Proxy = P::Proxy;
fn connect(&self) -> Result<Self::Proxy, anyhow::Error> {
if let Some(ref proxy) = *self.proxy.read() {
if !proxy.is_closed() {
return Ok(proxy.clone());
}
}
let mut proxy = self.proxy.write();
if let Some(ref proxy) = *proxy {
if !proxy.is_closed() {
return Ok(proxy.clone());
}
}
let p = connect_to_protocol_at_path::<P>(&self.service_path)?;
*proxy = Some(p.clone());
Ok(p)
}
}
#[cfg(test)]
mod tests {
use {
super::*,
fidl_test_fidl_connector::{TestMarker, TestRequest, TestRequestStream},
fuchsia_async as fasync,
fuchsia_component::server::ServiceFs,
futures::prelude::*,
std::cell::Cell,
};
#[fasync::run_singlethreaded(test)]
async fn test_service_reconnector() {
let ns = fdio::Namespace::installed().expect("installed namespace");
let service_device_path = "/test/service_connector/svc";
let c = ServiceReconnector::<TestMarker>::with_service_at(service_device_path);
let (service_channel, server_end) = fidl::endpoints::create_endpoints();
ns.bind(&service_device_path, service_channel).expect("bind test svc");
let gen = Cell::new(1);
let mut fs = ServiceFs::new_local();
fs.add_fidl_service(move |mut stream: TestRequestStream| {
let current_gen = gen.get();
gen.set(current_gen + 1);
fasync::Task::local(async move {
while let Some(req) = stream.try_next().await.unwrap_or(None) {
match req {
TestRequest::Ping { responder } => {
responder.send(current_gen).expect("patient client");
}
TestRequest::Disconnect { responder } => {
drop(responder);
}
}
}
})
.detach()
})
.serve_connection(server_end)
.expect("serve_connection");
fasync::Task::local(fs.collect()).detach();
let proxy = c.connect().expect("can connect");
assert_eq!(proxy.ping().await.expect("ping"), 1);
let proxy = c.connect().expect("can connect");
assert_eq!(proxy.ping().await.expect("ping"), 1);
proxy.disconnect().await.expect_err("oops");
let proxy = c.connect().expect("can connect");
assert_eq!(proxy.ping().await.expect("ping"), 2);
}
}