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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::net::SocketAddr;
use futures::Future;
use futures::future::{ok, Either, err};
use futures::sync::mpsc::{Sender, Receiver};
use futures::sync::oneshot::channel;
use tokio::prelude::*;
use tokio_core::reactor::Core;
use nt_packet::ClientMessage;
use proto::*;
use proto::types::*;
use std::sync::{Arc, Mutex};
use std::io::{Error, ErrorKind};
use std::thread;
use std::collections::HashMap;
pub(crate) mod state;
mod conn;
mod handler;
use self::handler::*;
use self::state::*;
use self::conn::Connection;
pub struct NetworkTables {
state: Arc<Mutex<State>>,
}
impl NetworkTables {
pub fn connect(client_name: &'static str, target: SocketAddr) -> NetworkTables {
let state = Arc::new(Mutex::new(State::new()));
state.lock().unwrap().set_connection_state(ConnectionState::Connecting);
let (tx, rx) = channel();
let thread_state = state.clone();
let _ = thread::spawn(move || {
let mut core = Core::new().unwrap();
let handle = core.handle();
let state = thread_state;
{
state.lock().unwrap().set_handle(handle.clone().remote().clone());
}
core.run(Connection::new(&handle, &target, client_name, state, tx)).unwrap()
});
rx.wait().unwrap();
{
while state.lock().unwrap().connection_state().connecting() {
}
}
NetworkTables {
state,
}
}
pub fn entries(&self) -> HashMap<u16, EntryData> {
self.state.lock().unwrap().entries()
}
pub fn get_entry(&self, id: u16) -> EntryData {
let state = self.state.lock().unwrap().clone();
state.get_entry(id).clone()
}
pub fn create_entry(&mut self, data: EntryData) {
let rx = self.state.lock().unwrap().create_entry(data);
rx.wait().next();
}
pub fn delete_entry(&mut self, id: u16) {
self.state.lock().unwrap().delete_entry(id);
}
pub fn delete_all_entries(&mut self) {
self.state.lock().unwrap().delete_all_entries();
}
pub fn update_entry(&mut self, id: u16, new_value: EntryValue) {
self.state.lock().unwrap().update_entry(id, new_value);
}
pub fn update_entry_flags(&mut self, id: u16, flags: u8) {
self.state.lock().unwrap().update_entry_flags(id, flags);
}
pub fn connected(&self) -> bool {
self.state.lock().unwrap().connection_state().connected()
}
}
#[doc(hidden)]
pub(crate) fn send_packets(tx: impl Sink<SinkItem=Box<ClientMessage>, SinkError=Error>, rx: Receiver<Box<ClientMessage>>) -> impl Future<Item=(), Error=()> {
debug!("Spawned packet send loop");
rx
.map_err(|_| ())
.fold(tx, |tx, packet| tx.send(packet).map_err(|_| ()))
.then(|_| Ok(()))
}
#[doc(hidden)]
pub fn poll_socket(state: Arc<Mutex<State>>, rx: impl Stream<Item=Packet, Error=Error>, tx: Sender<Box<ClientMessage>>) -> impl Future<Item=(), Error=()> {
debug!("Spawned socket poll");
rx
.fold(tx, move |tx, packet| {
match handle_packet(packet, state.clone(), tx.clone()) {
Ok(Some(packet)) => Either::A(tx.send(packet).map_err(|e| Error::new(ErrorKind::Other, format!("{}", e)))),
Ok(None) => Either::B(ok(tx)),
Err(e) => Either::B(err(e)),
}
})
.map_err(|e| error!("handle_packet encountered an error: {}", e))
.map(drop)
}