1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::{utils::println, User};
use std::{env, net};
pub enum Command {
Connect(String),
Open,
}
pub enum InputError {
InvalidCmd(String),
NoCommand,
}
impl InputError {
pub fn to_string(&self) -> String {
match self {
InputError::InvalidCmd(msg) => msg.to_string(),
InputError::NoCommand => "No command given".to_string(),
}
}
}
pub fn parse_command(input: &mut env::Args) -> Result<Command, InputError> {
input.next();
match input.next().as_deref() {
Some("open") => Ok(Command::Open),
Some("connect") => {
let host = input.next().ok_or(InputError::InvalidCmd(
"connect requires IP address".to_string(),
))?;
Ok(Command::Connect(host))
}
Some(cmd) => Err(InputError::InvalidCmd(format!("invalid command: {}", cmd))),
None => Err(InputError::NoCommand),
}
}
pub fn connect(addr: &str) {
let stream = net::TcpStream::connect(addr).expect("Failed to connect to server");
let user = User::new_get_name(stream);
user.start_session();
}
pub fn open() {
let url = format!("{}:{}", "0.0.0.0", 8080);
let socket = net::TcpListener::bind(url).unwrap();
println("Waiting for connection...");
let (stream, addr) = socket.accept().unwrap();
println(format!("Connected to {}", addr).as_str());
let user = User::new_get_name(stream);
user.start_session()
}