♻️ 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> {
rt: &'a Runtime,
frame_status: Frame,
forwarding_instances: Vec <Option <ForwardingInstance>>,
gui_ports: Vec <GuiPort>,
ports: Vec <Port>,
}
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 <'_> {
@ -50,7 +62,7 @@ impl GuiClient <'_> {
port_idx: usize,
) -> 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 forwarding_instance = self.rt.block_on (ForwardingInstance::new (
@ -58,16 +70,16 @@ impl GuiClient <'_> {
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 ();
Ok (())
}
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 {
old_instance.close ()
.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 ();
Ok (())
}
fn open_ports (&self) -> usize {
self.forwarding_instances.iter ()
.map (|x| if x.is_some () { 1 } else { 0 })
self.ports.iter ()
.map (|x| if x.forwarding_instance.is_some () { 1 } else { 0 })
.sum ()
}
@ -148,23 +160,25 @@ fn main () -> anyhow::Result <()> {
// y += h + margin;
// x = margin;
let gui_ports = vec! [
gui_port_0,
gui_port_1,
gui_port_2,
];
let forwarding_instances = vec! [
None,
None,
None,
let ports = vec! [
Port {
gui: gui_port_0,
forwarding_instance: None,
},
Port {
gui: gui_port_1,
forwarding_instance: None,
},
Port {
gui: gui_port_2,
forwarding_instance: None,
},
];
let mut gui_client = GuiClient {
rt: &rt,
frame_status,
forwarding_instances,
gui_ports,
ports,
};
// 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 {
fn new (fltk_tx: fltk::app::Sender <Message>, x: &mut i32, y: i32, port_idx: usize) -> Self {
let margin = 10;