annotate rust/src/params.rs @ 615:f153aec221be rust

move Params, epoch code
author Matt Johnston <matt@ucc.asn.au>
date Tue, 07 Mar 2017 23:56:12 +0800
parents rust/src/paramwaiter.rs@e1bab5b36352
children a85c0c9bc1fa
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;
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
12 use std::cell::{Cell,RefCell};
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 use tokio_core::reactor::Interval;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 use tokio_core::reactor::Handle;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
16 use tokio_curl::Session;
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
17 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
18 use curl::easy::Easy;
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
19 use curl::easy;
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
20
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 use types::*;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
22 use ::Config;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
24 #[derive(Deserialize, Serialize, Debug, Clone)]
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
25 pub struct Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
26 pub fridge_setpoint: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
27 pub fridge_difference: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
28 pub overshoot_delay: u32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
29 pub overshoot_factor: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
30 pub disabled: bool,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
31 pub nowort: bool,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
32 pub fridge_range_lower: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
33 pub fridge_range_upper: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
34
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
35 #[serde(skip_serializing)]
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
36 pub epoch: String,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
37 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
38
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
39 impl Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
40 pub fn defaults() -> Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
41 Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
42 fridge_setpoint: 16.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
43 fridge_difference: 0.2,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
44 overshoot_delay: 720, // 12 minutes
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
45 overshoot_factor: 1.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
46 disabled: false,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
47 nowort: false,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
48 fridge_range_lower: 3.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
49 fridge_range_upper: 3.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
50 epoch: String::new(),
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
51 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
52 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
53
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
54 pub fn load(config: &Config) -> Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
55 // generate random epoch on success.
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
56 // TODO: return failure? or just return default() ?
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
57 unimplemented!();
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
58 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
59 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
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 {
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
63 epoch_tag: String,
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
64 params: Params,
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
65 }
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
66
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
67 #[derive(Clone)]
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
68 pub struct ParamWaiter {
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
69 limitlog: NotTooOften,
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
70 epoch_tag: RefCell<String>,
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
71 session: RefCell<Option<Session>>,
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
72
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
73 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
74 handle: Handle,
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
77 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
78 const MAX_RESPONSE_SIZE: usize = 10000;
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
79 const TIMEOUT_MINUTES: u64 = 5;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
80
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
81 impl ParamWaiter {
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
82 fn make_request(&self) -> Easy {
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
83 let mut req = Easy::new();
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
84 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
85 // 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
86 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
87
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
88 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
89
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
90 // http header
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
91 // etag: epoch-tag
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
92 let e = self.epoch_tag.borrow();
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
93 if !e.is_empty() {
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
94 let mut list = easy::List::new();
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
95 let hd = format!("etag: {}", *e);
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
96 list.append(&hd).unwrap();
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
97 req.http_headers(list).unwrap();
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
98 }
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
99
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
100 req
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
101 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
102
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
103 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
104 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
105 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
106 match resp {
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
107 200 => {
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
108 // new params
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
109 let r: Response = serde_json::from_str(&text)?;
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
110 *self.epoch_tag.borrow_mut() = r.epoch_tag;
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
111 Ok(Some(r.params))
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
112 }
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
113 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
114 _ => {
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
115 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
116 },
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
117 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
118 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
119
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
120 fn step(rself: Rc<Self>) -> Box<Future<Item=Option<Params>, Error=TemplogError>> {
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
121
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
122 if rself.session.borrow().is_none() {
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
123 *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
124 }
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
125 let ses = rself.session.borrow().clone().unwrap();
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
126
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
127 let mut req = 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
128 let buf = Arc::new(Mutex::new(Vec::new()));
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
129
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
130 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
131 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
132 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
133 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
134 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
135 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
136 Ok(0)
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
137 } else {
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
138 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
139 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
140 }).unwrap();
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
141
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
142 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
143 .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
144 .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
145 let result = buf.lock().unwrap();
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
146 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
147 });
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
148 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
149
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
150 /*
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
151 let mut p = Params::defaults();
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
152 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
153 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
154 */
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
157 fn new(config: &Config, epoch: String, handle: &Handle) -> Self {
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
158 ParamWaiter {
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
159 limitlog: NotTooOften::new(LOG_MINUTES*60),
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
160 epoch_tag: RefCell::new(epoch),
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
161 session: RefCell::new(None),
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
162 config: config.clone(),
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
163 handle: handle.clone(),
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
164 }
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
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
167 pub fn stream(config: &Config, epoch: String, handle: &Handle) -> Box<Stream<Item=Params, Error=TemplogError>> {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
168 let rcself = Rc::new(ParamWaiter::new(config, epoch, handle));
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
170 let dur = Duration::from_millis(4000);
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
171 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
172 .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
173 .and_then(move |()| {
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
174 Self::step(rcself.clone())
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
175 })
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
176 // throw away None params
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
177 .filter_map(|p| p);
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
178 Box::new(i)
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181