First, naive and wrong attempt, have to add interval later on

This commit is contained in:
Bastian Gruber 2023-05-27 06:25:34 +02:00
parent 9dc8578006
commit e37bb7cfa3
No known key found for this signature in database
GPG key ID: BE9F8C772B188CBF

View file

@ -2,6 +2,9 @@ use std::io::{self, BufRead, Write};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
const SEQ_KV: &str = "seq-kv";
const KEY: &str = "counter";
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct Message { struct Message {
src: String, src: String,
@ -27,11 +30,12 @@ enum Body {
in_reply_to: u64, in_reply_to: u64,
}, },
Read { Read {
msg_id: u64, msg_id: Option<u64>,
key: Option<String>,
}, },
ReadOk { ReadOk {
in_reply_to: Option<u64>,
value: u64, value: u64,
in_reply_to: u64,
}, },
Add { Add {
msg_id: u64, msg_id: u64,
@ -40,7 +44,6 @@ enum Body {
AddOk { AddOk {
in_reply_to: u64, in_reply_to: u64,
}, },
//SEQ-KV
Write { Write {
msg_id: u64, msg_id: u64,
key: String, key: String,
@ -49,6 +52,16 @@ enum Body {
WriteOk { WriteOk {
in_reply_to: u64, in_reply_to: u64,
}, },
Cas {
msg_id: u64,
key: String,
from: u64,
to: u64,
},
CasOk {
msg_id: u64,
in_reply_to: u64,
},
} }
fn main() { fn main() {
@ -57,15 +70,39 @@ fn main() {
let mut stderr = io::stderr(); let mut stderr = io::stderr();
let mut id = String::new(); let mut id = String::new();
let mut counter = 0;
let mut tmp_counter = 0;
for line in stdin.lock().lines() { for line in stdin.lock().lines() {
let input = serde_json::from_str::<Message>(&line.as_ref().unwrap());
if let Err(e) = input {
writeln!(stderr, "Error: {:?}", e).unwrap();
stderr.flush().unwrap();
continue;
}
let input: Message = serde_json::from_str(&line.unwrap()).unwrap(); let input: Message = serde_json::from_str(&line.unwrap()).unwrap();
match input.body { match input.body {
Body::Error { text, .. } => { Body::Error { code, .. } => match code {
let output_json = serde_json::to_string(&text).unwrap(); 22 => {
writeln!(stderr, "{}", output_json).unwrap(); let output = Message {
src: id.clone(),
dest: SEQ_KV.to_string(),
body: Body::Read {
msg_id: Some(2),
key: Some(KEY.to_string()),
},
};
let output_json = serde_json::to_string(&output).unwrap();
writeln!(stdout, "{}", output_json).unwrap();
stdout.flush().unwrap(); stdout.flush().unwrap();
} }
_ => {
writeln!(stderr, "Error: {:?}", input).unwrap();
stderr.flush().unwrap();
}
},
Body::Init { Body::Init {
msg_id, node_id, .. msg_id, node_id, ..
} => { } => {
@ -82,31 +119,47 @@ fn main() {
let output = Message { let output = Message {
src: id.clone(), src: id.clone(),
dest: "seq-kv".to_string(), dest: SEQ_KV.to_string(),
body: Body::Write { body: Body::Write {
msg_id: 1, msg_id,
key: "TEST".to_string(), key: KEY.to_string(),
value: 42, value: counter,
}, },
}; };
let output_json = serde_json::to_string(&output).unwrap(); let output_json = serde_json::to_string(&output).unwrap();
writeln!(stdout, "{}", output_json).unwrap(); writeln!(stdout, "{}", output_json).unwrap();
stdout.flush().unwrap(); stdout.flush().unwrap();
} }
Body::Read { msg_id } => { Body::Read { msg_id, .. } => {
let output = Message { let output = Message {
src: id.clone(), src: id.clone(),
dest: input.src, dest: input.src,
body: Body::ReadOk { body: Body::ReadOk {
in_reply_to: msg_id, in_reply_to: msg_id,
value: 42, value: counter,
}, },
}; };
let output_json = serde_json::to_string(&output).unwrap(); let output_json = serde_json::to_string(&output).unwrap();
writeln!(stdout, "{}", output_json).unwrap(); writeln!(stdout, "{}", output_json).unwrap();
stdout.flush().unwrap(); stdout.flush().unwrap();
} }
Body::Add { msg_id, .. } => { Body::Add { msg_id, delta } => {
tmp_counter += delta;
let output = Message {
src: id.clone(),
dest: SEQ_KV.to_string(),
body: Body::Cas {
msg_id,
key: KEY.to_string(),
from: counter,
to: tmp_counter,
},
};
let output_json = serde_json::to_string(&output).unwrap();
writeln!(stdout, "{}", output_json).unwrap();
let output = Message { let output = Message {
src: id.clone(), src: id.clone(),
dest: input.src, dest: input.src,
@ -121,6 +174,15 @@ fn main() {
Body::WriteOk { .. } => { Body::WriteOk { .. } => {
// //
} }
Body::AddOk { .. } => {
//
}
Body::CasOk { .. } => {
counter = tmp_counter;
}
Body::ReadOk { value, .. } => {
counter = value;
}
_ => println!("Unhandled message: {:?}", input), _ => println!("Unhandled message: {:?}", input),
} }
} }