Skip to main content

rmux_client/commands/
server.rs

1use std::path::Path;
2
3use rmux_proto::{
4    DaemonStatusRequest, KillServerRequest, LockClientRequest, LockServerRequest,
5    LockSessionRequest, Request, Response, ServerAccessRequest, SessionName, ShutdownIfIdleRequest,
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 legacy wire-v1 `kill-server` request for pre-0.6 daemon cleanup.
34    pub fn kill_server_legacy_wire_v1(&mut self) -> Result<(), ClientError> {
35        self.write_legacy_wire_v1_request(&Request::KillServer(KillServerRequest))
36    }
37
38    /// Sends an internal daemon status request over the detached RPC channel.
39    pub fn daemon_status(&mut self) -> Result<Response, ClientError> {
40        self.roundtrip(&Request::DaemonStatus(DaemonStatusRequest))
41    }
42
43    /// Sends an internal idle-only daemon shutdown request.
44    pub fn shutdown_if_idle(&mut self) -> Result<Response, ClientError> {
45        self.roundtrip(&Request::ShutdownIfIdle(ShutdownIfIdleRequest))
46    }
47
48    /// Sends a `lock-server` request over the detached RPC channel.
49    pub fn lock_server(&mut self) -> Result<Response, ClientError> {
50        self.roundtrip(&Request::LockServer(LockServerRequest))
51    }
52
53    /// Sends a `lock-session` request over the detached RPC channel.
54    pub fn lock_session(&mut self, target: SessionName) -> Result<Response, ClientError> {
55        self.roundtrip(&Request::LockSession(LockSessionRequest { target }))
56    }
57
58    /// Sends a `lock-client` request over the detached RPC channel.
59    pub fn lock_client(&mut self, target_client: String) -> Result<Response, ClientError> {
60        self.roundtrip(&Request::LockClient(LockClientRequest { target_client }))
61    }
62
63    /// Sends a `server-access` request over the detached RPC channel.
64    pub fn server_access(&mut self, request: ServerAccessRequest) -> Result<Response, ClientError> {
65        self.roundtrip(&Request::ServerAccess(request))
66    }
67}
68
69/// Client-side `start-server` failure surface.
70#[derive(Debug)]
71pub enum StartServerError {
72    /// Connecting to an already-running server failed.
73    Client(ClientError),
74    /// Auto-starting the server failed.
75    AutoStart(AutoStartError),
76}