added base process code

This commit is contained in:
2025-06-26 17:13:26 +01:00
parent 58c80760de
commit 7eedcec536
7 changed files with 484 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
mod p2prc;
use std::process::Command;
use std::fs::{self, File};
use std::io::{self, Write};
use uuid::Uuid;
// Struct representing NodeInfo
#[derive(Debug)]
struct NodeInfo {
ipv4: String,
server_port: String,
}
// Struct representing Process
#[derive(Debug)]
struct Process {
id: String,
internal_port_no: String,
external_address: String,
node_id: String,
task_name: String,
command_to_run_script: String,
command_to_kill_script: String,
status: bool,
domain_name: String,
}
// Simulate mapping a port to a public address
fn p2prc_map_port(port: &str, domain: &str, server_address: &str) -> String {
format!("{}:{}", domain, port) // Simulate mapping
}
// Simulate saving a process to memory (e.g., to a file or in-memory list)
fn add_process_to_memory(process: &Process) {
println!("Process added to memory: {:?}", process);
}
// Simulate saving the process to disk (e.g., JSON file)
fn save_process(process: &Process) -> io::Result<()> {
let mut file = File::create("process_saved.txt")?;
writeln!(file, "{:?}", process)?;
Ok(())
}
// The SpinProcess function
fn spin_process(mut process: Process) -> Process {
// 1. Run the script
let _ = Command::new("bash")
.arg(&process.command_to_run_script)
.status()
.expect("Failed to start script");
// 2. Generate server address
let server_address = format!("{}:{}", "0.0.0.0", "1245");
// 3. Map to external address
process.external_address = p2prc_map_port(&process.internal_port_no, &process.domain_name, &server_address);
// 4. Generate UUID
process.id = Uuid::new_v4().to_string();
// 5. Set status
process.status = true;
// 6. Save process to memory and disk
add_process_to_memory(&process);
// save_process(&process).expect("Failed to save process");
process
}
// Main function
fn main() {
println!("Starting SpinProcess...");
let node_info = NodeInfo {
ipv4: "192.168.1.20".to_string(),
server_port: "9000".to_string(),
};
let process = Process {
id: String::new(),
internal_port_no: "".to_string(),
external_address: "".to_string(),
node_id: "Test".to_string(),
task_name: "ExampleTask".to_string(),
command_to_run_script: "./run_script.sh".to_string(),
command_to_kill_script: "./kill_script.sh".to_string(),
status: false,
domain_name: "example.com".to_string(),
};
let updated = spin_process(process);
println!("Final Process: {:?}", updated);
}