annotate rust/src/fridge.rs @ 593:bf138339d20a rust

fiddling with timeouts and closures
author Matt Johnston <matt@ucc.asn.au>
date Tue, 27 Dec 2016 00:51:28 +0800
parents 03b48ec0bb03
children aff50ee77252
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
1 use std::io;
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
2 use std::time::Duration;
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
3
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
4 use futures::Future;
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
5 use tokio_core::reactor::Timeout;
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
6 use tokio_core::reactor::Handle;
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
7
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 use types::*;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
10 pub struct Fridge {
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
11 params: Params,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
12 temp_wort: Option<f32>,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
13 temp_fridge: Option<f32>,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
14
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
15 // timeouts to wake ourself up again
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
16 //overshoot_timeout: Option<Future<Item=(), Error=io::Error>>,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
17 //fridgeoff_timeout: Option<Future<Item=(), Error=io::Error>>,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
18 wortvalid_timeout: Option<Timeout>,
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
21 impl Fridge {
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
22 pub fn new(p: Params) -> Fridge {
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
23 Fridge {
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
24 params: p,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
25 temp_wort: None,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
26 temp_fridge: None,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
27 //overshoot_timeout: None,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
28 //fridgeoff_timeout: None,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
29 wortvalid_timeout: None,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
30 }
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
31 }
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
32
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
33 fn tick(&mut self, handle: &Handle) {
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
34
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
37 pub fn set_params(&mut self, handle: &Handle, p: Params) {
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 self.params = p;
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
39 println!("params {:?}", self.params);
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
40
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
41 self.tick(handle);
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
42 }
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
43
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
44 pub fn set_temps(&mut self, handle: &Handle,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
45 wort: Option<f32>, fridge: Option<f32>) {
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
46 self.temp_wort = wort;
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
47 self.temp_fridge = fridge;
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
48
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
49 if let Some(_) = self.temp_wort {
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
50 // set a new timeout, replacing any existing
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
51 let dur = Duration::new(10, 0); // XXX
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
52 let t = Timeout::new(dur, handle).unwrap();
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
53 /*
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
54 handle.spawn(t.and_then(|_| {
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
55 self.tick(handle);
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
56 Ok(())
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
57 }).map_err(|x| ()));
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
58 */
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
59 self.wortvalid_timeout = Some(t);
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
60 }
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
61
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
62 self.tick(handle);
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 }