1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2020 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
//! Request-level client
use super::ipc_handler::{unix_socket, Connect};
use crate::error::{ClientErrorKind, Result};
use derivative::Derivative;
use parsec_interface::requests::{Request, Response};
use std::time::Duration;

const DEFAULT_MAX_BODY_SIZE: usize = usize::max_value();

/// Low level client structure optimised for communicating with the service
/// at a request level of abstraction.
///
/// Usage is recommended when fine control over the request header and IPC handler
/// is needed.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct RequestClient {
    /// Max size for response bodies
    ///
    /// Defaults to the max value of `usize` on the current platform
    pub max_body_size: usize,
    /// Handler for IPC-related functionality
    ///
    /// Defaults to using Unix domain sockets
    #[derivative(Debug = "ignore")]
    pub ipc_handler: Box<dyn Connect + Send + Sync>,
}

impl RequestClient {
    /// Send a request and get a response.
    pub fn process_request(&self, request: Request) -> Result<Response> {
        // Try to connect once, wait for a timeout until trying again.
        let mut stream = self.ipc_handler.connect()?;

        request
            .write_to_stream(&mut stream)
            .map_err(ClientErrorKind::Interface)?;
        Ok(Response::read_from_stream(&mut stream, self.max_body_size)
            .map_err(ClientErrorKind::Interface)?)
    }
}

impl Default for RequestClient {
    fn default() -> Self {
        RequestClient {
            max_body_size: DEFAULT_MAX_BODY_SIZE,
            ipc_handler: Box::from(unix_socket::Handler::default()),
        }
    }
}

/// Configuration methods for controlling IPC-level options.
impl crate::BasicClient {
    /// Set the maximum body size allowed for requests.
    ///
    /// Defaults to the maximum value of `usize`.
    pub fn set_max_body_size(&mut self, max_body_size: usize) {
        self.op_client.request_client.max_body_size = max_body_size;
    }

    /// Set the IPC handler used for communication with the service.
    ///
    /// By default the [Unix domain socket client](../ipc_handler/unix_socket/struct.Client.html) is used.
    pub fn set_ipc_handler(&mut self, ipc_handler: Box<dyn Connect + Send + Sync>) {
        self.op_client.request_client.ipc_handler = ipc_handler;
    }

    /// Set the timeout for operations on the IPC stream.
    ///
    /// The value defaults to 1 second.
    pub fn set_timeout(&mut self, timeout: Option<Duration>) {
        self.op_client
            .request_client
            .ipc_handler
            .set_timeout(timeout);
    }
}