Mercurial > templog
annotate rust/src/params.rs @ 626:efcbe0d3afd6 rust
fix to work with hyper
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 06 Dec 2017 00:09:45 +0800 |
parents | 2710649ab71e |
children | d5075136442f |
rev | line source |
---|---|
592 | 1 extern crate tokio_core; |
2 extern crate futures; | |
3 extern crate rand; | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
4 extern crate serde_json; |
616 | 5 extern crate base64; |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
6 extern crate atomicwrites; |
592 | 7 |
8 use std::time::Duration; | |
9 use std::io; | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
10 use std::str; |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
11 use std::rc::Rc; |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
12 use std::sync::{Arc,Mutex}; |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
13 use std::error::Error; |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
14 use std::cell::{Cell,RefCell}; |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
15 use std::fs::File; |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
16 use std::io::Read; |
592 | 17 |
18 use tokio_core::reactor::Interval; | |
19 use tokio_core::reactor::Handle; | |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
20 use futures::{Stream,Future,future}; |
616 | 21 use self::rand::Rng; |
626 | 22 use std::str::FromStr; |
23 use hyper; | |
24 use hyper::header::{Headers, ETag, EntityTag}; | |
25 use hyper::client::Client; | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
26 |
592 | 27 use types::*; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
28 use ::Config; |
592 | 29 |
615 | 30 #[derive(Deserialize, Serialize, Debug, Clone)] |
31 pub struct Params { | |
32 pub fridge_setpoint: f32, | |
33 pub fridge_difference: f32, | |
621 | 34 pub overshoot_delay: u64, |
615 | 35 pub overshoot_factor: f32, |
36 pub disabled: bool, | |
37 pub nowort: bool, | |
38 pub fridge_range_lower: f32, | |
39 pub fridge_range_upper: f32, | |
40 } | |
41 | |
42 impl Params { | |
43 pub fn defaults() -> Params { | |
44 Params { | |
45 fridge_setpoint: 16.0, | |
46 fridge_difference: 0.2, | |
47 overshoot_delay: 720, // 12 minutes | |
48 overshoot_factor: 1.0, | |
49 disabled: false, | |
50 nowort: false, | |
51 fridge_range_lower: 3.0, | |
52 fridge_range_upper: 3.0, | |
53 } | |
54 } | |
55 | |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
56 fn try_load(filename: &str) -> Result<Params, TemplogError> { |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
57 let mut s = String::new(); |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
58 File::open(filename)?.read_to_string(&mut s)?; |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
59 Ok(serde_json::from_str(&s)?) |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
60 } |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
61 |
615 | 62 pub fn load(config: &Config) -> Params { |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
63 Self::try_load(&config.PARAMS_FILE) |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
64 .unwrap_or_else(|_| Params::defaults()) |
615 | 65 } |
66 } | |
67 | |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
68 #[derive(Deserialize, Debug)] |
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
69 struct Response { |
616 | 70 // sent as an opaque etag: header. Has format "epoch-nonce", |
71 // responses where the epoch do not match ParamWaiter::epoch are dropped | |
72 etag: String, | |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
73 params: Params, |
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
74 } |
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
75 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
76 #[derive(Clone)] |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
77 pub struct ParamWaiter { |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
78 limitlog: NotTooOften, |
616 | 79 // last_etag is used for long-polling. |
80 last_etag: RefCell<String>, | |
81 epoch: String, | |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
82 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
83 config: Config, |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
84 handle: Handle, |
592 | 85 } |
86 | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
87 const LOG_MINUTES: u64 = 15; |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
88 const MAX_RESPONSE_SIZE: usize = 10000; |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
89 const TIMEOUT_MINUTES: u64 = 5; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
90 |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
91 impl ParamWaiter { |
626 | 92 fn make_request(&self) -> hyper::client::FutureResponse { |
93 let uri = self.config.SETTINGS_URL.parse().expect("Bad SETTINGS_URL in config"); | |
94 let mut req = hyper::client::Request::new(hyper::Method::Get, uri); | |
95 { | |
96 let mut headers = req.headers_mut(); | |
97 headers.set(hyper::header::ETag(EntityTag::new(false, self.last_etag.borrow().clone()))); | |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
98 } |
626 | 99 hyper::client::Client::new(&self.handle).request(req) |
100 // XXX how do we do this? | |
101 // req.timeout(Duration::new(TIMEOUT_MINUTES * 60, 0)).unwrap(); | |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
102 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
103 |
626 | 104 |
105 fn handle_response(&self, buf : hyper::Chunk, status: hyper::StatusCode) -> Result<Params, TemplogError> { | |
106 let text = String::from_utf8_lossy(buf.as_ref()); | |
107 match status { | |
108 hyper::StatusCode::Ok => { | |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
109 // new params |
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
110 let r: Response = serde_json::from_str(&text)?; |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
111 let mut le = self.last_etag.borrow_mut(); |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
112 *le = r.etag; |
626 | 113 |
114 // update params if the epoch is correct | |
115 if let Some(e) = le.split('-').next() { | |
116 if e == &self.epoch { | |
117 self.write_params(&r.params); | |
118 return Ok(r.params); | |
119 } | |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
120 } |
626 | 121 Err(TemplogError::new(&format!("Bad epoch from server '{}' expected '{}'", *le, self.epoch))) |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
122 } |
626 | 123 hyper::StatusCode::NotModified => { |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
124 // XXX this isn't really an error |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
125 Err(TemplogError::new("304 unmodified (long polling timeout at the server)")) |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
126 }, |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
127 _ => { |
626 | 128 Err(TemplogError::new(&format!("Wrong server response code {}: {}", status.as_u16(), text))) |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
129 }, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
130 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
131 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
132 |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
133 fn write_params(&self, params: &Params) { |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
134 let p = atomicwrites::AtomicFile::new(&self.config.PARAMS_FILE, atomicwrites::AllowOverwrite); |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
135 p.write(|f| { |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
136 serde_json::to_writer(f, params) |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
137 }); |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
138 } |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
139 |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
140 fn step(rself: Rc<Self>) -> Box<Future<Item=Params, Error=TemplogError>> { |
626 | 141 let resp = rself.make_request(); |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
142 |
626 | 143 let s = resp.map_err(|e| TemplogError::new_hyper("response", e)) |
144 .and_then(move |r| { | |
145 let status = r.status(); | |
146 r.body().concat2() | |
147 .map_err(|e| TemplogError::new_hyper("body", e)) | |
148 .and_then(move |b| { | |
149 rself.handle_response(b, status) | |
150 }) | |
151 }); | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
152 Box::new(s) |
592 | 153 } |
154 | |
616 | 155 fn new(config: &Config, handle: &Handle) -> Self { |
156 let mut b = [0u8; 15]; // 15 bytes -> 20 characters base64 | |
626 | 157 rand::OsRng::new().expect("Opening system RNG failed").fill_bytes(&mut b); |
616 | 158 let epoch = base64::encode(&b); |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
159 ParamWaiter { |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
160 limitlog: NotTooOften::new(LOG_MINUTES*60), |
616 | 161 last_etag: RefCell::new(String::new()), |
162 epoch: epoch, | |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
163 config: config.clone(), |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
164 handle: handle.clone(), |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
165 } |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
166 } |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
167 |
616 | 168 pub fn stream(config: &Config, handle: &Handle) -> Box<Stream<Item=Params, Error=TemplogError>> { |
169 let rcself = Rc::new(ParamWaiter::new(config, handle)); | |
592 | 170 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
171 let dur = Duration::from_millis(4000); |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
172 let i = Interval::new(dur, &rcself.handle).unwrap() |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
173 .map_err(|e| TemplogError::new_io("interval failed", e)) |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
174 .and_then(move |()| { |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
175 Self::step(rcself.clone()) |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
176 }); |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
177 |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
178 // TODO use consume_errors() instead once "impl trait" is stable |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
179 //Box::new(consume_errors(i)) |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
180 let f = i.then(|r| { |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
181 match r { |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
182 Ok(v) => Ok(Some(v)), |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
183 Err(e) => { |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
184 debug!("Params stream error: {}", e); |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
185 Ok(None) |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
186 } |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
187 } |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
188 }) |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
189 .filter_map(|p| p); |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
190 Box::new(f) |
592 | 191 } |
192 } | |
193 |