start crating wrap over fcp api

This commit is contained in:
Horhik 2021-01-05 15:58:18 +02:00
parent 7d56d094f1
commit f29a308209
4 changed files with 73 additions and 1 deletions

0
src/fcp/basic.rs Normal file
View file

2
src/fcp/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod basic;
pub mod types;

68
src/fcp/types.rs Normal file
View file

@ -0,0 +1,68 @@
use std::error::Error;
use std::io;
use std::net::{IpAddr, SocketAddr, TcpListener};
use tokio::net::TcpStream;
use tokio::prelude::*;
struct Fcp {
connected: bool,
stream: TcpStream,
addr: SocketAddr,
name: str,
}
trait FcpConnection {
fn new(addr: str, port: str, name: str) -> Result<Self, Box<dyn Error>>;
}
// TODO add error if freenet not connected
impl Fcp for FcpConnection {
fn new(addr: str, port: str, name: str) -> Result<Self, Box<dyn Error>> {
let socket = SocketAddr::new(IpAddr::V4(addr.parse().unwrap()), port);
match TcpStream::connect(&socket) {
Ok(mut stream) => {
let _ = stream
.write(
(format!(
"ClientHello\nName={}\nExpectedVersion=2.0\nEndMessage\n\n",
name
),)
.as_bytes(),
)
.await?;
let mut buffer = [0; 1024];
stream.read(&mut buffer).await?;
let responce = String::from_utf8_lossy(&buffer[..]);
println!("response");
Ok(stream)
}
Error(e) => {
println!("error: {}", e);
Error::new(ErrorKind::Other, e)
}
}
}
}
/*
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Connect to a peer
let mut stream = TcpStream::connect("127.0.0.1:9481").await?;
let _ = stream
.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes())
.await?;
println!("Reading response...");
let mut buffer = [0; 1024];
stream.read(&mut buffer).await?;
let received = String::from_utf8_lossy(&buffer[..]);
print!("<<< {}", received);
let _ = stream.write(
("ClientHello\n
Name=ggg\n
ExpectedVersion=2.0\n
EndMessage\n\n")
.as_bytes(),
);
}
*/

View file

@ -2,6 +2,7 @@ mod chat;
mod db; mod db;
mod encrypting; mod encrypting;
mod fcp;
use async_std::io; use async_std::io;
use chat::front_conn::listen_client; use chat::front_conn::listen_client;
use chat::types::PackedMessage; use chat::types::PackedMessage;
@ -14,7 +15,6 @@ use std::{
thread, thread,
}; };
/* /*
+-----------------------------------------------------------------------------------+ +-----------------------------------------------------------------------------------+
| Client | | Client |
| | | |
@ -41,6 +41,7 @@ use std::{
| | | |
+-----------------------------------------------------------------------------------+ +-----------------------------------------------------------------------------------+
*/ */
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
let (server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) = let (server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel(); mpsc::channel();
@ -63,6 +64,7 @@ fn main() -> io::Result<()> {
client_thread.join(); client_thread.join();
Ok(()) Ok(())
} }
/* /*
fn main() { fn main() {
let server = TcpListener::bind("127.0.0.1:9001").unwrap(); let server = TcpListener::bind("127.0.0.1:9001").unwrap();