diff --git a/Cargo.lock b/Cargo.lock index ec9691b..21b36bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,14 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "local_ipaddress" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6a104730949fbc4c78e4fa98ed769ca0faa02e9818936b61032d2d77526afa9" + [[package]] name = "terminal_chat" version = "0.1.0" +dependencies = [ + "local_ipaddress", +] diff --git a/Cargo.toml b/Cargo.toml index 7df4366..0b15122 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +local_ipaddress = "0.1.3" diff --git a/src/lib.rs b/src/lib.rs index e69de29..dcc0c4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1,38 @@ +use std::net::{TcpStream, TcpListener, IpAddr, Ipv4Addr}; +use std::io::{self, Read}; +use std::sync::{Mutex, Arc}; +use crate::IpAddress::{Normal, WithPort}; + +pub enum IpAddress { + WithPort(String, u32), + Normal(String) +} + +pub fn connect(address: IpAddress) -> io::Result { + let address = match address { + Normal(s) => { + match &*s { + "default" => String::from("localhost:7979"), + _ => s + } + }, + WithPort(a, p) => format!("{}:{}", a, p) + }; + + TcpStream::connect(address) +} + +pub fn listen(port: usize) -> io::Result { + let listener = TcpListener::bind(format!("127.0.0.1:{}", port)).unwrap(); + println!("Listening on: {}:{}...", local_ipaddress::get().unwrap(), 7979); + + listener.incoming().next().unwrap() +} + +pub fn read_stream_print(mut stream: TcpStream) { + let mut data = [0 as u8; 50]; // using 50 byte buffer + + for _ in stream.read(&mut data) { + println!("{}", String::from_utf8_lossy(&data)); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 728d8ed..858b477 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,68 @@ -use std::io::{self, Write}; +use std::io::{self, Write, Read}; +use terminal_chat::{connect, listen, read_stream_print, IpAddress}; +use std::thread; +use std::sync::{Arc, Mutex}; + fn main() { - println!(" + println!(r" _______ _ _ _____ _ _ |__ __| (_) | |/ ____| | | | | | ___ _ __ _ __ ___ _ _ __ __ _| | | | |__ __ _| |_ - | |/ _ \\ '__| '_ ` _ \\| | '_ \\ / _` | | | | '_ \\ / _` | __| + | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | | | | '_ \ / _` | __| | | __/ | | | | | | | | | | | (_| | | |____| | | | (_| | |_ - |_|\\___|_| |_| |_| |_|_|_| |_|\\__,_|_|\\_____|_| |_|\\__,_|\\__| + |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_|\_____|_| |_|\__,_|\__| --------------------------------------------------------------- Version 0.1"); - let stdin = io::stdin(); + + println!("Do you want to listen(l) or connect(c) to a listener?"); + let mut did_listen; + let stream = + if input().contains("l") { + did_listen = true; + listen(7979) + } else { + did_listen = false; + let mut address = input().trim().to_string(); + println!("Trying to connect to '{}'", address.clone()); + + connect(IpAddress::Normal(address.clone())) + }; + + let stream = match stream { + Ok(x) => x, + Err(_) => { + println!("Error opening stream"); + return; + } + }; + + let mut stream2 = match did_listen { + true => connect(IpAddress::WithPort(String::from("127.0.0.1"), 7980)).unwrap(), + false => listen(7989).unwrap() + }; + + thread::spawn(move || { + read_stream_print(stream); + }); + + println!("Connected."); + + let connect_msg = "Hello World!"; + stream2.write(connect_msg.as_bytes()).unwrap(); loop { - print!("> "); - io::stdout().flush(); - let mut buffer = String::new(); - - stdin.read_line(&mut buffer); - - println!("{}", buffer); + let input = input(); + stream2.write(input.clone().as_bytes()); + println!("-- Trying to send {}", input); } } + + +fn input() -> String { + print!("> "); + io::stdout().flush().unwrap(); + let mut buffer = String::new(); + let _ = io::stdin().read_line(&mut buffer); + buffer +} \ No newline at end of file