1
0

fixed bugs in parsing inputs and responding to them

This commit is contained in:
Ishan Jain 2024-01-04 00:44:15 +05:30
parent e911ee72d4
commit b7728d43c5
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27

View File

@ -43,6 +43,7 @@ impl MalestormIo {
fn write(&self, buf: &[u8]) -> Result<(), IoError> { fn write(&self, buf: &[u8]) -> Result<(), IoError> {
let mut writer = self.stdout.lock(); let mut writer = self.stdout.lock();
writer.write_all(buf)?; writer.write_all(buf)?;
writer.write_all(&[b'\n'])?;
writer.flush() writer.flush()
} }
@ -59,27 +60,30 @@ impl<'a> Malestorm<'a> {
let mut buf = Vec::new(); let mut buf = Vec::new();
loop {
let read = match program.io.read_line(&mut buf) { let read = match program.io.read_line(&mut buf) {
Ok(v) => v, Ok(v) => v,
Err(e) => panic!("{:?}", e), Err(e) => panic!("{:?}", e),
}; };
if read == 0 { if read == 0 {
return; continue;
} }
let message: Message = match serde_json::from_slice(&buf) { let message: Message = match serde_json::from_slice(&buf) {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
eprintln!( eprintln!(
"error in parsing input: {:?} msg = {}", "error in parsing input: {:?} msg = {:?}",
e, e,
String::from_utf8_lossy(&buf) String::from_utf8_lossy(&buf)
); );
return; continue;
} }
}; };
buf.clear();
let mtype = message.body.message_type.clone(); let mtype = message.body.message_type.clone();
let pc = program.clone(); let pc = program.clone();
@ -88,12 +92,13 @@ impl<'a> Malestorm<'a> {
Some(v) => v, Some(v) => v,
None => { None => {
eprintln!("no handler found for {}", message.body.message_type); eprintln!("no handler found for {}", message.body.message_type);
return; continue;
} }
}; };
handler(message, pc); handler(message, pc);
} }
}
pub fn set_node_id(&self, node_id: String) { pub fn set_node_id(&self, node_id: String) {
let mut node = self.node.borrow_mut(); let mut node = self.node.borrow_mut();