diff --git a/prototypes/quic_demo/src/bin/client_gui.rs b/prototypes/quic_demo/src/bin/client_gui.rs index 4abafda..a8752d6 100644 --- a/prototypes/quic_demo/src/bin/client_gui.rs +++ b/prototypes/quic_demo/src/bin/client_gui.rs @@ -45,7 +45,9 @@ fn main () -> anyhow::Result <()> { let mut x = margin; let mut y = margin; - let frame_status; + let mut frame_status; + + let mut input_client_port; let mut input_server_id; let mut input_server_port; @@ -53,7 +55,7 @@ fn main () -> anyhow::Result <()> { let mut but_close; { - frame_status = Frame::new (10, 10, 800 - 20, 30, "0 ports forwarded"); + frame_status = Frame::new (10, 10, 800 - 20, 30, "Forwarding 0 ports"); } y += h + margin; @@ -110,6 +112,14 @@ fn main () -> anyhow::Result <()> { set_active (&mut but_close, false); } + let mut gui_port = GuiPort { + input_client_port, + input_server_id, + input_server_port, + but_open, + but_close, + }; + // y += h + margin; wind.end (); @@ -137,31 +147,25 @@ fn main () -> anyhow::Result <()> { while app.wait () { match fltk_rx.recv () { Some (Message::OpenPort) => { - let connection_p2_p3 = connection_p2_p3.clone (); - let client_tcp_port = u16::from_str (&input_client_port.value ())?; - let server_id = input_server_id.value (); - let server_tcp_port = u16::from_str (&input_server_port.value ())?; - let (shutdown_flag, shutdown_flag_rx) = tokio::sync::watch::channel (true); - - let task = rt.spawn (async move { - forward_port ( + if let Ok (params) = gui_port.get_params () { + let connection_p2_p3 = connection_p2_p3.clone (); + let (shutdown_flag, shutdown_flag_rx) = tokio::sync::watch::channel (true); + + let task = rt.spawn (forward_port ( connection_p2_p3, - client_tcp_port, - server_id, - server_tcp_port, + params, shutdown_flag_rx - ).await - }); - - forwarding_instance.replace (ForwardingInstance { - task, - shutdown_flag, - }); - - set_active (&mut but_open, false); - set_active (&mut but_close, true); - but_open.set (true); - but_close.set (false); + )); + + forwarding_instance.replace (ForwardingInstance { + task, + shutdown_flag, + }); + + gui_port.set_forwarding (true); + + frame_status.set_label ("Forwarding 1 port"); + } }, Some (Message::ClosePort) => { if let Some (mut old_instance) = forwarding_instance.take () { @@ -172,10 +176,8 @@ fn main () -> anyhow::Result <()> { })?; } - set_active (&mut but_open, true); - set_active (&mut but_close, false); - but_open.set (false); - but_close.set (true); + gui_port.set_forwarding (false); + frame_status.set_label ("Forwarding 0 ports"); }, None => (), } @@ -200,12 +202,16 @@ struct ForwardingInstance { async fn forward_port ( connection_p2_p3: quinn::Connection, - client_tcp_port: u16, - server_id: String, - server_tcp_port: u16, + params: ForwardingParams, shutdown_flag_rx: tokio::sync::watch::Receiver , ) -> anyhow::Result <()> { + let ForwardingParams { + client_tcp_port, + server_id, + server_tcp_port, + } = params; + let listener = TcpListener::bind (("127.0.0.1", client_tcp_port)).await?; trace! ("Accepting local TCP connections from P1 on {}", client_tcp_port); @@ -307,3 +313,43 @@ async fn handle_p1 ( Ok (()) } + +struct GuiPort { + input_client_port: Input, + input_server_id: Input, + input_server_port: Input, + but_open: Button, + but_close: Button, +} + +struct ForwardingParams { + client_tcp_port: u16, + server_id: String, + server_tcp_port: u16, +} + +impl GuiPort { + fn get_params (&self) -> anyhow::Result + { + let client_tcp_port = u16::from_str (&self.input_client_port.value ())?; + let server_id = self.input_server_id.value (); + let server_tcp_port = u16::from_str (&self.input_server_port.value ())?; + + Ok (ForwardingParams { + client_tcp_port, + server_id, + server_tcp_port, + }) + } + + fn set_forwarding (&mut self, x: bool) { + set_active (&mut self.input_client_port, !x); + set_active (&mut self.input_server_id, !x); + set_active (&mut self.input_server_port, !x); + set_active (&mut self.but_open, !x); + set_active (&mut self.but_close, x); + + self.but_open.set (x); + self.but_close.set (!x); + } +}