Get username and public key from ssh server.
This commit is contained in:
parent
0cff0e13da
commit
2c97d4b023
@ -1,11 +1,13 @@
|
|||||||
use clichess::UserRole;
|
|
||||||
use shakmaty::fen::Fen;
|
use shakmaty::fen::Fen;
|
||||||
use shakmaty::{Chess, Color, Setup};
|
use shakmaty::{Chess, Setup};
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
use std::os::unix::net::UnixStream;
|
use std::os::unix::net::UnixStream;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let username = std::env::args().nth(1).expect("no name given");
|
||||||
|
let public_key = std::env::args().nth(2).expect("no public key given");
|
||||||
|
println!("Name: {}, Public key: {}", username, public_key);
|
||||||
let mut stream = match UnixStream::connect("/tmp/clichess.socket") {
|
let mut stream = match UnixStream::connect("/tmp/clichess.socket") {
|
||||||
Ok(sock) => sock,
|
Ok(sock) => sock,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
@ -13,11 +15,15 @@ fn main() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
//send username and public key to server
|
||||||
|
clichess::write_to_stream(&mut stream, username);
|
||||||
|
clichess::write_to_stream(&mut stream, public_key);
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
let (client, chess) = get_connection_info_from_stream(&stream);
|
let (client, chess) = get_connection_info_from_stream(&stream);
|
||||||
//First prompt when connecting to the server
|
//First prompt when connecting to the server
|
||||||
println!(
|
println!(
|
||||||
"Hello, anonymous !\n\r You're playing with the {} pieces",
|
"Hello, {} !\n\r You're playing with the {} pieces",
|
||||||
|
client.username,
|
||||||
client.role.to_string()
|
client.role.to_string()
|
||||||
);
|
);
|
||||||
//then we get the initial role of the connected client.
|
//then we get the initial role of the connected client.
|
||||||
@ -27,7 +33,7 @@ fn main() {
|
|||||||
"{}",
|
"{}",
|
||||||
clichess::board_representation(¤t_position, clichess::to_color(client.side))
|
clichess::board_representation(¤t_position, clichess::to_color(client.side))
|
||||||
);
|
);
|
||||||
if clichess::is_player_turn(client, current_position.turn()) {
|
if clichess::is_player_turn(&client, current_position.turn()) {
|
||||||
//it's the user turn, taking user input
|
//it's the user turn, taking user input
|
||||||
io::stdin().read_line(&mut buffer).unwrap();
|
io::stdin().read_line(&mut buffer).unwrap();
|
||||||
println!("trying to play {}", buffer);
|
println!("trying to play {}", buffer);
|
||||||
|
@ -4,6 +4,7 @@ use clichess::UserRole;
|
|||||||
use shakmaty::{fen, Chess, Color, Setup};
|
use shakmaty::{fen, Chess, Color, Setup};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
use std::os::unix::net::{UnixListener, UnixStream};
|
use std::os::unix::net::{UnixListener, UnixStream};
|
||||||
use std::sync::{Arc, Condvar, Mutex};
|
use std::sync::{Arc, Condvar, Mutex};
|
||||||
|
|
||||||
@ -49,7 +50,7 @@ fn main() {
|
|||||||
|
|
||||||
fn handle_client(mut stream: UnixStream, server: Server) {
|
fn handle_client(mut stream: UnixStream, server: Server) {
|
||||||
//create client
|
//create client
|
||||||
let client = create_client(&server);
|
let client = create_client(&server, &stream);
|
||||||
//send its information to the client
|
//send its information to the client
|
||||||
println!(
|
println!(
|
||||||
"server {}, send first information to the client...",
|
"server {}, send first information to the client...",
|
||||||
@ -70,7 +71,7 @@ fn handle_client(mut stream: UnixStream, server: Server) {
|
|||||||
}
|
}
|
||||||
println!("server {}, current position to the client sent", server.id);
|
println!("server {}, current position to the client sent", server.id);
|
||||||
loop {
|
loop {
|
||||||
if clichess::is_player_turn(client, player_turn) {
|
if clichess::is_player_turn(&client, player_turn) {
|
||||||
//let go of the lock while waiting for user input.
|
//let go of the lock while waiting for user input.
|
||||||
println!("server {}, waiting for client move..", server.id);
|
println!("server {}, waiting for client move..", server.id);
|
||||||
let input = clichess::read_line_from_stream(&stream);
|
let input = clichess::read_line_from_stream(&stream);
|
||||||
@ -110,8 +111,22 @@ fn handle_client(mut stream: UnixStream, server: Server) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_client(server: &Server) -> Client {
|
fn create_client(server: &Server, stream: &UnixStream) -> Client {
|
||||||
println!("Creating client {}...", server.id);
|
println!("Creating client {}...", server.id);
|
||||||
|
//get client name and pubkey
|
||||||
|
println!("Read lines from stream...");
|
||||||
|
let mut buf = String::new();
|
||||||
|
let mut reader = BufReader::new(stream);
|
||||||
|
//first, username.
|
||||||
|
reader
|
||||||
|
.read_line(&mut buf)
|
||||||
|
.expect("Server closed connection.");
|
||||||
|
let username = String::from(buf.trim());
|
||||||
|
buf.clear();
|
||||||
|
reader
|
||||||
|
.read_line(&mut buf)
|
||||||
|
.expect("Server closed connection.");
|
||||||
|
let public_key = String::from(buf.trim());
|
||||||
let role = match server.id {
|
let role = match server.id {
|
||||||
0 => UserRole::White,
|
0 => UserRole::White,
|
||||||
1 => UserRole::Black,
|
1 => UserRole::Black,
|
||||||
@ -121,8 +136,10 @@ fn create_client(server: &Server) -> Client {
|
|||||||
let client = Client {
|
let client = Client {
|
||||||
role,
|
role,
|
||||||
side: clichess::from_color(clichess::get_default_side(role)),
|
side: clichess::from_color(clichess::get_default_side(role)),
|
||||||
|
username,
|
||||||
|
public_key,
|
||||||
};
|
};
|
||||||
clients.insert(server.id, (client, server.receiving_buffer.clone()));
|
clients.insert(server.id, (client.clone(), server.receiving_buffer.clone()));
|
||||||
println!("Created client {}", server.id);
|
println!("Created client {}", server.id);
|
||||||
client
|
client
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,12 @@ use std::io::{BufReader, Write};
|
|||||||
use std::os::unix::net::UnixStream;
|
use std::os::unix::net::UnixStream;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Serialize, Deserialize)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
pub role: UserRole,
|
pub role: UserRole,
|
||||||
pub side: BoardSide,
|
pub side: BoardSide,
|
||||||
|
pub username: String,
|
||||||
|
pub public_key: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
|
#[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
|
||||||
@ -220,7 +222,7 @@ pub fn from_color(color: Color) -> BoardSide {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_player_turn(client: Client, playing_side: Color) -> bool {
|
pub fn is_player_turn(client: &Client, playing_side: Color) -> bool {
|
||||||
(playing_side == Color::White && client.role == UserRole::White)
|
(playing_side == Color::White && client.role == UserRole::White)
|
||||||
|| (playing_side == Color::Black && client.role == UserRole::Black)
|
|| (playing_side == Color::Black && client.role == UserRole::Black)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user