Skip to content

Commit

Permalink
Make main function asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Jun 11, 2019
1 parent e0f359c commit 0a5c7db
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jsonrpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2018"
[dependencies]
futures-boxed = { path = "../futures_boxed" }
futures-preview = { version = "0.3.0-alpha.15", features = ["compat"] }
runtime = "0.3.0-alpha.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_repr = "0.1"
16 changes: 4 additions & 12 deletions jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ pub use self::{
types::*,
};

use futures::executor::ThreadPool;
use futures::lock::Mutex;
use futures::prelude::*;
use futures::task::*;
use std::sync::Arc;

pub struct MessageHandler<S, C, I, O> {
pub server: Arc<S>,
pub client: Arc<C>,
pub input: I,
pub output: Arc<Mutex<O>>,
pub pool: ThreadPool,
}

impl<S, C, I, O> MessageHandler<S, C, I, O>
Expand All @@ -43,25 +40,20 @@ where
Ok(Message::Request(request)) => {
let server = Arc::clone(&self.server);
let output = Arc::clone(&self.output);
let handler = async move {
runtime::spawn(async move {
let response = server.handle_request(request).await;
let json = serde_json::to_string(&response).unwrap();
let mut output = output.lock().await;
output.send(json).await.unwrap();
server.execute_actions().await;
};

self.pool.spawn(handler).unwrap();
});
}
Ok(Message::Notification(notification)) => {
self.server.handle_notification(notification);

let server = Arc::clone(&self.server);
let handler = async move {
runtime::spawn(async move {
server.execute_actions().await;
};

self.pool.spawn(handler).unwrap();
});
}
Ok(Message::Response(response)) => {
self.client.handle(response).await;
Expand Down
12 changes: 2 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

use clap::*;
use futures::compat::*;
use futures::executor::*;
use futures::lock::Mutex;
use futures::prelude::*;
use jsonrpc::MessageHandler;
use std::sync::Arc;
use stderrlog::{ColorChoice, Timestamp};
Expand All @@ -15,7 +13,8 @@ use tokio::codec::FramedRead;
use tokio_codec::FramedWrite;
use tokio_stdin_stdout;

fn main() {
#[runtime::main]
async fn main() {
let matches = app_from_crate!()
.author("")
.arg(
Expand All @@ -41,12 +40,6 @@ fn main() {
.init()
.unwrap();

let mut pool = ThreadPool::new().expect("Failed to create the thread pool");
let task = run(pool.clone());
pool.run(task.unit_error()).unwrap();
}

async fn run(pool: ThreadPool) {
let stdin = tokio_stdin_stdout::stdin(0);
let stdout = tokio_stdin_stdout::stdout(0);
let input = FramedRead::new(stdin, LspCodec).compat();
Expand All @@ -58,7 +51,6 @@ async fn run(pool: ThreadPool) {
client,
input,
output,
pool,
};

handler.listen().await;
Expand Down

0 comments on commit 0a5c7db

Please sign in to comment.