add server menu, work on nick reg
This commit is contained in:
parent
52b6422e1a
commit
7e4a6578dc
@ -1,5 +1,6 @@
|
|||||||
use prompted::input;
|
use prompted::input;
|
||||||
use std::io::{self, Read, Write};
|
use rust_irc::codes::client::*;
|
||||||
|
use std::io::{ Read, Write};
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
@ -23,21 +24,29 @@ fn read_messages(mut stream: TcpStream) {
|
|||||||
|
|
||||||
fn process_message(message: &[u8]) {
|
fn process_message(message: &[u8]) {
|
||||||
if let Ok(text) = String::from_utf8(message.to_vec()) {
|
if let Ok(text) = String::from_utf8(message.to_vec()) {
|
||||||
println!("{}", text);
|
println!("from serv: {}", text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
println!("Starting the IRC client");
|
println!("Starting the IRC client");
|
||||||
// let nick: String = input!("Enter your nickname: ");
|
let nick: String = input!("Enter your nickname: ");
|
||||||
let nick: String = "testy".to_string();
|
|
||||||
// let host: String = input!("Enter the server host: ");
|
// let host: String = input!("Enter the server host: ");
|
||||||
let host: &str = "localhost";
|
let host: &str = "localhost";
|
||||||
|
|
||||||
if let Ok(mut stream) = TcpStream::connect(host.to_owned() + ":6667") {
|
if let Ok(mut stream) = TcpStream::connect(host.to_owned() + ":6667") {
|
||||||
println!("Connected to {}", host);
|
println!("Connected to {}", host);
|
||||||
|
|
||||||
let stream_clone: TcpStream = stream.try_clone().expect("Faile to clone stream");
|
//try to register the nickname
|
||||||
|
let mut buf: Vec<u8> = vec![0; nick.capacity()];
|
||||||
|
buf[0] = REGISTER_NICK;
|
||||||
|
for i in 1..nick.len()+1 {
|
||||||
|
buf[i] = *nick.as_bytes().get(i - 1).unwrap();
|
||||||
|
}
|
||||||
|
stream.write(&buf);
|
||||||
|
|
||||||
|
//another stream for reading messages
|
||||||
|
let stream_clone: TcpStream = stream.try_clone().expect("Failed to clone stream");
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
read_messages(stream_clone);
|
read_messages(stream_clone);
|
||||||
});
|
});
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
pub mod codes {
|
pub mod codes {
|
||||||
|
pub const END: u8 = 0x00;
|
||||||
pub mod client {
|
pub mod client {
|
||||||
pub const JOIN_ROOM: u8 = 0x01;
|
pub const JOIN_ROOM: u8 = 0x01;
|
||||||
pub const JOIN_SERVER: u8 = 0x02;
|
pub const JOIN_SERVER: u8 = 0x02;
|
||||||
pub const LEAVE_ROOM: u8 = 0x03;
|
pub const LEAVE_ROOM: u8 = 0x03;
|
||||||
pub const LIST_ROOMS: u8 = 0x04;
|
pub const LIST_ROOMS: u8 = 0x04;
|
||||||
pub const SEND_MESSAGE: u8 = 0x05;
|
pub const SEND_MESSAGE: u8 = 0x05;
|
||||||
|
pub const REGISTER_NICK: u8 = 0x06;
|
||||||
}
|
}
|
||||||
pub const KEEP_ALIVE: u8 = 0x0C;
|
pub const KEEP_ALIVE: u8 = 0x0C;
|
||||||
pub const RESPONSE: u8 = 0x0D;
|
pub const RESPONSE: u8 = 0x0D;
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use std::sync::{Arc, Mutex};
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
io::{Read, Write},
|
io::{Read, Write},
|
||||||
@ -5,6 +6,7 @@ use std::{
|
|||||||
thread,
|
thread,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use prompted::input;
|
||||||
use rust_irc::codes;
|
use rust_irc::codes;
|
||||||
|
|
||||||
const SERVER_ADDRESS: &str = "0.0.0.0:6667";
|
const SERVER_ADDRESS: &str = "0.0.0.0:6667";
|
||||||
@ -50,30 +52,46 @@ impl Server {
|
|||||||
stream.write_all(&[codes::RESPONSE_OK]);
|
stream.write_all(&[codes::RESPONSE_OK]);
|
||||||
|
|
||||||
// handle user commands
|
// handle user commands
|
||||||
loop {
|
|
||||||
let mut cmd_buf = [0; 2];
|
|
||||||
println!("yo, {:?}", self.users);
|
|
||||||
// todo!();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
let listener: TcpListener = TcpListener::bind(SERVER_ADDRESS).expect("Failed to bind to port");
|
let listener: TcpListener = TcpListener::bind(SERVER_ADDRESS).expect("Failed to bind to port");
|
||||||
let server: Server = Server::new();
|
let server = Server::new();
|
||||||
|
let server_mutx = Arc::new(Mutex::new(server));
|
||||||
|
let io_thread = Arc::clone(&server_mutx);
|
||||||
|
|
||||||
println!("Server listening on {}", SERVER_ADDRESS);
|
println!("Server listening on {}", SERVER_ADDRESS);
|
||||||
|
|
||||||
|
thread::spawn(move || loop {
|
||||||
|
println!("0: Quit Server");
|
||||||
|
println!("1: list connected users");
|
||||||
|
println!("2: list rooms");
|
||||||
|
let inp: String = input!(":");
|
||||||
|
let local_server = io_thread.lock().unwrap();
|
||||||
|
match inp.parse::<u8>() {
|
||||||
|
Ok(num) => match num {
|
||||||
|
0 => break,
|
||||||
|
1 => println!("Users: {:?}", local_server.users),
|
||||||
|
2 => println!("Rooms: {:?}", local_server.rooms),
|
||||||
|
_ => println!("Invalid Input"),
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
println!("Invalid input");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
match stream {
|
match stream {
|
||||||
Ok(stream) => {
|
Ok(mut stream) => {
|
||||||
if server.users.len() < MAX_USERS {
|
let mut cmd_buf: [i32; 2] = [0; 2];
|
||||||
let mut server_clone = server.clone();
|
let mut local_server = server_mutx.lock().unwrap();
|
||||||
thread::spawn(move || {
|
|
||||||
server_clone.handle_client(stream);
|
if local_server.users.len() < MAX_USERS {
|
||||||
});
|
local_server.handle_client(stream);
|
||||||
} else {
|
} else {
|
||||||
// stream.write_all(&[codes::ERROR, codes::error::SERVER_FULL]);
|
let _ = stream.write_all(&[codes::ERROR, codes::error::SERVER_FULL]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
Reference in New Issue
Block a user