Initial commit

This commit is contained in:
Dave Hrycyszyn
2019-11-28 19:33:20 +00:00
commit f4a62fd64c
5 changed files with 852 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
use tokio::net::TcpListener;
use tokio::prelude::*;
use crate::mix_peer::MixPeer;
mod mix_peer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let my_address = "127.0.0.1:8080";
let mut listener = TcpListener::bind(my_address).await?;
println!("Starting echo server on address {:?}", my_address);
println!("Waiting for input...");
loop {
let (mut inbound, _) = listener.accept().await?;
tokio::spawn(async move {
let mut buf = [0; 1024];
loop {
let _ = match inbound.read(&mut buf).await {
Ok(length) if length == 0 =>
{
println!("Remote connection closed.");
return
}
Ok(length) => {
// println!("Received: {:?}", String::from_utf8(buf[0..length].to_vec()).unwrap());
let next_mix = MixPeer::new();
match next_mix.send(buf).await {
Ok(()) => length,
Err(e) => {
println!("failed to write bytes to next mix peer. err = {:?}", e);
return;
}
}
}
Err(e) => {
println!("failed to read from socket; err = {:?}", e);
return;
}
};
}
});
}
}
+24
View File
@@ -0,0 +1,24 @@
use tokio::net::TcpStream;
use tokio::prelude::*;
use std::error::Error;
pub struct MixPeer {
connection: &'static str,
}
impl MixPeer {
pub fn new() -> MixPeer {
let next_hop_address = "127.0.0.1:8081";
let node = MixPeer {
connection: next_hop_address,
};
node
}
pub async fn send(&self, buf: [u8; 1024]) -> Result<(), Box<dyn Error>>{
let mut stream = TcpStream::connect(self.connection).await?;
stream.write_all(&buf).await?;
Ok(())
}
}