moves constants out of client handler

This commit is contained in:
Trevor Merritt 2025-05-23 09:20:39 -04:00
parent c9c9cf67da
commit 74bf2e71e4
4 changed files with 27 additions and 19 deletions

View File

@ -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);

View File

@ -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 <filename> -> Load Chip-8 Program from server
menu -> display the menu
help -> display this help
step -> step chip-8 machine 1 step
* steps <number> -> 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();
}

View File

@ -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";

View File

@ -1,3 +1,3 @@
pub mod telnet_utils;
pub mod client_handler;
pub mod client_handler;
pub mod constants;