Skip to content

Commit

Permalink
Get servers compiling without Arc wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofl committed Mar 27, 2024
1 parent 2feeae5 commit c88496f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
21 changes: 12 additions & 9 deletions src/servers/ftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::sync::Arc;
#[async_trait]
pub trait FTPRunner {
fn new(path: PathBuf, bind_ip: String, port: u16) -> Self;
async fn runner(self: Arc<Self>) -> JoinHandle<()>;
async fn runner(&self) -> JoinHandle<()>;
}

#[async_trait]
Expand All @@ -31,29 +31,32 @@ impl FTPRunner for Server {
s
}

async fn runner(self: Arc<Self>) -> JoinHandle<()> {
async fn runner(&self) -> JoinHandle<()> {
let mut receiver = self.sender.subscribe();

let bind_address = self.bind_address.clone();
let port = self.port;

tokio::spawn(async move {
// Get notified about the server's spawned task
let mut receiver = self.sender.subscribe();
loop {
let m = receiver.recv().await.unwrap();
let mut receiver_c = self.sender.subscribe();

if m.connect {
// Define new server
let _ = libunftp::Server::with_fs(*__self.path.clone())
let _ = libunftp::Server::with_fs("/tmp/")
.passive_ports(50000..65535)
.metrics()
.shutdown_indicator(async move {
loop {
let _ = receiver_c.recv().await.unwrap();
let _ = receiver.recv().await.unwrap();
break;
}
debug!("Gracefully terminating the FTP server");
//Give seconds to potential ongoing connections to finish, otherwise finish immediately
// Give a few seconds to potential ongoing connections to finish,
// otherwise finish immediately
libunftp::options::Shutdown::new().grace_period(Duration::from_secs(5))
})
.listen(format!("{}:{}", self.bind_address, self.port))
.listen(format!("{}:{}", bind_address, port))
.await;

break;
Expand Down
19 changes: 7 additions & 12 deletions src/servers/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use log::{debug, info};

use tower_http::services::ServeDir;
use std::net::SocketAddr;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::Arc;
use crate::servers::Protocol;
Expand Down Expand Up @@ -37,29 +38,23 @@ impl HTTPRunner for Server {
let mut receiver = self.sender.subscribe();

// TODO: get it from the right place
// Parse the IP address string into an IpAddr
// let ip: IpAddr = self.bind_address.parse().expect("Invalid IP address");
let ip = [127, 0, 0, 1];
// let port = self.port;
let path = *self.path.clone();
let port = self.port;
let path = self.path.clone();

tokio::spawn(async move {
loop {
debug!("Runner started. Waiting command to connect...");
let m = receiver.recv().await.unwrap();
debug!("Message received");

// if m.stop { return };
if m.connect {
info!("Connecting...");
// Spin and await the actual server here
// Parse the IP address string into an IpAddr
// let ip: IpAddr = self.bind_address.parse().expect("Invalid IP address");
// let me = Arc::clone(&self);

// Create a SocketAddr from the IpAddr and port
let socket_addr = SocketAddr::new(ip.into(), self.port);
// let me = Arc::clone(&self);
// let p = String::from("");
let service = ServeDir::new(path);
let socket_addr = SocketAddr::new(ip.into(), port);
let service = ServeDir::new(path.deref());
let _ = hyper::server::Server::bind(&socket_addr)
.serve(tower::make::Shared::new(service))
.with_graceful_shutdown(async {
Expand Down
21 changes: 12 additions & 9 deletions src/servers/tftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use async_trait::async_trait;

// Create the TFTP server.
use async_tftp::server::TftpServerBuilder;
use std::path::PathBuf;
use std::{ops::Deref, path::PathBuf};
use crate::utils::validation;
use std::sync::Arc;
use tokio::task::JoinHandle;

#[async_trait]
pub trait TFTPRunner {
fn new(path: PathBuf, bind_ip: String, port: u16) -> Self;
async fn runner(self: Arc<Self>) -> JoinHandle<()>;
async fn runner(&self) -> JoinHandle<()>;
}

#[async_trait]
Expand All @@ -32,26 +32,29 @@ impl TFTPRunner for Server {

return s;
}
async fn runner(self: Arc<Self>) -> JoinHandle<()> {
async fn runner(&self) -> JoinHandle<()> {

let path = self.path.clone();

let bind_address = self.bind_address.clone();
let port = self.port;
let mut receiver = self.sender.subscribe();

tokio::spawn(async move {
// Get notified about the server's spawned task
let mut receiver = self.sender.subscribe();

loop {
// Get notified about the server's spawned task
let msg = receiver.recv().await.unwrap();

if msg.connect {
let tsk = tokio::spawn({
let me = Arc::clone(&self);
let path = path.clone();
let bind_address = bind_address.clone();
let port = port;

async move {
let addr = format!("{}:{}", me.bind_address, me.port);
let addr = format!("{}:{}", bind_address, port);
let tftpd =
TftpServerBuilder::with_dir_ro(me.path.clone()).unwrap()
TftpServerBuilder::with_dir_ro(path.deref()).unwrap()
.bind(addr.parse().unwrap())
.build().await.unwrap();

Expand Down

0 comments on commit c88496f

Please sign in to comment.