perspective_client/session.rs
1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors. ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use std::error::Error as StdError;
14use std::sync::Arc;
15
16use futures::Future;
17use prost::Message;
18
19use crate::proto::request::ClientReq;
20use crate::proto::{Request, Response};
21use crate::{Client, ClientError, asyncfn};
22#[cfg(doc)]
23use crate::{Table, View};
24
25/// The server-side representation of a connection to a [`Client`].
26///
27/// For each [`Client`] that wants to connect to a `perspective_server::Server`,
28/// a dedicated [`Session`] must be created. The [`Session`] handles routing
29/// messages emitted by the `perspective_server::Server`ve_server::Server`, as
30/// well as owning any resources the [`Client`] may request.
31pub trait Session<E> {
32 /// Handle an incoming request from the [`Client`]. Calling
33 /// [`Session::handle_request`] will result in the `send_response` parameter
34 /// which was used to construct this [`Session`] to fire one or more times.
35 ///
36 /// # Arguments
37 ///
38 /// - `request` An incoming request message, generated from a
39 /// [`Client::new`]'s `send_request` handler (which may-or-may-not be
40 /// local).
41 fn handle_request(&self, request: &[u8]) -> impl Future<Output = Result<(), E>>;
42
43 /// Close this [`Session`], cleaning up any callbacks (e.g. arguments
44 /// provided to [`Session::handle_request`]) and resources (e.g. views
45 /// returned by a call to [`Table::view`]).
46 ///
47 /// Dropping a [`Session`] outside of the context of [`Session::close`]
48 /// will cause a [`tracing`] error-level log to be emitted, but won't fail.
49 /// They will, however, leak.
50 fn close(self) -> impl Future<Output = ()>;
51}
52
53type ProxyCallbackError = Box<dyn StdError + Send + Sync>;
54type ProxyCallback = Arc<dyn Fn(&[u8]) -> Result<(), ProxyCallbackError> + Send + Sync>;
55
56/// A [`Session`] implementation which tunnels through another [`Client`].
57#[derive(Clone)]
58pub struct ProxySession {
59 parent: Client,
60 callback: ProxyCallback,
61}
62
63impl ProxySession {
64 pub fn new(
65 client: Client,
66 send_response: impl Fn(&[u8]) -> Result<(), ProxyCallbackError> + Send + Sync + 'static,
67 ) -> Self {
68 ProxySession {
69 parent: client,
70 callback: Arc::new(send_response),
71 }
72 }
73}
74
75fn encode(response: Response, callback: ProxyCallback) -> Result<(), ClientError> {
76 let mut enc = vec![];
77 response.encode(&mut enc)?;
78 callback(&enc).map_err(|x| ClientError::Unknown(x.to_string()))?;
79 Ok(())
80}
81
82impl Session<ClientError> for ProxySession {
83 async fn handle_request(&self, request: &[u8]) -> Result<(), ClientError> {
84 let req = Request::decode(request)?;
85 let callback = self.callback.clone();
86 match req.client_req.as_ref() {
87 Some(ClientReq::ViewOnUpdateReq(_)) => {
88 let on_update =
89 asyncfn!(callback, async move |response| encode(response, callback));
90 self.parent.subscribe(&req, on_update).await?
91 },
92 Some(_) => {
93 let on_update = move |response| encode(response, callback);
94 self.parent
95 .subscribe_once(&req, Box::new(on_update))
96 .await?
97 },
98 None => {
99 return Err(ClientError::Internal(
100 "ProxySession::handle_request: invalid request".to_string(),
101 ));
102 },
103 };
104
105 Ok(())
106 }
107
108 async fn close(self) {}
109}