Browse Source

Return login info from Client::login().

wip
Titouan Rigoudy 4 years ago
parent
commit
b69bef26dc
3 changed files with 29 additions and 8 deletions
  1. +1
    -1
      client/src/main.rs
  2. +18
    -3
      proto/src/server/client.rs
  3. +10
    -4
      proto/tests/connect.rs

+ 1
- 1
client/src/main.rs View File

@ -57,7 +57,7 @@ async fn run_client(
.context("validating credentials")?;
info!("Logging in to server.");
let mut worker = solstice_proto::server::Client::new(stream)
let (_info, mut worker) = solstice_proto::server::Client::new(stream)
.login(credentials)
.await
.context("logging in")?;


+ 18
- 3
proto/src/server/client.rs View File

@ -1,6 +1,7 @@
//! A client interface for remote servers.
use std::io;
use std::net::Ipv4Addr;
use log::{debug, info};
use thiserror::Error;
@ -20,6 +21,15 @@ pub struct Client {
version: Version,
}
/// Contents of a successful login response.
pub struct LoginInfo {
/// The server's message of the day.
pub motd: String,
/// Our public IP address, as observed by the server.
pub public_ip: Ipv4Addr,
}
/// An error that arose while logging in to a remote server.
#[derive(Debug, Error)]
pub enum ClientLoginError {
@ -63,7 +73,7 @@ impl Client {
pub async fn login(
mut self,
credentials: Credentials,
) -> Result<ClientWorker, ClientLoginError> {
) -> Result<(LoginInfo, ClientWorker), ClientLoginError> {
let login_request = credentials.into_login_request(self.version);
debug!("Sending login request: {:?}", login_request);
self
@ -94,7 +104,12 @@ impl Client {
info!("Login: Public IP address: {}", ip);
info!("Login: Password MD5: {:?}", password_md5_opt);
Ok(Worker::from_parts(self.reader, self.writer))
let info = LoginInfo {
motd,
public_ip: ip,
};
let worker = Worker::from_parts(self.reader, self.writer);
Ok((info, worker))
}
ServerResponse::LoginResponse(LoginResponse::LoginFail { reason }) => {
Err(ClientLoginError::LoginFailed(reason, self))
@ -190,7 +205,7 @@ mod tests {
.await
.expect("connecting");
let mut worker = Client::new(stream)
let (_, mut worker) = Client::new(stream)
.login(credentials())
.await
.expect("logging in");


+ 10
- 4
proto/tests/connect.rs View File

@ -28,10 +28,13 @@ fn make_credentials(user_name: String) -> Credentials {
async fn integration_connect() {
init();
let stream = connect().await.unwrap();
let stream = connect().await.expect("connecting");
let credentials = make_credentials(make_user_name("connect"));
let mut worker = Client::new(stream).login(credentials).await.unwrap();
let (_info, mut worker) = Client::new(stream)
.login(credentials)
.await
.expect("logging in");
let (request_tx, request_rx) = mpsc::channel(100);
let (response_tx, _response_rx) = mpsc::channel(100);
@ -50,11 +53,14 @@ async fn integration_connect() {
async fn integration_check_user_status() {
init();
let stream = connect().await.unwrap();
let stream = connect().await.expect("connecting");
let user_name = make_user_name("check_user_status");
let credentials = make_credentials(user_name.clone());
let mut worker = Client::new(stream).login(credentials).await.unwrap();
let (_, mut worker) = Client::new(stream)
.login(credentials)
.await
.expect("logging in");
let (request_tx, request_rx) = mpsc::channel(100);
let (response_tx, mut response_rx) = mpsc::channel(100);


Loading…
Cancel
Save