From 74bf2e71e4bc4d325e1dd4652747dffebe1e7beb Mon Sep 17 00:00:00 2001 From: Trevor Merritt Date: Fri, 23 May 2025 09:20:39 -0400 Subject: [PATCH] moves constants out of client handler --- gemma/src/chip8/computer_manager.rs | 3 +-- gemmatelnet/src/client_handler.rs | 30 ++++++++++++++--------------- gemmatelnet/src/constants.rs | 9 +++++++++ gemmatelnet/src/lib.rs | 4 ++-- 4 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 gemmatelnet/src/constants.rs diff --git a/gemma/src/chip8/computer_manager.rs b/gemma/src/chip8/computer_manager.rs index 98be529..c9d0d30 100644 --- a/gemma/src/chip8/computer_manager.rs +++ b/gemma/src/chip8/computer_manager.rs @@ -1,6 +1,6 @@ use std::fs::File; use std::io::{BufReader, Read}; -use std::path::{Path, PathBuf}; +use std::path::Path; use crate::chip8::computer::Chip8Computer; use crate::chip8::cpu_states::Chip8CpuStates; use crate::chip8::cpu_states::Chip8CpuStates::WaitingForInstruction; @@ -147,7 +147,6 @@ impl Chip8ComputerManager { } pub fn load_new_program_from_disk_to_system_memory(&mut self, file_to_load: &Path) { - println!("ComputerManager: Preparing to load {}", file_to_load.display()); // load the file into bytes... let file = File::open(file_to_load).unwrap(); let mut reader = BufReader::new(file); diff --git a/gemmatelnet/src/client_handler.rs b/gemmatelnet/src/client_handler.rs index deabf3a..432f74d 100644 --- a/gemmatelnet/src/client_handler.rs +++ b/gemmatelnet/src/client_handler.rs @@ -6,15 +6,16 @@ use crate::telnet_utils::list_of_files_in_directory; use std::io::Write; use std::path::Path; use gemma::constants::RESOURCES_ROOT; +use crate::constants::*; -const CANT_NOTIFY_ERROR: &str = "Unable to notify client"; +const ERROR_CANT_NOTIFY: &str = "Unable to notify client"; const MENU_TEXT: &str = r#" --------------------------- status -> Current status of the Instance new -> Start New Instance * list -> list available files to load into Chip-8 Instance * load -> Load Chip-8 Program from server -menu -> display the menu +help -> display this help step -> step chip-8 machine 1 step * steps -> Step a fixed number of steps * memory -> dump current memory from chip-8 instance @@ -64,19 +65,19 @@ pub fn handle_client(mut stream: TcpStream) { let mut message_to_send: String = "Unrecognized Command".to_string(); match command.trim() { - "status" => { + CMD_STATUS => { message_to_send = chip8.status_as_string(); } - "disconnect" => { + CMD_DISCONNECT => { message_to_send = "Disconnecting User".to_string(); time_to_die = true; break; } - "new" => { + CMD_NEW => { message_to_send = "Starting new Chip-8 Instance".to_string(); chip8.reset(QuirkMode::Chip8); } - "list" => { + CMD_LIST => { message_to_send = format!("Listing files in path: \n{}", list_of_files_in_directory( @@ -84,20 +85,19 @@ pub fn handle_client(mut stream: TcpStream) { ) ); } - "load" => { - let file_to_use = p1.unwrap_or("default.ch8"); + CMD_LOAD => { + let file_to_use = p1.unwrap_or("default.ch8").trim(); // chip8 = Chip8ComputerManager::from(chip8).load_new_program_to_system_memory() message_to_send = format!("Preparing to load {} from disk.", - p1.unwrap_or("default.ch8").trim()); - + file_to_use); chip8.load_new_program_from_disk_to_system_memory( Path::new(format!("{}/roms/{}", RESOURCES_ROOT, file_to_use).as_str()) ); } - "menu" => { + CMD_HELP => { message_to_send = MENU_TEXT.to_string(); } - "steps" => { + CMD_STEPS => { if chip8.core_should_run { let num_steps = p1.unwrap_or("0").trim(); message_to_send = format!("Advancing {} steps.", num_steps); @@ -108,7 +108,7 @@ pub fn handle_client(mut stream: TcpStream) { message_to_send = "Not Ready to Step".to_string(); } } - "step" => { + CMD_STEP => { if chip8.core_should_run { message_to_send = "Advancing 1 step.".to_string(); chip8.tick(); @@ -116,7 +116,7 @@ pub fn handle_client(mut stream: TcpStream) { message_to_send = "Not Ready To Step".to_string(); } } - "window" => { + CMD_WINDOW => { message_to_send = format!("Memory window from {} to {}", p1.unwrap_or("0x0000").trim(), p2.unwrap_or("0xFFFF").trim()); @@ -124,7 +124,7 @@ pub fn handle_client(mut stream: TcpStream) { _ => {} } - write!(stream, "{}\n", message_to_send).expect(CANT_NOTIFY_ERROR); + write!(stream, "{}\n", message_to_send).expect(ERROR_CANT_NOTIFY); stream.flush().unwrap(); } diff --git a/gemmatelnet/src/constants.rs b/gemmatelnet/src/constants.rs new file mode 100644 index 0000000..233740a --- /dev/null +++ b/gemmatelnet/src/constants.rs @@ -0,0 +1,9 @@ +pub const CMD_NEW: &str = "new"; +pub const CMD_LIST: &str = "list"; +pub const CMD_LOAD: &str = "load"; +pub const CMD_STATUS: &str = "status"; +pub const CMD_DISCONNECT: &str = "disconnect"; +pub const CMD_HELP: &str = "help"; +pub const CMD_STEPS: &str = "steps"; +pub const CMD_STEP: &str = "step"; +pub const CMD_WINDOW: &str = "window"; diff --git a/gemmatelnet/src/lib.rs b/gemmatelnet/src/lib.rs index 5b3a199..cb99245 100644 --- a/gemmatelnet/src/lib.rs +++ b/gemmatelnet/src/lib.rs @@ -1,3 +1,3 @@ - pub mod telnet_utils; -pub mod client_handler; \ No newline at end of file +pub mod client_handler; +pub mod constants;