Finished the abstraction of the configuration socket

This commit is contained in:
Guillermo Roche 2022-12-21 23:32:56 +01:00
parent e0a9fe3678
commit 84b39aa2a7
2 changed files with 19 additions and 22 deletions

View File

@ -1,12 +1,16 @@
use std::net::TcpListener; use std::net::TcpListener;
use std::net::TcpStream;
use std::os::unix::net::UnixListener; use std::os::unix::net::UnixListener;
use std::io::Error; use std::os::unix::net::UnixStream;
use std::io::{Error, ErrorKind};
use std::io::prelude::*; use std::io::prelude::*;
pub const TCP_LIS : u8 = 0; pub const TCP_LIS : u8 = 0;
pub const UNIX_LIS : u8 = 1; pub const UNIX_LIS : u8 = 1;
trait NewTrait: std::io::Read + std::io::Write {} pub trait NewTrait: std::io::Read + std::io::Write + Send {}
impl NewTrait for TcpStream {}
impl NewTrait for UnixStream {}
pub struct GenericListener { pub struct GenericListener {
tcp_lis : Option<TcpListener>, tcp_lis : Option<TcpListener>,
@ -42,19 +46,14 @@ impl GenericListener {
/*pub fn incoming(&self) -> dyn Iterator<Item = Result<TcpStream, Error>> { pub fn accept(&self) -> Result<Box<dyn NewTrait>,Error> {
match self.type_lis { match self.type_lis {
TCP_LIS => self.tcp_lis.unwrap().incoming(), TCP_LIS => Ok(Box::new(self.tcp_lis.as_ref().unwrap().accept()?.0)),
UNIX_LIS => self.unix_lis.unwrap().incoming(), UNIX_LIS => Ok(Box::new(self.unix_lis.as_ref().unwrap().accept()?.0)),
_=> Err(Error::new(ErrorKind::Other, "Unexpected type"))
} }
}*/
/*pub fn accept(&self) -> Result<dyn NewTrait, Error> { }
match self.type_lis {
TCP_LIS => self.tcp_lis.unwrap().accept(),
UNIX_LIS => self.unix_lis.unwrap().accept(),
}
}*/
} }

View File

@ -1,5 +1,3 @@
use std::net::TcpListener;
use std::os::unix::net::UnixListener;
use crate::conf; use crate::conf;
use crate::server_conf::conexion::Conexion; use crate::server_conf::conexion::Conexion;
use crate::server_conf::listener::GenericListener; use crate::server_conf::listener::GenericListener;
@ -8,14 +6,14 @@ use std::sync::{Arc, RwLock};
pub struct ConfSer{ pub struct ConfSer{
path: String, path: String,
listener: UnixListener, listener: GenericListener,
} }
impl ConfSer { impl ConfSer {
fn new(path: String) -> ConfSer{ fn new(path: String) -> ConfSer{
ConfSer{ ConfSer{
path: path.clone(), path: path.clone(),
listener: UnixListener::bind(path).unwrap(), listener: GenericListener::bind(path, 1).unwrap(),
} }
} }
} }
@ -31,13 +29,13 @@ impl Drop for ConfSer {
pub fn start(conf: Arc<RwLock<conf::Config>>){ pub fn start(conf: Arc<RwLock<conf::Config>>){
let ser = ConfSer::new(String::from("mineproxy")); let ser = ConfSer::new(String::from("mineproxy"));
for stream in ser.listener.incoming() { loop{
match stream{ match ser.listener.accept() {
Ok(s) => { Ok(stream) => {
let c = conf.clone(); let c = conf.clone();
thread::spawn(|| Conexion::new(c, s).process_reques()); thread::spawn(|| Conexion::new(c, stream).process_reques());
}, }
Err(_e) => println!("{}",_e), Err(e) => println!("{}", e)
} }
} }