I DID THE THING
parent
2c587b8457
commit
9cc5983223
|
|
@ -121,25 +121,15 @@ impl InputEngine for InputEngineUDP {
|
||||||
self.last_socket = Some(std::net::UdpSocket::bind(addr).unwrap());
|
self.last_socket = Some(std::net::UdpSocket::bind(addr).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = self._get();
|
let mut buf = [0; 128];
|
||||||
|
let result = self.last_socket.as_ref().unwrap().recv_from(&mut buf);
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
self.last_socket = None;
|
self.last_socket = None;
|
||||||
return Vec::<char>::new();
|
return Vec::<char>::new();
|
||||||
}
|
}
|
||||||
return result.unwrap();
|
let (amt, _) = result.unwrap();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InputEngineUDP {
|
|
||||||
fn _get(&mut self) -> Result<Vec<char>, std::io::Error> {
|
|
||||||
let mut buf = [0; 128];
|
|
||||||
if !self.last_socket.is_some() {
|
|
||||||
return Ok(Vec::<char>::new());
|
|
||||||
}
|
|
||||||
let (amt, _) = self.last_socket.as_ref().unwrap().recv_from(&mut buf)?;
|
|
||||||
let buf = &mut buf[..amt];
|
let buf = &mut buf[..amt];
|
||||||
return Ok(std::str::from_utf8(buf).unwrap().chars().collect());
|
return std::str::from_utf8(buf).unwrap().chars().collect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -191,7 +181,7 @@ mod test_input {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait OutputEngine {
|
pub trait OutputEngine {
|
||||||
fn put(&self, v: Vec<char>);
|
fn put(&mut self, v: Vec<char>);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_output_engine(cfg: &Engine) -> Result<Box<dyn OutputEngine>, String> {
|
pub fn build_output_engine(cfg: &Engine) -> Result<Box<dyn OutputEngine>, String> {
|
||||||
|
|
@ -205,7 +195,7 @@ pub fn build_output_engine(cfg: &Engine) -> Result<Box<dyn OutputEngine>, String
|
||||||
struct OutputEngineStdout {}
|
struct OutputEngineStdout {}
|
||||||
|
|
||||||
impl OutputEngine for OutputEngineStdout {
|
impl OutputEngine for OutputEngineStdout {
|
||||||
fn put(&self, v: Vec<char>) {
|
fn put(&mut self, v: Vec<char>) {
|
||||||
println!("{}", v.iter().cloned().collect::<String>());
|
println!("{}", v.iter().cloned().collect::<String>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -216,22 +206,23 @@ mod test_output {
|
||||||
|
|
||||||
struct OutputEngineTest {}
|
struct OutputEngineTest {}
|
||||||
impl OutputEngine for OutputEngineTest {
|
impl OutputEngine for OutputEngineTest {
|
||||||
fn put(&self, _v: Vec<char>) {
|
fn put(&mut self, _v: Vec<char>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_output_engine_impl() {
|
fn test_output_engine_impl() {
|
||||||
let output_engine_test = OutputEngineTest{};
|
let mut output_engine_test = OutputEngineTest{};
|
||||||
_test_output_engine_impl(&output_engine_test);
|
_test_output_engine_impl(&mut output_engine_test);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _test_output_engine_impl(engine: &dyn OutputEngine) {
|
fn _test_output_engine_impl(engine: &mut dyn OutputEngine) {
|
||||||
engine.put("teehee".to_string().chars().collect());
|
engine.put("teehee".to_string().chars().collect());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct OutputEngineUDP {
|
struct OutputEngineUDP {
|
||||||
|
last_socket: Option<std::net::UdpSocket>,
|
||||||
host: String,
|
host: String,
|
||||||
port: i32,
|
port: i32,
|
||||||
}
|
}
|
||||||
|
|
@ -239,15 +230,29 @@ struct OutputEngineUDP {
|
||||||
pub fn build_output_engine_udp(cfg: &Engine) -> Result<Box<dyn OutputEngine>, String> {
|
pub fn build_output_engine_udp(cfg: &Engine) -> Result<Box<dyn OutputEngine>, String> {
|
||||||
let udp_cfg = cfg.udp.as_ref().unwrap();
|
let udp_cfg = cfg.udp.as_ref().unwrap();
|
||||||
return Ok(Box::new(OutputEngineUDP{
|
return Ok(Box::new(OutputEngineUDP{
|
||||||
|
last_socket: None,
|
||||||
host: udp_cfg.host.clone().unwrap(),
|
host: udp_cfg.host.clone().unwrap(),
|
||||||
port: udp_cfg.port,
|
port: udp_cfg.port,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OutputEngine for OutputEngineUDP {
|
impl OutputEngine for OutputEngineUDP {
|
||||||
fn put(&self, v: Vec<char>) {
|
fn put(&mut self, v: Vec<char>) {
|
||||||
let socket = std::net::UdpSocket::bind("127.0.0.1:".to_string() + &(self.port+10).to_string()).unwrap();
|
if self.last_socket.is_none() {
|
||||||
socket.connect(self.host.to_string() + ":" + &self.port.to_string()).unwrap();
|
let result = std::net::UdpSocket::bind("127.0.0.1:".to_string() + &(self.port+10).to_string());
|
||||||
socket.send(&v.iter().cloned().collect::<String>().as_bytes()).unwrap();
|
if result.is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.last_socket = Some(result.unwrap());
|
||||||
|
if self.last_socket.as_ref().unwrap().connect(self.host.to_string() + ":" + &self.port.to_string()).is_err() {
|
||||||
|
self.last_socket = None;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = self.last_socket.as_ref().unwrap().send(&v.iter().cloned().collect::<String>().as_bytes());
|
||||||
|
if result.is_err() {
|
||||||
|
self.last_socket = None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ fn main() {
|
||||||
println!("{:?} => {}", cfg.streams.input.engine.name, input_engine.is_ok());
|
println!("{:?} => {}", cfg.streams.input.engine.name, input_engine.is_ok());
|
||||||
println!("{:?} => {}", cfg.streams.output.engine.name, output_engine.is_ok());
|
println!("{:?} => {}", cfg.streams.output.engine.name, output_engine.is_ok());
|
||||||
let mut input_engine = input_engine.unwrap();
|
let mut input_engine = input_engine.unwrap();
|
||||||
let output_engine = output_engine.unwrap();
|
let mut output_engine = output_engine.unwrap();
|
||||||
loop {
|
loop {
|
||||||
output_engine.put(input_engine.get());
|
output_engine.put(input_engine.get());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue