feat(service): Add IpcClient API
This commit is contained in:
parent
ed7b6a9bd0
commit
98e386621e
4 changed files with 67 additions and 0 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1411,6 +1411,7 @@ dependencies = [
|
|||
"anyhow",
|
||||
"async-io",
|
||||
"async-oneshot",
|
||||
"async-process",
|
||||
"async-trait",
|
||||
"futures",
|
||||
"futures-core",
|
||||
|
|
|
|||
|
|
@ -28,3 +28,4 @@ toml = "0.5"
|
|||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["fmt"] }
|
||||
futures-core = "0.3.16"
|
||||
async-process = "1.3.0"
|
||||
|
|
|
|||
62
service/src/client.rs
Normal file
62
service/src/client.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
// Copyright 2021 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use async_process as process;
|
||||
use futures::{AsyncBufReadExt, AsyncWriteExt, Stream, StreamExt};
|
||||
use pop_launcher::{Request, Response};
|
||||
use std::io;
|
||||
|
||||
pub struct IpcClient {
|
||||
pub child: process::Child,
|
||||
pub stdin: process::ChildStdin,
|
||||
}
|
||||
|
||||
impl IpcClient {
|
||||
pub fn new() -> io::Result<(Self, impl Stream<Item = Response>)> {
|
||||
let mut child = process::Command::new("pop-launcher")
|
||||
.stdin(process::Stdio::piped())
|
||||
.stdout(process::Stdio::piped())
|
||||
.spawn()?;
|
||||
|
||||
let stdin = child
|
||||
.stdin
|
||||
.take()
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "failed to find child stdin"))?;
|
||||
|
||||
let stdout = child
|
||||
.stdout
|
||||
.take()
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "failed to find child stdout"))?;
|
||||
|
||||
let responses =
|
||||
futures::io::BufReader::new(stdout)
|
||||
.lines()
|
||||
.filter_map(|result| async move {
|
||||
if let Ok(line) = result {
|
||||
if let Ok(event) = serde_json::from_str::<Response>(&line) {
|
||||
return Some(event);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
});
|
||||
|
||||
let client = Self { child, stdin };
|
||||
|
||||
Ok((client, responses))
|
||||
}
|
||||
|
||||
pub async fn send(&mut self, request: Request) -> io::Result<()> {
|
||||
let mut request_json = serde_json::to_string(&request)
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
|
||||
|
||||
request_json.push('\n');
|
||||
|
||||
self.stdin.write_all(request_json.as_bytes()).await
|
||||
}
|
||||
|
||||
pub async fn exit(mut self) {
|
||||
let _ = self.send(Request::Exit).await;
|
||||
let _ = self.child.status().await;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
// Copyright 2021 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
mod client;
|
||||
mod plugins;
|
||||
|
||||
pub use client::*;
|
||||
|
||||
use crate::plugins::*;
|
||||
use futures::SinkExt;
|
||||
use futures_core::Stream;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue