♻️ refactor

main
_ 2021-10-10 18:31:52 +00:00
parent 1a775622cf
commit 4ffcca8559
1 changed files with 35 additions and 29 deletions

View File

@ -39,8 +39,20 @@ enum Message {
struct GuiClient <'a> { struct GuiClient <'a> {
rt: &'a Runtime, rt: &'a Runtime,
frame_status: Frame, frame_status: Frame,
forwarding_instances: Vec <Option <ForwardingInstance>>, ports: Vec <Port>,
gui_ports: Vec <GuiPort>, }
struct Port {
gui: GuiPort,
forwarding_instance: Option <ForwardingInstance>,
}
struct GuiPort {
input_client_port: Input,
input_server_id: Input,
input_server_port: Input,
but_open: Button,
but_close: Button,
} }
impl GuiClient <'_> { impl GuiClient <'_> {
@ -50,7 +62,7 @@ impl GuiClient <'_> {
port_idx: usize, port_idx: usize,
) -> anyhow::Result <()> ) -> anyhow::Result <()>
{ {
let params = self.gui_ports [port_idx].get_params ()?; let params = self.ports [port_idx].gui.get_params ()?;
let _guard = self.rt.enter (); let _guard = self.rt.enter ();
let forwarding_instance = self.rt.block_on (ForwardingInstance::new ( let forwarding_instance = self.rt.block_on (ForwardingInstance::new (
@ -58,16 +70,16 @@ impl GuiClient <'_> {
params, params,
))?; ))?;
self.forwarding_instances [port_idx].replace (forwarding_instance); self.ports [port_idx].forwarding_instance.replace (forwarding_instance);
self.gui_ports [port_idx].set_forwarding (true); self.ports [port_idx].gui.set_forwarding (true);
self.sync_status (); self.sync_status ();
Ok (()) Ok (())
} }
pub fn close_port (&mut self, port_idx: usize) -> anyhow::Result <()> { pub fn close_port (&mut self, port_idx: usize) -> anyhow::Result <()> {
if let Some (old_instance) = self.forwarding_instances [port_idx].take () { if let Some (old_instance) = self.ports [port_idx].forwarding_instance.take () {
self.rt.block_on (async { self.rt.block_on (async {
old_instance.close () old_instance.close ()
.await .await
@ -77,15 +89,15 @@ impl GuiClient <'_> {
})?; })?;
} }
self.gui_ports [port_idx].set_forwarding (false); self.ports [port_idx].gui.set_forwarding (false);
self.sync_status (); self.sync_status ();
Ok (()) Ok (())
} }
fn open_ports (&self) -> usize { fn open_ports (&self) -> usize {
self.forwarding_instances.iter () self.ports.iter ()
.map (|x| if x.is_some () { 1 } else { 0 }) .map (|x| if x.forwarding_instance.is_some () { 1 } else { 0 })
.sum () .sum ()
} }
@ -148,23 +160,25 @@ fn main () -> anyhow::Result <()> {
// y += h + margin; // y += h + margin;
// x = margin; // x = margin;
let gui_ports = vec! [ let ports = vec! [
gui_port_0, Port {
gui_port_1, gui: gui_port_0,
gui_port_2, forwarding_instance: None,
]; },
Port {
let forwarding_instances = vec! [ gui: gui_port_1,
None, forwarding_instance: None,
None, },
None, Port {
gui: gui_port_2,
forwarding_instance: None,
},
]; ];
let mut gui_client = GuiClient { let mut gui_client = GuiClient {
rt: &rt, rt: &rt,
frame_status, frame_status,
forwarding_instances, ports,
gui_ports,
}; };
// y += h + margin; // y += h + margin;
@ -224,14 +238,6 @@ fn set_active <W: WidgetExt> (w: &mut W, b: bool) {
} }
} }
struct GuiPort {
input_client_port: Input,
input_server_id: Input,
input_server_port: Input,
but_open: Button,
but_close: Button,
}
impl GuiPort { impl GuiPort {
fn new (fltk_tx: fltk::app::Sender <Message>, x: &mut i32, y: i32, port_idx: usize) -> Self { fn new (fltk_tx: fltk::app::Sender <Message>, x: &mut i32, y: i32, port_idx: usize) -> Self {
let margin = 10; let margin = 10;