Skip to content

Commit

Permalink
JSON-RPC stdio server
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Oct 25, 2022
1 parent a73fbf7 commit 364902a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ tokio = { version = "1", features = ["parking_lot", "rt-multi-thread", "macros"]
members = [
"deltachat-ffi",
"deltachat_derive",
"deltachat-jsonrpc"
"deltachat-jsonrpc",
"deltachat-server"
]

[[example]]
Expand Down
26 changes: 26 additions & 0 deletions deltachat-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "deltachat-server"
version = "1.98.0"
description = "DeltaChat JSON-RPC server"
authors = ["Delta Chat Developers (ML) <delta@codespeak.net>"]
edition = "2021"
readme = "README.md"
license = "MPL-2.0"

keywords = ["deltachat", "chat", "openpgp", "email", "encryption"]
categories = ["cryptography", "std", "email"]

[[bin]]
name = "deltachat-server"

[dependencies]
deltachat-jsonrpc = { path = "../deltachat-jsonrpc" }

anyhow = "1"
env_logger = { version = "0.9.1" }
futures-lite = "1.12.0"
log = "0.4"
serde_json = "1.0.85"
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.21.2", features = ["io-std"] }
yerpc = { version = "^0.3.1", features = ["anyhow_expose"] }
67 changes: 67 additions & 0 deletions deltachat-server/src/bin/deltachat-server/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use anyhow::Result;

use std::path::PathBuf;
use yerpc::{RpcClient, RpcSession};

use deltachat_jsonrpc::api::events::event_to_json_rpc_notification;
use deltachat_jsonrpc::api::{Accounts, CommandApi};

use futures_lite::stream::StreamExt;
use std::sync::Arc;
use tokio::io::{self, AsyncBufReadExt, BufReader};
use tokio::sync::RwLock;
use tokio::task::JoinHandle;

#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();

let path = std::env::var("DC_ACCOUNTS_PATH").unwrap_or_else(|_| "./accounts".to_string());
log::info!("Starting with accounts directory `{path}`.");
let accounts = Arc::new(RwLock::new(
Accounts::new(PathBuf::from(&path)).await.unwrap(),
));

log::info!("Creating JSON-RPC API.");
let state = CommandApi::from_arc(accounts.clone());

let (client, mut out_receiver) = RpcClient::new();
let session = RpcSession::new(client.clone(), state);

// Events task converts core events to JSON-RPC notifications.
let events_task: JoinHandle<Result<()>> = tokio::spawn(async move {
let events = accounts.read().await.get_event_emitter();
accounts.read().await.start_io().await;
while let Some(event) = events.recv().await {
let event = event_to_json_rpc_notification(event);
client.send_notification("event", Some(event)).await?;
}
Ok(())
});

// Send task prints JSON responses to stdout.
let send_task: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
while let Some(message) = out_receiver.next().await {
let message = serde_json::to_string(&message)?;
log::trace!("RPC send {}", message);
println!("{}", message);
}
Ok(())
});

let recv_task: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
let stdin = io::stdin();
let mut lines = BufReader::new(stdin).lines();
while let Some(message) = lines.next_line().await? {
log::trace!("RPC recv {}", message);
session.handle_incoming(&message).await;
}
Ok(())
});

events_task.await??;
send_task.await??;
recv_task.await??;

Ok(())
}
7 changes: 6 additions & 1 deletion scripts/set_core_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ def main():
parser = ArgumentParser(prog="set_core_version")
parser.add_argument("newversion")

toml_list = ["Cargo.toml", "deltachat-ffi/Cargo.toml", "deltachat-jsonrpc/Cargo.toml"]
json_list = ["package.json", "deltachat-jsonrpc/typescript/package.json"]
toml_list = [
"Cargo.toml",
"deltachat-ffi/Cargo.toml",
"deltachat-jsonrpc/Cargo.toml",
"deltachat-server/Cargo.toml",
]
try:
opts = parser.parse_args()
except SystemExit:
Expand Down

0 comments on commit 364902a

Please sign in to comment.