comparison rust/src/main.rs @ 588:038734052b20 rust

fiddling with futures-rs instead
author Matt Johnston <matt@ucc.asn.au>
date Fri, 16 Dec 2016 01:10:57 +0800
parents 646f03870762
children f2508125adf1
comparison
equal deleted inserted replaced
587:646f03870762 588:038734052b20
1 extern crate robots; 1 #![feature(conservative_impl_trait)]
2 extern crate tokio_core;
3 extern crate futures;
2 4
3 mod sensor; 5 use std::time::Duration;
6 use std::cell::RefCell;
4 7
5 use std::sync::Arc; 8 use tokio_core::reactor::Interval;
6 use std::time::Duration; 9 use tokio_core::reactor::Core;
10 use tokio_core::reactor::Handle;
11 use futures::Stream;
12 use futures::Future;
13 use futures::IntoFuture;
7 14
8 use robots::actors::{Actor, ActorSystem, ActorCell, ActorContext, Props}; 15 pub struct Reading {
16 name: String,
17 value: Option<f32>,
18 }
19
20 pub type Readings = Vec<Reading>;
21
22 pub struct Sensor {
23 current: f32,
24 }
25
26 impl Sensor {
27
28 fn step(&mut self) -> Readings {
29 let mut r = Vec::new();
30 self.current = self.current + 0.1;
31 r.push(Reading { name: "aaa".to_string(), value: Some(self.current) });
32 r
33 }
34
35 pub fn new() -> Sensor {
36 Sensor { current: 22.0 }
37 }
38
39 pub fn run(handle: &Handle) -> impl Stream<Item=Readings> {
40 let mut s = Sensor::new();
41
42 let dur = Duration::from_millis(400);
43 Interval::new(dur, handle).unwrap().map(move |()| {
44 println!("each one");
45 // TODO read the sensor here
46 s.step()
47 } )
48 }
49 }
9 50
10 fn main() { 51 fn main() {
11 println!("Wort Templog"); 52 println!("Wort Templog");
12 let actor_system = ActorSystem::new("templog".to_string());
13 53
14 let props = Props::new(Arc::new(sensor::Sensor::new), ()); 54 let mut core = Core::new().unwrap();
15 let sensor = actor_system.actor_of(props, "sensor".to_string()); 55 let handle = core.handle();
16 56
17 actor_system.tell(sensor, "yeah".to_string()); 57 let s = Sensor::run(&handle);
18
19 std::thread::sleep(Duration::from_millis(100));
20 actor_system.shutdown();
21 58
59 let mut re = Readings::new();
22 60
61 let h = s.for_each(|r| {
62 re = r;
63 for rx in &re {
64 match rx.value {
65 Some(x) => println!("re is {} {}", rx.name, x),
66 None => println!("re is {} broken", rx.name),
67 }
68 };
69 Ok(())
70 });
71
72 core.run(h);
23 } 73 }
24 74