summaryrefslogtreecommitdiffstats
path: root/src/bin/client/repl.rs
blob: d38c54d137b0a0610798da9d0a8d92843f84b19d (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::io::{BufRead, BufReader, Write, stdin, stdout};
use std::net::TcpStream;
use rusqlite::Connection;

use crate::connection;
use crate::database;

pub struct ClientState {
    pub stream: Option<TcpStream>,
    pub conn: Connection,
    pub addr: Option<String>,
    pub default_port: u16,
}

impl ClientState {
    pub fn new() -> Self {
        let conn = database::init_db();
        let default_ip = "127.0.0.1".to_string();
        let default_port = 11525;
        let last_host = database::get_last_host(&conn);
        let default_host = format!("{}:{}", default_ip, default_port);
        let addr = last_host.or(Some(default_host));

        Self {
            stream: None,
            conn,
            addr,
            default_port,
        }
    }

    pub fn connect_to(&mut self, addr: &str) {
        self.addr = Some(addr.to_string());
        match connection::connect(addr) {
            Ok(s) => {
                self.stream = Some(s);
                database::save_host(&self.conn, addr);
            }
            Err(e) => println!("Failed to connect: {}", e),
        }
    }

    pub fn run(&mut self) {
        loop {
            print!("> ");
            stdout().flush().unwrap();
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            let parts: Vec<&str> = line.splitn(2, ' ').collect();
            match parts[0] {
                "connect" => {
                    let addr_str = if parts.len() < 2 {
                        if let Some(h) = &self.addr {
                            h.clone()
                        } else {
                            println!("No host specified and no previous connection. Usage: connect <ip>:<port>");
                            continue;
                        }
                    } else {
                        let mut a = parts[1].to_string();
                        if !a.contains(':') {
                            a = format!("{}:{}", a, self.default_port);
                        }
                        a
                    };
                    self.connect_to(&addr_str);
                }
                "send" => {
                    if self.stream.is_none() {
                        println!("Not connected. Use 'connect <ip>:<port>' first.");
                        continue;
                    }
                    if parts.len() < 2 {
                        println!("Usage: send <message>");
                        continue;
                    }
                    let msg = parts[1];
                    let s = self.stream.as_mut().unwrap();
                    let writer_stream = s.try_clone().unwrap();
                    let mut writer = writer_stream;
                    let response_reader_stream = s.try_clone().unwrap();
                    let mut response_reader = BufReader::new(response_reader_stream);
                    writeln!(writer, "{}", msg).unwrap();
                    let mut response = String::new();
                    response_reader.read_line(&mut response).unwrap();
                    println!("Echo: {}", response.trim());
                }
                "disconnect" => {
                    self.stream = None;
                    println!("Disconnected");
                }
                "help" => {
                    help();
                }
                "quit" => break,
                _ => help(),
            }
        }
    }
}

fn help() {
    println!("Available commands: connect <ip>:<port>, send <message>, disconnect, help, quit");
}