update on the server works now
This commit is contained in:
@@ -79,20 +79,26 @@ pub fn run_server(args: Vec<String>) {
|
|||||||
//println!("{}", err.message);
|
//println!("{}", err.message);
|
||||||
//}
|
//}
|
||||||
|
|
||||||
println!("TEST");
|
//println!("TEST");
|
||||||
//testing
|
//testing
|
||||||
let val = "mize.works".to_string().into_bytes();
|
//let val = "mize.works".to_string().into_bytes();
|
||||||
let mut update: Vec<u8> = Vec::new();
|
//let mut update: Vec<u8> = Vec::new();
|
||||||
update.push(0);
|
//update.push(2);
|
||||||
update.extend(u32::to_be_bytes(3));
|
//update.extend(u32::to_be_bytes(3));
|
||||||
update.extend(u32::to_be_bytes(5));
|
//update.extend(u32::to_be_bytes(4));
|
||||||
update.extend("$$".to_string().into_bytes());
|
//update.extend("$$".to_string().into_bytes());
|
||||||
let new_val = proto::apply_update(&val, &update);
|
|
||||||
println!("TEST");
|
//update.push(0);
|
||||||
println!("TEST: {}", String::from_utf8(new_val).unwrap());
|
//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
|
//run the webserver
|
||||||
//warp_server(mize_folder);
|
warp_server(mize_folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -187,6 +193,7 @@ async fn handle_socket_connection(
|
|||||||
},
|
},
|
||||||
Response::All(response) => {
|
Response::All(response) => {
|
||||||
let clients = clients_clone.lock().await;
|
let clients = clients_clone.lock().await;
|
||||||
|
println!("Clients: {}", clients.len());
|
||||||
for client in &clients[..] {
|
for client in &clients[..] {
|
||||||
client.tx.send(Ok(ws::Message::binary(response.clone())));
|
client.tx.send(Ok(ws::Message::binary(response.clone())));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,8 +91,7 @@ impl Itemstore {
|
|||||||
};
|
};
|
||||||
|
|
||||||
for field in new_item {
|
for field in new_item {
|
||||||
let mut key = id.to_be_bytes().to_vec();
|
let mut key = format!("{}:", id).into_bytes();
|
||||||
key.push(':' as u8);
|
|
||||||
key.extend(field[0].clone());
|
key.extend(field[0].clone());
|
||||||
let val = field[1].clone();
|
let val = field[1].clone();
|
||||||
keys.push(field[0].clone());
|
keys.push(field[0].clone());
|
||||||
|
|||||||
@@ -176,16 +176,17 @@ pub async fn handle_mize_message(
|
|||||||
|
|
||||||
let num_of_updates = message.get_u32();
|
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_len = message.get_u32();
|
||||||
let key = message.get_bytes(key_len as usize);
|
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_len = message.get_u32();
|
||||||
let update = message.get_bytes(update_len as usize);
|
let update = message.get_bytes(update_len as usize);
|
||||||
|
|
||||||
let mut found = false;
|
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 {
|
if item[a][0] == key {
|
||||||
item[a][1] = apply_update(&item[a][1], &update);
|
item[a][1] = apply_update(&item[a][1], &update);
|
||||||
found = true;
|
found = true;
|
||||||
@@ -193,10 +194,14 @@ pub async fn handle_mize_message(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if found == false {
|
if found == false {
|
||||||
|
println!("should not be here");
|
||||||
let index = item.len() +1;
|
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;
|
itemstore.update(id, item).await;
|
||||||
|
|
||||||
return Response::All(answer);
|
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<u8>, updates: &Vec<u8>) -> Vec<u8>{
|
pub fn apply_update(val: &Vec<u8>, updates: &Vec<u8>) -> Vec<u8>{
|
||||||
|
println!("VAL: {:?}", val.clone());
|
||||||
|
println!("Update: {:?}", updates.clone());
|
||||||
let not_enough_bytes = "not enough bytes in update";
|
let not_enough_bytes = "not enough bytes in update";
|
||||||
let mut val = val.clone();
|
let mut val = val.clone();
|
||||||
let mut update_iter = updates.clone().into_iter();
|
let mut update_iter = updates.clone().into_iter();
|
||||||
@@ -263,6 +272,7 @@ pub fn apply_update(val: &Vec<u8>, updates: &Vec<u8>) -> Vec<u8>{
|
|||||||
match operation {
|
match operation {
|
||||||
//r,start:u32,stop:u32,bytes start..stop
|
//r,start:u32,stop:u32,bytes start..stop
|
||||||
0 => {
|
0 => {
|
||||||
|
//get start and stop
|
||||||
let mut start_bytes: [u8; 4] = [0,0,0,0];
|
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);};
|
for i in 0..4 {start_bytes[i] = update_iter.next().expect(not_enough_bytes);};
|
||||||
let start = u32::from_be_bytes(start_bytes);
|
let start = u32::from_be_bytes(start_bytes);
|
||||||
@@ -287,6 +297,7 @@ pub fn apply_update(val: &Vec<u8>, updates: &Vec<u8>) -> Vec<u8>{
|
|||||||
},
|
},
|
||||||
//i,start:u32, stop:u32, bytes stop-start
|
//i,start:u32, stop:u32, bytes stop-start
|
||||||
1 => {
|
1 => {
|
||||||
|
//get start and stop
|
||||||
let mut start_bytes: [u8; 4] = [0,0,0,0];
|
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);};
|
for i in 0..4 {start_bytes[i] = update_iter.next().expect(not_enough_bytes);};
|
||||||
let start = u32::from_be_bytes(start_bytes);
|
let start = u32::from_be_bytes(start_bytes);
|
||||||
@@ -314,50 +325,34 @@ pub fn apply_update(val: &Vec<u8>, updates: &Vec<u8>) -> Vec<u8>{
|
|||||||
},
|
},
|
||||||
//d,start:u32,stop:u32
|
//d,start:u32,stop:u32
|
||||||
2 => {
|
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")}
|
_ => {panic!("unknown update command")}
|
||||||
}
|
}
|
||||||
val = new_val;
|
val = new_val;
|
||||||
} else {println!("here");break;}
|
} else {break;}
|
||||||
};
|
};
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
//old approach
|
|
||||||
//pub fn apply_update(val: &Vec<u8>, update: &Vec<u8>) -> Vec<u8>{
|
|
||||||
//let new_val: Vec<u8> = 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;}
|
|
||||||
//}
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user