rename engine to stream to capture more config
parent
321630a945
commit
ca9623b61d
|
|
@ -17,6 +17,7 @@ pub struct Streams {
|
|||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Stream {
|
||||
pub engine: Engine,
|
||||
pub format: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
|
|
@ -67,6 +68,7 @@ fn build_config_std() -> Config {
|
|||
return Config {
|
||||
streams: Streams{
|
||||
input: Stream {
|
||||
format: None,
|
||||
engine: Engine{
|
||||
name: String::from("gui"),
|
||||
kafka: None,
|
||||
|
|
@ -75,6 +77,7 @@ fn build_config_std() -> Config {
|
|||
},
|
||||
},
|
||||
output: Stream {
|
||||
format: None,
|
||||
engine: Engine{
|
||||
name: String::from("stdout"),
|
||||
kafka: None,
|
||||
|
|
|
|||
10
src/gui.rs
10
src/gui.rs
|
|
@ -5,13 +5,13 @@ use iced::event;
|
|||
use iced::subscription;
|
||||
use iced::{Alignment, Element, Application, Settings, Subscription, Theme, Command};
|
||||
|
||||
use crate::engine::OutputEngine;
|
||||
use crate::stream::OutputStream;
|
||||
|
||||
pub fn main(outputEngine: Box<dyn OutputEngine>) -> iced::Result {
|
||||
pub fn main(outputStream: Box<dyn OutputStream>) -> iced::Result {
|
||||
let def: iced::Settings<()> = Settings::default();
|
||||
let settings = Settings{
|
||||
flags: Flags {
|
||||
outputEngine: outputEngine,
|
||||
outputStream: outputStream,
|
||||
},
|
||||
antialiasing: def.antialiasing,
|
||||
default_font: def.default_font,
|
||||
|
|
@ -33,7 +33,7 @@ struct Main {
|
|||
}
|
||||
|
||||
struct Flags {
|
||||
outputEngine: Box<dyn OutputEngine>,
|
||||
outputStream: Box<dyn OutputStream>,
|
||||
}
|
||||
|
||||
struct Inputs {
|
||||
|
|
@ -153,7 +153,7 @@ impl Application for Main {
|
|||
_ => String::from(""),
|
||||
};
|
||||
if s.len() > 0 {
|
||||
self.flags.outputEngine.put(s.chars().collect());
|
||||
self.flags.outputStream.put(s.chars().collect());
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
|
|
|
|||
12
src/main.rs
12
src/main.rs
|
|
@ -1,21 +1,21 @@
|
|||
mod config;
|
||||
mod engine;
|
||||
mod stream;
|
||||
mod gui;
|
||||
|
||||
fn main() {
|
||||
let cfg = config::build_config().unwrap();
|
||||
if cfg.streams.input.engine.name == "gui" {
|
||||
let output_engine = engine::build_output_engine(&cfg.streams.output.engine);
|
||||
gui::main(output_engine).unwrap();
|
||||
let output_stream = stream::build_output_stream(&cfg.streams.output);
|
||||
gui::main(output_stream).unwrap();
|
||||
} else {
|
||||
main_cli(cfg);
|
||||
}
|
||||
}
|
||||
|
||||
fn main_cli(cfg: config::Config) {
|
||||
let mut input_engine = engine::build_input_engine(&cfg.streams.input.engine);
|
||||
let mut output_engine = engine::build_output_engine(&cfg.streams.output.engine);
|
||||
let mut input_stream = stream::build_input_stream(&cfg.streams.input);
|
||||
let mut output_stream = stream::build_output_stream(&cfg.streams.output);
|
||||
loop {
|
||||
output_engine.put(input_engine.get());
|
||||
output_stream.put(input_stream.get());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,38 +1,38 @@
|
|||
use crate::config::Engine;
|
||||
use crate::config::Stream;
|
||||
|
||||
use hidapi::HidApi;
|
||||
use rusb::UsbContext;
|
||||
use gilrs::{Gilrs, Button, Event};
|
||||
|
||||
pub trait InputEngine {
|
||||
pub trait InputStream {
|
||||
fn get(&mut self) -> Vec<char>;
|
||||
}
|
||||
|
||||
pub fn build_input_engine(cfg: &Engine) -> Box<dyn InputEngine> {
|
||||
match cfg.name.as_str() {
|
||||
pub fn build_input_stream(cfg: &Stream) -> Box<dyn InputStream> {
|
||||
match cfg.engine.name.as_str() {
|
||||
"stdin" => {
|
||||
return Box::new(InputEngineStdin{});
|
||||
return Box::new(InputStreamStdin{});
|
||||
},
|
||||
"kafka" => {
|
||||
return Box::new(build_input_engine_kafka(&cfg).unwrap());
|
||||
return Box::new(build_input_stream_kafka(&cfg).unwrap());
|
||||
},
|
||||
"udp" => return Box::new(build_input_engine_udp(&cfg).unwrap()),
|
||||
"device" => return Box::new(build_input_engine_device(&cfg).unwrap()),
|
||||
"udp" => return Box::new(build_input_stream_udp(&cfg).unwrap()),
|
||||
"device" => return Box::new(build_input_stream_device(&cfg).unwrap()),
|
||||
_ => {},
|
||||
};
|
||||
assert!(false);
|
||||
Box::new(InputEngineStdin{})
|
||||
Box::new(InputStreamStdin{})
|
||||
}
|
||||
|
||||
pub struct InputEngineDevice {
|
||||
pub struct InputStreamDevice {
|
||||
}
|
||||
|
||||
pub fn build_input_engine_device(cfg: &Engine) -> Result<InputEngineDevice, String> {
|
||||
return build_input_engine_device_gilrs(cfg)
|
||||
pub fn build_input_stream_device(cfg: &Stream) -> Result<InputStreamDevice, String> {
|
||||
return build_input_stream_device_gilrs(cfg)
|
||||
}
|
||||
|
||||
pub fn build_input_engine_device_gilrs(cfg: &Engine) -> Result<InputEngineDevice, String> {
|
||||
let _device_cfg = cfg.device.as_ref().unwrap();
|
||||
pub fn build_input_stream_device_gilrs(cfg: &Stream) -> Result<InputStreamDevice, String> {
|
||||
let _device_cfg = cfg.engine.device.as_ref().unwrap();
|
||||
|
||||
let mut gilrs = Gilrs::new().unwrap();
|
||||
|
||||
|
|
@ -65,8 +65,8 @@ pub fn build_input_engine_device_gilrs(cfg: &Engine) -> Result<InputEngineDevice
|
|||
return Err("do what".to_string());
|
||||
}
|
||||
|
||||
pub fn build_input_engine_device_hidapi(cfg: &Engine) -> Result<InputEngineDevice, String> {
|
||||
let _device_cfg = cfg.device.as_ref().unwrap();
|
||||
pub fn build_input_stream_device_hidapi(cfg: &Stream) -> Result<InputStreamDevice, String> {
|
||||
let _device_cfg = cfg.engine.device.as_ref().unwrap();
|
||||
|
||||
match HidApi::new() {
|
||||
Ok(api) => {
|
||||
|
|
@ -81,8 +81,8 @@ pub fn build_input_engine_device_hidapi(cfg: &Engine) -> Result<InputEngineDevic
|
|||
return Err("do what".to_string());
|
||||
}
|
||||
|
||||
pub fn build_input_engine_device_rusb(cfg: &Engine) -> Result<InputEngineDevice, String> {
|
||||
let _device_cfg = cfg.device.as_ref().unwrap();
|
||||
pub fn build_input_stream_device_rusb(cfg: &Stream) -> Result<InputStreamDevice, String> {
|
||||
let _device_cfg = cfg.engine.device.as_ref().unwrap();
|
||||
|
||||
assert!(rusb::has_capability());
|
||||
|
||||
|
|
@ -100,26 +100,26 @@ pub fn build_input_engine_device_rusb(cfg: &Engine) -> Result<InputEngineDevice,
|
|||
return Err("do what".to_string());
|
||||
}
|
||||
|
||||
impl InputEngine for InputEngineDevice {
|
||||
impl InputStream for InputStreamDevice {
|
||||
fn get(&mut self) -> Vec<char> {
|
||||
Err("end".to_string()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct InputEngineUDP {
|
||||
pub struct InputStreamUDP {
|
||||
last_socket: Option<std::net::UdpSocket>,
|
||||
port: i32,
|
||||
}
|
||||
|
||||
pub fn build_input_engine_udp(cfg: &Engine) -> Result<InputEngineUDP, String> {
|
||||
let udp_cfg = cfg.udp.as_ref().unwrap();
|
||||
return Ok(InputEngineUDP{
|
||||
pub fn build_input_stream_udp(cfg: &Stream) -> Result<InputStreamUDP, String> {
|
||||
let udp_cfg = cfg.engine.udp.as_ref().unwrap();
|
||||
return Ok(InputStreamUDP{
|
||||
last_socket: None,
|
||||
port: udp_cfg.port,
|
||||
});
|
||||
}
|
||||
|
||||
impl InputEngine for InputEngineUDP {
|
||||
impl InputStream for InputStreamUDP {
|
||||
fn get(&mut self) -> Vec<char> {
|
||||
let addr = "0.0.0.0:".to_string() + &self.port.to_string();
|
||||
println!("$ echo -n 'hello world' | nc -4u -w0 localhost {}", &self.port.to_string());
|
||||
|
|
@ -130,7 +130,7 @@ impl InputEngine for InputEngineUDP {
|
|||
let mut buf = [0; 128];
|
||||
let result = self.last_socket.as_ref().unwrap().recv_from(&mut buf);
|
||||
if result.is_err() {
|
||||
println!("InputEngineUDP: failed to recv: {:?}", result.err());
|
||||
println!("InputStreamUDP: failed to recv: {:?}", result.err());
|
||||
self.last_socket = None;
|
||||
return Vec::<char>::new();
|
||||
}
|
||||
|
|
@ -140,23 +140,23 @@ impl InputEngine for InputEngineUDP {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct InputEngineKafka {
|
||||
pub struct InputStreamKafka {
|
||||
}
|
||||
|
||||
pub fn build_input_engine_kafka(cfg: &Engine) -> Result<InputEngineKafka, String> {
|
||||
let _kafka_cfg = cfg.kafka.as_ref().unwrap();
|
||||
pub fn build_input_stream_kafka(cfg: &Stream) -> Result<InputStreamKafka, String> {
|
||||
let _kafka_cfg = cfg.engine.kafka.as_ref().unwrap();
|
||||
return Err("do what".to_string());
|
||||
}
|
||||
|
||||
impl InputEngine for InputEngineKafka {
|
||||
impl InputStream for InputStreamKafka {
|
||||
fn get(&mut self) -> Vec<char> {
|
||||
Err("end".to_string()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct InputEngineStdin {}
|
||||
pub struct InputStreamStdin {}
|
||||
|
||||
impl InputEngine for InputEngineStdin {
|
||||
impl InputStream for InputStreamStdin {
|
||||
fn get(&mut self) -> Vec<char> {
|
||||
let stdin = std::io::stdin();
|
||||
let mut result = String::new();
|
||||
|
|
@ -169,45 +169,45 @@ impl InputEngine for InputEngineStdin {
|
|||
mod test_input {
|
||||
use super::*;
|
||||
|
||||
struct InputEngineTest {}
|
||||
impl InputEngine for InputEngineTest {
|
||||
struct InputStreamTest {}
|
||||
impl InputStream for InputStreamTest {
|
||||
fn get(&mut self) -> Vec<char> {
|
||||
return "hello world".chars().collect();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_input_engine_impl() {
|
||||
let mut input_engine_test = InputEngineTest{};
|
||||
_test_input_engine_impl(&mut input_engine_test);
|
||||
fn test_input_stream_impl() {
|
||||
let mut input_stream_test = InputStreamTest{};
|
||||
_test_input_stream_impl(&mut input_stream_test);
|
||||
}
|
||||
|
||||
fn _test_input_engine_impl(engine: &mut dyn InputEngine) {
|
||||
fn _test_input_stream_impl(engine: &mut dyn InputStream) {
|
||||
assert_eq!("hello world".to_string(), engine.get().iter().cloned().collect::<String>());
|
||||
}
|
||||
}
|
||||
|
||||
pub trait OutputEngine {
|
||||
pub trait OutputStream {
|
||||
fn put(&mut self, v: Vec<char>);
|
||||
}
|
||||
|
||||
pub fn build_output_engine(cfg: &Engine) -> Box<dyn OutputEngine> {
|
||||
return _build_output_engine(cfg)
|
||||
pub fn build_output_stream(cfg: &Stream) -> Box<dyn OutputStream> {
|
||||
return _build_output_stream(cfg)
|
||||
}
|
||||
|
||||
pub fn _build_output_engine(cfg: &Engine) -> Box<dyn OutputEngine> {
|
||||
if cfg.name.as_str() == "stdout" {
|
||||
return Box::new(OutputEngineStdout{});
|
||||
} else if cfg.name.as_str() == "udp" {
|
||||
return Box::new(build_output_engine_udp(&cfg).unwrap());
|
||||
pub fn _build_output_stream(cfg: &Stream) -> Box<dyn OutputStream> {
|
||||
if cfg.engine.name.as_str() == "stdout" {
|
||||
return Box::new(OutputStreamStdout{});
|
||||
} else if cfg.engine.name.as_str() == "udp" {
|
||||
return Box::new(build_output_stream_udp(&cfg).unwrap());
|
||||
}
|
||||
assert!(false);
|
||||
Box::new(OutputEngineStdout{})
|
||||
Box::new(OutputStreamStdout{})
|
||||
}
|
||||
|
||||
pub struct OutputEngineStdout {}
|
||||
pub struct OutputStreamStdout {}
|
||||
|
||||
impl OutputEngine for OutputEngineStdout {
|
||||
impl OutputStream for OutputStreamStdout {
|
||||
fn put(&mut self, v: Vec<char>) {
|
||||
println!("{}", v.iter().cloned().collect::<String>());
|
||||
}
|
||||
|
|
@ -217,50 +217,50 @@ impl OutputEngine for OutputEngineStdout {
|
|||
mod test_output {
|
||||
use super::*;
|
||||
|
||||
struct OutputEngineTest {}
|
||||
impl OutputEngine for OutputEngineTest {
|
||||
struct OutputStreamTest {}
|
||||
impl OutputStream for OutputStreamTest {
|
||||
fn put(&mut self, _v: Vec<char>) {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_output_engine_impl() {
|
||||
let mut output_engine_test = OutputEngineTest{};
|
||||
_test_output_engine_impl(&mut output_engine_test);
|
||||
fn test_output_stream_impl() {
|
||||
let mut output_stream_test = OutputStreamTest{};
|
||||
_test_output_stream_impl(&mut output_stream_test);
|
||||
}
|
||||
|
||||
fn _test_output_engine_impl(engine: &mut dyn OutputEngine) {
|
||||
fn _test_output_stream_impl(engine: &mut dyn OutputStream) {
|
||||
engine.put("teehee".to_string().chars().collect());
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OutputEngineUDP {
|
||||
pub struct OutputStreamUDP {
|
||||
last_socket: Option<std::net::UdpSocket>,
|
||||
host: String,
|
||||
port: i32,
|
||||
}
|
||||
|
||||
pub fn build_output_engine_udp(cfg: &Engine) -> Result<OutputEngineUDP, String> {
|
||||
let udp_cfg = cfg.udp.as_ref().unwrap();
|
||||
return Ok(OutputEngineUDP{
|
||||
pub fn build_output_stream_udp(cfg: &Stream) -> Result<OutputStreamUDP, String> {
|
||||
let udp_cfg = cfg.engine.udp.as_ref().unwrap();
|
||||
return Ok(OutputStreamUDP{
|
||||
last_socket: None,
|
||||
host: udp_cfg.host.clone().unwrap(),
|
||||
port: udp_cfg.port,
|
||||
});
|
||||
}
|
||||
|
||||
impl OutputEngine for OutputEngineUDP {
|
||||
impl OutputStream for OutputStreamUDP {
|
||||
fn put(&mut self, v: Vec<char>) {
|
||||
if self.last_socket.is_none() {
|
||||
let result = std::net::UdpSocket::bind("127.0.0.1:".to_string() + &(self.port+10).to_string());
|
||||
if result.is_err() {
|
||||
println!("OutputEngineUDP: failed to bind to 127.0.0.1:{}: {:?}", &(self.port+10).to_string(), result.err());
|
||||
println!("OutputStreamUDP: failed to bind to 127.0.0.1:{}: {:?}", &(self.port+10).to_string(), result.err());
|
||||
return;
|
||||
}
|
||||
self.last_socket = Some(result.unwrap());
|
||||
let result = self.last_socket.as_ref().unwrap().connect(self.host.to_string() + ":" + &self.port.to_string());
|
||||
if result.is_err() {
|
||||
println!("OutputEngineUDP: failed to connect to {}:{}: {:?}", self.host.to_string(), self.port.to_string(), result.err());
|
||||
println!("OutputStreamUDP: failed to connect to {}:{}: {:?}", self.host.to_string(), self.port.to_string(), result.err());
|
||||
self.last_socket = None;
|
||||
return;
|
||||
}
|
||||
|
|
@ -268,7 +268,7 @@ impl OutputEngine for OutputEngineUDP {
|
|||
|
||||
let result = self.last_socket.as_ref().unwrap().send(&v.iter().cloned().collect::<String>().as_bytes());
|
||||
if result.is_err() {
|
||||
println!("OutputEngineUDP: failed to send: {:?}", result.err());
|
||||
println!("OutputStreamUDP: failed to send: {:?}", result.err());
|
||||
self.last_socket = None;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue