Skip to main content

rmux_client/commands/
server.rs

1use std::path::Path;
2
3use rmux_proto::{
4    KillServerRequest, LockClientRequest, LockServerRequest, LockSessionRequest, Request, Response,
5    ServerAccessRequest, SessionName,
6};
7
8use crate::{
9    auto_start::{ensure_server_running_with_config, AutoStartConfig, AutoStartError},
10    connection::{connect, Connection},
11    ClientError,
12};
13
14impl Connection {
15    /// Ensures the server is available, honouring top-level no-start-server behavior.
16    pub fn start_server(
17        socket_path: &Path,
18        no_start_server: bool,
19        config: AutoStartConfig,
20    ) -> Result<Self, StartServerError> {
21        if no_start_server {
22            return connect(socket_path).map_err(StartServerError::Client);
23        }
24
25        ensure_server_running_with_config(socket_path, config).map_err(StartServerError::AutoStart)
26    }
27
28    /// Sends a `kill-server` request over the detached RPC channel.
29    pub fn kill_server(&mut self) -> Result<Response, ClientError> {
30        self.roundtrip(&Request::KillServer(KillServerRequest))
31    }
32
33    /// Sends a `lock-server` request over the detached RPC channel.
34    pub fn lock_server(&mut self) -> Result<Response, ClientError> {
35        self.roundtrip(&Request::LockServer(LockServerRequest))
36    }
37
38    /// Sends a `lock-session` request over the detached RPC channel.
39    pub fn lock_session(&mut self, target: SessionName) -> Result<Response, ClientError> {
40        self.roundtrip(&Request::LockSession(LockSessionRequest { target }))
41    }
42
43    /// Sends a `lock-client` request over the detached RPC channel.
44    pub fn lock_client(&mut self, target_client: String) -> Result<Response, ClientError> {
45        self.roundtrip(&Request::LockClient(LockClientRequest { target_client }))
46    }
47
48    /// Sends a `server-access` request over the detached RPC channel.
49    pub fn server_access(&mut self, request: ServerAccessRequest) -> Result<Response, ClientError> {
50        self.roundtrip(&Request::ServerAccess(request))
51    }
52}
53
54/// Client-side `start-server` failure surface.
55#[derive(Debug)]
56pub enum StartServerError {
57    /// Connecting to an already-running server failed.
58    Client(ClientError),
59    /// Auto-starting the server failed.
60    AutoStart(AutoStartError),
61}