agentmux_srv_core/backend/remote_panes/
wire.rs1use super::pane::{Change, Connection, RemotePane};
7use crate::backend::rpc_types::{RemoteComputerInfo, RemotePaneChange, RemotePaneConnection, RemotePaneLines, RemotePaneOpenResult};
8use crate::backend::tower::peers::Peer;
9
10pub fn event_id(peer_id: &str, agent: &str) -> String {
13 format!("{peer_id}:{agent}")
14}
15
16pub fn broker_event_id(peer_id: &str, agent: &str) -> String {
19 format!("remotepane:{}", event_id(peer_id, agent))
20}
21
22pub fn computer(peer: &Peer) -> RemoteComputerInfo {
23 RemoteComputerInfo {
24 id: peer.id.clone(),
25 hostname: peer.hostname.clone(),
26 address: format!("{}:{}", peer.host, peer.port),
27 paired_ms: peer.paired_ms,
28 }
29}
30
31pub fn connection(c: &Connection) -> RemotePaneConnection {
32 let state = |s: &str| RemotePaneConnection { state: s.into(), ..Default::default() };
33 match c {
34 Connection::Connecting => state("connecting"),
35 Connection::Live => state("live"),
36 Connection::Reconnecting { why } => RemotePaneConnection { why: Some(why.clone()), ..state("reconnecting") },
37 Connection::Ended { reason, message } => RemotePaneConnection { reason: Some(reason.clone()), message: Some(message.clone()), ..state("ended") },
38 }
39}
40
41pub fn change(c: &Change) -> RemotePaneChange {
42 let kind = |k: &str| RemotePaneChange { kind: k.into(), ..Default::default() };
43 match c {
44 Change::Snapshot { from_line, next_line } => {
45 RemotePaneChange { from_line: Some(*from_line), next_line: Some(*next_line), ..kind("snapshot") }
46 }
47 Change::Append { line, count } => {
48 RemotePaneChange { line: Some(*line), count: Some(u32::try_from(*count).unwrap_or(u32::MAX)), ..kind("append") }
49 }
50 Change::Older { from_line } => RemotePaneChange { from_line: Some(*from_line), ..kind("older") },
51 Change::Reset { reason } => RemotePaneChange { reason: Some(reason.clone()), ..kind("reset") },
52 Change::Status(data) => RemotePaneChange { data: Some(data.clone()), ..kind("status") },
53 Change::Pending(data) => RemotePaneChange { data: Some(data.clone()), ..kind("pending") },
54 Change::Connection(c) => RemotePaneChange { connection: Some(connection(c)), ..kind("connection") },
55 Change::Send(send) => RemotePaneChange { send: Some(send.clone()), ..kind("send") },
56 }
57}
58
59pub fn lines(pane: &RemotePane, offset: u64, limit: usize) -> RemotePaneLines {
61 let ring = pane.ring.lock();
62 let (from_line, lines) = ring.range(offset, limit);
63 RemotePaneLines { gen: ring.gen().to_string(), from_line, lines, next_line: ring.next_line(), held_from: ring.from_line() }
64}
65
66pub fn opened(pane: &RemotePane, tail: usize) -> RemotePaneOpenResult {
68 let facts = pane.facts();
69 let next = pane.ring.lock().next_line();
70 RemotePaneOpenResult {
71 event: broker_event_id(&pane.peer_id, &pane.agent),
72 lines: lines(pane, next.saturating_sub(tail as u64), tail),
73 provider: facts.provider,
74 identity: facts.identity,
75 status: facts.status,
76 pending: facts.pending,
77 send: facts.send,
78 connection: facts.connection.as_ref().map(connection).unwrap_or_else(|| connection(&Connection::Connecting)),
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
89 fn the_open_result_names_the_full_broker_event() {
90 assert_eq!(event_id("peer-1", "agent-2"), "peer-1:agent-2");
91 assert_eq!(broker_event_id("peer-1", "agent-2"), "remotepane:peer-1:agent-2");
92 }
93
94 #[test]
95 fn changes_reach_the_window_with_only_their_own_fields() {
96 let json = |c: Change| serde_json::to_value(change(&c)).unwrap();
97 assert_eq!(json(Change::Append { line: 7, count: 2 }), serde_json::json!({ "kind": "append", "line": 7, "count": 2 }));
98 assert_eq!(json(Change::Older { from_line: 3 }), serde_json::json!({ "kind": "older", "from_line": 3 }));
99 assert_eq!(
100 json(Change::Connection(Connection::Ended { reason: "unpaired".into(), message: "Pair again.".into() })),
101 serde_json::json!({ "kind": "connection", "connection": { "state": "ended", "reason": "unpaired", "message": "Pair again." } })
102 );
103 assert_eq!(json(Change::Connection(Connection::Live)), serde_json::json!({ "kind": "connection", "connection": { "state": "live" } }));
104 assert_eq!(json(Change::Send("off".into())), serde_json::json!({ "kind": "send", "send": "off" }));
105 }
106}