Skip to main content

isothrum_client/client/
console.rs

1//! A graphical console the service reached, held on a connection of its own: an SSH tunnel that
2//! carries it stays open for as long as the connection does.
3
4use super::{ClientError, Reply, Result};
5use crate::proto::ReachedConsole;
6use crate::wire::{Reader, Writer};
7
8/// A graphical console ([`Client::graphical_console`](crate::Client::graphical_console)): where
9/// it answers and how to sign in, and the connection that keeps its tunnel open. Dropping it
10/// closes the connection, and the service ends the tunnel.
11pub struct HeldConsole {
12    /// The console.
13    pub console: ReachedConsole,
14    pub(super) reader: Reader,
15    pub(super) _writer: Writer,
16}
17
18impl HeldConsole {
19    /// Waits for the console's tunnel to end on its own (its `ssh` exited, the service stopped),
20    /// and says how: the console no longer answers. A console reached through no tunnel ends only
21    /// with the service.
22    pub async fn closed(&mut self) -> String {
23        loop {
24            match self.reader.next::<Reply>().await {
25                Ok(Some(Reply::ConsoleClosed { reason, .. })) => return reason,
26                Ok(Some(_)) => {}
27                Ok(None) => return "the runner service closed the connection".to_owned(),
28                Err(e) => return format!("the runner service's connection broke: {e}"),
29            }
30        }
31    }
32}
33
34impl std::fmt::Debug for HeldConsole {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        f.debug_struct("HeldConsole").field("console", &self.console).finish_non_exhaustive()
37    }
38}
39
40/// The console the reply to a `graphical_console` gives.
41pub(super) fn of_reply(reply: Option<Reply>) -> Result<ReachedConsole> {
42    match reply {
43        Some(Reply::Console { console, .. }) => Ok(*console),
44        Some(Reply::Error { report, .. }) => Err(ClientError::Refused(Box::new(report))),
45        Some(other) => Err(super::unexpected(&other)),
46        None => Err(ClientError::closed()),
47    }
48}