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.
This commit is contained in:
Starbeamrainbowlabs 2021-09-29 02:44:33 +01:00
parent f55fd2f91b
commit 2e062fd78f
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 54 additions and 2 deletions

View file

@ -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

View file

@ -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<PeerConnection>
}
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
});
}
}
}

View file

@ -0,0 +1,11 @@
use tokio::net::TcpStream;
pub struct PeerConnection {
stream: TcpStream
}
impl PeerConnection {
pub async fn handle(&self) {
}
}

View file

@ -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 :-)");