I added threading (I don't really know what I'm doing)

The bytes update whenever a tracked file is edited/deleted.
This commit is contained in:
2025-11-28 23:50:38 +00:00
parent dfd6c9c370
commit ce643f2066
5 changed files with 139 additions and 204 deletions

View File

View File

@@ -1,27 +1,66 @@
extern crate notify;
use notify::{
DebouncedEvent,
RecommendedWatcher,
RecursiveMode,
Watcher
Watcher,
Result,
Event,
};
use std::
{
fs,
time::Duration,
path::Path,
sync::mpsc::channel,
path::PathBuf,
sync::mpsc,
};
use crate::
{
ROOT_DIR,
};
pub fn watch_files(file_list: &Vec<PathBuf>) -> u8
{
let (tx, rx) = mpsc::channel::<Result<Event>>(); // Use recommended_watcher() to automatically select the best implementation
// for your platform. The `EventHandler` passed to this constructor can be a
// closure, a `std::sync::mpsc::Sender`, a `crossbeam_channel::Sender`, or
// another type the trait is implemented for.
let mut watcher = notify::recommended_watcher(tx).expect("Watcher start failed"); // Add a path to be watched. All files and directories at that path and
// below will be monitored for changes.
watcher.watch(Path::new("root"), RecursiveMode::Recursive).expect("Path watching failed"); // Block forever, printing out events as they come in
for res in rx
{
match res
{
Ok(event) =>
{
// Ignore access event kinds as they're irrelevant
if ! notify::EventKind::is_access(&event.kind)
&& file_list.contains(&event.paths[0])
{
return 0
}
},
Err(e) =>
{
println!("watch error: {:?}", e);
return 2
}
}
}
println!("Watching files...");
return 1
}
pub fn read_file(file_name: &str, depth: u8) ->
(
Vec<u8>,
Vec<String>,
Vec<PathBuf>,
)
{
if let Ok(file_contents) = fs::read_to_string(format!("root/{}",file_name))
{
let (bytes, mut file_list) = encode_npon(&file_contents, depth);
let (bytes, file_list) = encode_npon(&file_contents, depth);
return (bytes, file_list)
}
return (vec![], vec![])
@@ -30,11 +69,11 @@ pub fn read_file(file_name: &str, depth: u8) ->
fn encode_npon(file_contents: &String, depth: u8) ->
(
Vec<u8>,
Vec<String>,
Vec<PathBuf>,
)
{
let mut bytes: Vec<u8> = vec![];
let mut file_list: Vec<String> = vec![];
let mut file_list: Vec<PathBuf> = vec![];
if depth > 63 {
println!("Depth level safety reached (63), recursion error suspected");
return (vec![], file_list);
@@ -139,7 +178,7 @@ fn encode_npon(file_contents: &String, depth: u8) ->
{
58 =>
{
file_list.push((&file_contents[index_flag..index.clone()]).to_string());
file_list.push(ROOT_DIR.join(&file_contents[index_flag..index.clone()]));
let (new_bytes, mut new_file_list) = read_file(&file_contents[index_flag..index], depth+1);
file_list.append(&mut new_file_list);
for byte in new_bytes.iter()

View File

@@ -1,24 +1,67 @@
use std::
{
io,
fs,
io::{BufReader, prelude::*},
net::{TcpListener, TcpStream},
sync::mpsc,
thread,
path::PathBuf,
};
use lazy_static::lazy_static;
mod files;
lazy_static! {
static ref ROOT_DIR: PathBuf = fs::canonicalize(PathBuf::from("./root")).unwrap();
}
// TODO, remove .unwrap()
// TODO, make 'code' an enum
// TODO, safely modify bytes in thread.
// TODO, Makes paths more consistent.
// TODO, Avoid copying bytes
fn main() -> io::Result<()>
{
let bind_address: &str = "127.0.0.1:2025";
let index_file: &str = "index.npon";
let listener = TcpListener::bind(bind_address).unwrap();
println!("Listening at {}", bind_address);
let mut file_list: Vec<String> = vec![index_file.to_string()];
let (bytes, mut new_file_list) = files::read_file(index_file, 0);
// Default values
const BIND_ADDRESS: &str = "127.0.0.1:2008";
const INDEX_FILE: &str = "index.npon";
let (tx, rx) = mpsc::channel();
let listener = TcpListener::bind(BIND_ADDRESS).unwrap();
println!("Listening at {}", BIND_ADDRESS);
/*
let mut file_list: Vec<std::path::PathBuf> = vec![ROOT_DIR.join(INDEX_FILE)];
let (mut bytes, mut new_file_list) = files::read_file(INDEX_FILE, 0);
file_list.append(&mut new_file_list);
println!("Files encoded: {:?}", file_list);
*/
// File watch thread
thread::spawn(
move || {
let mut bytes: Vec<u8>;
let mut file_list: Vec<PathBuf>;
(bytes, file_list) = files::read_file(INDEX_FILE, 0);
file_list.push(ROOT_DIR.join(INDEX_FILE));
tx.send(bytes.clone()).unwrap();
'watch_loop: loop
{
let code = files::watch_files(&file_list);
if code == 0
{
println!("Re encoding index");
(bytes, file_list) = files::read_file(INDEX_FILE, 0);
file_list.push(ROOT_DIR.join(INDEX_FILE));
tx.send(bytes.clone()).unwrap();
}
}
}
);
for stream in listener.incoming() {
let bytes = rx.recv().unwrap();
let stream = stream.unwrap();
handle_connection(stream, &bytes);
}