diff --git a/Cargo.lock b/Cargo.lock index ea3e894..1577f8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -182,9 +182,9 @@ version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fdde5c9cd29ebd706ce1b35600920a33550e402fc998a2e53ad3b42c3c47a192" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", + "proc-macro2 1.0.81", + "quote 1.0.36", + "syn 2.0.60", ] [[package]] @@ -280,8 +280,8 @@ checksum = "ae6371b8bdc8b7d3959e9cf7b22d4435ef3e79e138688421ec654acf8c81b008" dependencies = [ "heck 0.4.1", "proc-macro-error", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "syn 1.0.109", ] @@ -377,6 +377,13 @@ dependencies = [ "memchr", ] +[[package]] +name = "command_sever" +version = "0.1.0" +dependencies = [ + "serde_json", +] + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -646,8 +653,8 @@ checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b" dependencies = [ "fnv", "ident_case", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "strsim 0.9.3", "syn 1.0.109", ] @@ -660,8 +667,8 @@ checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610" dependencies = [ "fnv", "ident_case", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "strsim 0.10.0", "syn 1.0.109", ] @@ -673,7 +680,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" dependencies = [ "darling_core 0.10.2", - "quote 1.0.26", + "quote 1.0.36", "syn 1.0.109", ] @@ -684,7 +691,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835" dependencies = [ "darling_core 0.13.4", - "quote 1.0.26", + "quote 1.0.36", "syn 1.0.109", ] @@ -704,8 +711,8 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "syn 1.0.109", ] @@ -967,9 +974,9 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", + "proc-macro2 1.0.81", + "quote 1.0.36", + "syn 2.0.60", ] [[package]] @@ -1797,8 +1804,8 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01fcc0b8149b4632adc89ac3b7b31a12fb6099a0317a4eb2ebff574ef7de7218" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "syn 1.0.109", ] @@ -1919,8 +1926,8 @@ checksum = "05d1c6307dc424d0f65b9b06e94f88248e6305726b14729fd67a5e47b2dc481d" dependencies = [ "darling 0.10.2", "proc-macro-crate 0.1.5", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "syn 1.0.109", ] @@ -1932,8 +1939,8 @@ checksum = "0df7ac00c4672f9d5aece54ee3347520b7e20f158656c7db2e6de01902eb7a6c" dependencies = [ "darling 0.13.4", "proc-macro-crate 1.3.1", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "syn 1.0.109", ] @@ -2159,8 +2166,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffa5a33ddddfee04c0283a7653987d634e880347e96b5b2ed64de07efb59db9d" dependencies = [ "proc-macro-crate 0.1.5", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "syn 1.0.109", ] @@ -2171,8 +2178,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcbff9bc912032c62bf65ef1d5aea88983b420f4f839db1e9b0c281a25c9c799" dependencies = [ "proc-macro-crate 1.3.1", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "syn 1.0.109", ] @@ -2392,8 +2399,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "syn 1.0.109", "version_check", ] @@ -2404,8 +2411,8 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "version_check", ] @@ -2420,9 +2427,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.56" +version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba" dependencies = [ "unicode-ident", ] @@ -2438,11 +2445,11 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.26" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ - "proc-macro2 1.0.56", + "proc-macro2 1.0.81", ] [[package]] @@ -2676,29 +2683,29 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.162" +version = "1.0.199" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71b2f6e1ab5c2b98c05f0f35b236b22e8df7ead6ffbf51d7808da7f8817e7ab6" +checksum = "0c9f6e76df036c77cd94996771fb40db98187f096dd0b9af39c6c6e452ba966a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.162" +version = "1.0.199" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2a0814352fd64b58489904a44ea8d90cb1a91dcb6b4f5ebabc32c8318e93cb6" +checksum = "11bd257a6541e141e42ca6d24ae26f7714887b47e89aa739099104c7e4d3b7fc" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", + "proc-macro2 1.0.81", + "quote 1.0.36", + "syn 2.0.60", ] [[package]] name = "serde_json" -version = "1.0.96" +version = "1.0.116" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" +checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813" dependencies = [ "itoa", "ryu", @@ -2914,19 +2921,19 @@ version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "unicode-ident", ] [[package]] name = "syn" -version = "2.0.15" +version = "2.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "unicode-ident", ] @@ -2960,9 +2967,9 @@ version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", + "proc-macro2 1.0.81", + "quote 1.0.36", + "syn 2.0.60", ] [[package]] @@ -3186,8 +3193,8 @@ dependencies = [ "bumpalo", "log", "once_cell", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "syn 1.0.109", "wasm-bindgen-shared", ] @@ -3210,7 +3217,7 @@ version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5" dependencies = [ - "quote 1.0.26", + "quote 1.0.36", "wasm-bindgen-macro-support", ] @@ -3220,8 +3227,8 @@ version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "syn 1.0.109", "wasm-bindgen-backend", "wasm-bindgen-shared", @@ -3361,8 +3368,8 @@ version = "0.28.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce923eb2deb61de332d1f356ec7b6bf37094dc5573952e1c8936db03b54c03f1" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "xml-rs", ] @@ -3372,8 +3379,8 @@ version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f4303d8fa22ab852f789e75a967f0a2cdc430a607751c0499bada3e451cbd53" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.81", + "quote 1.0.36", "xml-rs", ] diff --git a/Cargo.toml b/Cargo.toml index c18908c..43372ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["ui_demo", "mjoy_core"] +members = ["ui_demo", "mjoy_core", "command_server"] diff --git a/command_server/Cargo.toml b/command_server/Cargo.toml new file mode 100644 index 0000000..5615e13 --- /dev/null +++ b/command_server/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "command_sever" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde_json = "1.0.116" + +[lib] +name = "command_server" +path = "src/lib.rs" + +[[example]] +name = "helloworld" +path = "examples/helloworld.rs" + diff --git a/command_server/examples/helloworld.rs b/command_server/examples/helloworld.rs new file mode 100644 index 0000000..72b3f93 --- /dev/null +++ b/command_server/examples/helloworld.rs @@ -0,0 +1,24 @@ +use command_server; +use std::sync::mpsc::channel; +use std::thread; + +fn main() { + // Create a channel for sending commands to the library + let (sender, receiver) = channel(); + + // Start the library's server in a separate thread + thread::spawn(move || { + if let Err(err) = command_server::field_commands_forever(sender) { + eprintln!("Error in library: {:?}", err); + } + }); + + // Main thread can now handle received commands or perform other tasks + // For demonstration, let's just print received commands + for received_cmd in receiver { + println!("Received command: {:?}", received_cmd); + // Implement your logic to handle the received commands here + } + + // Optionally, you can perform cleanup or other tasks before exiting +} diff --git a/command_server/src/lib.rs b/command_server/src/lib.rs new file mode 100644 index 0000000..24554a5 --- /dev/null +++ b/command_server/src/lib.rs @@ -0,0 +1,109 @@ +use std::io::prelude::*; +use std::net::{TcpListener, TcpStream}; +use std::sync::mpsc::{SendError, Sender}; +use std::thread; + +// Constants for server configuration +const HOST: &str = "0.0.0.0"; +const PORT: &str = "5001"; + +// Enum to represent different commands +#[derive(Debug)] +pub enum Command { + Setup, + Start, + Teams(usize), +} + +pub fn field_commands_forever(sender: Sender) -> Result<(), SendError> { + // Bind to the host and port + let endpoint = format!("{}:{}", HOST, PORT); + let listener = TcpListener::bind(endpoint).unwrap(); + println!("Web server is listening at port {}", PORT); + + // Accept incoming connections + for incoming_stream in listener.incoming() { + let stream = incoming_stream.unwrap(); + + // Spawn a new thread to handle the connection + let sender_clone = sender.clone(); + thread::spawn(move || { + if let Err(err) = handle_connection(stream, sender_clone) { + eprintln!("Error handling connection: {:?}", err); + } + }); + } + Ok(()) +} + +fn handle_connection(mut stream: TcpStream, sender: Sender) -> Result<(), std::io::Error> { + // Buffer to read the incoming request + let mut buffer = [0; 1024]; + stream.read(&mut buffer)?; + + // Convert the request buffer to a string + let request_str = String::from_utf8_lossy(&buffer); + println!("Request: {}", request_str); + + // Parse the request to determine the command + let command = parse_command(&request_str); + match command { + Some(cmd) => { + // Send the command through the channel + sender.send(cmd).unwrap(); + } + None => { + eprintln!("Invalid command received"); + } + } + + let response = "HTTP/1.1 200 OK\n"; + + // Send the response back to the client + stream.write(response.as_bytes())?; + stream.flush()?; + Ok(()) +} + +// Function to parse the command from the request string +fn parse_command(request_str: &str) -> Option { + // Implement your parsing logic here + // For simplicity, let's assume the command is extracted from the request + // and construct a Command enum accordingly. + // In a real application, you would parse the request according to your protocol. + // This is just a placeholder. + if request_str.contains("setup") { + Some(Command::Setup) + } else if request_str.contains("start") { + Some(Command::Start) + } else if let Some(teams_count) = extract_teams_count(request_str) { + Some(Command::Teams(teams_count)) + } else { + None + } +} + +use serde_json::Value; + +// Function to extract the number of teams from the request string +fn extract_teams_count(request_str: &str) -> Option { + // Find the start and end positions of the JSON object within curly braces + let start_pos = request_str.find('{')?; + let end_pos = request_str.rfind('}')?; + + // Extract the JSON string + let json_str = &request_str[start_pos..=end_pos]; + + // Parse the JSON string + if let Ok(json_value) = serde_json::from_str::(json_str) { + // Check if the JSON object contains the "teams" field + if let Some(teams_value) = json_value.get("teams") { + // Try to parse the value of "teams" as usize + if let Some(teams_count) = teams_value.as_u64() { + return Some(teams_count as usize); + } + } + } + + None +} diff --git a/config.json b/config.json index be4b73d..bd5d5f7 100644 --- a/config.json +++ b/config.json @@ -1,8 +1,8 @@ { - "team_allocation": [2,2], - "path_hash_salt": 10030, + "team_allocation": [2,2,1], + "path_hash_salt": 10031, "path_common_name_max_length": 12, - "team_hash_salt": 20006, + "team_hash_salt": 20010, "hat_only_players": [], "number_of_multi_port_controllers_to_use": 1 } diff --git a/webapp/app.py b/webapp/app.py new file mode 100644 index 0000000..296748d --- /dev/null +++ b/webapp/app.py @@ -0,0 +1,44 @@ +from flask import Flask, render_template, request, redirect +import requests +import time + +app = Flask(__name__) + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/team1', methods=['POST']) +def team1(): + data = {'teams': 1} + requests.post('http://192.168.0.238:5001/team', json=data) + return render_template('success.html', redirect_url='/') +@app.route('/team2', methods=['POST']) +def team2(): + data = {'teams': 2} + requests.post('http://192.168.0.238:5001/team', json=data) + return render_template('success.html', redirect_url='/') +@app.route('/team3', methods=['POST']) +def team3(): + data = {'teams': 3} + requests.post('http://192.168.0.238:5001/team', json=data) + return render_template('success.html', redirect_url='/') +@app.route('/team4', methods=['POST']) +def team4(): + data = {'teams': 4} + requests.post('http://192.168.0.238:5001/team', json=data) + return render_template('success.html', redirect_url='/') + +@app.route('/setup', methods=['POST']) +def reset(): + requests.post('http://192.168.0.238:5001/setup') + return render_template('success.html', redirect_url='/') + +@app.route('/start', methods=['POST']) +def start(): + requests.post('http://192.168.0.238:5001/start') + return render_template('success.html', redirect_url='/') + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000, debug=True) + diff --git a/webapp/templates/index.html b/webapp/templates/index.html new file mode 100644 index 0000000..b6e38da --- /dev/null +++ b/webapp/templates/index.html @@ -0,0 +1,76 @@ + + + + Control Console + + + +
+
+ +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+ +
+
+ + diff --git a/webapp/templates/success.html b/webapp/templates/success.html new file mode 100644 index 0000000..6fc5c49 --- /dev/null +++ b/webapp/templates/success.html @@ -0,0 +1,12 @@ + + + + Success + + + +

Request sent successfully

+

Redirecting...

+ + +