annotate rust/src/paramwaiter.rs @ 611:f3e39e2107fd rust

still doesn't compile, improvements to TemplogError and tokio curl though
author Matt Johnston <matt@ucc.asn.au>
date Tue, 28 Feb 2017 22:58:47 +0800
parents 7bda01659426
children 5c2b0d47bb83
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 extern crate tokio_core;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 extern crate futures;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 use std::time::Duration;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 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
8 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
9 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
10 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
11 use std::error::Error;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 use tokio_core::reactor::Interval;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 use tokio_core::reactor::Handle;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
15 use tokio_curl::Session;
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
16 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
17 use curl::easy::Easy;
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
18
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 use types::*;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
20 use ::Config;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
22 #[derive(Clone)]
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
23 pub struct ParamWaiter {
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
24 limitlog: NotTooOften,
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
25 epoch_tag: String,
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
26 session: Option<Session>,
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
27 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
28 handle: Handle,
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
31 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
32 const MAX_RESPONSE_SIZE: usize = 10000;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
33
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
34 impl ParamWaiter {
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
35 pub fn new(config: &Config, handle: &Handle) -> Self {
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
36 ParamWaiter {
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
37 limitlog: NotTooOften::new(LOG_MINUTES*60),
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
38 epoch_tag: String::new(),
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
39 session: None,
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
40 config: config.clone(),
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
41 handle: handle.clone(),
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
42 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
43 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
44
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
45 fn make_req(&self) -> Easy {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
46 let mut req = Easy::new();
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
47 req.get(true).unwrap();
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
48 // supposedly req.url won't fail, checking is later?
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
49 req.url(&self.config.SETTINGS_URL).unwrap();
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
50 req
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
51 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
52
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
53 fn handle_response(&self, req: &mut Easy, buf: &Vec<u8>) -> Result<Option<Params>, TemplogError> {
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
54 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
55 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
56 match resp {
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
57 200 => Ok(Some(serde_json::from_str(&text)?)), // new params
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
58 304 => Ok(None), // unmodified (long polling timeout at the server)
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
59 _ => {
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
60 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
61 },
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
62 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
63 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
64
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
65 fn step(&mut self) -> Box<Future<Item=Option<Params>, Error=TemplogError>> {
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
66 if self.session.is_none() {
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
67 self.session = Some(Session::new(self.handle.clone()))
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
68 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
69
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
70 let mut req = self.make_req();
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
71 let buf = Arc::new(Mutex::new(Vec::new()));
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
72
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
73 let dst = buf.clone();
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
74 req.write_function(move |data| {
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
75 let mut dst = dst.lock().unwrap();
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
76 dst.extend_from_slice(data);
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
77 if dst.len() > MAX_RESPONSE_SIZE {
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
78 debug!("Too large params response from server: {}", dst.len());
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
79 Ok(0)
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
80 } else {
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
81 Ok(data.len())
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
82 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
83 }).unwrap();
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
84
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
85 // XXX too many clones
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
86 let se = self.clone();
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
87 let s = self.clone().session.unwrap().perform(req)
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
88 .map_err(|e| TemplogError::new_io("tokio curl error", e.into_error()))
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
89 .and_then(move |mut es| {
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
90 let result = buf.lock().unwrap();
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
91 se.handle_response(&mut es, &result)
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
92 });
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
93 Box::new(s)
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
94
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
95 /*
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
96 let mut p = Params::defaults();
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
97 p.fridge_setpoint = 17.0 + 4.0*rand::random::<f32>();
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
98 future::ok(Some(p)).boxed()
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
99 */
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
102 pub fn stream(&mut self) -> Box<Stream<Item=Params, Error=TemplogError>> {
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
104 let dur = Duration::from_millis(4000);
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
105 let mut s = Rc::new(self.clone());
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
106 let i = Interval::new(dur, &self.handle).unwrap()
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
107 .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
108 .and_then(move |()| {
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
109 let ss = Rc::get_mut(&mut s).unwrap();
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
110 ss.step()
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
111 })
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
112 // throw away None params
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
113 .filter_map(|p| p);
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
114 Box::new(i)
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117