forked from sigil-03/number-station
wip
This commit is contained in:
parent
f2248200dd
commit
4bcc454d5b
1 changed files with 51 additions and 43 deletions
52
src/main.rs
52
src/main.rs
|
|
@ -2,33 +2,32 @@ 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 {
|
||||
pub struct NumberStation {
|
||||
id: AddressHash,
|
||||
current_number: usize,
|
||||
}
|
||||
}
|
||||
|
||||
impl NumberStation {
|
||||
impl NumberStation {
|
||||
pub fn new(id: AddressHash) -> Self {
|
||||
Self {
|
||||
id,
|
||||
|
|
@ -43,13 +42,13 @@ impl NumberStation {
|
|||
number: self.current_number,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Encode, BorrowDecode, Debug)]
|
||||
pub struct NumberStationPacket {
|
||||
#[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;
|
||||
|
|
@ -122,8 +129,6 @@ 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));
|
||||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue