2024-05-04 14:21:27 +00:00
|
|
|
#![feature(async_closure)]
|
|
|
|
|
2024-01-02 22:01:13 +00:00
|
|
|
use distributed_systems_flyio::{
|
2024-05-04 14:21:27 +00:00
|
|
|
types::{GrowCounterReadMessage, GrowCounterUpdateMessage, InitMessage, Message},
|
|
|
|
Malestorm, MalestormIo,
|
2024-01-02 22:01:13 +00:00
|
|
|
};
|
2024-05-04 14:21:27 +00:00
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
|
|
|
use tokio::runtime::Runtime;
|
2024-01-01 20:13:11 +00:00
|
|
|
|
2024-01-01 19:32:01 +00:00
|
|
|
fn main() {
|
2024-01-02 22:01:13 +00:00
|
|
|
let mut program = Malestorm::default();
|
|
|
|
|
2024-01-03 19:21:52 +00:00
|
|
|
program.register(
|
|
|
|
"init",
|
|
|
|
// TODO: Replace error string with a type
|
2024-05-04 14:21:27 +00:00
|
|
|
|mut msg: Message,
|
|
|
|
program: Arc<RwLock<Malestorm>>,
|
|
|
|
io: Arc<Mutex<MalestormIo>>|
|
|
|
|
-> Result<(), String> {
|
2024-01-03 19:21:52 +00:00
|
|
|
let message_body: InitMessage = serde_json::from_value(msg.body.message_body.take())
|
|
|
|
.map_err(|e| format!("error in parsing response body: {}", e))?;
|
|
|
|
|
2024-05-04 14:21:27 +00:00
|
|
|
let mut program = program.write().unwrap();
|
2024-01-03 19:21:52 +00:00
|
|
|
program.set_node_id(message_body.node_id);
|
|
|
|
program.set_nodes(message_body.nodes);
|
|
|
|
msg.body.message_type = "init_ok".into();
|
|
|
|
|
|
|
|
program
|
2024-05-04 14:21:27 +00:00
|
|
|
.send(io, msg)
|
2024-01-03 19:21:52 +00:00
|
|
|
.map_err(|e| format!("init: error in writing response: {}", e))
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
program.register(
|
2024-05-04 14:21:27 +00:00
|
|
|
"add",
|
|
|
|
|mut msg: Message,
|
|
|
|
program: Arc<RwLock<Malestorm>>,
|
|
|
|
io: Arc<Mutex<MalestormIo>>|
|
|
|
|
-> Result<(), String> {
|
|
|
|
let body: GrowCounterUpdateMessage =
|
|
|
|
serde_json::from_value(msg.body.message_body.take())
|
|
|
|
.map_err(|e| format!("error in parsing response body: {}", e))?;
|
|
|
|
msg.body.message_type = "add_ok".into();
|
|
|
|
|
|
|
|
let mut program = program.write().unwrap();
|
|
|
|
let val = program.read_counter(&msg.src);
|
|
|
|
program.write_counter(&msg.src, val + body.delta);
|
2024-01-03 19:21:52 +00:00
|
|
|
|
|
|
|
program
|
2024-05-04 14:21:27 +00:00
|
|
|
.send(io, msg.clone())
|
|
|
|
.map_err(|e| format!("add: error in writing response: {}", e))
|
2024-01-03 19:21:52 +00:00
|
|
|
},
|
|
|
|
);
|
2024-01-02 22:01:13 +00:00
|
|
|
|
2024-01-03 19:53:34 +00:00
|
|
|
program.register(
|
2024-05-04 14:21:27 +00:00
|
|
|
"read",
|
|
|
|
|mut msg: Message,
|
|
|
|
program: Arc<RwLock<Malestorm>>,
|
|
|
|
io: Arc<Mutex<MalestormIo>>|
|
|
|
|
-> Result<(), String> {
|
|
|
|
msg.body.message_type = "read_ok".into();
|
2024-01-03 19:53:34 +00:00
|
|
|
|
2024-05-04 14:21:27 +00:00
|
|
|
let program = program.write().unwrap();
|
|
|
|
let mut sum = 0;
|
|
|
|
for node in program.get_nodes() {
|
|
|
|
if node == msg.src {
|
|
|
|
sum += program.read_counter(&node);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Sync first then add
|
2024-01-03 20:58:26 +00:00
|
|
|
|
2024-05-04 14:21:27 +00:00
|
|
|
program.sync(&node, msg.clone(), io.clone());
|
|
|
|
|
|
|
|
sum += program.read_counter(&msg.src);
|
|
|
|
}
|
2024-01-03 20:58:26 +00:00
|
|
|
|
2024-05-04 14:21:27 +00:00
|
|
|
msg.body.message_body =
|
|
|
|
serde_json::to_value(GrowCounterReadMessage { value: sum }).unwrap();
|
2024-01-03 20:58:26 +00:00
|
|
|
|
|
|
|
program
|
2024-05-04 14:21:27 +00:00
|
|
|
.send(io, msg.clone())
|
|
|
|
.map_err(|e| format!("read: error in writing response: {}", e))
|
2024-01-03 20:58:26 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
program.register(
|
2024-05-04 14:21:27 +00:00
|
|
|
"counter_sync",
|
|
|
|
|mut msg: Message,
|
|
|
|
program: Arc<RwLock<Malestorm>>,
|
|
|
|
io: Arc<Mutex<MalestormIo>>|
|
|
|
|
-> Result<(), String> {
|
|
|
|
msg.body.message_type = "counter_sync_ok".into();
|
|
|
|
|
|
|
|
let program = program.write().unwrap();
|
|
|
|
msg.body.message_body = serde_json::to_value(GrowCounterReadMessage {
|
|
|
|
value: program.read_counter(&msg.src),
|
2024-01-03 20:58:26 +00:00
|
|
|
})
|
2024-05-04 14:21:27 +00:00
|
|
|
.unwrap();
|
2024-01-03 20:58:26 +00:00
|
|
|
|
|
|
|
program
|
2024-05-04 14:21:27 +00:00
|
|
|
.send(io, msg.clone())
|
|
|
|
.map_err(|e| format!("read: error in writing response: {}", e))
|
2024-01-03 20:58:26 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-05-04 14:21:27 +00:00
|
|
|
let io = MalestormIo::default();
|
|
|
|
let runtime = Runtime::new().unwrap();
|
2024-01-03 20:58:26 +00:00
|
|
|
|
2024-05-04 14:21:27 +00:00
|
|
|
program.run(runtime, io);
|
2024-01-01 19:32:01 +00:00
|
|
|
}
|