summaryrefslogtreecommitdiffstats
path: root/src/client.rs
blob: 320ae8685c03418b6e4f2360cfa04fb4ae01bd58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use tokio::io::*;

use bytes::BufMut;
use tokio::net::TcpStream;

use std::net::Shutdown;
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::Duration;

use super::events::Event;
use super::events::stream_mapper::*;
use super::commands::stream_mapper::CommandToByteMapper;
use super::commands::Command;

type EventClosure = dyn FnMut(Event) + Sync + Send + 'static;
type EventClosureMutex = Box<EventClosure>;

pub fn event_handler<F>(f: F) -> EventClosureMutex
        where F: FnMut(Event) + Sync + Send + 'static
    {
        Box::new(f)
    }

pub struct FlicClient {
    stream: TcpStream,
    command_mapper: CommandToByteMapper,
    map: Vec<EventClosureMutex>,
    is_running: bool,
}

impl FlicClient {
    pub async fn new(conn: &str) -> Result<FlicClient> {
        match TcpStream::connect(conn).await {
            Ok(stream) => {
                println!("stream open");
                Ok(FlicClient{
                    stream,
                    command_mapper: CommandToByteMapper::new(),
                    map: vec![],
                    is_running: true,
                })
            }
            Err(err) => Err(err)
        }
        
    }
    pub fn register_event_handler(mut self, event: EventClosureMutex) -> Self {
        self.map.push(event);
        self
    }
    pub async fn listen(&mut self) {
        let mut mapper = ByteToEventMapper::new();
        let (mut reader, _writer) = self.stream.split();
        let mut buffer = vec![];
        while self.is_running {
            if let Some(size) = reader.read_buf(&mut buffer).await.ok() {
                for b in buffer.iter() {
                    match mapper.map(*b) {
                        EventResult::Some(Event::NoOp) => {}
                        EventResult::Some(event) => for ref mut f in &mut self.map {
                            f(event.clone());
                        }
                        _ => {}
                    }
                }

            }
        }
    }
    pub async fn stop(&mut self) {
        self.is_running = false;
        self.stream.shutdown(Shutdown::Both);
        println!("stopped");
    }

    pub async fn submit(&mut self, cmd: Command) {
        let (_reader, mut writer) = self.stream.split();
            for b in self.command_mapper.map(cmd) {
                writer.write_u8(b).await;
                println!("{:?}", b);
            }
    }
}