Skip to content

Commit

Permalink
Add session ext (#53)
Browse files Browse the repository at this point in the history
* Add `SessionExt` trait for friendlier methods
* Create `Mailbox` and internal `PostOffice` to manage responses to requests
* Refactor `Session` to use a new `SessionChannel` underneath
* Refactor `Response` to always include an origin_id field instead of being optional
* Update `ProcStdout`, `ProcStderr`, and `ProcDone` to include origin id
* Replace `verbose` option with `log-level`
  • Loading branch information
chipsenkbeil authored Sep 19, 2021
1 parent c45aea8 commit c4d1011
Show file tree
Hide file tree
Showing 19 changed files with 1,274 additions and 704 deletions.
8 changes: 4 additions & 4 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ servers that operate on remote machines and clients that talk to them.

- Asynchronous in nature, powered by [`tokio`](https://tokio.rs/)
- Data is serialized to send across the wire via [`CBOR`](https://cbor.io/)
- Encryption & authentication are handled via [`orion`](https://crates.io/crates/orion)
- [XChaCha20Poly1305](https://cryptopp.com/wiki/XChaCha20Poly1305) for an authenticated encryption scheme
- [BLAKE2b-256](https://www.blake2.net/) in keyed mode for a second authentication
- [Elliptic Curve Diffie-Hellman](https://en.wikipedia.org/wiki/Elliptic-curve_Diffie%E2%80%93Hellman) (ECDH) for key exchange
- Encryption & authentication are handled via
[XChaCha20Poly1305](https://tools.ietf.org/html/rfc8439) for an authenticated
encryption scheme via
[RustCrypto/ChaCha20Poly1305](https://github.com/RustCrypto/AEADs/tree/master/chacha20poly1305)

## Installation

Expand Down
62 changes: 29 additions & 33 deletions core/src/client/lsp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::{RemoteProcess, RemoteProcessError, RemoteStderr, RemoteStdin, RemoteStdout};
use crate::{
client::Session,
net::{Codec, DataStream},
};
use crate::client::Session;
use futures::stream::{Stream, StreamExt};
use std::{
fmt::Write,
Expand All @@ -26,16 +23,12 @@ pub struct RemoteLspProcess {
impl RemoteLspProcess {
/// Spawns the specified process on the remote machine using the given session, treating
/// the process like an LSP server
pub async fn spawn<T, U>(
tenant: String,
session: Session<T, U>,
cmd: String,
pub async fn spawn(
tenant: impl Into<String>,
session: &mut Session,
cmd: impl Into<String>,
args: Vec<String>,
) -> Result<Self, RemoteProcessError>
where
T: DataStream + 'static,
U: Codec + Send + 'static,
{
) -> Result<Self, RemoteProcessError> {
let mut inner = RemoteProcess::spawn(tenant, session, cmd, args).await?;
let stdin = inner.stdin.take().map(RemoteLspStdin::new);
let stdout = inner.stdout.take().map(RemoteLspStdout::new);
Expand Down Expand Up @@ -266,21 +259,24 @@ mod tests {
// Configures an lsp process with a means to send & receive data from outside
async fn spawn_lsp_process() -> (Transport<InmemoryStream, PlainCodec>, RemoteLspProcess) {
let (mut t1, t2) = Transport::make_pair();
let session = Session::initialize(t2).unwrap();
let spawn_task = tokio::spawn(RemoteLspProcess::spawn(
String::from("test-tenant"),
session,
String::from("cmd"),
vec![String::from("arg")],
));
let mut session = Session::initialize(t2).unwrap();
let spawn_task = tokio::spawn(async move {
RemoteLspProcess::spawn(
String::from("test-tenant"),
&mut session,
String::from("cmd"),
vec![String::from("arg")],
)
.await
});

// Wait until we get the request from the session
let req = t1.receive::<Request>().await.unwrap().unwrap();

// Send back a response through the session
t1.send(Response::new(
"test-tenant",
Some(req.id),
req.id,
vec![ResponseData::ProcStart { id: rand::random() }],
))
.await
Expand Down Expand Up @@ -524,7 +520,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStdout {
id: proc.id(),
data: make_lsp_msg(serde_json::json!({
Expand Down Expand Up @@ -561,7 +557,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStdout {
id: proc.id(),
data: msg_a.to_string(),
Expand All @@ -580,7 +576,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStdout {
id: proc.id(),
data: msg_b.to_string(),
Expand Down Expand Up @@ -615,7 +611,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStdout {
id: proc.id(),
data: format!("{}{}", msg, extra),
Expand Down Expand Up @@ -659,7 +655,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStdout {
id: proc.id(),
data: format!("{}{}", msg_1, msg_2),
Expand Down Expand Up @@ -694,7 +690,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStdout {
id: proc.id(),
data: make_lsp_msg(serde_json::json!({
Expand Down Expand Up @@ -725,7 +721,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStderr {
id: proc.id(),
data: make_lsp_msg(serde_json::json!({
Expand Down Expand Up @@ -762,7 +758,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStderr {
id: proc.id(),
data: msg_a.to_string(),
Expand All @@ -781,7 +777,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStderr {
id: proc.id(),
data: msg_b.to_string(),
Expand Down Expand Up @@ -816,7 +812,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStderr {
id: proc.id(),
data: format!("{}{}", msg, extra),
Expand Down Expand Up @@ -860,7 +856,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStderr {
id: proc.id(),
data: format!("{}{}", msg_1, msg_2),
Expand Down Expand Up @@ -895,7 +891,7 @@ mod tests {
transport
.send(Response::new(
"test-tenant",
None,
proc.origin_id,
vec![ResponseData::ProcStderr {
id: proc.id(),
data: make_lsp_msg(serde_json::json!({
Expand Down
Loading

0 comments on commit c4d1011

Please sign in to comment.