Revert "Merge branch 'master' into ref-method"
This reverts commit1a6b344b58, reversing changes made tobe167055e8.
This commit is contained in:
parent
1a6b344b58
commit
dbf49a65b2
|
|
@ -25,7 +25,6 @@ rust-bert = "0.7.11"
|
|||
log = "0.4"
|
||||
|
||||
|
||||
|
||||
[profile.dev]
|
||||
opt-level = 0
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,10 @@
|
|||
use std::{
|
||||
net::TcpStream,
|
||||
io::{Write, Read}
|
||||
};
|
||||
use std::{net::TcpStream, io::{Write, Read}, io};
|
||||
use crate::{
|
||||
server::client::client_profile::Client,
|
||||
commands::Commands,
|
||||
};
|
||||
use zeroize::Zeroize;
|
||||
use std::time::Duration;
|
||||
use async_std::net::SocketAddrV4;
|
||||
use std::str::FromStr;
|
||||
use std::net::SocketAddr;
|
||||
use zeroize::Zeroize;
|
||||
|
||||
|
||||
pub struct ClientApi {
|
||||
|
|
@ -32,32 +26,42 @@ impl ClientApi {
|
|||
addr: addr.to_string(),
|
||||
on_client_add_handle: on_add,
|
||||
on_client_remove_handle: on_remove,
|
||||
}
|
||||
};
|
||||
Ok(a)
|
||||
}
|
||||
|
||||
pub fn set_on_client_add(&mut self, Fn: fn(Client) -> ()) {
|
||||
self.on_client_add_handle = Fn;
|
||||
pub fn set_on_client_add(&mut self, func: fn(Client) -> ()) {
|
||||
self.on_client_add_handle = func;
|
||||
}
|
||||
|
||||
pub fn set_on_client_removed(&mut self, Fn: fn(String) -> ()) {
|
||||
self.on_client_remove_handle = Fn;
|
||||
pub fn set_on_client_removed(&mut self, func: fn(String) -> ()) {
|
||||
self.on_client_remove_handle = func;
|
||||
}
|
||||
|
||||
pub fn get_info(host: &str) -> Option<Commands> {
|
||||
pub fn get_info(host: &str) -> Result<Commands, io::Error> {
|
||||
let mut buffer: [u8; 1024] = [0; 1024];
|
||||
let addr = SocketAddr::from_str(host).ok()?;
|
||||
let mut stream = TcpStream::connect_timeout(&addr, Duration::from_millis(500)).ok()?;
|
||||
let addr = host.parse().unwrap();
|
||||
let mut stream = TcpStream::connect_timeout(&addr, Duration::from_millis(1000))?;
|
||||
|
||||
stream.read(&mut buffer).ok()?;
|
||||
|
||||
match Commands::from(&mut buffer) {
|
||||
let _ = stream.read(&mut buffer)?;
|
||||
println!("data recieved: {:?}", &buffer[0..20]);
|
||||
match Commands::from(&buffer) {
|
||||
Commands::Request(None) => {
|
||||
stream.write_all(Commands::Info(None).to_string().as_bytes()).unwrap();
|
||||
stream.read(&mut buffer).ok()?;
|
||||
Some(Commands::from(String::from(String::from_utf8_lossy(&buffer))))
|
||||
println!("zeroing");
|
||||
buffer.zeroize();
|
||||
println!("writing");
|
||||
let sending_command = Commands::Info(None).to_string();
|
||||
println!("sending string: {:?} as_bytes: {:?}", &sending_command, &sending_command.as_bytes());
|
||||
stream.write_all(sending_command.as_bytes())?;
|
||||
stream.flush()?;
|
||||
println!("reading");
|
||||
let bytes = stream.read(&mut buffer)?;
|
||||
println!("new buffer size: {:?} contents: {:?}", bytes, &buffer[0..20]);
|
||||
println!("commanding");
|
||||
Ok(Commands::from(String::from(String::from_utf8_lossy(&buffer))))
|
||||
},
|
||||
_ => {
|
||||
None
|
||||
Err(io::Error::new(io::ErrorKind::InvalidData, "the data was not expected"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ pub enum CommandParseError {
|
|||
UnknownCommand,
|
||||
NoString,
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for Commands {
|
||||
|
||||
|
|
@ -49,7 +48,6 @@ impl ToString for Commands {
|
|||
Commands::ClientInfo(arguments) => { ("!clientInfo:", arguments) },
|
||||
Commands::ClientRemove(arguments) => { ("!clientRemove", arguments) }
|
||||
Commands::Client(arguments) => { ("!client:", arguments) },
|
||||
Commands::Success(arguments) => { ("!success:", arguments) },
|
||||
Commands::Error(arguments) => { ("!error:", arguments) },
|
||||
_ => { ("!error:", &None) }
|
||||
};
|
||||
|
|
@ -153,7 +151,8 @@ mod test_commands_v2 {
|
|||
|
||||
#[test]
|
||||
fn test_creation_from_string() {
|
||||
let command_result = Commands::from("!connect: name:bop host:127.0.0.1 uuid:123456-1234-1234-123456");
|
||||
let command_result = Commands::from_str("!connect: name:bop host:127.0.0.1 uuid:123456-1234-1234-123456").expect("parse error");
|
||||
()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ enum Message {
|
|||
Terminate,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ThreadPool{
|
||||
workers: Vec<Worker>,
|
||||
sender: Sender<Message>,
|
||||
|
|
@ -51,7 +50,6 @@ impl ThreadPool{
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Worker {
|
||||
id: usize,
|
||||
thread: Option<thread::JoinHandle<()>>,
|
||||
|
|
|
|||
17
src/main.rs
17
src/main.rs
|
|
@ -21,22 +21,7 @@ use crossterm::ErrorKind;
|
|||
use log::info;
|
||||
use clap::{App, Arg};
|
||||
|
||||
|
||||
use crate::server::server_profile::Server;
|
||||
use client_api::ClientApi;
|
||||
use crossterm::ErrorKind;
|
||||
use cursive::{
|
||||
Cursive,
|
||||
menu::*,
|
||||
event::Key,
|
||||
views::{ Dialog, TextView, LinearLayout, ListView, ResizedView, Panel },
|
||||
Rect,
|
||||
CursiveExt,
|
||||
align::{Align, HAlign},
|
||||
view::SizeConstraint,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use log::info;
|
||||
|
||||
fn main() -> Result<(), ErrorKind> {
|
||||
|
||||
|
|
@ -174,4 +159,4 @@ mod tests {
|
|||
std::thread::sleep(std::time::Duration::from_secs(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,233 @@
|
|||
mod request;
|
||||
mod info;
|
||||
mod success;
|
||||
mod error;
|
||||
mod connect;
|
||||
mod disconnect;
|
||||
mod client_update;
|
||||
mod client_info;
|
||||
mod client;
|
||||
mod test;
|
||||
mod message;
|
||||
|
||||
use std::string::ToString;
|
||||
use std::collections::HashMap;
|
||||
use std::borrow::Borrow;
|
||||
use regex::Regex;
|
||||
use std::ops::Index;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
impl ClientCommands{
|
||||
pub fn execute(&self, client: &mut Client, server: &Server, buffer: &mut [u8; 1024], connected_clients: &Arc<Mutex<HashMap<String, Client>>>){
|
||||
let stream = client.get_stream();
|
||||
match &*self{
|
||||
ClientCommands::Info => {
|
||||
let server_details = server.get_info();
|
||||
|
||||
client.transmit_success(&server_details);
|
||||
},
|
||||
ClientCommands::Connect(data) => {
|
||||
connect::add_client(connected_clients, client);
|
||||
|
||||
let new_client = ServerCommands::Client(data.clone());
|
||||
server.update_all_clients(&new_client);
|
||||
|
||||
client.transmit_success(&String::from(""));
|
||||
},
|
||||
ClientCommands::Disconnect => {
|
||||
disconnect::remove_client(connected_clients, client);
|
||||
|
||||
let mut data: HashMap<String, String> = HashMap::new();
|
||||
data.insert("uuid".to_string(), client.get_uuid().to_string());
|
||||
|
||||
let old_client = ServerCommands::ClientRemove(data);
|
||||
server.update_all_clients(&old_client);
|
||||
|
||||
client.transmit_success(&String::from(""));
|
||||
client.disconnect();
|
||||
println!("disconnected!");
|
||||
},
|
||||
ClientCommands::ClientUpdate => {
|
||||
let clients_hashmap = connected_clients.lock().unwrap();
|
||||
for (key, value) in clients_hashmap.iter(){
|
||||
let formatted_data = client_update::format_client_data(&key, &value);
|
||||
client.transmit_data(&formatted_data);
|
||||
|
||||
client.confirm_success(buffer, &formatted_data);
|
||||
}
|
||||
client.transmit_success(&String::from(""));
|
||||
client.confirm_success(buffer, &String::from("!success:"));
|
||||
},
|
||||
ClientCommands::ClientInfo(data) => {
|
||||
let requested_data = client_info::get_client_data(connected_clients, &data);
|
||||
client.transmit_data(&requested_data);
|
||||
},
|
||||
ClientCommands::Unknown => {
|
||||
println!("Unknown Command");
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ServerCommands{
|
||||
pub fn execute(&self, client: &mut Client, buffer: &mut [u8; 1024]){
|
||||
match &*self{
|
||||
ServerCommands::Client(data) => {
|
||||
let mut message = String::from("");
|
||||
message.push_str(&"!client: name:");
|
||||
message.push_str(&data.get("name").unwrap());
|
||||
message.push_str(&" host:");
|
||||
message.push_str(&data.get("host").unwrap());
|
||||
message.push_str(&" uuid:");
|
||||
message.push_str(&data.get("uuid").unwrap());
|
||||
|
||||
client.transmit_data(&message);
|
||||
|
||||
client.confirm_success(buffer, &message);
|
||||
},
|
||||
ServerCommands::ClientRemove(data) => {
|
||||
let mut message = String::from("");
|
||||
message.push_str(&"!client: uuid:");
|
||||
message.push_str(&data.get("uuid").unwrap());
|
||||
|
||||
client.transmit_data(&message);
|
||||
|
||||
client.confirm_success(buffer, &message);
|
||||
},
|
||||
ServerCommands::Unknown => {
|
||||
println!("Unknown Command!");
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// MARK: - commands_v2 electric boogaloo
|
||||
#[derive(Clone)]
|
||||
pub enum Commands {
|
||||
Request(Option<HashMap<String, String>>),
|
||||
Info(Option<HashMap<String, String>>),
|
||||
|
||||
Connect(Option<HashMap<String, String>>),
|
||||
Disconnect(Option<HashMap<String, String>>),
|
||||
|
||||
ClientUpdate(Option<HashMap<String, String>>),
|
||||
ClientInfo(Option<HashMap<String, String>>),
|
||||
ClientRemove(Option<HashMap<String, String>>),
|
||||
Client(Option<HashMap<String, String>>),
|
||||
|
||||
Success(Option<HashMap<String, String>>),
|
||||
Error(Option<HashMap<String, String>>),
|
||||
}
|
||||
|
||||
impl ToString for Commands {
|
||||
|
||||
fn to_string(&self) -> std::string::String {
|
||||
let mut out_string = String::new();
|
||||
|
||||
let (command, parameters) = match self {
|
||||
Commands::Request(arguments) => { ("!request:", arguments) },
|
||||
Commands::Info(arguments) => { ("!info:", arguments) },
|
||||
Commands::Connect(arguments) => { ("!connect:", arguments) },
|
||||
Commands::Disconnect(arguments) => { ("!disconnect:", arguments) },
|
||||
Commands::ClientUpdate(arguments) => { ("!clientUpdate:", arguments) },
|
||||
Commands::ClientInfo(arguments) => { ("!clientInfo:", arguments) },
|
||||
Commands::Client(arguments) => { ("!client:", arguments) },
|
||||
Commands::Error(arguments) => { ("!error:", arguments) },
|
||||
_ => { ("!error:", &None) }
|
||||
};
|
||||
|
||||
out_string.push_str(command);
|
||||
|
||||
if parameters.is_some() {
|
||||
let hash_map = parameters.borrow().as_ref().unwrap();
|
||||
for (k, v) in hash_map.iter() {
|
||||
out_string.push_str(" ");
|
||||
out_string.push_str(k.as_str());
|
||||
out_string.push_str(":");
|
||||
out_string.push_str(v.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
out_string
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Commands {
|
||||
fn from(data: &str) -> Self {
|
||||
let regex = Regex::new(r###"(\?|!)([a-zA-z0-9]*):|([a-zA-z]*):([a-zA-Z0-9\-\+\[\]{}_=/]+|("(.*?)")+)"###).unwrap();
|
||||
let mut iter = regex.find_iter(data);
|
||||
let command = iter.next().unwrap().as_str();
|
||||
|
||||
println!("command: {:?}", command);
|
||||
|
||||
let mut map: HashMap<String, String> = HashMap::new();
|
||||
|
||||
for i in iter {
|
||||
let parameter = i.as_str().to_string();
|
||||
let parts:Vec<&str> = parameter.split(":").collect();
|
||||
|
||||
map.insert(parts.index(0).to_string(), parts.index(1).to_string());
|
||||
}
|
||||
|
||||
let params = if map.capacity() > 1 {Some(map)} else { None };
|
||||
|
||||
match command {
|
||||
"!request:" => Commands::Request(params),
|
||||
"!info:" => Commands::Info(params),
|
||||
|
||||
"!connect:" => Commands::Connect(params),
|
||||
"!disconnect:" => Commands::Disconnect(params),
|
||||
|
||||
"!clientUpdate:" => Commands::ClientUpdate(params),
|
||||
"!clientInfo:" => Commands::ClientInfo(params),
|
||||
"!client:" => Commands::Client(params),
|
||||
"!clientRemove:" => Commands::ClientRemove(params),
|
||||
|
||||
"!success:" => Commands::Success(params),
|
||||
"!error:" => Commands::Error(params),
|
||||
|
||||
_ => Commands::Error(params),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Commands {
|
||||
fn from(data: String) -> Self {
|
||||
Commands::from(data.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&[u8]> for Commands {
|
||||
fn from(data: &[u8]) -> Self {
|
||||
let incoming_message = String::from(String::from_utf8_lossy(data));
|
||||
Commands::from(incoming_message.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_commands_v2 {
|
||||
use super::Commands;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn test_creation_from_string() {
|
||||
let command_result = Commands::from("!connect: name:bop host:127.0.0.1 uuid:123456-1234-1234-123456");
|
||||
()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_string() {
|
||||
|
||||
let mut a: HashMap<String, String> = HashMap::new();
|
||||
a.insert("name".to_string(), "michael".to_string());
|
||||
a.insert("host".to_string(), "127.0.0.1".to_string());
|
||||
a.insert("uuid".to_string(), "123456-1234-1234-123456".to_string());
|
||||
|
||||
let command = Commands::Connect(Some(a));
|
||||
|
||||
println!("{:?}", command.to_string())
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
pub mod client;
|
||||
pub mod commands;
|
||||
pub mod server_profile;
|
||||
|
|
|
|||
|
|
@ -66,11 +66,7 @@ impl Server {
|
|||
receiver,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> String{
|
||||
self.name.to_string()
|
||||
}
|
||||
|
||||
|
||||
pub fn get_address(&self) -> String{
|
||||
self.address.to_string()
|
||||
}
|
||||
|
|
@ -79,7 +75,7 @@ impl Server {
|
|||
|
||||
}
|
||||
|
||||
pub fn start(&self) -> Result<(), io::Error>{
|
||||
pub fn start<'a>(&self) -> Result<(), io::Error>{
|
||||
info!("server: starting server...");
|
||||
// clone elements for thread
|
||||
let client_map = self.connected_clients.clone();
|
||||
|
|
@ -199,12 +195,6 @@ impl Server {
|
|||
info!("server: sending stop message");
|
||||
let _ = self.sender.send(ServerMessages::Shutdown);
|
||||
}
|
||||
|
||||
pub fn start(&'static self) -> Result<(), io::Error> {
|
||||
info!("server: starting server...");
|
||||
// clone elements for thread
|
||||
let client_map = self.connected_clients.clone();
|
||||
let receiver = self.receiver.clone();
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn get_info(&self, tx: Sender<Commands>) {
|
||||
|
|
@ -212,83 +202,8 @@ impl Server {
|
|||
params.insert(String::from("name"), self.name.to_string().clone());
|
||||
params.insert(String::from("owner"), self.author.to_string().clone());
|
||||
|
||||
info!("server: spawning threads");
|
||||
thread::Builder::new().name("Server Thread".to_string()).spawn(move || {
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
'outer: loop {
|
||||
// get messages from the servers channel.
|
||||
info!("server: getting messages");
|
||||
for i in receiver.try_iter() {
|
||||
match i {
|
||||
ServerMessages::Shutdown => {
|
||||
// TODO: implement disconnecting all clients and shutting down the server
|
||||
info!("server: shutting down...");
|
||||
|
||||
break 'outer;
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
info!("server: checking for new connections");
|
||||
if let Ok((mut stream, addr)) = listener.accept() {
|
||||
stream.set_read_timeout(Some(Duration::from_millis(10000))).unwrap();
|
||||
|
||||
let request = Commands::Request(None);
|
||||
self.transmit_data(&stream, &request.to_string().as_str());
|
||||
|
||||
match self.read_data(&stream, &mut buffer) {
|
||||
Ok(command) => {
|
||||
match command {
|
||||
Commands::Connect(Some(data)) => {
|
||||
let uuid = data.get("uuid").unwrap();
|
||||
let username = data.get("name").unwrap();
|
||||
let address = data.get("host").unwrap();
|
||||
|
||||
info!("{}", format!("Server: new Client connection: addr = {}", address ));
|
||||
|
||||
let mut client = Client::new(self, stream, &uuid, &username, &address);
|
||||
|
||||
let tx = client.get_transmitter();
|
||||
|
||||
let mut clients_hashmap = self.connected_clients.lock().unwrap();
|
||||
clients_hashmap.insert(uuid.to_string(), tx.clone());
|
||||
std::mem::drop(clients_hashmap);
|
||||
|
||||
let success = Commands::Success(None);
|
||||
tx.send(success).unwrap();
|
||||
|
||||
self.thread_pool.execute(move || {
|
||||
client.handle_connection();
|
||||
});
|
||||
|
||||
let params: HashMap<String, String> = [(String::from("name"), username.clone()), (String::from("host"), address.clone()), (String::from("uuid"), uuid.clone())].iter().cloned().collect();
|
||||
let new_client = Commands::Client(Some(params));
|
||||
self.update_all_clients(uuid.as_str(), new_client);
|
||||
},
|
||||
Commands::Info(None) => {
|
||||
info!("Server: info requested");
|
||||
|
||||
let params: HashMap<String, String> = [(String::from("name"), self.name.to_string().clone()), (String::from("owner"), self.author.to_string().clone())].iter().cloned().collect();
|
||||
let command = Commands::Success(Some(params));
|
||||
|
||||
self.transmit_data(&stream, command.to_string().as_str());
|
||||
},
|
||||
_ => {
|
||||
info!("Server: Invalid command sent");
|
||||
self.transmit_data(&stream, Commands::Error(None).to_string().as_str());
|
||||
},
|
||||
}
|
||||
},
|
||||
Err(_) => println!("ERROR: stream closed"),
|
||||
}
|
||||
}
|
||||
}
|
||||
info!("server: stopped")
|
||||
});
|
||||
info!("server: started");
|
||||
Ok(())
|
||||
let command = Commands::Info(Some(params));
|
||||
tx.send(command).unwrap();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
|
@ -322,352 +237,4 @@ impl Drop for Server {
|
|||
println!("server dropped");
|
||||
let _ = self.sender.send(ServerMessages::Shutdown);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests{
|
||||
use super::*;
|
||||
use std::{thread, time};
|
||||
use std::sync::Once;
|
||||
use std::time::Duration;
|
||||
|
||||
lazy_static!{
|
||||
static ref SERVER_NAME: &'static str = "test";
|
||||
static ref SERVER_ADDRESS: &'static str = "0.0.0.0:6000";
|
||||
static ref SERVER_AUTHOR: &'static str = "test";
|
||||
static ref SERVER: Server<'static> = Server::new(&SERVER_NAME, &SERVER_ADDRESS, &SERVER_AUTHOR);
|
||||
}
|
||||
|
||||
static START: Once = Once::new();
|
||||
|
||||
/*
|
||||
* These tests must be executed individually to ensure that no errors
|
||||
* occur, this is due to the fact that the server is created everytime.
|
||||
* Setup a system for the server to close after every test.
|
||||
*/
|
||||
fn setup_server(){
|
||||
unsafe{
|
||||
START.call_once(|| {
|
||||
thread::spawn(|| {
|
||||
SERVER.start();
|
||||
});
|
||||
});
|
||||
|
||||
let millis = time::Duration::from_millis(1000);
|
||||
thread::sleep(millis);
|
||||
}
|
||||
}
|
||||
|
||||
fn establish_client_connection(uuid: &str) -> TcpStream {
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
let mut stream = TcpStream::connect("0.0.0.0:6000").unwrap();
|
||||
|
||||
let mut command = read_data(&stream, &mut buffer);
|
||||
|
||||
assert_eq!(command, Commands::Request(None));
|
||||
|
||||
let msg: String = format!("!connect: uuid:{uuid} name:\"{name}\" host:\"{host}\"", uuid=uuid, name="alice", host="127.0.0.1");
|
||||
transmit_data(&stream, msg.as_str());
|
||||
|
||||
command = read_data(&stream, &mut buffer);
|
||||
|
||||
assert_eq!(command, Commands::Success(None));
|
||||
|
||||
stream
|
||||
}
|
||||
|
||||
fn transmit_data(mut stream: &TcpStream, data: &str){
|
||||
stream.write(data.to_string().as_bytes()).unwrap();
|
||||
stream.flush().unwrap();
|
||||
}
|
||||
|
||||
fn read_data(mut stream: &TcpStream, buffer: &mut [u8; 1024]) -> Commands {
|
||||
match stream.read(buffer) {
|
||||
Ok(_) => Commands::from(buffer),
|
||||
Err(_) => Commands::Error(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn force_disconnect(mut stream: &TcpStream){
|
||||
let msg = "!disconnect:";
|
||||
transmit_data(&stream, msg);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_server_connect(){
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
setup_server();
|
||||
|
||||
let mut stream = TcpStream::connect("0.0.0.0:6000").unwrap();
|
||||
|
||||
stream.read(&mut buffer).unwrap();
|
||||
let mut command = Commands::from(&mut buffer);
|
||||
|
||||
assert_eq!(command, Commands::Request(None));
|
||||
|
||||
let msg = b"!connect: uuid:123456-1234-1234-123456 name:\"alice\" host:\"127.0.0.1\"";
|
||||
stream.write(msg).unwrap();
|
||||
|
||||
stream.read(&mut buffer).unwrap();
|
||||
command = Commands::from(&mut buffer);
|
||||
|
||||
assert_eq!(command, Commands::Success(None));
|
||||
|
||||
let msg = b"!disconnect:";
|
||||
stream.write(msg).unwrap();
|
||||
|
||||
let dur = time::Duration::from_millis(500);
|
||||
thread::sleep(dur);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_server_info(){
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
setup_server();
|
||||
|
||||
let mut stream = TcpStream::connect("0.0.0.0:6000").unwrap();
|
||||
|
||||
let command = read_data(&stream, &mut buffer);
|
||||
|
||||
assert_eq!(command, Commands::Request(None));
|
||||
|
||||
let msg = "!info:";
|
||||
transmit_data(&stream, msg);
|
||||
|
||||
let command = read_data(&stream, &mut buffer);
|
||||
|
||||
let params: HashMap<String, String> = [(String::from("name"), String::from("test")), (String::from("owner"), String::from("test"))].iter().cloned().collect();
|
||||
assert_eq!(command, Commands::Success(Some(params)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_client_info(){
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
setup_server();
|
||||
|
||||
let mut stream = establish_client_connection("1234-5542-2124-155");
|
||||
|
||||
let msg = "!info:";
|
||||
transmit_data(&stream, msg);
|
||||
|
||||
let command = read_data(&stream, &mut buffer);
|
||||
|
||||
let params: HashMap<String, String> = [(String::from("name"), String::from("test")), (String::from("owner"), String::from("test"))].iter().cloned().collect();
|
||||
assert_eq!(command, Commands::Success(Some(params)));
|
||||
|
||||
let msg = "!disconnect:";
|
||||
transmit_data(&stream, msg);
|
||||
|
||||
let dur = time::Duration::from_millis(500);
|
||||
thread::sleep(dur);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clientUpdate_solo(){
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
setup_server();
|
||||
|
||||
let mut stream = establish_client_connection("1222-555-6-7");
|
||||
|
||||
let msg = "!clientUpdate:";
|
||||
transmit_data(&stream, msg);
|
||||
|
||||
let command = read_data(&stream, &mut buffer);
|
||||
|
||||
assert_eq!(command, Commands::Success(None));
|
||||
|
||||
let msg = "!disconnect:";
|
||||
transmit_data(&stream, msg);
|
||||
|
||||
let dur = time::Duration::from_millis(500);
|
||||
thread::sleep(dur);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_clientUpdate_multi(){
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
setup_server();
|
||||
|
||||
let mut stream_one = establish_client_connection("0001-776-6-5");
|
||||
let mut stream_two = establish_client_connection("0010-776-6-5");
|
||||
let mut stream_three = establish_client_connection("0011-776-6-5");
|
||||
let mut stream_four = establish_client_connection("0100-776-6-5");
|
||||
|
||||
let client_uuids: [String; 3] = [String::from("0010-776-6-5"), String::from("0011-776-6-5"), String::from("0100-776-6-5")];
|
||||
let mut user_1 = true;
|
||||
let mut user_2 = true;
|
||||
let mut user_3 = true;
|
||||
|
||||
for uuid in client_uuids.iter() {
|
||||
let command = read_data(&stream_one, &mut buffer);
|
||||
|
||||
if *uuid == String::from("0010-776-6-5") && user_1 {
|
||||
let params: HashMap<String, String> = [(String::from("uuid"), String::from("0010-776-6-5")), (String::from("name"), String::from("\"alice\"")), (String::from("host"), String::from("\"127.0.0.1\""))].iter().cloned().collect();
|
||||
assert_eq!(command, Commands::Client(Some(params)));
|
||||
|
||||
user_1 = false;
|
||||
} else if *uuid == String::from("0011-776-6-5") && user_2 {
|
||||
let params: HashMap<String, String> = [(String::from("uuid"), String::from("0011-776-6-5")), (String::from("name"), String::from("\"alice\"")), (String::from("host"), String::from("\"127.0.0.1\""))].iter().cloned().collect();
|
||||
assert_eq!(command, Commands::Client(Some(params)));
|
||||
|
||||
user_2 = false;
|
||||
} else if *uuid == String::from("0100-776-6-5") && user_3 {
|
||||
let params: HashMap<String, String> = [(String::from("uuid"), String::from("0100-776-6-5")), (String::from("name"), String::from("\"alice\"")), (String::from("host"), String::from("\"127.0.0.1\""))].iter().cloned().collect();
|
||||
assert_eq!(command, Commands::Client(Some(params)));
|
||||
|
||||
user_3 = false;
|
||||
} else {
|
||||
assert!(false);
|
||||
}
|
||||
let msg = "!success:";
|
||||
transmit_data(&stream_one, msg);
|
||||
}
|
||||
|
||||
stream_one.set_read_timeout(Some(Duration::from_millis(3000))).unwrap();
|
||||
let mut unsuccessful = true;
|
||||
while unsuccessful {
|
||||
let msg = "!clientUpdate:";
|
||||
transmit_data(&stream_one, msg);
|
||||
|
||||
let command = read_data(&stream_one, &mut buffer);
|
||||
match command.clone() {
|
||||
Commands::Error(None) => println!("resending..."),
|
||||
_ => {
|
||||
assert_eq!(command, Commands::Success(None));
|
||||
unsuccessful = false;
|
||||
},
|
||||
}
|
||||
}
|
||||
stream_one.set_read_timeout(None).unwrap();
|
||||
|
||||
for x in 0..3 {
|
||||
let command = read_data(&stream_one, &mut buffer);
|
||||
|
||||
let command_clone = command.clone();
|
||||
match command{
|
||||
Commands::Client(Some(params)) => {
|
||||
let uuid = params.get("uuid").unwrap();
|
||||
|
||||
if *uuid == String::from("0010-776-6-5") {
|
||||
let params: HashMap<String, String> = [(String::from("uuid"), String::from("0010-776-6-5")), (String::from("name"), String::from("\"alice\"")), (String::from("host"), String::from("\"127.0.0.1\""))].iter().cloned().collect();
|
||||
assert_eq!(command_clone, Commands::Client(Some(params)));
|
||||
} else if *uuid == String::from("0011-776-6-5") {
|
||||
let params: HashMap<String, String> = [(String::from("uuid"), String::from("0011-776-6-5")), (String::from("name"), String::from("\"alice\"")), (String::from("host"), String::from("\"127.0.0.1\""))].iter().cloned().collect();
|
||||
assert_eq!(command_clone, Commands::Client(Some(params)));
|
||||
} else if *uuid == String::from("0100-776-6-5") {
|
||||
let params: HashMap<String, String> = [(String::from("uuid"), String::from("0100-776-6-5")), (String::from("name"), String::from("\"alice\"")), (String::from("host"), String::from("\"127.0.0.1\""))].iter().cloned().collect();
|
||||
assert_eq!(command_clone, Commands::Client(Some(params)));
|
||||
} else {
|
||||
assert!(false);
|
||||
}
|
||||
},
|
||||
_ => assert!(false),
|
||||
}
|
||||
|
||||
let msg = "!success:";
|
||||
transmit_data(&stream_one, msg);
|
||||
}
|
||||
|
||||
let dur = time::Duration::from_millis(500);
|
||||
thread::sleep(dur);
|
||||
|
||||
let msg = "!disconnect:";
|
||||
transmit_data(&stream_one, msg);
|
||||
transmit_data(&stream_two, msg);
|
||||
transmit_data(&stream_three, msg);
|
||||
transmit_data(&stream_four, msg);
|
||||
|
||||
let dur = time::Duration::from_millis(500);
|
||||
thread::sleep(dur);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clientInfo(){
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
setup_server();
|
||||
|
||||
let mut stream_one = establish_client_connection("0001-776-6-5");
|
||||
let mut stream_two = establish_client_connection("\"0010-776-6-5\"");
|
||||
|
||||
let command = read_data(&stream_one, &mut buffer);
|
||||
let params: HashMap<String, String> = [(String::from("uuid"), String::from("\"0010-776-6-5\"")), (String::from("name"), String::from("\"alice\"")), (String::from("host"), String::from("\"127.0.0.1\""))].iter().cloned().collect();
|
||||
assert_eq!(command, Commands::Client(Some(params)));
|
||||
|
||||
let msg = "!success:";
|
||||
transmit_data(&stream_one, msg);
|
||||
|
||||
|
||||
stream_one.set_read_timeout(Some(Duration::from_millis(3000))).unwrap();
|
||||
let mut unsuccessful = true;
|
||||
while unsuccessful {
|
||||
let msg = "!clientInfo: uuid:\"0010-776-6-5\"";
|
||||
transmit_data(&stream_one, msg);
|
||||
|
||||
let command = read_data(&stream_one, &mut buffer);
|
||||
match command.clone() {
|
||||
Commands::Error(None) => println!("resending..."),
|
||||
_ => {
|
||||
let params: HashMap<String, String> = [(String::from("uuid"), String::from("\"0010-776-6-5\"")), (String::from("name"), String::from("\"alice\"")), (String::from("host"), String::from("\"127.0.0.1\""))].iter().cloned().collect();
|
||||
assert_eq!(command, Commands::Success(Some(params)));
|
||||
unsuccessful = false;
|
||||
},
|
||||
}
|
||||
}
|
||||
stream_one.set_read_timeout(None).unwrap();
|
||||
|
||||
let msg = "!disconnect:";
|
||||
transmit_data(&stream_one, msg);
|
||||
transmit_data(&stream_two, msg);
|
||||
|
||||
let dur = time::Duration::from_millis(500);
|
||||
thread::sleep(dur);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_client_disconnect(){
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
setup_server();
|
||||
|
||||
let mut stream_one = establish_client_connection("0001-776-6-5");
|
||||
let mut stream_two = establish_client_connection("0010-776-6-5");
|
||||
|
||||
let command = read_data(&stream_one, &mut buffer);
|
||||
let params: HashMap<String, String> = [(String::from("uuid"), String::from("0010-776-6-5")), (String::from("name"), String::from("\"alice\"")), (String::from("host"), String::from("\"127.0.0.1\""))].iter().cloned().collect();
|
||||
assert_eq!(command, Commands::Client(Some(params)));
|
||||
|
||||
let msg = "!success:";
|
||||
transmit_data(&stream_one, msg);
|
||||
|
||||
let msg = "!disconnect:";
|
||||
transmit_data(&stream_two, msg);
|
||||
|
||||
let command = read_data(&stream_one, &mut buffer);
|
||||
let params: HashMap<String, String> = [(String::from("uuid"), String::from("0010-776-6-5"))].iter().cloned().collect();
|
||||
assert_eq!(command, Commands::Client(Some(params)));
|
||||
|
||||
let msg = "!success:";
|
||||
transmit_data(&stream_one, msg);
|
||||
|
||||
stream_one.set_read_timeout(Some(Duration::from_millis(2000))).unwrap();
|
||||
match stream_one.peek(&mut buffer) {
|
||||
Ok(_) => assert!(false),
|
||||
Err(_) => assert!(true),
|
||||
}
|
||||
stream_one.set_read_timeout(None).unwrap();
|
||||
|
||||
let msg = "!disconnect:";
|
||||
transmit_data(&stream_one, msg);
|
||||
|
||||
let dur = time::Duration::from_millis(500);
|
||||
thread::sleep(dur);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue