Mercurial > templog
changeset 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 | 03167105f6de |
children | 8152ef251dbb |
files | rust/Cargo.lock rust/Cargo.toml rust/src/main.rs rust/src/params.rs |
diffstat | 4 files changed, 57 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/Cargo.lock Sun Apr 16 22:50:20 2017 +0800 +++ b/rust/Cargo.lock Sun Apr 16 23:29:27 2017 +0800 @@ -2,6 +2,7 @@ name = "wort-templog" version = "0.1.0" dependencies = [ + "atomicwrites 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "curl 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "docopt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -41,6 +42,17 @@ ] [[package]] +name = "atomicwrites" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "base64" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -272,6 +284,19 @@ ] [[package]] +name = "nix" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "num-traits" version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -453,6 +478,14 @@ ] [[package]] +name = "tempdir" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "thread-id" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -607,6 +640,7 @@ [metadata] "checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699" +"checksum atomicwrites 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c4328d3fa4af4aedc582587976a0f74decc73f90ea7cae3d757c0535f983d16f" "checksum base64 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9892882c3bd89ed02dec391c128984c772b663a29700c32b5de0b33861cdf2bd" "checksum bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dead7461c1127cf637931a1e50934eb6eee8bff2f74433ac7909e9afcee04a3" "checksum byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c40977b0ee6b9885c9013cd41d9feffdd22deb3bb4dc3a71d901cc7a77de18c8" @@ -635,6 +669,7 @@ "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" "checksum net2 0.2.27 (registry+https://github.com/rust-lang/crates.io-index)" = "18b9642ad6222faf5ce46f6966f59b71b9775ad5758c9e09fcf0a6c8061972b4" "checksum nix 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a7bb1da2be7da3cbffda73fc681d509ffd9e665af478d2bee1907cee0bc64b2" +"checksum nix 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a0d95c5fa8b641c10ad0b8887454ebaafa3c92b5cd5350f8fc693adafd178e7b" "checksum num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "e1cbfa3781f3fe73dc05321bed52a06d2d491eaa764c52335cf4399f046ece99" "checksum num_cpus 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a18c392466409c50b87369414a2680c93e739aedeb498eb2bff7d7eb569744e2" "checksum openssl-probe 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d98df0270d404ccd3c050a41d579c52d1db15375168bb3471e04ec0f5f378daf" @@ -660,6 +695,7 @@ "checksum syn 0.11.10 (registry+https://github.com/rust-lang/crates.io-index)" = "171b739972d9a1bfb169e8077238b51f9ebeaae4ff6e08072f7ba386a8802da2" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum sysfs_gpio 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "90fc056b3e1a0f1cd10de67b14b62b85dfe450134e87bd9af2f066981c6ebb75" +"checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" "checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a" "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
--- a/rust/Cargo.toml Sun Apr 16 22:50:20 2017 +0800 +++ b/rust/Cargo.toml Sun Apr 16 23:29:27 2017 +0800 @@ -23,6 +23,7 @@ serde_json = "0.9" base64 = "0.4" libc = "0.2" +atomicwrites = "0.1" # linux only [target.linux.dependencies]
--- a/rust/src/main.rs Sun Apr 16 22:50:20 2017 +0800 +++ b/rust/src/main.rs Sun Apr 16 23:29:27 2017 +0800 @@ -9,6 +9,7 @@ extern crate curl; extern crate serde_json; extern crate libc; +extern crate atomicwrites; #[macro_use] extern crate lazy_static;
--- a/rust/src/params.rs Sun Apr 16 22:50:20 2017 +0800 +++ b/rust/src/params.rs Sun Apr 16 23:29:27 2017 +0800 @@ -3,6 +3,7 @@ extern crate rand; extern crate serde_json; extern crate base64; +extern crate atomicwrites; use std::time::Duration; use std::io; @@ -11,6 +12,8 @@ use std::sync::{Arc,Mutex}; use std::error::Error; use std::cell::{Cell,RefCell}; +use std::fs::File; +use std::io::Read; use tokio_core::reactor::Interval; use tokio_core::reactor::Handle; @@ -49,12 +52,15 @@ } } + fn try_load(filename: &str) -> Result<Params, TemplogError> { + let mut s = String::new(); + File::open(filename)?.read_to_string(&mut s)?; + Ok(serde_json::from_str(&s)?) + } + pub fn load(config: &Config) -> Params { - // generate random epoch on success. - // TODO: return failure? or just return default() ? - let mut p = Params::defaults(); - - unimplemented!(); + Self::try_load(&config.PARAMS_FILE) + .unwrap_or_else(|_| Params::defaults()) } } @@ -128,6 +134,7 @@ let mut le = self.last_etag.borrow_mut(); *le = r.etag; if le.split('-').next().unwrap() == &self.epoch { + self.write_params(&r.params); Ok(r.params) } else { Err(TemplogError::new(&format!("Bad epoch from server '{}' expected '{}'", *le, self.epoch))) @@ -143,6 +150,13 @@ } } + fn write_params(&self, params: &Params) { + let p = atomicwrites::AtomicFile::new(&self.config.PARAMS_FILE, atomicwrites::AllowOverwrite); + p.write(|f| { + serde_json::to_writer(f, params) + }); + } + fn step(rself: Rc<Self>) -> Box<Future<Item=Params, Error=TemplogError>> { if rself.session.borrow().is_none() {