Get username and public key from ssh server.

This commit is contained in:
Artlef 2020-01-05 22:38:49 +01:00
parent 0cff0e13da
commit 2c97d4b023
3 changed files with 35 additions and 10 deletions

View File

@ -1,11 +1,13 @@
use clichess::UserRole;
use shakmaty::fen::Fen;
use shakmaty::{Chess, Color, Setup};
use shakmaty::{Chess, Setup};
use std::io;
use std::io::{BufRead, BufReader};
use std::os::unix::net::UnixStream;
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") {
Ok(sock) => sock,
Err(_) => {
@ -13,11 +15,15 @@ fn main() {
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 (client, chess) = get_connection_info_from_stream(&stream);
//First prompt when connecting to the server
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()
);
//then we get the initial role of the connected client.
@ -27,7 +33,7 @@ fn main() {
"{}",
clichess::board_representation(&current_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
io::stdin().read_line(&mut buffer).unwrap();
println!("trying to play {}", buffer);

View File

@ -4,6 +4,7 @@ use clichess::UserRole;
use shakmaty::{fen, Chess, Color, Setup};
use std::collections::HashMap;
use std::fs;
use std::io::{BufRead, BufReader};
use std::os::unix::net::{UnixListener, UnixStream};
use std::sync::{Arc, Condvar, Mutex};
@ -49,7 +50,7 @@ fn main() {
fn handle_client(mut stream: UnixStream, server: Server) {
//create client
let client = create_client(&server);
let client = create_client(&server, &stream);
//send its information to the client
println!(
"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);
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.
println!("server {}, waiting for client move..", server.id);
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);
//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 {
0 => UserRole::White,
1 => UserRole::Black,
@ -121,8 +136,10 @@ fn create_client(server: &Server) -> Client {
let client = Client {
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);
client
}

View File

@ -10,10 +10,12 @@ use std::io::{BufReader, Write};
use std::os::unix::net::UnixStream;
use std::str;
#[derive(Copy, Clone, Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize)]
pub struct Client {
pub role: UserRole,
pub side: BoardSide,
pub username: String,
pub public_key: String,
}
#[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::Black && client.role == UserRole::Black)
}