allow 3 ports to be forwarded for test purposes

Later it would be some larger number, with some better GUI.
main
_ 2021-07-19 02:37:09 +00:00
parent 16b86ff45c
commit eeadea6bc0
1 changed files with 39 additions and 23 deletions

View File

@ -27,8 +27,8 @@ struct Opt {
#[derive (Clone, Copy)] #[derive (Clone, Copy)]
enum Message { enum Message {
OpenPort, OpenPort (usize),
ClosePort, ClosePort (usize),
} }
fn main () -> anyhow::Result <()> { fn main () -> anyhow::Result <()> {
@ -49,33 +49,51 @@ fn main () -> anyhow::Result <()> {
let mut x = margin; let mut x = margin;
let mut y = margin; let mut y = margin;
let mut frame_status; let mut frame_status = Frame::new (x, y, 800 - 20, h, "Forwarding 0 ports");
{
frame_status = Frame::new (10, 10, 800 - 20, 30, "Forwarding 0 ports");
}
y += h + margin; y += h + margin;
x = margin; x = margin;
{ {
let w = 80; let w = 80;
let frame_client_port = Frame::new (x, y, w, h, "Local port"); Frame::new (x, y, w, h, "Local port");
x += w + margin; x += w + margin;
let w = 120; let w = 120;
let frame_server_id = Frame::new (x, y, w, h, "Server ID"); Frame::new (x, y, w, h, "Server ID");
x += w + margin; x += w + margin;
let w = 80; let w = 80;
let frame_server_port = Frame::new (x, y, 80, h, "Server port"); Frame::new (x, y, w, h, "Server port");
// x += w + margin; // x += w + margin;
} }
y += h + margin; y += h + margin;
x = margin; x = margin;
let mut gui_port = GuiPort::new (fltk_tx, &mut x, y); let gui_port_0 = GuiPort::new (fltk_tx, &mut x, y, 0);
y += h + margin;
x = margin;
let gui_port_1 = GuiPort::new (fltk_tx, &mut x, y, 1);
y += h + margin;
x = margin;
let gui_port_2 = GuiPort::new (fltk_tx, &mut x, y, 2);
// y += h + margin;
// x = margin;
let mut gui_ports = vec! [
gui_port_0,
gui_port_1,
gui_port_2,
];
let mut forwarding_instances = vec! [
None,
None,
None,
];
// y += h + margin; // y += h + margin;
@ -99,12 +117,10 @@ fn main () -> anyhow::Result <()> {
Ok::<_, anyhow::Error> (connection) Ok::<_, anyhow::Error> (connection)
})?; })?;
let mut forwarding_instance = None;
while app.wait () { while app.wait () {
match fltk_rx.recv () { match fltk_rx.recv () {
Some (Message::OpenPort) => { Some (Message::OpenPort (port_idx)) => {
if let Ok (params) = gui_port.get_params () { if let Ok (params) = gui_ports [port_idx].get_params () {
let connection_p2_p3 = connection_p2_p3.clone (); let connection_p2_p3 = connection_p2_p3.clone ();
let (shutdown_flag, shutdown_flag_rx) = tokio::sync::watch::channel (true); let (shutdown_flag, shutdown_flag_rx) = tokio::sync::watch::channel (true);
@ -114,18 +130,18 @@ fn main () -> anyhow::Result <()> {
shutdown_flag_rx shutdown_flag_rx
)); ));
forwarding_instance.replace (ForwardingInstance { forwarding_instances [port_idx].replace (ForwardingInstance {
task, task,
shutdown_flag, shutdown_flag,
}); });
gui_port.set_forwarding (true); gui_ports [port_idx].set_forwarding (true);
frame_status.set_label ("Forwarding 1 port"); frame_status.set_label ("Forwarding 1 port");
} }
}, },
Some (Message::ClosePort) => { Some (Message::ClosePort (port_idx)) => {
if let Some (mut old_instance) = forwarding_instance.take () { if let Some (old_instance) = forwarding_instances [port_idx].take () {
rt.block_on (async { rt.block_on (async {
old_instance.shutdown_flag.send (false)?; old_instance.shutdown_flag.send (false)?;
old_instance.task.await??; old_instance.task.await??;
@ -133,7 +149,7 @@ fn main () -> anyhow::Result <()> {
})?; })?;
} }
gui_port.set_forwarding (false); gui_ports [port_idx].set_forwarding (false);
frame_status.set_label ("Forwarding 0 ports"); frame_status.set_label ("Forwarding 0 ports");
}, },
None => (), None => (),
@ -286,7 +302,7 @@ struct ForwardingParams {
} }
impl GuiPort { impl GuiPort {
fn new (fltk_tx: fltk::app::Sender <Message>, x: &mut i32, y: i32) -> Self { fn new (fltk_tx: fltk::app::Sender <Message>, x: &mut i32, y: i32, port_idx: usize) -> Self {
let margin = 10; let margin = 10;
let h = 30; let h = 30;
@ -315,9 +331,9 @@ impl GuiPort {
input_server_port.set_value ("5900"); input_server_port.set_value ("5900");
but_open.set_trigger (CallbackTrigger::Changed); but_open.set_trigger (CallbackTrigger::Changed);
but_open.emit (fltk_tx, Message::OpenPort); but_open.emit (fltk_tx, Message::OpenPort (port_idx));
but_close.set_trigger (CallbackTrigger::Changed); but_close.set_trigger (CallbackTrigger::Changed);
but_close.emit (fltk_tx, Message::ClosePort); but_close.emit (fltk_tx, Message::ClosePort (port_idx));
set_active (&mut but_open, true); set_active (&mut but_open, true);
set_active (&mut but_close, false); set_active (&mut but_close, false);