1
Fork 0
mirror of https://github.com/thegeneralist01/p2p-failover synced 2026-03-07 12:29:54 +01:00

remove dead code

This commit is contained in:
TheGeneralist 2025-03-30 15:49:46 +02:00
parent bc39e5caaf
commit 2a87dabc55
No known key found for this signature in database
GPG key ID: C391D4D52D630F45
5 changed files with 13 additions and 208 deletions

View file

@ -15,7 +15,6 @@ macro_rules! debug {
($($arg:tt)*) => {
if $crate::debug::is_debug_enabled() {
println!($($arg)*);
// println!("DEBUG: {}", format!($($arg)*));
}
};
}

View file

@ -20,7 +20,6 @@ macro_rules! log {
($($arg:tt)*) => {
if $crate::log::is_verbose_enabled() {
println!($($arg)*);
// println!("LOG: {}", format!($($arg)*));
}
};
}

View file

@ -10,8 +10,6 @@ pub struct Node {
alives: Vec<bool>,
process: Option<Arc<Mutex<Process>>>,
pub node_connections: NodeConnections,
// tick: u8,
// tick_dir: u8,
}
impl Node {
@ -24,8 +22,6 @@ impl Node {
alives,
process: None,
node_connections: NodeConnections::new(),
// tick: 0,
// tick_dir: 1,
}
}
@ -94,39 +90,6 @@ impl Node {
self.process = Some(Arc::new(Mutex::new(process)));
}
/// Check for config updates and update
#[allow(dead_code)]
async fn check_config_diffs(&mut self) -> bool {
let c = self.config.lock().unwrap().clone();
'outer: for host in c.ddns.iter().enumerate() {
if host.1.name == c.config_metadata.name {
continue;
}
if !self.alives[host.0] {
continue;
}
// Connection
let connection_mutex = if let Some(conn) = self
.node_connections
.get_node_connection(host.1.name.clone())
{
conn
} else if let Some(conn) = self.node_connections.create_node_connection(host.1) {
conn
} else {
// If no connection can be established, continue the outer loop
continue 'outer;
};
log!("Checking for config updates");
let mut connection = connection_mutex.lock().unwrap();
let _ = connection.update_config(self.config.clone());
}
false
}
pub async fn heartbeat(&mut self) {
log!("\n====> Heartbeat");
@ -185,20 +148,6 @@ impl Node {
}
}
// if alives != 0 && self.tick % 5 == 0 {
// self.check_config_diffs().await;
// }
// if self.tick == 0 {
// self.tick_dir = 1;
// } else if self.tick == 5 {
// self.tick_dir = 0;
// }
// if self.tick_dir == 1 {
// self.tick += 1
// } else {
// self.tick -= 1
// };
log!("====> Hearbeat end");
}
}

View file

@ -26,7 +26,7 @@ pub struct NodeInfo {
pub target: String,
pub port: u32,
pub preference: u8,
streamp: Option<TcpStream>,
stream: Option<TcpStream>,
}
impl NodeInfo {
@ -42,12 +42,12 @@ impl NodeInfo {
target,
port,
preference,
streamp,
stream: streamp,
}
}
pub fn update_config(&mut self, config_self_mutex: Arc<Mutex<Config>>) -> Result<()> {
if let Some(ref mut streamp) = self.streamp {
if let Some(ref mut streamp) = self.stream {
let (tx, rx) = mpsc::channel();
let read_stream = streamp.try_clone().unwrap();
@ -64,14 +64,6 @@ impl NodeInfo {
tx.send(String::new()).unwrap_or_default();
}
}
// let mut response = String::new();
// for line in reader.lines().map_while(Result::ok) {
// if line == "DONE" {
// break;
// }
// response.push_str(&line);
// }
// tx.send(response).unwrap_or_default();
});
streamp.write_all(b"GET CONFIG\n")?;
@ -141,7 +133,6 @@ impl NodeInfo {
}
}
#[allow(dead_code)]
#[derive(Clone)]
pub struct NodeConnections {
connections: Vec<Arc<Mutex<NodeInfo>>>,
@ -163,7 +154,7 @@ impl NodeConnections {
pub fn get_node_connection(&self, node_name: String) -> Option<Arc<Mutex<NodeInfo>>> {
for connection in &self.connections {
let conn = connection.lock().unwrap();
if conn.target_name == node_name && conn.streamp.is_some() {
if conn.target_name == node_name && conn.stream.is_some() {
return Some(connection.clone());
}
}
@ -200,12 +191,12 @@ impl NodeConnections {
let connection = connection.unwrap();
let connection_guard = connection.lock().unwrap();
if connection_guard.streamp.is_none() {
if connection_guard.stream.is_none() {
return false;
}
let mut streamp = connection_guard
.streamp
.stream
.as_ref()
.unwrap()
.try_clone()
@ -288,17 +279,16 @@ impl NodeConnections {
pub fn confirm(&mut self, source: &str, is_ip: bool) -> Option<String> {
for connection in &self.connections {
let conn = connection.lock().unwrap();
if conn.streamp.is_none() {
if conn.stream.is_none() {
continue;
}
let mut streamp = conn.streamp.as_ref().unwrap();
let mut streamp = conn.stream.as_ref().unwrap();
streamp
.write_all(format!("CONFIRM:{}:{}\n", is_ip as u8, source).as_bytes())
.unwrap();
let reader = BufReader::new(streamp);
// let mut writer = &streamp;
let sis_ip = is_ip.to_string();
@ -348,11 +338,11 @@ impl NodeConnections {
) -> Option<ProviderNode> {
for connection in &self.connections {
let conn = connection.lock().unwrap();
if conn.streamp.is_none() || conn.target_name != target_name {
if conn.stream.is_none() || conn.target_name != target_name {
continue;
}
let mut streamp = conn.streamp.as_ref().unwrap();
let mut streamp = conn.stream.as_ref().unwrap();
streamp.write_all(b"GET CONFIG\n").unwrap();
let reader = BufReader::new(streamp);
@ -386,11 +376,11 @@ impl NodeConnections {
fn is_connection_alive(connection: Arc<Mutex<NodeInfo>>) -> bool {
let connection_guard = connection.lock().unwrap();
if connection_guard.streamp.is_none() {
if connection_guard.stream.is_none() {
return false;
}
let mut streamp = connection_guard.streamp.as_ref().unwrap();
let mut streamp = connection_guard.stream.as_ref().unwrap();
match streamp.write(&[]) {
Ok(_) => true,
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => true,

View file

@ -7,11 +7,6 @@ use std::thread;
pub fn start_tcp_listener(config: Arc<Mutex<Config>>, config_string: Arc<Mutex<String>>) {
thread::spawn(move || {
//let trustkey_path =
//std::env::var("P2P_TRUSTKEY_PATH").unwrap_or_else(|_| ".p2p-trustkey".to_string());
//let mut trustkey = String::new();
//let _ = get_file(&trustkey_path).read_to_string(&mut trustkey);
let config = config.clone();
let port = {
@ -30,7 +25,7 @@ pub fn start_tcp_listener(config: Arc<Mutex<Config>>, config_string: Arc<Mutex<S
log!("Rocking on port {port}!");
for stream in listener.incoming() {
debug!("CONNECTION established");
debug!("Connection established");
if let Ok(stream) = stream {
let reader = BufReader::new(&stream);
@ -57,133 +52,6 @@ pub fn start_tcp_listener(config: Arc<Mutex<Config>>, config_string: Arc<Mutex<S
let _ = writer.flush();
debug!("Sent config to {}", remote_addr);
}
// else if line.len() > 8 && &line[0..8] == "CONFIRM:" {
// // Template: CONFIRM:is_ip:source
// // is_ip is either 0 or 1
// let parts = line.split(":").collect::<Vec<&str>>();
// if parts.len() != 3 || !(parts[1] == "0" || parts[1] == "1") {
// debug!("CONFIRM FAIL: BAD REQUEST");
// let _ = writer.write_all(b"AUTH FAIL: BAD REQUEST\n");
// continue;
// }
//
// let is_ip = parts[1] == "1";
// let source = parts[2];
//
// let config_guard = config.lock().unwrap();
// let found = config_guard.ddns.iter().any(|d| {if is_ip {&d.ip} else {&d.ddns}} == source);
// let _ = writer
// .write_all({
// if found {
// b"1\n"
// } else {
// b"0\n"
// }
// });
// }
// else if line.len() >= 8 && &line[0..8] == "AUTH REQ" {
// // Template: AUTH:source:port:trustkey:redirect_node
// let parts = line.split(":").collect::<Vec<&str>>();
// if parts.len() < 4 || parts.len() > 5 {
// writer.write_all(b"AUTH FAIL: BAD REQUEST\n").unwrap();
// continue;
// }
//
// let source = parts[2];
// let _source_port = match parts[3].parse::<u32>() {
// Ok(p) => p,
// Err(_) => continue,
// };
// let is_ip = source.chars().all(|c: char| c == '.' || c.is_ascii_digit());
//
// // Check if other Nodes have it
// {
// let mut node_guard = node.lock().unwrap();
// let node_confirmed = node_guard.node_connections.confirm(source, is_ip);
// if let Some(node_confirmed) = node_confirmed {
// let provider = node_guard.node_connections.get_config_for(
// source,
// is_ip,
// node_confirmed,
// );
// if let Some(provider) = provider {
// let mut config_guard = config.lock().unwrap();
// config_guard.ddns.push(provider);
// }
// }
// };
//
// // DDNS; Verification
// if is_ip && remote_addr != source {
// writer.write_all(b"AUTH FAIL: SOURCE MISMATCH\\nn").unwrap();
// continue;
// }
//
// let request_trustkey = parts[4];
// if request_trustkey != trustkey {
// writer.write_all(b"AUTH FAIL: TRUSTKEY MISMATCH\n").unwrap();
// continue;
// }
//
// let config_guard = config.lock().unwrap();
// let ddns = config_guard.ddns.iter().find(|d| d.name == source);
// if ddns.is_some() {
// writer.write_all(b"AUTH SUCCESS: ALREADY EXISTS\n").unwrap();
// continue;
// }
//
// // TODO: A) This. Search for `TODO: A)`
// verifications.push(PendingVerification {
// source: source.to_string(),
// remote_addr,
// redirect_node: {
// if parts.len() >= 5 {
// parts[5]
// } else {
// ""
// }
// }
// .to_string(),
// is_ip,
// });
//
// writer.write_all(b"GET CONFIG\n").unwrap();
// }
// else if line.len() > 12 && &line[0..12] == "AUTH PENDING" {
// // Template: AUTH PENDING:config
// if !verifications.iter().any(|v| remote_addr == v.remote_addr) {
// writer.write_all(b"AUTH FAIL: NOT PENDING\n").unwrap();
// continue;
// };
//
// let config_incoming = &line[13..line.len()];
// let mut parser_incoming = Parser::new(config_incoming.as_bytes());
//
// let config_incoming = match parser_incoming.parse(None) {
// Ok(cfg) => cfg,
// Err(_) => {
// writer.write_all(b"AUTH FAIL: BAD CONFIG\n").unwrap();
// continue;
// }
// };
//
// let ddns_incoming = match config_incoming
// .ddns
// .iter()
// .find(|d| d.name == config_incoming.config_metadata.name)
// {
// Some(d) => d,
// None => {
// writer
// .write_all(b"AUTH FAIL: SELF ABSENT IN DDNS\n")
// .unwrap();
// continue;
// }
// };
//
// let mut config_guard = config.lock().unwrap();
// config_guard.ddns.push(ddns_incoming.clone());
// }
}
}
}