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",
]
[[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]]
name = "async-tungstenite"
version = "0.8.0"
@ -512,6 +523,7 @@ version = "0.0.1"
dependencies = [
"async-std",
"async-tls 0.10.0",
"async-trait",
"async-tungstenite",
"futures",
"futures-util",

View file

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

View file

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