From 7eedcec53671d43425fdddbdab0d2fb98ac8df3e Mon Sep 17 00:00:00 2001 From: Akilan Date: Thu, 26 Jun 2025 17:13:26 +0100 Subject: [PATCH] added base process code --- process/.gitignore | 104 +++++++++++++++++++ process/Cargo.lock | 202 +++++++++++++++++++++++++++++++++++++ process/Cargo.toml | 14 +++ process/p2prc-process.sh | 66 ++++++++++++ process/src/SpinProcess.rs | 97 ++++++++++++++++++ process/src/p2prc.rs | 0 process/test.sh | 1 + 7 files changed, 484 insertions(+) create mode 100644 process/.gitignore create mode 100644 process/Cargo.lock create mode 100644 process/Cargo.toml create mode 100644 process/p2prc-process.sh create mode 100644 process/src/SpinProcess.rs create mode 100644 process/src/p2prc.rs create mode 100644 process/test.sh diff --git a/process/.gitignore b/process/.gitignore new file mode 100644 index 0000000..6419b78 --- /dev/null +++ b/process/.gitignore @@ -0,0 +1,104 @@ +# This file should only ignore things that are generated during a `x.py` build, +# generated by common IDEs, and optional files controlled by the user that +# affect the build (such as bootstrap.toml). +# In particular, things like `mir_dump` should not be listed here; they are only +# created during manual debugging and many people like to clean up instead of +# having git ignore such leftovers. You can use `.git/info/exclude` to +# configure your local ignore list. + +## File system +.DS_Store +desktop.ini + +## Editor +*.swp +*.swo +Session.vim +.cproject +.idea +*.iml +.vscode +.project +.vim/ +.helix/ +.zed/ +.favorites.json +.settings/ +.vs/ +.dir-locals.el + +## Tool +.valgrindrc +.cargo +# Included because it is part of the test case +!/tests/run-make/thumb-none-qemu/example/.cargo + +## Configuration +/bootstrap.toml +/config.toml +/Makefile +config.mk +config.stamp +no_llvm_build + +## Build +/dl/ +/doc/ +/inst/ +/llvm/ +/mingw-build/ +/build +/build-rust-analyzer/ +/dist/ +/unicode-downloads +/target +/library/target +/src/bootstrap/target +/src/ci/citool/target +/src/tools/x/target +# Created by `x vendor` +/vendor +# Created by default with `src/ci/docker/run.sh` +/obj/ +# Created by nix dev shell / .envrc +src/tools/nix-dev-shell/flake.lock + +## ICE reports +rustc-ice-*.txt + +## Temporary files +*~ +\#* +\#*\# +.#* + +## Tags +tags +tags.* +TAGS +TAGS.* + +## Python +__pycache__/ +*.py[cod] +*$py.class + +## Node +node_modules +package-lock.json +package.json +/src/doc/rustc-dev-guide/mermaid.min.js + +## Rustdoc GUI tests +tests/rustdoc-gui/src/**.lock + +## direnv +/.envrc +/.direnv/ + +## nix +/flake.nix +flake.lock +/default.nix + +# Before adding new lines, see the comment at the top. \ No newline at end of file diff --git a/process/Cargo.lock b/process/Cargo.lock new file mode 100644 index 0000000..a62c01a --- /dev/null +++ b/process/Cargo.lock @@ -0,0 +1,202 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bitflags" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" + +[[package]] +name = "bumpalo" +version = "3.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" + +[[package]] +name = "cfg-if" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" + +[[package]] +name = "getrandom" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi", +] + +[[package]] +name = "js-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.174" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rustversion" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d" + +[[package]] +name = "spin_process" +version = "0.1.0" +dependencies = [ + "uuid", +] + +[[package]] +name = "syn" +version = "2.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "uuid" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cf4199d1e5d15ddd86a694e4d0dffa9c323ce759fea589f00fef9d81cc1931d" +dependencies = [ + "getrandom", + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] diff --git a/process/Cargo.toml b/process/Cargo.toml new file mode 100644 index 0000000..8fc86bb --- /dev/null +++ b/process/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "spin_process" +version = "0.1.0" +edition = "2021" +authors = ["Your Name "] +description = "Simulates a process launcher similar to a Bash script" +license = "MIT" + +[dependencies] +uuid = { version = "1", features = ["v4"] } + +[[bin]] +name = "spin_process" +path = "src/SpinProcess.rs" \ No newline at end of file diff --git a/process/p2prc-process.sh b/process/p2prc-process.sh new file mode 100644 index 0000000..7e88688 --- /dev/null +++ b/process/p2prc-process.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# Simulate NodeInfo as separate variables +NodeInfo_IPV4="192.168.1.20" +NodeInfo_ServerPort="9000" + +# UUID fallback +generate_uuid() { + echo "uuid-$(date +%s%N)" +} + +P2PRCMapPort() { + local port="$1" + local domain="$2" + local server="$3" + echo "${domain}:${port}" +} + +AddProcessToMemory() { + local id="$1" + echo "Added to memory: $id" +} + +SaveProcess() { + echo "Saved to disk (simulated)" +} + +SpinProcess() { + # Parameters are passed as positional args + local InternalPortNo="$1" + local CommandToRunScript="$2" + local DomainName="$3" + + # Run script (locally) + bash "$CommandToRunScript" + + # Server address + local ServerAddress="${NodeInfo_IPV4}:${NodeInfo_ServerPort}" + + # Map port + local ExternalAddress + ExternalAddress=$(P2PRCMapPort "$InternalPortNo" "$DomainName" "$ServerAddress") + + # Generate UUID and set status + local ID + ID=$(generate_uuid) + local Status="true" + + # Output simulated "process object" + echo "Process Started:" + echo "ID: $ID" + echo "InternalPortNo: $InternalPortNo" + echo "ExternalAddress: $ExternalAddress" + echo "DomainName: $DomainName" + echo "Status: $Status" + + AddProcessToMemory "$ID" + SaveProcess +} + +# Example usage +InternalPortNo="8080" +CommandToRunScript="test.sh" +DomainName="example.com" + +SpinProcess "$InternalPortNo" "$CommandToRunScript" "$DomainName" diff --git a/process/src/SpinProcess.rs b/process/src/SpinProcess.rs new file mode 100644 index 0000000..dd6f806 --- /dev/null +++ b/process/src/SpinProcess.rs @@ -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); +} \ No newline at end of file diff --git a/process/src/p2prc.rs b/process/src/p2prc.rs new file mode 100644 index 0000000..e69de29 diff --git a/process/test.sh b/process/test.sh new file mode 100644 index 0000000..267fdb9 --- /dev/null +++ b/process/test.sh @@ -0,0 +1 @@ +echo "hello start" \ No newline at end of file