2024-01-02 22:01:13 +00:00
|
|
|
use distributed_systems_flyio::{
|
2024-01-03 18:58:06 +00:00
|
|
|
types::{EchoMessage, InitMessage, Message},
|
2024-01-02 22:01:13 +00:00
|
|
|
Malestorm,
|
|
|
|
};
|
2024-01-03 18:58:06 +00:00
|
|
|
use std::rc::Rc;
|
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 18:58:06 +00:00
|
|
|
program.register("init", |mut msg: Message, program: Rc<Malestorm>| {
|
|
|
|
let message_body: InitMessage = serde_json::from_value(msg.body.message_body.take())
|
|
|
|
.expect("error in parsing message body");
|
|
|
|
|
|
|
|
program.set_node_id(message_body.node_id);
|
|
|
|
program.set_nodes(message_body.nodes);
|
|
|
|
|
|
|
|
msg.body.message_type = "init_ok".into();
|
|
|
|
|
|
|
|
match program.reply(msg) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("init: error in writing response: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-03 07:30:48 +00:00
|
|
|
program.register("echo", |mut msg: Message, program: Rc<Malestorm>| {
|
2024-01-03 18:58:06 +00:00
|
|
|
let message_body: EchoMessage = serde_json::from_value(msg.body.message_body.take())
|
|
|
|
.expect("error in parsing message body");
|
2024-01-02 22:01:13 +00:00
|
|
|
|
2024-01-03 07:30:48 +00:00
|
|
|
msg.body.message_type = "echo_ok".into();
|
|
|
|
msg.body.message_body = serde_json::to_value(message_body).unwrap();
|
2024-01-02 22:01:13 +00:00
|
|
|
|
2024-01-03 07:30:48 +00:00
|
|
|
match program.reply(msg) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
2024-01-03 18:58:06 +00:00
|
|
|
eprintln!("echo: error in writing response: {}", e);
|
2024-01-03 07:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-01-02 22:01:13 +00:00
|
|
|
|
|
|
|
program.run();
|
2024-01-01 19:32:01 +00:00
|
|
|
}
|