scientific_workflow/
client.rs1pub use crate::protocol::{Command, Event, Outcome, Response, Status};
3use crate::protocol::{Request, VERSION, read_frame, write_frame};
4use std::io;
5use std::os::unix::net::UnixStream;
6use std::path::{Path, PathBuf};
7#[derive(Clone, Debug)]
9pub struct RunClient {
10 socket: PathBuf,
11}
12impl RunClient {
13 pub fn connect(socket: impl AsRef<Path>) -> io::Result<Self> {
15 let client = Self {
16 socket: socket.as_ref().into(),
17 };
18 client.request(Command::Hello)?;
19 Ok(client)
20 }
21 pub fn request(&self, command: Command) -> io::Result<Response> {
23 let mut stream = UnixStream::connect(&self.socket)?;
24 stream.set_write_timeout(Some(std::time::Duration::from_secs(10)))?;
25 if !matches!(command, Command::Wait) {
26 stream.set_read_timeout(Some(std::time::Duration::from_secs(30)))?;
27 }
28 let id = format!(
29 "{}-{}",
30 std::process::id(),
31 std::time::SystemTime::now()
32 .duration_since(std::time::UNIX_EPOCH)
33 .unwrap_or_default()
34 .as_nanos()
35 );
36 write_frame(
37 &mut stream,
38 &Request {
39 version: VERSION,
40 id: id.clone(),
41 command,
42 },
43 )?;
44 let response: Response = read_frame(&mut stream)?;
45 if response.version != VERSION || response.id != id {
46 return Err(io::Error::new(
47 io::ErrorKind::InvalidData,
48 "mismatched protocol response",
49 ));
50 }
51 if let Some(error) = &response.error {
52 return Err(io::Error::other(format!(
53 "{error}: {}",
54 response.message.as_deref().unwrap_or("")
55 )));
56 }
57 Ok(response)
58 }
59 pub fn status(&self) -> io::Result<Status> {
61 Ok(self.request(Command::Status)?.status)
62 }
63 pub fn events(&self, after: u64) -> io::Result<Response> {
65 self.request(Command::Events { after })
66 }
67 pub fn wait(&self) -> io::Result<Status> {
69 Ok(self.request(Command::Wait)?.status)
70 }
71 pub fn pause(&self) -> io::Result<Status> {
73 Ok(self.request(Command::Pause)?.status)
74 }
75 pub fn resume(&self) -> io::Result<Status> {
77 Ok(self.request(Command::Resume)?.status)
78 }
79 pub fn cancel(&self) -> io::Result<Status> {
81 Ok(self.request(Command::Cancel)?.status)
82 }
83 pub fn socket(&self) -> &Path {
85 &self.socket
86 }
87}