Skip to content

implement basic nonblocking TCP functionality #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn walk_dir(path: &Path, prefix: &str) -> Result<BTreeMap<PathBuf, blake3::H
.replace(&cwd, "")
.starts_with(&prefix)
{
let blake3_hash = hash_file(&entry_path, &USR_CONFIG.hash_key.to_owned())?;
let blake3_hash = hash_file(&entry_path, &USR_CONFIG.hash_key)?;

map.insert(entry_path, blake3_hash);
}
Expand Down Expand Up @@ -86,7 +86,7 @@ pub async fn upload_path(prefix: &str, data_dir: &Path) -> Result<()> {
bao_hash,
read,
written,
} = encode(&file_path, &blake3_hash.to_hex().to_string()).await?;
} = encode(&file_path, &blake3_hash.to_hex()).await?;

let parent_rev = upsert_path(&file_path.to_string_lossy(), blake3_bytes)?;
let mime_type = infer_mime_type(&file_path)?;
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::thread;

use anyhow::Result;
use log::{error, info, warn};
use tokio::signal;

use crate::net::listen;

pub mod config;
pub mod db;
pub mod file;
Expand Down Expand Up @@ -109,8 +113,12 @@ pub async fn list_files(_prefix: &str, _depth: usize) -> Result<()> {

pub async fn start() -> Result<()> {
info!("Starting Forage node...");

let _net_task = thread::spawn(move || listen(Some("0.0.0.0:5000".to_string())));
signal::ctrl_c().await?;

info!("Forage shutting down ..");

Ok(())
}

Expand Down
44 changes: 44 additions & 0 deletions src/net.rs
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
// use torut;

use std::{
io::Read,
net::{Shutdown, TcpListener, TcpStream},
thread,
};

pub fn listen(address: Option<String>) {
let listener = match address {
Some(addr) => TcpListener::bind(addr),
None => TcpListener::bind("0.0.0.0:5000"),
}
.unwrap();

let _ = listener.set_nonblocking(true);

log::info!(
"Forage listening on port {}",
listener.local_addr().unwrap().port()
);
for stream in listener.incoming().flatten() {
log::info!(
"Connection recieved with peer {}",
stream.peer_addr().unwrap()
);
let _ = thread::spawn(move || handle_client(stream));
}
}

fn handle_client(mut stream: TcpStream) {
let mut data: Vec<u8> = Vec::new();
while match stream.read(&mut data) {
Ok(_) => true,
Err(_) => {
log::error!(
"error occured, terminating connection with {}",
stream.peer_addr().unwrap()
);
stream.shutdown(Shutdown::Both).unwrap();
false
}
} {}
println!("{:?}", data);
}