annotate rust/src/params.rs @ 633:490e9e15b98c rust

move some bits to riker
author Matt Johnston <matt@ucc.asn.au>
date Wed, 04 Sep 2019 23:24:13 +0800
parents bde302def78e
children a5721c02d3ee
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 use std::time::Duration;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 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
3 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
4 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
5 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
6 use std::error::Error;
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
7 use std::cell::{Cell,RefCell};
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
8 use std::fs::File;
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
9 use std::io::Read;
627
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
10
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
11 use serde::{Serialize,Deserialize};
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
13 use rand::rngs::{StdRng, OsRng};
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
14 use rand::{RngCore, SeedableRng};
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
15
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
16 use std::str::FromStr;
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
17 use hyper;
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
18 use hyper::client::Client;
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
19
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
20 use riker::actors::*;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
21
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
22 use super::types::*;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
23 use super::config::Config;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
25 #[derive(Deserialize, Serialize, Debug, Clone)]
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
26 pub struct Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
27 pub fridge_setpoint: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
28 pub fridge_difference: f32,
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 618
diff changeset
29 pub overshoot_delay: u64,
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
30 pub overshoot_factor: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
31 pub disabled: bool,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
32 pub nowort: bool,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
33 pub fridge_range_lower: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
34 pub fridge_range_upper: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
35 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
36
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
37 impl Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
38 pub fn defaults() -> Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
39 Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
40 fridge_setpoint: 16.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
41 fridge_difference: 0.2,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
42 overshoot_delay: 720, // 12 minutes
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
43 overshoot_factor: 1.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
44 disabled: false,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
45 nowort: false,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
46 fridge_range_lower: 3.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
47 fridge_range_upper: 3.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
48 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
49 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
50
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
51 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
52 let mut s = String::new();
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
53 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
54 Ok(serde_json::from_str(&s)?)
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
55 }
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
56
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
57 pub fn load(config: &Config) -> Params {
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
58 Self::try_load(&config.PARAMS_FILE)
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
59 .unwrap_or_else(|_| Params::defaults())
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
60 }
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
61
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
62 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
63
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
64 pub struct ParamWaiter {
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
65 limitlog: NotTooOften,
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
66 // last_etag is used for long-polling.
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
67 last_etag: RefCell<String>,
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
68 epoch: String,
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
69 chan: ChannelRef<Params>,
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
70
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
71 config: Config,
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
74 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
75 const MAX_RESPONSE_SIZE: usize = 10000;
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
76 const TIMEOUT_MINUTES: u64 = 5;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
77
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
78 impl ParamWaiter {
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
79
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
80 fn new(config: Config) -> Self {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
81 let mut b = [0u8; 15]; // 15 bytes -> 20 characters base64
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
82 OsRng.fill_bytes(&mut b);
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
83 let epoch = base64::encode(&b);
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
84
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
85 ParamWaiter {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
86 limitlog: NotTooOften::new(LOG_MINUTES*60),
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
87 last_etag: RefCell::new(String::new()),
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
88 epoch: epoch,
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
89 config: config,
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
90 }
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
91 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
92
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
93
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
94 async fn keep_waiting(&mut self) {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
95 loop {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
96 self.wait_updates().await;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
97 }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
98 }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
99
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
100 async fn wait_updates(&mut self) {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
101
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
102 let uri = self.config.SETTINGS_URL.parse().expect("Bad SETTINGS_URL in config");
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
103 let mut req = hyper::Request::new(hyper::Method::Get, uri);
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
104 req.headers_mut().insert(hyper::header::ETAG, self.last_etag.borrow());
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
105 let resp = hyper::Client::new(&self.handle).request(req).await?;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
106 let b = resp.body().concat2().await?;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
107 let new_params = self.handle_response(b)?;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
108 self.chan.tell(Publish{msg: new_params, topic: "params".into()}, None);
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
109 }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
110
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
111 fn handle_response(&self, buf : hyper::Chunk, status: hyper::StatusCode) -> Result<Params, TemplogError> {
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
112 #[derive(Deserialize, Debug)]
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
113 struct Response {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
114 // sent as an opaque etag: header. Has format "epoch-nonce",
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
115 // responses where the epoch do not match ParamWaiter::epoch are dropped
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
116 etag: String,
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
117 params: Params,
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
118 }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
119
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
120 let text = String::from_utf8_lossy(buf.as_ref());
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
121 match status {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
122 hyper::StatusCode::Ok => {
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
123 // new params
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
124 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
125 let mut le = self.last_etag.borrow_mut();
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
126 *le = r.etag;
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
127
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
128 // update params if the epoch is correct
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
129 if let Some(e) = le.split('-').next() {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
130 if e == &self.epoch {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
131 self.write_params(&r.params);
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
132 return Ok(r.params);
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
133 }
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
134 }
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
135 Err(TemplogError::new(&format!("Bad epoch from server '{}' expected '{}'", *le, self.epoch)))
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
136 }
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
137 hyper::StatusCode::NotModified => {
629
3e5e52d50af5 comment
Matt Johnston <matt@ucc.asn.au>
parents: 627
diff changeset
138 // XXX this isn't really an error. Should handle_response() return
3e5e52d50af5 comment
Matt Johnston <matt@ucc.asn.au>
parents: 627
diff changeset
139 // Result<Option<Params>, TemplogError> instead?
3e5e52d50af5 comment
Matt Johnston <matt@ucc.asn.au>
parents: 627
diff changeset
140
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
141 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
142 },
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
143 _ => {
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
144 Err(TemplogError::new(&format!("Wrong server response code {}: {}", status.as_u16(), text)))
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
145 },
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
146 }
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
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
149 fn write_params(&self, params: &Params) {
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
150 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
151 p.write(|f| {
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
152 serde_json::to_writer(f, params)
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
153 });
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
154 }
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
155 }
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
156
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
157 impl Actor for ParamWaiter {
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
158
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
159 fn post_start(&mut self, ctx: &Context<Self::Msg>) {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
160 self.chan = channel("params", &ctx.system).unwrap();
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
161 ctx.run(self.wait_updates());
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
165 // pub fn stream(config: Config, handle: Handle) -> Result<(), TemplogError> {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
166 // let rcself = Rc::new(ParamWaiter::new(config, handle));
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
167
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
168 // let dur = Duration::from_millis(4000);
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
169 // for _ in Interval::new(dur, &rcself.handle).unwrap() {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
170 // // fetch params
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
171 // // TODO - skip if inflight.
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
172 // let r = await!(rcself.make_request()).map_err(|e| TemplogError::new_hyper("response", e))?;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
173 // let status = r.status();
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
174 // let b = await!(r.body().concat2()).map_err(|e| TemplogError::new_hyper("body", e))?;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
175 // if let Ok(params) = rcself.handle_response(b, status) {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
176 // stream_yield!(params);
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
177 // }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
178 // }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
179 // Ok(())
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
180 // }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
181