From 2e062fd78f73a169901ad2cc5ea2516fe22d3127 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Wed, 29 Sep 2021 02:44:33 +0100 Subject: [PATCH] Start setting up a handler, but it's not working right just yet. We probably want to go back and look at the tokio examples again. --- Cargo.toml | 1 + src/agent/mod.rs | 40 ++++++++++++++++++++++++++++++++++++ src/agent/peer_connection.rs | 11 ++++++++++ src/main.rs | 4 ++-- 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/agent/peer_connection.rs diff --git a/Cargo.toml b/Cargo.toml index ffdc7d3..1af9d59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ serde = { version = "1.0", features = ["derive"] } # Serialisation serde_derive = "1.0" # Serialisation helper toml = "0.5" # TOML serialisation module +tokio = { version = "1", features = ["full"] } # Async runtime psk-client = { version = "0.1", features = ["vendored-openssl"] } # Pre-shared key TLS support diff --git a/src/agent/mod.rs b/src/agent/mod.rs index e69de29..2c01974 100644 --- a/src/agent/mod.rs +++ b/src/agent/mod.rs @@ -0,0 +1,40 @@ +use tokio::net::{ TcpListener, TcpStream }; + +use crate::settings::Settings; + +mod peer_connection; + +use peer_connection::PeerConnection; + +pub struct Agent { + settings: Settings, + listener: TcpListener, + peers: Vec +} + +impl Agent { + pub async fn new(settings: Settings) { + let bind_address = settings.bind_address.clone(); + let mut agent = Agent { + settings, + listener: match TcpListener::bind(bind_address.clone()).await { + Ok(listener) => listener, + Err(error) => panic!("Error: Failed to open socket using the address '{}': {}", bind_address, error) + }, + peers: Vec::new() + }; + + loop { + let (client, _) = agent.listener.accept().await.unwrap(); + let peer = PeerConnection { + stream: client + }; + agent.peers.push(peer); + tokio::spawn(async { + let peer_borrow = &peer; + peer_borrow.handle().await + }); + } + + } +} diff --git a/src/agent/peer_connection.rs b/src/agent/peer_connection.rs new file mode 100644 index 0000000..b51f775 --- /dev/null +++ b/src/agent/peer_connection.rs @@ -0,0 +1,11 @@ +use tokio::net::TcpStream; + +pub struct PeerConnection { + stream: TcpStream +} + +impl PeerConnection { + pub async fn handle(&self) { + + } +} diff --git a/src/main.rs b/src/main.rs index a03c697..63b0dce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,7 @@ use clap::{ App, AppSettings, Arg, SubCommand }; mod settings; - -use settings::Settings; +mod agent; fn main() { // Initialise the logging system @@ -39,6 +38,7 @@ fn main() { .default_value("unix-abstract:systemquery"))); let args = cli.clone().get_matches(); + match args.subcommand_name() { Some("agent") => { println!("Coming soon :-)");