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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use core::{marker::PhantomData, pin::pin};
use serde::{Serialize, de::DeserializeOwned};
use crate::{
Address, AnyAllAppendix, DEFAULT_TTL, FrameKind, Header, HeaderSeq, Key, nash::NameHash,
socket::HeaderMessage, traits::Endpoint,
};
use super::{NetStackHandle, NetStackSendError, ReqRespError};
/// A proxy type usable for creating helper services
#[derive(Clone)]
pub struct Endpoints<NS: NetStackHandle> {
pub(super) inner: NS,
}
pub struct EndpointClient<'a, E: Endpoint, NS: NetStackHandle> {
inner: NS,
name: Option<&'a str>,
address: Address,
_pd: PhantomData<fn() -> E>,
}
impl<E, NS> EndpointClient<'_, E, NS>
where
E: Endpoint,
NS: NetStackHandle,
{
pub async fn request(&self, req: &E::Request) -> Result<E::Response, ReqRespError>
where
E: Endpoint,
E::Request: Serialize + Clone + DeserializeOwned + 'static,
E::Response: Serialize + Clone + DeserializeOwned + 'static,
{
let ep = Endpoints {
inner: self.inner.clone(),
};
ep.request::<E>(self.address, req, self.name).await
}
}
impl<NS: NetStackHandle> Endpoints<NS> {
pub fn client<E: Endpoint>(
self,
address: Address,
name: Option<&str>,
) -> EndpointClient<'_, E, NS> {
EndpointClient {
inner: self.inner,
_pd: PhantomData,
name,
address,
}
}
/// Perform an [`Endpoint`] Request, and await Response.
///
/// ## Example
///
/// ```rust
/// # use mutex::raw_impls::cs::CriticalSectionRawMutex as CSRMutex;
/// # use ergot::NetStack;
/// # use ergot::interface_manager::profiles::null::Null;
/// use ergot::Address;
/// // Define an example endpoint
/// ergot::endpoint!(Example, u32, i32, "pathho");
///
/// static STACK: NetStack<CSRMutex, Null> = NetStack::new();
///
/// #[tokio::main]
/// async fn main() {
/// // (not shown: starting an `Example` service...)
/// # let jhdl = tokio::task::spawn(async {
/// # println!("Serve!");
/// # let srv = STACK.endpoints().bounded_server::<Example, 16>(None);
/// # let srv = core::pin::pin!(srv);
/// # let mut hdl = srv.attach();
/// # hdl.serve(async |p| *p as i32).await.unwrap();
/// # println!("Served!");
/// # });
/// # // TODO: let the server attach first
/// # tokio::task::yield_now().await;
/// # tokio::time::sleep(core::time::Duration::from_millis(50)).await;
/// // Make a ping request to local
/// let res = STACK.endpoints().request::<Example>(
/// Address::unknown(),
/// &42u32,
/// None,
/// ).await;
/// assert_eq!(res, Ok(42i32));
/// # jhdl.await.unwrap();
/// }
/// ```
pub async fn request<E>(
self,
dst: Address,
req: &E::Request,
name: Option<&str>,
) -> Result<E::Response, ReqRespError>
where
E: Endpoint,
E::Request: Serialize + Clone + DeserializeOwned + 'static,
E::Response: Serialize + Clone + DeserializeOwned + 'static,
{
let resp = self.request_full::<E>(dst, req, name).await?;
Ok(resp.t)
}
/// Same as [`Self::request`], but also returns the full message with header
pub async fn request_full<E>(
self,
dst: Address,
req: &E::Request,
name: Option<&str>,
) -> Result<HeaderMessage<E::Response>, ReqRespError>
where
E: Endpoint,
E::Request: Serialize + Clone + DeserializeOwned + 'static,
E::Response: Serialize + Clone + DeserializeOwned + 'static,
{
// Response doesn't need a name because we will reply back.
//
// We can also use a "single"/oneshot response because we know
// this request will get exactly one response.
let stack = self.inner.stack();
let resp_sock = self.clone().single_client::<E>();
let resp_sock = pin!(resp_sock);
let mut resp_hdl = resp_sock.attach();
// If the destination is wildcard, include the any_all appendix to the
// header
let any_all = match dst.port_id {
0 => Some(AnyAllAppendix {
key: Key(E::REQ_KEY.to_bytes()),
nash: name.map(NameHash::new),
}),
255 => {
return Err(ReqRespError::NoBroadcast);
}
_ => None,
};
let hdr = Header {
src: Address {
network_id: 0,
node_id: 0,
port_id: resp_hdl.port(),
},
dst,
any_all,
seq_no: None,
kind: FrameKind::ENDPOINT_REQ,
ttl: DEFAULT_TTL,
};
stack.send_ty(&hdr, req).map_err(ReqRespError::Local)?;
// TODO: assert seq nos match somewhere? do we NEED seq nos if we have
// port ids now?
let resp = resp_hdl.recv().await;
match resp {
Ok(msg) => Ok(msg),
Err(e) => Err(ReqRespError::Remote(e.t)),
}
}
/// Send an endpoint response. Useful if you used `recv_manual()` and need to make a manual
/// response.
pub fn respond_owned<E>(
self,
req_hdr: &HeaderSeq,
resp: &E::Response,
) -> Result<(), NetStackSendError>
where
E: Endpoint,
E::Response: Serialize + Clone + 'static,
{
// NOTE: We swap src/dst, AND we go from req -> resp (both in kind and key)
let hdr: Header = Header {
src: req_hdr.dst,
dst: req_hdr.src,
any_all: None,
seq_no: Some(req_hdr.seq_no),
kind: FrameKind::ENDPOINT_RESP,
ttl: DEFAULT_TTL,
};
self.inner.stack().send_ty::<E::Response>(&hdr, resp)
}
pub fn single_client<E: Endpoint>(self) -> crate::socket::endpoint::single::Client<E, NS>
where
E::Request: Serialize + DeserializeOwned + Clone,
E::Response: Serialize + DeserializeOwned + Clone,
{
crate::socket::endpoint::single::Client::new(self.inner, None)
}
pub fn single_server<E: Endpoint>(
self,
name: Option<&str>,
) -> crate::socket::endpoint::single::Server<E, NS>
where
E::Request: Serialize + DeserializeOwned + Clone,
E::Response: Serialize + DeserializeOwned + Clone,
{
crate::socket::endpoint::single::Server::new(self.inner, name)
}
pub fn bounded_server<E: Endpoint, const N: usize>(
self,
name: Option<&str>,
) -> crate::socket::endpoint::stack_vec::Server<E, NS, N>
where
E::Request: Serialize + DeserializeOwned + Clone,
E::Response: Serialize + DeserializeOwned + Clone,
{
crate::socket::endpoint::stack_vec::Server::new(self.inner, name)
}
#[cfg(feature = "std")]
pub fn heap_bounded_server<E: Endpoint>(
self,
bound: usize,
name: Option<&str>,
) -> crate::socket::endpoint::std_bounded::Server<E, NS>
where
E::Request: Serialize + DeserializeOwned + Clone,
E::Response: Serialize + DeserializeOwned + Clone,
{
crate::socket::endpoint::std_bounded::Server::new(self.inner, bound, name)
}
}