comparison 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
comparison
equal deleted inserted replaced
623:03167105f6de 624:2710649ab71e
1 extern crate tokio_core; 1 extern crate tokio_core;
2 extern crate futures; 2 extern crate futures;
3 extern crate rand; 3 extern crate rand;
4 extern crate serde_json; 4 extern crate serde_json;
5 extern crate base64; 5 extern crate base64;
6 extern crate atomicwrites;
6 7
7 use std::time::Duration; 8 use std::time::Duration;
8 use std::io; 9 use std::io;
9 use std::str; 10 use std::str;
10 use std::rc::Rc; 11 use std::rc::Rc;
11 use std::sync::{Arc,Mutex}; 12 use std::sync::{Arc,Mutex};
12 use std::error::Error; 13 use std::error::Error;
13 use std::cell::{Cell,RefCell}; 14 use std::cell::{Cell,RefCell};
15 use std::fs::File;
16 use std::io::Read;
14 17
15 use tokio_core::reactor::Interval; 18 use tokio_core::reactor::Interval;
16 use tokio_core::reactor::Handle; 19 use tokio_core::reactor::Handle;
17 use tokio_curl::Session; 20 use tokio_curl::Session;
18 use futures::{Stream,Future,future}; 21 use futures::{Stream,Future,future};
47 fridge_range_lower: 3.0, 50 fridge_range_lower: 3.0,
48 fridge_range_upper: 3.0, 51 fridge_range_upper: 3.0,
49 } 52 }
50 } 53 }
51 54
55 fn try_load(filename: &str) -> Result<Params, TemplogError> {
56 let mut s = String::new();
57 File::open(filename)?.read_to_string(&mut s)?;
58 Ok(serde_json::from_str(&s)?)
59 }
60
52 pub fn load(config: &Config) -> Params { 61 pub fn load(config: &Config) -> Params {
53 // generate random epoch on success. 62 Self::try_load(&config.PARAMS_FILE)
54 // TODO: return failure? or just return default() ? 63 .unwrap_or_else(|_| Params::defaults())
55 let mut p = Params::defaults();
56
57 unimplemented!();
58 } 64 }
59 } 65 }
60 66
61 #[derive(Deserialize, Debug)] 67 #[derive(Deserialize, Debug)]
62 struct Response { 68 struct Response {
126 // new params 132 // new params
127 let r: Response = serde_json::from_str(&text)?; 133 let r: Response = serde_json::from_str(&text)?;
128 let mut le = self.last_etag.borrow_mut(); 134 let mut le = self.last_etag.borrow_mut();
129 *le = r.etag; 135 *le = r.etag;
130 if le.split('-').next().unwrap() == &self.epoch { 136 if le.split('-').next().unwrap() == &self.epoch {
137 self.write_params(&r.params);
131 Ok(r.params) 138 Ok(r.params)
132 } else { 139 } else {
133 Err(TemplogError::new(&format!("Bad epoch from server '{}' expected '{}'", *le, self.epoch))) 140 Err(TemplogError::new(&format!("Bad epoch from server '{}' expected '{}'", *le, self.epoch)))
134 } 141 }
135 } 142 }
139 }, 146 },
140 _ => { 147 _ => {
141 Err(TemplogError::new(&format!("Wrong server response code {}: {}", resp, text))) 148 Err(TemplogError::new(&format!("Wrong server response code {}: {}", resp, text)))
142 }, 149 },
143 } 150 }
151 }
152
153 fn write_params(&self, params: &Params) {
154 let p = atomicwrites::AtomicFile::new(&self.config.PARAMS_FILE, atomicwrites::AllowOverwrite);
155 p.write(|f| {
156 serde_json::to_writer(f, params)
157 });
144 } 158 }
145 159
146 fn step(rself: Rc<Self>) -> Box<Future<Item=Params, Error=TemplogError>> { 160 fn step(rself: Rc<Self>) -> Box<Future<Item=Params, Error=TemplogError>> {
147 161
148 if rself.session.borrow().is_none() { 162 if rself.session.borrow().is_none() {