Mercurial > templog
annotate rust/src/params.rs @ 618:2d65a9f0bed3 rust
params returning stream of success
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 21 Mar 2017 22:35:58 +0800 |
parents | a85c0c9bc1fa |
children | 8136a6b99866 |
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; |
592 | 6 |
7 use std::time::Duration; | |
8 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
|
9 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
|
10 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
|
11 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
|
12 use std::error::Error; |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
13 use std::cell::{Cell,RefCell}; |
592 | 14 |
15 use tokio_core::reactor::Interval; | |
16 use tokio_core::reactor::Handle; | |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
17 use tokio_curl::Session; |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
18 use futures::{Stream,Future,future}; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
19 use curl::easy::Easy; |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
20 use curl::easy; |
616 | 21 use self::rand::Rng; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
22 |
592 | 23 use types::*; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
24 use ::Config; |
592 | 25 |
615 | 26 #[derive(Deserialize, Serialize, Debug, Clone)] |
27 pub struct Params { | |
28 pub fridge_setpoint: f32, | |
29 pub fridge_difference: f32, | |
30 pub overshoot_delay: u32, | |
31 pub overshoot_factor: f32, | |
32 pub disabled: bool, | |
33 pub nowort: bool, | |
34 pub fridge_range_lower: f32, | |
35 pub fridge_range_upper: f32, | |
36 } | |
37 | |
38 impl Params { | |
39 pub fn defaults() -> Params { | |
40 Params { | |
41 fridge_setpoint: 16.0, | |
42 fridge_difference: 0.2, | |
43 overshoot_delay: 720, // 12 minutes | |
44 overshoot_factor: 1.0, | |
45 disabled: false, | |
46 nowort: false, | |
47 fridge_range_lower: 3.0, | |
48 fridge_range_upper: 3.0, | |
49 } | |
50 } | |
51 | |
52 pub fn load(config: &Config) -> Params { | |
53 // generate random epoch on success. | |
54 // TODO: return failure? or just return default() ? | |
616 | 55 let mut p = Params::defaults(); |
56 | |
615 | 57 unimplemented!(); |
58 } | |
59 } | |
60 | |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
61 #[derive(Deserialize, Debug)] |
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
62 struct Response { |
616 | 63 // sent as an opaque etag: header. Has format "epoch-nonce", |
64 // responses where the epoch do not match ParamWaiter::epoch are dropped | |
65 etag: String, | |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
66 params: Params, |
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
67 } |
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
68 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
69 #[derive(Clone)] |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
70 pub struct ParamWaiter { |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
71 limitlog: NotTooOften, |
616 | 72 // last_etag is used for long-polling. |
73 last_etag: RefCell<String>, | |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
74 session: RefCell<Option<Session>>, |
616 | 75 epoch: String, |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
76 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
77 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
|
78 handle: Handle, |
592 | 79 } |
80 | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
81 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
|
82 const MAX_RESPONSE_SIZE: usize = 10000; |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
83 const TIMEOUT_MINUTES: u64 = 5; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
84 |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
85 impl ParamWaiter { |
616 | 86 fn make_request(&self) -> (Easy, Arc<Mutex<Vec<u8>>>) { |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
87 let mut req = Easy::new(); |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
88 req.get(true).unwrap(); |
616 | 89 // supposedly req.url won't fail, checking happens later? |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
90 req.url(&self.config.SETTINGS_URL).unwrap(); |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
91 |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
92 req.timeout(Duration::new(TIMEOUT_MINUTES * 60, 0)).unwrap(); |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
93 |
616 | 94 // store response |
95 let max_response_size = 10000; | |
96 let buf = Arc::new(Mutex::new(Vec::new())); | |
97 let dst = buf.clone(); | |
98 req.write_function(move |data| { | |
99 let mut dst = dst.lock().unwrap(); | |
100 dst.extend_from_slice(data); | |
101 if dst.len() > max_response_size { | |
102 error!("Too large params response from server: {}", dst.len()); | |
103 Ok(0) | |
104 } else { | |
105 Ok(data.len()) | |
106 } | |
107 }).unwrap(); | |
108 | |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
109 // http header |
616 | 110 let e = self.last_etag.borrow(); |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
111 if !e.is_empty() { |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
112 let mut list = easy::List::new(); |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
113 let hd = format!("etag: {}", *e); |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
114 list.append(&hd).unwrap(); |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
115 req.http_headers(list).unwrap(); |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
116 } |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
117 |
616 | 118 (req, buf) |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
119 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
120 |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
121 fn handle_response(&self, req: &mut Easy, buf: &Vec<u8>) -> Result<Params, TemplogError> { |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
122 let text = String::from_utf8_lossy(buf).to_string(); |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
123 let resp = req.response_code()?; |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
124 match resp { |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
125 200 => { |
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
126 // new params |
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
127 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
|
128 let mut le = self.last_etag.borrow_mut(); |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
129 *le = r.etag; |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
130 if le.split('-').next().unwrap() == &self.epoch { |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
131 Ok(r.params) |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
132 } else { |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
133 Err(TemplogError::new(&format!("Bad epoch from server '{}' expected '{}'", *le, self.epoch))) |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
134 } |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
135 } |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
136 304 => { |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
137 // XXX this isn't really an error |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
138 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
|
139 }, |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
140 _ => { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
141 Err(TemplogError::new(&format!("Wrong server response code {}: {}", resp, text))) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
142 }, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
143 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
144 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
145 |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
146 fn step(rself: Rc<Self>) -> Box<Future<Item=Params, Error=TemplogError>> { |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
147 |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
148 if rself.session.borrow().is_none() { |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
149 *rself.session.borrow_mut() = Some(Session::new(rself.handle.clone())); |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
150 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
151 |
616 | 152 let (mut req, buf) = 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
|
153 |
616 | 154 let ses = rself.session.borrow().clone().unwrap(); |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
155 let s = ses.perform(req) |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
156 .map_err(|e| TemplogError::new_io("tokio curl error", e.into_error())) |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
157 .and_then(move |mut rq| { |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
158 let result = buf.lock().unwrap(); |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
159 rself.handle_response(&mut rq, &result) |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
160 }); |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
161 Box::new(s) |
592 | 162 } |
163 | |
616 | 164 fn new(config: &Config, handle: &Handle) -> Self { |
165 let mut b = [0u8; 15]; // 15 bytes -> 20 characters base64 | |
166 rand::OsRng::new().unwrap().fill_bytes(&mut b); | |
167 let epoch = base64::encode(&b); | |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
168 ParamWaiter { |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
169 limitlog: NotTooOften::new(LOG_MINUTES*60), |
616 | 170 last_etag: RefCell::new(String::new()), |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
171 session: RefCell::new(None), |
616 | 172 epoch: epoch, |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
173 config: config.clone(), |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
174 handle: handle.clone(), |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
175 } |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
176 } |
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
177 |
616 | 178 pub fn stream(config: &Config, handle: &Handle) -> Box<Stream<Item=Params, Error=TemplogError>> { |
179 let rcself = Rc::new(ParamWaiter::new(config, handle)); | |
592 | 180 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
181 let dur = Duration::from_millis(4000); |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
182 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
|
183 .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
|
184 .and_then(move |()| { |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
185 Self::step(rcself.clone()) |
618
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 // 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
|
189 //Box::new(consume_errors(i)) |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
190 let f = i.then(|r| { |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
191 match r { |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
192 Ok(v) => Ok(Some(v)), |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
193 Err(e) => { |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
194 debug!("Params stream error: {}", e); |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
195 Ok(None) |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
196 } |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
197 } |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
198 }) |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
199 .filter_map(|p| p); |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
200 Box::new(f) |
592 | 201 } |
202 } | |
203 |