annotate rust/src/params.rs @ 624:2710649ab71e rust

read/write params local file. untested
author Matt Johnston <matt@ucc.asn.au>
date Sun, 16 Apr 2017 23:29:27 +0800
parents 8136a6b99866
children efcbe0d3afd6
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;
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
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
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 use std::time::Duration;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 use tokio_core::reactor::Interval;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 use tokio_core::reactor::Handle;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
20 use tokio_curl::Session;
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
21 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
22 use curl::easy::Easy;
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
23 use curl::easy;
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
24 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
25
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 use types::*;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
27 use ::Config;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
29 #[derive(Deserialize, Serialize, Debug, Clone)]
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
30 pub struct Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
31 pub fridge_setpoint: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
32 pub fridge_difference: f32,
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 618
diff changeset
33 pub overshoot_delay: u64,
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
34 pub overshoot_factor: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
35 pub disabled: bool,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
36 pub nowort: bool,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
37 pub fridge_range_lower: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
38 pub fridge_range_upper: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
39 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
40
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
41 impl Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
42 pub fn defaults() -> Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
43 Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
44 fridge_setpoint: 16.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
45 fridge_difference: 0.2,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
46 overshoot_delay: 720, // 12 minutes
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
47 overshoot_factor: 1.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
48 disabled: false,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
49 nowort: false,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
50 fridge_range_lower: 3.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
51 fridge_range_upper: 3.0,
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
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
55 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
56 let mut s = String::new();
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
57 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
58 Ok(serde_json::from_str(&s)?)
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
59 }
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
60
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
61 pub fn load(config: &Config) -> Params {
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
62 Self::try_load(&config.PARAMS_FILE)
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
63 .unwrap_or_else(|_| Params::defaults())
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
64 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
65 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
66
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
67 #[derive(Deserialize, Debug)]
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
68 struct Response {
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
69 // sent as an opaque etag: header. Has format "epoch-nonce",
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
70 // responses where the epoch do not match ParamWaiter::epoch are dropped
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
71 etag: String,
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
72 params: Params,
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
73 }
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
74
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
75 #[derive(Clone)]
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
76 pub struct ParamWaiter {
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
77 limitlog: NotTooOften,
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
78 // last_etag is used for long-polling.
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
79 last_etag: RefCell<String>,
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
80 session: RefCell<Option<Session>>,
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
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
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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 {
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
92 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
93 let mut req = Easy::new();
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
94 req.get(true).unwrap();
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
95 // 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
96 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
97
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
98 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
99
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
100 // store response
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
101 let max_response_size = 10000;
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
102 let buf = Arc::new(Mutex::new(Vec::new()));
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
103 let dst = buf.clone();
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
104 req.write_function(move |data| {
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
105 let mut dst = dst.lock().unwrap();
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
106 dst.extend_from_slice(data);
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
107 if dst.len() > max_response_size {
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
108 error!("Too large params response from server: {}", dst.len());
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
109 Ok(0)
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
110 } else {
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
111 Ok(data.len())
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
112 }
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
113 }).unwrap();
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
114
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
115 // http header
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
116 let e = self.last_etag.borrow();
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
117 if !e.is_empty() {
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
118 let mut list = easy::List::new();
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
119 let hd = format!("etag: {}", *e);
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
120 list.append(&hd).unwrap();
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
121 req.http_headers(list).unwrap();
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
122 }
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
123
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
124 (req, buf)
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
125 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
126
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
127 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
128 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
129 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
130 match resp {
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
131 200 => {
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
132 // new params
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
133 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
134 let mut le = self.last_etag.borrow_mut();
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
135 *le = r.etag;
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
136 if le.split('-').next().unwrap() == &self.epoch {
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
137 self.write_params(&r.params);
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
138 Ok(r.params)
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
139 } else {
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
140 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
141 }
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
142 }
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
143 304 => {
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
144 // XXX this isn't really an error
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
145 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
146 },
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 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
149 },
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
150 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
151 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
152
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
153 fn write_params(&self, params: &Params) {
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
154 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
155 p.write(|f| {
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
156 serde_json::to_writer(f, params)
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
157 });
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
158 }
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
159
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
160 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
161
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
162 if rself.session.borrow().is_none() {
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
163 *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
164 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
165
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
166 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
167
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
168 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
169 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
170 .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
171 .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
172 let result = buf.lock().unwrap();
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
173 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
174 });
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
175 Box::new(s)
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
178 fn new(config: &Config, handle: &Handle) -> Self {
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
179 let mut b = [0u8; 15]; // 15 bytes -> 20 characters base64
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
180 rand::OsRng::new().unwrap().fill_bytes(&mut b);
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
181 let epoch = base64::encode(&b);
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
182 ParamWaiter {
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
183 limitlog: NotTooOften::new(LOG_MINUTES*60),
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
184 last_etag: RefCell::new(String::new()),
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
185 session: RefCell::new(None),
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
186 epoch: epoch,
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
187 config: config.clone(),
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
188 handle: handle.clone(),
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
189 }
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
190 }
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
191
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
192 pub fn stream(config: &Config, handle: &Handle) -> Box<Stream<Item=Params, Error=TemplogError>> {
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
193 let rcself = Rc::new(ParamWaiter::new(config, handle));
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
195 let dur = Duration::from_millis(4000);
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
196 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
197 .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
198 .and_then(move |()| {
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
199 Self::step(rcself.clone())
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
200 });
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
201
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
202 // 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
203 //Box::new(consume_errors(i))
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
204 let f = i.then(|r| {
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
205 match r {
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
206 Ok(v) => Ok(Some(v)),
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
207 Err(e) => {
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
208 debug!("Params stream error: {}", e);
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
209 Ok(None)
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
210 }
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
211 }
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
212 })
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
213 .filter_map(|p| p);
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
214 Box::new(f)
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217