parsec_client/core/ipc_handler/
mod.rs

1// Copyright 2020 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3//! Types implementing an abstraction over IPC channels
4use crate::error::{ClientErrorKind, Error, Result};
5use std::io::{Read, Write};
6use std::time::Duration;
7use url::Url;
8
9pub mod unix_socket;
10
11/// Default timeout for client IPC requests.
12pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
13
14/// This trait is created to allow the iterator returned by incoming to iterate over a trait object
15/// that implements both Read and Write.
16pub trait ReadWrite: Read + Write {}
17// Automatically implements ReadWrite for all types that implement Read and Write.
18impl<T: Read + Write> ReadWrite for T {}
19
20/// Trait that must be implemented by any IPC client
21///
22/// The trait is used by the request handler for obtaining a stream to the service.
23pub trait Connect {
24    /// Connect to underlying IPC and return a readable and writeable stream
25    fn connect(&self) -> Result<Box<dyn ReadWrite>>;
26
27    /// Set timeout for all produced streams.
28    fn set_timeout(&mut self, timeout: Option<Duration>);
29}
30
31/// Create an implementation of `Connect` from the socket URL
32pub fn connector_from_url(socket_url: Url) -> Result<Box<dyn Connect + Send + Sync>> {
33    match socket_url.scheme() {
34        "unix" => Ok(Box::from(unix_socket::Handler::new(
35            socket_url.path().into(),
36            Some(DEFAULT_TIMEOUT),
37        )?)),
38        _ => Err(Error::Client(ClientErrorKind::InvalidSocketUrl)),
39    }
40}