Browse Source

Handle LoginStatusRequest.

wip
Titouan Rigoudy 4 years ago
parent
commit
8f490cf1e9
7 changed files with 111 additions and 20 deletions
  1. +1
    -0
      client/src/config.rs
  2. +13
    -1
      client/src/context.rs
  3. +12
    -12
      client/src/control/response.rs
  4. +10
    -3
      client/src/dispatcher.rs
  5. +66
    -0
      client/src/handlers/login_status_request_handler.rs
  6. +2
    -0
      client/src/handlers/mod.rs
  7. +7
    -4
      client/src/main.rs

+ 1
- 0
client/src/config.rs View File

@ -1,6 +1,7 @@
use solstice_proto::server::Credentials;
// TODO: Implement reading this from .toml files.
#[derive(Debug)]
pub struct Config {
/// Credentials to present to the server.
pub credentials: Credentials,


+ 13
- 1
client/src/context.rs View File

@ -8,14 +8,26 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
use crate::control::Response as ControlResponse;
use crate::room::RoomMap;
use crate::user::UserMap;
use crate::Config;
/// Contains all the different bits of client state.
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct State {
pub user_name: String,
pub rooms: RoomMap,
pub users: UserMap,
}
impl Default for State {
fn default() -> Self {
return State {
user_name: Config::default().credentials.user_name().to_string(),
rooms: RoomMap::default(),
users: UserMap::default(),
};
}
}
/// Holds process-wide context for message handlers to execute against.
#[derive(Debug)]
pub struct Context {


+ 12
- 12
client/src/control/response.rs View File

@ -35,22 +35,22 @@ pub enum LoginStatusResponse {
/// The login request has been sent to the server, but the response hasn't
/// been received yet.
Pending {
/// The username used to log in.
username: String,
/// The user name used to log in.
user_name: String,
},
/// Login was successful.
Success {
/// The username used to log in.
username: String,
/// The user name used to log in.
user_name: String,
/// The message of the day sent by the server.
motd: String,
},
/// Login failed.
Failure {
/// The username used to log in.
username: String,
/// The user name used to log in.
user_name: String,
/// The reason the server gave for refusing the login request.
reason: String,
},
@ -122,12 +122,12 @@ mod tests {
assert_eq!(
serde_json::from_str::<LoginStatusResponse>(
r#"{
"Pending": { "username": "karandeep" }
"Pending": { "user_name": "karandeep" }
}"#
)
.unwrap(),
LoginStatusResponse::Pending {
username: "karandeep".to_string()
user_name: "karandeep".to_string()
}
);
}
@ -138,14 +138,14 @@ mod tests {
serde_json::from_str::<LoginStatusResponse>(
r#"{
"Success": {
"username": "karandeep",
"user_name": "karandeep",
"motd": "message of the day"
}
}"#
)
.unwrap(),
LoginStatusResponse::Success {
username: "karandeep".to_string(),
user_name: "karandeep".to_string(),
motd: "message of the day".to_string(),
}
);
@ -157,14 +157,14 @@ mod tests {
serde_json::from_str::<LoginStatusResponse>(
r#"{
"Failure": {
"username": "karandeep",
"user_name": "karandeep",
"reason": "garbage!"
}
}"#
)
.unwrap(),
LoginStatusResponse::Failure {
username: "karandeep".to_string(),
user_name: "karandeep".to_string(),
reason: "garbage!".to_string(),
}
);


+ 10
- 3
client/src/dispatcher.rs View File

@ -9,9 +9,10 @@ use crate::context::Context;
use crate::control::Request as ControlRequest;
use crate::executor::Job;
use crate::handlers::{
PrivilegedUsersResponseHandler, RoomJoinRequestHandler,
RoomJoinResponseHandler, RoomListRequestHandler, RoomListResponseHandler,
RoomMessageRequestHandler, RoomMessageResponseHandler,
LoginStatusRequestHandler, PrivilegedUsersResponseHandler,
RoomJoinRequestHandler, RoomJoinResponseHandler, RoomListRequestHandler,
RoomListResponseHandler, RoomMessageRequestHandler,
RoomMessageResponseHandler,
};
use crate::message_handler::MessageHandler;
@ -70,6 +71,12 @@ impl Dispatcher {
/// Dispatches the given message by wrapping it with a handler.
pub fn dispatch(&self, message: Message) -> Option<Box<dyn Job>> {
match message {
Message::ControlRequest(ControlRequest::LoginStatusRequest) => {
Some(Box::new(DispatchedMessage {
message: (),
handler: LoginStatusRequestHandler::default(),
}))
}
Message::ServerResponse(ServerResponse::PrivilegedUsersResponse(
response,
)) => Some(Box::new(DispatchedMessage {


+ 66
- 0
client/src/handlers/login_status_request_handler.rs View File

@ -0,0 +1,66 @@
use anyhow::Context as AnyhowContext;
use crate::context::Context;
use crate::control;
use crate::message_handler::MessageHandler;
#[derive(Debug, Default)]
pub struct LoginStatusRequestHandler;
impl MessageHandler for LoginStatusRequestHandler {
type Message = ();
fn run(self, context: &Context, &(): &Self::Message) -> anyhow::Result<()> {
let user_name = context.state.lock().user_name.clone();
context
.control_response_tx
.blocking_send(control::Response::LoginStatusResponse(
control::LoginStatusResponse::Success {
user_name,
motd: String::new(),
},
))
.context("sending login status response")?;
Ok(())
}
fn name() -> String {
"LoginStatusRequestHandler".to_string()
}
}
#[cfg(test)]
mod tests {
use anyhow::Context;
use crate::context::{ContextBundle, ContextOptions};
use crate::control;
use crate::message_handler::MessageHandler;
use super::LoginStatusRequestHandler;
#[test]
fn run_sends_response() -> anyhow::Result<()> {
let mut options = ContextOptions::default();
options.initial_state.user_name = "shruti".to_string();
let mut bundle = ContextBundle::new(options);
LoginStatusRequestHandler::default()
.run(&bundle.context, &())
.context("running handler")?;
let response = bundle.control_response_rx.blocking_recv().unwrap();
assert_eq!(
response,
control::Response::LoginStatusResponse(
control::LoginStatusResponse::Success {
user_name: "shruti".to_string(),
motd: String::new(),
}
)
);
Ok(())
}
}

+ 2
- 0
client/src/handlers/mod.rs View File

@ -1,3 +1,4 @@
mod login_status_request_handler;
mod privileged_users_response_handler;
mod room_join_request_handler;
mod room_join_response_handler;
@ -6,6 +7,7 @@ mod room_list_response_handler;
mod room_message_request_handler;
mod room_message_response_handler;
pub use login_status_request_handler::LoginStatusRequestHandler;
pub use privileged_users_response_handler::PrivilegedUsersResponseHandler;
pub use room_join_request_handler::RoomJoinRequestHandler;
pub use room_join_response_handler::RoomJoinResponseHandler;


+ 7
- 4
client/src/main.rs View File

@ -23,7 +23,7 @@ mod room;
mod user;
use config::Config;
use context::ContextBundle;
use context::{ContextBundle, ContextOptions};
use dispatcher::Dispatcher;
use executor::Executor;
@ -81,13 +81,16 @@ async fn run_client(
}
async fn async_main() -> anyhow::Result<()> {
let bundle = ContextBundle::default();
let config = Config::default();
let mut options = ContextOptions::default();
options.initial_state.user_name = config.credentials.user_name().to_string();
let bundle = ContextBundle::new(options);
// TODO: Define a constant for this, or something.
let (dispatcher_tx, mut dispatcher_rx) = mpsc::channel(100);
let config = Config::default();
let mut control_listener =
control::Listener::bind(&config.control_listen_address)
.await


Loading…
Cancel
Save