ptth/crates/ptth_server_gui/src/main.rs

102 lines
2.2 KiB
Rust
Raw Normal View History

use std::{
sync::Arc,
};
use fltk::{
app,
button::Button,
frame::Frame,
prelude::*,
window::Window
};
use tokio::{
sync::Mutex,
sync::oneshot,
};
struct ServerInstance {
task: tokio::task::JoinHandle <()>,
shutdown_tx: oneshot::Sender <()>,
}
fn main ()
{
let rt = tokio::runtime::Runtime::new ().unwrap ();
let rt_handle = rt.handle ();
tracing_subscriber::fmt::init ();
let stopped_msg = "PTTH server: Stopped";
let started_msg = "PTTH server: Running";
let server_instance = Arc::new (Mutex::new (None));
// let shutdown_rx = ptth_core::graceful_shutdown::init ();
let app = app::App::default();
let mut wind = Window::new(100, 100, 400, 300, "Hello from rust");
let frame = Frame::new(0, 0, 400, 200, stopped_msg);
let mut but_run = Button::new (120, 210, 80, 40, "Run");
let mut but_stop = Button::new (200, 210, 80, 40, "Stop");
wind.end();
wind.show();
{
let server_instance = Arc::clone (&server_instance);
let mut frame = frame.clone ();
let rt_handle = rt_handle.clone ();
but_run.set_callback (move |_| {
rt_handle.block_on (async {
let mut server_instance = server_instance.lock ().await;
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel ();
let task = tokio::spawn (async {
let config_file = ptth_server::ConfigFile {
name: "ptth_server".into (),
api_key: ptth_core::gen_key (),
relay_url: "http://127.0.0.1:4000/7ZSFUKGV".into (),
file_server_root: Some (".".into ()),
throttle_upload: false,
};
if let Err (e) = ptth_server::run_server (
config_file,
shutdown_rx,
None,
None
).await
{
tracing::error! ("ptth_server crashed: {}", e);
}
});
server_instance.replace (Some (ServerInstance {
task,
shutdown_tx,
}));
});
frame.set_label (started_msg);
});
}
{
let server_instance = Arc::clone (&server_instance);
let mut frame = frame.clone ();
let rt_handle = rt_handle.clone ();
but_stop.set_callback (move |_| {
rt_handle.block_on (async {
let mut server_instance = server_instance.lock ().await;
server_instance.replace (None);
});
frame.set_label (stopped_msg);
});
}
app.run ().unwrap ();
}