This commit is contained in:
Sebastian Kuzminsky 2025-12-01 21:39:42 -07:00
parent f2248200dd
commit 4bcc454d5b

View file

@ -2,54 +2,53 @@ use std::time::Duration;
use clap::{Parser, Subcommand};
use std::sync::Arc;
use std::collections::HashMap;
use std::sync::Arc;
use rand_core::OsRng;
use reticulum::destination::link::{Link, LinkEvent, LinkStatus};
use reticulum::destination::{DestinationName, SingleInputDestination};
use reticulum::hash::AddressHash;
use reticulum::identity::PrivateIdentity;
use reticulum::iface::tcp_client::TcpClient;
use reticulum::transport::{Transport, TransportConfig};
use reticulum::destination::link::{Link, LinkEvent, LinkStatus};
use tokio::time::interval;
use crate::number_station::NumberStationPacket;
mod number_station {
use bincode::{BorrowDecode, Decode, Encode};
use reticulum::hash::AddressHash;
use serde::{Serialize, Deserialize};
use bincode::{Encode, Decode, BorrowDecode};
use serde::{Deserialize, Serialize};
pub struct NumberStation {
id: AddressHash,
current_number: usize,
}
pub struct NumberStation {
id: AddressHash,
current_number: usize,
}
impl NumberStation {
pub fn new(id: AddressHash) -> Self {
Self {
id,
current_number: 0,
impl NumberStation {
pub fn new(id: AddressHash) -> Self {
Self {
id,
current_number: 0,
}
}
// generate / return the next number in the station
pub fn next(&mut self) -> NumberStationPacket {
self.current_number = self.current_number.wrapping_add(1);
NumberStationPacket {
// id: self.id,
number: self.current_number,
}
}
}
// generate / return the next number in the station
pub fn next(&mut self) -> NumberStationPacket {
self.current_number = self.current_number.wrapping_add(1);
NumberStationPacket {
// id: self.id,
number: self.current_number,
}
}
}
#[derive(Encode, BorrowDecode, Debug)]
pub struct NumberStationPacket {
// id: AddressHash,
number: usize,
}
#[derive(Encode, BorrowDecode, Debug)]
pub struct NumberStationPacket {
// id: AddressHash,
number: usize,
}
}
#[derive(Subcommand)]
@ -58,23 +57,29 @@ enum Mode {
Generator,
}
enum ReticulumLink {
TcpClient { socket_addr: std::net::SocketAddr },
}
#[derive(Parser)]
struct Cli {
#[command(subcommand)]
mode: Mode,
/// Connect to Reticulum Network by making a TCP Client interface to this host:port.
#[arg(short, long, default_value_t = String::from("127.0.0.1:4242"))]
tcp_client: String,
}
async fn run_client() {
async fn run_client(tcp_client: String) {
log::info!(">>> NUMBER STATION CLIENT <<<");
let mut transport = Transport::new(TransportConfig::default());
let client_addr = transport
.iface_manager()
.lock()
.await
.spawn(TcpClient::new("127.0.0.1:4242"), TcpClient::spawn);
.spawn(TcpClient::new(tcp_client), TcpClient::spawn);
log::info!("client ID: {}", client_addr);
@ -83,7 +88,9 @@ async fn run_client() {
log::info!("private ID: {}", id.address_hash());
// let destination = SingleInputDestination::new(id, DestinationName::new("generator", "number_station.generator"));
let destination = transport.add_destination(id, DestinationName::new("receiver", "generator.numbers")).await;
let destination = transport
.add_destination(id, DestinationName::new("receiver", "generator.numbers"))
.await;
log::info!("dest ID: {}", destination.lock().await.desc.address_hash);
let mut announce_recv = transport.recv_announces().await;
@ -102,7 +109,7 @@ async fn run_client() {
};
},
Ok(link_event) = out_link_events.recv() => {
match link_event.event {
LinkEvent::Activated => log::info!("link {} activated", link_event.id),
@ -110,20 +117,18 @@ async fn run_client() {
LinkEvent::Data(payload) => {
let payload: NumberStationPacket = bincode::borrow_decode_from_slice(payload.as_slice(), bincode::config::standard()).unwrap().0;
log::info!("link {} data payload: {:?}", link_event.id, payload);
}
}
}
}
}
}
}
}
async fn run_server() {
log::info!(">>> NUMBER STATION SERVER <<<");
let mut transport = Transport::new(TransportConfig::default());
let client_addr = transport
.iface_manager()
.lock()
@ -137,7 +142,9 @@ async fn run_server() {
log::info!("private ID: {}", id.address_hash());
// let destination = SingleInputDestination::new(id, DestinationName::new("generator", "number_station.generator"));
let destination = transport.add_destination(id, DestinationName::new("generator", "generator.numbers")).await;
let destination = transport
.add_destination(id, DestinationName::new("generator", "generator.numbers"))
.await;
log::info!("dest ID: {}", destination.lock().await.desc.address_hash);
// let mut announce_recv = transport.recv_announces().await;
@ -146,7 +153,8 @@ async fn run_server() {
let mut links = HashMap::<AddressHash, Arc<tokio::sync::Mutex<Link>>>::new();
let mut number_generator = number_station::NumberStation::new(destination.lock().await.desc.address_hash);
let mut number_generator =
number_station::NumberStation::new(destination.lock().await.desc.address_hash);
// let timer_fut = tokio::time::sleep(Duration::from_secs(1));
let mut announce_interval = interval(Duration::from_secs(5));
@ -155,7 +163,7 @@ async fn run_server() {
loop {
tokio::select! {
_ = announce_interval.tick() => {
log::info!("announce tick");
log::info!("announce tick");
transport
.send_announce(&destination, None)
.await;
@ -173,7 +181,7 @@ async fn run_server() {
}
_ = number_transmit_interval.tick() => {
let payload = number_generator.next();
log::info!("transmit payload: {:?}", payload);
log::info!("transmit payload: {:?}", payload);
transport.send_to_in_links(&destination.lock().await.desc.address_hash, &bincode::encode_to_vec(payload, bincode::config::standard()).unwrap()).await;
}
}
@ -187,7 +195,7 @@ async fn main() {
let cli = Cli::parse();
match cli.mode {
Mode::Receiver => run_client().await,
Mode::Receiver => run_client(cli.tcp_client).await,
Mode::Generator => run_server().await,
}
}