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
use std::{
io::{Read, Write},
net::TcpStream,
sync::{Arc, RwLock},
};
use robot_behavior::{RobotException, RobotResult};
use crate::PORT_CMD;
use crate::types::{CommandSerde, StateSerde};
#[derive(Default)]
pub struct NetWork {
tcp_cmd: Option<TcpStream>,
}
impl NetWork {
pub fn new(ip: &str) -> NetWork {
let tcp_cmd = Some(TcpStream::connect(format!("{ip}:{PORT_CMD}")).unwrap());
NetWork { tcp_cmd }
}
pub fn state_connect<S>(_ip: &str) -> Arc<RwLock<S>>
where
S: Default + StateSerde + Send + Sync + 'static,
{
let state = Arc::new(RwLock::new(S::default()));
// let ip_owned = ip.to_string();
// thread::spawn(move || {
// let mut tcp_state = TcpStream::connect(format!("{}:{}", ip_owned, PORT_STATE)).unwrap();
// loop {
// let mut receive_buffer = Vec::new();
// loop {
// let mut buffer = vec![0_u8; 1024 * 5];
// if let Ok(size) = tcp_state.read(&mut buffer) {
// receive_buffer.append(&mut buffer[..size].to_vec());
// println!("size:{}", size);
// } else {
// break;
// }
// }
// let data = S::state_from_str(std::str::from_utf8(&receive_buffer).unwrap());
// let mut write_guard = state.write().unwrap();
// *write_guard = data;
// }
// });
Arc::clone(&state)
}
pub fn send_and_recv<D, S>(&mut self, data: D) -> RobotResult<S>
where
D: CommandSerde,
S: CommandSerde,
{
if let Some(tcp_cmd) = &mut self.tcp_cmd {
let data = data.serialize();
#[cfg(feature = "debug")]
println!("Sending command: {}", data);
tcp_cmd.write_all(data.as_bytes()).unwrap();
let mut buffer = [0; 1024 * 10];
let size = tcp_cmd.read(&mut buffer).unwrap();
let data = std::str::from_utf8(&buffer[..size]).unwrap();
#[cfg(feature = "debug")]
println!("Received response: {}", data);
let data = S::deserialize(data).unwrap();
Ok(data)
} else {
Err(RobotException::NetworkError(
"TCP command stream is not initialized".to_string(),
))
}
}
}