From 2a0a4867f9cd590b4b0c072d720d8d02173e930e Mon Sep 17 00:00:00 2001 From: Sebastian Moser Date: Mon, 24 Oct 2022 22:29:38 +0200 Subject: [PATCH] update on the server works now --- src/server.rs | 29 ++++++++++------ src/server/itemstore.rs | 3 +- src/server/proto.rs | 75 +++++++++++++++++++---------------------- 3 files changed, 54 insertions(+), 53 deletions(-) diff --git a/src/server.rs b/src/server.rs index 08baca0..ab934fa 100644 --- a/src/server.rs +++ b/src/server.rs @@ -79,20 +79,26 @@ pub fn run_server(args: Vec) { //println!("{}", err.message); //} - println!("TEST"); + //println!("TEST"); //testing - let val = "mize.works".to_string().into_bytes(); - let mut update: Vec = Vec::new(); - update.push(0); - update.extend(u32::to_be_bytes(3)); - update.extend(u32::to_be_bytes(5)); - update.extend("$$".to_string().into_bytes()); - let new_val = proto::apply_update(&val, &update); - println!("TEST"); - println!("TEST: {}", String::from_utf8(new_val).unwrap()); + //let val = "mize.works".to_string().into_bytes(); + //let mut update: Vec = Vec::new(); + //update.push(2); + //update.extend(u32::to_be_bytes(3)); + //update.extend(u32::to_be_bytes(4)); + //update.extend("$$".to_string().into_bytes()); + + //update.push(0); + //update.extend(u32::to_be_bytes(5)); + //update.extend(u32::to_be_bytes(7)); + //update.extend("$$".to_string().into_bytes()); + + //let new_val = proto::apply_update(&val, &update); + //println!("TEST"); + //println!("TEST: {}", String::from_utf8(new_val).unwrap()); //run the webserver - //warp_server(mize_folder); + warp_server(mize_folder); } @@ -187,6 +193,7 @@ async fn handle_socket_connection( }, Response::All(response) => { let clients = clients_clone.lock().await; + println!("Clients: {}", clients.len()); for client in &clients[..] { client.tx.send(Ok(ws::Message::binary(response.clone()))); } diff --git a/src/server/itemstore.rs b/src/server/itemstore.rs index 656ae21..c0f55b8 100644 --- a/src/server/itemstore.rs +++ b/src/server/itemstore.rs @@ -91,8 +91,7 @@ impl Itemstore { }; for field in new_item { - let mut key = id.to_be_bytes().to_vec(); - key.push(':' as u8); + let mut key = format!("{}:", id).into_bytes(); key.extend(field[0].clone()); let val = field[1].clone(); keys.push(field[0].clone()); diff --git a/src/server/proto.rs b/src/server/proto.rs index c74b103..9bf1f52 100644 --- a/src/server/proto.rs +++ b/src/server/proto.rs @@ -176,16 +176,17 @@ pub async fn handle_mize_message( let num_of_updates = message.get_u32(); - for i in 0..num_of_updates as usize { + for i in 1..num_of_updates as usize { let key_len = message.get_u32(); let key = message.get_bytes(key_len as usize); - //println!("Key: {}", String::from_utf8(key.clone()).unwrap()); + //println!("KEY: {}", String::from_utf8(key.clone()).expect("here utf-8")); let update_len = message.get_u32(); let update = message.get_bytes(update_len as usize); let mut found = false; - for a in 0..num_of_updates as usize { + for a in 0..item.len() as usize { + println!("OTHER KEY: {}", String::from_utf8(item[a][0].clone()).expect("here utf-8")); if item[a][0] == key { item[a][1] = apply_update(&item[a][1], &update); found = true; @@ -193,10 +194,14 @@ pub async fn handle_mize_message( } } if found == false { + println!("should not be here"); let index = item.len() +1; - item[index][1] = apply_update(&item[index][1], &update) + //item[index][1] = apply_update(&item[index][1], &update) + item.push([key.clone(), apply_update(&item[index][1], &update)]) } } + //println!("ITEM KEY: {}", String::from_utf8(item[1][0].clone()).unwrap()); + //println!("ITEM VAL: {}", String::from_utf8(item[1][1].clone()).unwrap()); itemstore.update(id, item).await; return Response::All(answer); @@ -252,7 +257,11 @@ pub async fn handle_mize_message( } +// just like all of this crate, definetly could be done with less clones().... and better error +// handling pub fn apply_update(val: &Vec, updates: &Vec) -> Vec{ + println!("VAL: {:?}", val.clone()); + println!("Update: {:?}", updates.clone()); let not_enough_bytes = "not enough bytes in update"; let mut val = val.clone(); let mut update_iter = updates.clone().into_iter(); @@ -263,6 +272,7 @@ pub fn apply_update(val: &Vec, updates: &Vec) -> Vec{ match operation { //r,start:u32,stop:u32,bytes start..stop 0 => { + //get start and stop let mut start_bytes: [u8; 4] = [0,0,0,0]; for i in 0..4 {start_bytes[i] = update_iter.next().expect(not_enough_bytes);}; let start = u32::from_be_bytes(start_bytes); @@ -287,6 +297,7 @@ pub fn apply_update(val: &Vec, updates: &Vec) -> Vec{ }, //i,start:u32, stop:u32, bytes stop-start 1 => { + //get start and stop let mut start_bytes: [u8; 4] = [0,0,0,0]; for i in 0..4 {start_bytes[i] = update_iter.next().expect(not_enough_bytes);}; let start = u32::from_be_bytes(start_bytes); @@ -314,50 +325,34 @@ pub fn apply_update(val: &Vec, updates: &Vec) -> Vec{ }, //d,start:u32,stop:u32 2 => { + //get start and stop + let mut start_bytes: [u8; 4] = [0,0,0,0]; + for i in 0..4 {start_bytes[i] = update_iter.next().expect(not_enough_bytes);}; + let start = u32::from_be_bytes(start_bytes); + + let mut stop_bytes: [u8; 4] = [0,0,0,0]; + for i in 0..4 {stop_bytes[i] = update_iter.next().expect(not_enough_bytes);}; + let stop = u32::from_be_bytes(stop_bytes); + + //add the stuff before + for i in 0..start {new_val.push(val_iter.next().expect("not_enough_bytes in val_iter"));}; + + //skip all the bytes that should be replaced + for i in 0..stop-start {let h = val_iter.next().expect("not_enough_bytes in val_iter"); println!("VAL ITER: {}", h);} + + //add stuff after + for byte in val_iter { + new_val.push(byte); + } }, _ => {panic!("unknown update command")} } val = new_val; - } else {println!("here");break;} + } else {break;} }; return val; } -//old approach -//pub fn apply_update(val: &Vec, update: &Vec) -> Vec{ - //let new_val: Vec = Vec::new(); - //let update_iter = update.into_iter(); - //let val_iter = val.into_iter(); - //while true { - //if let Some(operation) = update_iter.next(){ - //match *operation { - ////r,start:u32,stop:u32,bytes start..stop - //0 => { - //let start_bytes: [u8; 4] = [0,0,0,0]; - //for i in 0..4 {start_bytes[i] = update_iter.next();}; - //let start = u32::from_be_bytes(start_bytes); -// - //let stop_bytes: [u8; 4] = [0,0,0,0]; - //for i in 0..4 {stop_bytes[i] = update_iter.next();}; - //let start = u32::from_be_bytes(stop_bytes); -// - ////add the stuff before - //for i in 0..start {new_val.push(val_iter.next());}; -// - ////add the new stuff - //for i in start..stop {new_val.push(update_iter.next());}; - //}, - ////i,start:u32, stop:u32, bytes stop-start - //1 => { - //}, - ////d,start:u32,stop:u32 - //2 => { - //}, - //} - //} else {break;} - //} -//} -