joecalsend/src/main.rs

148 lines
3.9 KiB
Rust
Raw Normal View History

2025-07-05 20:54:12 +00:00
use std::{io, sync::Arc};
2025-07-05 18:53:55 +00:00
use joecalsend::{Client, JoecalState, error, models::device::DeviceInfo};
2025-07-05 17:12:09 +00:00
use local_ip_address::local_ip;
use network_interface::{Addr, NetworkInterface, NetworkInterfaceConfig, V4IfAddr};
2025-07-05 20:54:12 +00:00
use ratatui::{
DefaultTerminal, Frame,
buffer::Buffer,
crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind},
layout::Rect,
style::Stylize,
symbols::border,
text::{Line, Text},
widgets::{Block, Paragraph, Widget},
};
2025-07-04 00:00:11 +00:00
#[tokio::main]
2025-07-05 17:12:09 +00:00
async fn main() -> error::Result<()> {
2025-07-04 00:00:11 +00:00
let device = DeviceInfo::default();
2025-07-04 22:15:52 +00:00
dbg!(&device);
2025-07-04 00:00:11 +00:00
2025-07-05 18:53:55 +00:00
let std::net::IpAddr::V4(ip) = local_ip()? else {
unreachable!()
};
// for enumerating subnet peers when multicast fails (https://github.com/localsend/protocol?tab=readme-ov-file#32-http-legacy-mode)
let mut network_ip = ip;
let nifs = NetworkInterface::show().unwrap();
for addr in nifs.into_iter().flat_map(|i| i.addr) {
if let Addr::V4(V4IfAddr {
ip: ifip,
netmask: Some(netmask),
..
}) = addr
&& ip == ifip
{
network_ip = ip & netmask;
break;
}
}
dbg!(network_ip);
2025-07-04 22:15:52 +00:00
let client = Client::with_config(device, 53317, "/home/ardent/joecalsend".into())
.await
.unwrap();
2025-07-04 00:00:11 +00:00
let (h1, h2, h3) = client.start().await.unwrap();
2025-07-05 17:12:09 +00:00
2025-07-05 20:54:12 +00:00
let mut app = App::new(Arc::new(client.clone()));
let mut terminal = ratatui::init();
let result = app.run(&mut terminal);
ratatui::restore();
//let _ = tokio::join!(h1, h2, h3);
Ok(result?)
}
struct App {
client: Arc<Client>,
runstate: AppRunState,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AppRunState {
Running,
Stopping,
}
2025-07-04 22:15:52 +00:00
2025-07-05 20:54:12 +00:00
impl Default for AppRunState {
fn default() -> Self {
Self::Running
}
}
impl App {
pub fn new(client: Arc<Client>) -> Self {
App {
client,
runstate: AppRunState::Running,
}
}
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
while AppRunState::Running == self.runstate {
terminal.draw(|frame| self.draw(frame))?;
self.handle_events()?;
}
Ok(())
}
fn draw(&self, frame: &mut Frame) {
frame.render_widget(self, frame.area());
}
fn handle_events(&mut self) -> io::Result<()> {
match event::read()? {
// it's important to check that the event is a key press event as
// crossterm also emits key release and repeat events on Windows.
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
self.handle_key_event(key_event)
}
_ => {}
};
Ok(())
}
fn handle_key_event(&mut self, key_event: KeyEvent) {
match key_event.code {
KeyCode::Char('q') => self.exit(),
KeyCode::Char('s') => {}
KeyCode::Char('r') => {}
KeyCode::Char('d') => {}
_ => {}
}
}
fn exit(&mut self) {
self.runstate = AppRunState::Stopping
}
}
impl Widget for &App {
fn render(self, area: Rect, buf: &mut Buffer) {
let title = Line::from(" Counter App Tutorial ".bold());
let instructions = Line::from(vec![
" Send ".into(),
"<S>".blue().bold(),
" Receive ".into(),
"<R>".blue().bold(),
" Discover ".into(),
"<D>".blue().bold(),
" Quit ".into(),
"<Q> ".blue().bold(),
]);
let block = Block::bordered()
.title(title.centered())
.title_bottom(instructions.centered())
.border_set(border::THICK);
let rs = format!("{:?}", self.runstate);
let state_text = Text::from(vec![Line::from(vec!["runstate: ".into(), rs.yellow()])]);
Paragraph::new(state_text)
.centered()
.block(block)
.render(area, buf);
}
2025-07-04 00:00:11 +00:00
}