-
Notifications
You must be signed in to change notification settings - Fork 0
/
peers.rs
81 lines (73 loc) · 2.16 KB
/
peers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#[macro_use] extern crate rotor;
extern crate rotor_cantal;
extern crate rotor_tools;
extern crate argparse;
extern crate env_logger;
use std::time::Duration;
use rotor::{Machine, EventSet, Scope, Response};
use rotor::void::{unreachable, Void};
use rotor_cantal::{Schedule, connect_localhost};
use rotor_tools::loop_ext::LoopExt;
use argparse::{ArgumentParser, StoreTrue};
pub struct Printer(bool);
rotor_compose!(enum Fsm/Seed<Context> {
Print(Printer),
Cantal(rotor_cantal::Fsm<Context>),
});
impl Machine for Printer {
type Context = Context;
type Seed = Void;
fn create(seed: Self::Seed, _scope: &mut Scope<Context>)
-> Response<Self, Void>
{
unreachable(seed)
}
fn ready(self, _events: EventSet, _scope: &mut Scope<Context>)
-> Response<Self, Self::Seed>
{
unimplemented!();
}
fn spawned(self, _scope: &mut Scope<Context>) -> Response<Self, Self::Seed>
{
unimplemented!();
}
fn timeout(self, _scope: &mut Scope<Context>) -> Response<Self, Self::Seed>
{
unimplemented!();
}
fn wakeup(self, scope: &mut Scope<Context>) -> Response<Self, Self::Seed>
{
println!("{:#?}", scope.cantal.get_peers());
if !self.0 {
scope.shutdown_loop();
}
Response::ok(self)
}
}
pub struct Context {
cantal: Schedule,
}
fn main() {
env_logger::init().unwrap();
let mut monitor = false;
{
let mut ap = ArgumentParser::new();
ap.refer(&mut monitor)
.add_option(&["-m", "--monitor"], StoreTrue,
"Poll the cantal every 10 seconds");
ap.parse_args_or_exit();
}
let mut creator = rotor::Loop::new(&rotor::Config::new()).unwrap();
let schedule = creator.add_and_fetch(Fsm::Cantal, |scope| {
connect_localhost(scope)
}).unwrap();
schedule.set_peers_interval(Duration::new(10, 0));
let mut loop_inst = creator.instantiate(Context {
cantal: schedule.clone(),
});
loop_inst.add_machine_with(|scope| {
schedule.add_listener(scope.notifier());
Response::ok(Fsm::Print(Printer(monitor)))
}).unwrap();
loop_inst.run().unwrap();
}