create FcpConnection trait

This commit is contained in:
Horhik 2021-01-05 19:40:00 +02:00
parent f29a308209
commit 18b666b604
3 changed files with 85 additions and 19 deletions

12
Cargo.lock generated
View file

@ -131,6 +131,17 @@ dependencies = [
"webpki-roots", "webpki-roots",
] ]
[[package]]
name = "async-trait"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d3a45e77e34375a7923b1e8febb049bb011f064714a8e17a1a616fef01da13d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "async-tungstenite" name = "async-tungstenite"
version = "0.8.0" version = "0.8.0"
@ -512,6 +523,7 @@ version = "0.0.1"
dependencies = [ dependencies = [
"async-std", "async-std",
"async-tls 0.10.0", "async-tls 0.10.0",
"async-trait",
"async-tungstenite", "async-tungstenite",
"futures", "futures",
"futures-util", "futures-util",

View file

@ -20,6 +20,7 @@ futures-util = "*"
serde_json = "1.0.59" serde_json = "1.0.59"
serde = "1.0.116" serde = "1.0.116"
serde_derive = "1.0.116" serde_derive = "1.0.116"
async-trait = "0.1.42"
[dependencies.async-tungstenite] [dependencies.async-tungstenite]
version = "0.8.0" version = "0.8.0"

View file

@ -1,47 +1,100 @@
use async_trait::async_trait;
use std::error::Error; use std::error::Error;
use std::io; use std::io;
use std::io::prelude::*;
use std::net::TcpStream;
use std::net::{IpAddr, SocketAddr, TcpListener}; use std::net::{IpAddr, SocketAddr, TcpListener};
use tokio::net::TcpStream;
use tokio::prelude::*;
struct Fcp { struct Fcp {
connected: bool, connected: bool,
stream: TcpStream, stream: TcpStream,
addr: SocketAddr, addr: SocketAddr,
name: str, name: String,
} }
struct NodeHello {
responce: String,
}
trait FcpConnection { trait FcpConnection {
fn new(addr: str, port: str, name: str) -> Result<Self, Box<dyn Error>>; fn new(addr: &'static str, port: u16, name: String) -> Result<Box<Self>, Box<dyn Error>>;
} }
impl FcpConnection for Fcp {
fn new(addr: &'static str, port: u16, name: String) -> Result<Box<Self>, Box<dyn Error>> {
let socket = SocketAddr::new(IpAddr::V4(addr.parse().unwrap()), port);
match TcpStream::connect(&socket) {
Ok(mut stream) => Ok(Box::new(Fcp {
connected: true,
stream: stream,
addr: socket,
name: name,
})),
Err(e) => Err(Box::new(e)),
}
}
}
/*
use futures::try_join;
use tokio::io::{self, AsyncBufRead, AsyncReadExt};
use tokio::net::TcpStream;
#[async_trait]
trait FCP {
async fn connect(&self) -> io::Result<()>;
}
#[async_trait]
impl FCP for Fcp {
async fn connect(&self) -> io::Result<()> {
let stream = self.stream;
let _ =
stream.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes());
let mut buffer = String::new();
let smth = stream.read_to_string(&mut buffer).await?;
println!("{:?}", smth);
Ok(())
// }
}
}
*/
/*
// TODO add error if freenet not connected // TODO add error if freenet not connected
impl Fcp for FcpConnection { impl FcpConnection for Fcp {
fn new(addr: str, port: str, name: str) -> Result<Self, Box<dyn Error>> { fn new(addr: &'static str, port: u16, name: &'static str) -> Result<&'static Self> {
let socket = SocketAddr::new(IpAddr::V4(addr.parse().unwrap()), port); let socket = SocketAddr::new(IpAddr::V4(addr.parse().unwrap()), port);
match TcpStream::connect(&socket) { match TcpStream::connect(&socket) {
Ok(mut stream) => { Ok(&mut stream) => {
let _ = stream let _ = stream.write(
.write( (format!(
(format!( "ClientHello\nName={}\nExpectedVersion=2.0\nEndMessage\n\n",
"ClientHello\nName={}\nExpectedVersion=2.0\nEndMessage\n\n", name
name ),)
),) .as_bytes(),
.as_bytes(), )?;
) /*
.await?; * -- Getting Responce -- *
let mut buffer = [0; 1024]; let mut buffer = [0; 1024];
stream.read(&mut buffer).await?; stream.read(&mut buffer).await?;
let responce = String::from_utf8_lossy(&buffer[..]); let responce = String::from_utf8_lossy(&buffer[..]);
println!("response"); println!("response");
Ok(stream) */
Ok(Fcp {
connected: true,
stream: stream,
addr: socket,
name: name,
})
} }
Error(e) => { Err(e) => {
println!("error: {}", e); println!("error: {}", e);
Error::new(ErrorKind::Other, e) Error::new(io::ErrorKind::Other, e)
} }
} }
} }
} }
*/
/* /*
#[tokio::main] #[tokio::main]