p2p: mDNS

This commit is contained in:
2025-11-08 16:49:36 +01:00
parent 403168a00a
commit 858d41265c
3 changed files with 19 additions and 35 deletions
+16 -31
View File
@@ -9,49 +9,34 @@ mod req;
use std::{convert::Into, net::SocketAddr, time::Duration};
use chrono::{DateTime, Local};
use clap::Parser as _;
use cli::Cli;
use gethostname::gethostname;
use lanspread_compat::eti;
use lanspread_db::db::{Game, GameDB};
use lanspread_mdns::{
DaemonEvent,
LANSPREAD_INSTANCE_NAME,
LANSPREAD_SERVICE_TYPE,
MdnsAdvertiser,
};
use lanspread_mdns::{DaemonEvent, LANSPREAD_SERVICE_TYPE, MdnsAdvertiser};
use tracing_subscriber::EnvFilter;
use uuid::Uuid;
fn spawn_mdns_task(server_addr: SocketAddr) -> eyre::Result<()> {
let combined_str = if 1 == 2 {
let peer_id = Uuid::now_v7().simple().to_string();
let peer_id = Uuid::now_v7().simple().to_string();
let uidddd = Uuid::now_v7();
let hostname = gethostname();
let hostname_str = hostname.to_str().unwrap_or("");
// TODO
let uidddd = uidddd
.get_timestamp()
.expect("failed to get timestamp from UUID")
.to_unix();
let local_datetime: DateTime<Local> =
DateTime::from_timestamp(i64::try_from(uidddd.0).unwrap_or(0), uidddd.1)
.expect("Failed to create DateTime from uuid unix timestamp")
.into();
dbg!(local_datetime);
let hostname = gethostname();
let mut hostname = hostname.to_str().unwrap_or("");
if hostname.len() + peer_id.len() > 63 {
hostname = &hostname[..63 - peer_id.len()];
}
format!("{hostname}-{peer_id}")
// Calculate maximum hostname length that fits with UUID in 63 char limit
let max_hostname_len = 63usize.saturating_sub(peer_id.len() + 1); // +1 for the dash
let truncated_hostname = if hostname_str.len() > max_hostname_len {
hostname_str.get(..max_hostname_len).unwrap_or(hostname_str)
} else {
String::from(LANSPREAD_INSTANCE_NAME)
hostname_str
};
let combined_str = if truncated_hostname.is_empty() {
// If no hostname is available, use just the UUID
peer_id
} else {
format!("{truncated_hostname}-{peer_id}")
};
let mdns = MdnsAdvertiser::new(LANSPREAD_SERVICE_TYPE, &combined_str, server_addr)?;