-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
162 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "deltachat-rpc-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-rpc-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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::path::PathBuf; | ||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use deltachat_jsonrpc::api::events::event_to_json_rpc_notification; | ||
use deltachat_jsonrpc::api::{Accounts, CommandApi}; | ||
use futures_lite::stream::StreamExt; | ||
use tokio::io::{self, AsyncBufReadExt, BufReader}; | ||
use tokio::sync::RwLock; | ||
use tokio::task::JoinHandle; | ||
use yerpc::{RpcClient, RpcSession}; | ||
|
||
#[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(); | ||
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(()) | ||
}); | ||
|
||
// Receiver task reads JSON requests from stdin. | ||
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; | ||
} | ||
log::info!("EOF reached on stdin"); | ||
Ok(()) | ||
}); | ||
|
||
recv_task.await??; | ||
|
||
// TODO: shutdown cleanly | ||
drop(send_task); | ||
drop(events_task); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters