Browse Source

Build server context with initial state.

main
Titouan Rigoudy 2 years ago
parent
commit
a5919c478c
2 changed files with 33 additions and 17 deletions
  1. +10
    -6
      client/src/context.rs
  2. +23
    -11
      client/src/server/context.rs

+ 10
- 6
client/src/context.rs View File

@ -9,7 +9,9 @@ use crate::clock::SimulatedSystemClock;
use crate::clock::SystemClock; use crate::clock::SystemClock;
use crate::control::Response as ControlResponse; use crate::control::Response as ControlResponse;
use crate::peer::PeerMap; use crate::peer::PeerMap;
use crate::server::context::{ServerContext, ServerLoginInfo};
use crate::server::context::{
ServerContext, ServerLoggedInContext, ServerLoggedInContextOptions,
};
use crate::server::room::RoomMap; use crate::server::room::RoomMap;
use crate::server::user::UserMap; use crate::server::user::UserMap;
use crate::Config; use crate::Config;
@ -122,13 +124,15 @@ impl ContextBundle {
peers: options.initial_state.peers, peers: options.initial_state.peers,
}, },
// TODO: Do not login immediately, wait for a login event. // TODO: Do not login immediately, wait for a login event.
server: ServerContext::new_logged_in(
options.user_name.clone(),
ServerLoginInfo {
server: ServerContext::LoggedIn(ServerLoggedInContext::new(
server_request_tx.clone(),
ServerLoggedInContextOptions {
user_name: options.user_name.clone(),
motd: "unimplemented".to_string(), motd: "unimplemented".to_string(),
request_tx: server_request_tx.clone(),
rooms: options.initial_state.rooms,
users: options.initial_state.users,
}, },
),
)),
user_name: options.user_name, user_name: options.user_name,
server_request_tx, server_request_tx,
control_response_tx, control_response_tx,


+ 23
- 11
client/src/server/context.rs View File

@ -25,7 +25,30 @@ pub struct ServerLoggedInContext {
motd: String, motd: String,
} }
/// Initial values from which a `ServerLoggedInContext` can be built.
pub struct ServerLoggedInContextOptions {
/// See similarly-named fields in `ServerLoggedInContext`.
pub user_name: String,
pub motd: String,
pub rooms: RoomMap,
pub users: UserMap,
}
impl ServerLoggedInContext { impl ServerLoggedInContext {
/// Mainly for use in tests.
// TODO: Mark as #[cfg(test)]?
pub fn new(
request_tx: Sender<ServerRequest>,
options: ServerLoggedInContextOptions,
) -> Self {
Self {
rooms: options.rooms,
users: options.users,
user_name: options.user_name,
motd: options.motd,
request_tx,
}
}
/// The user name with which we logged in. /// The user name with which we logged in.
pub fn user_name(&self) -> &str { pub fn user_name(&self) -> &str {
&self.user_name &self.user_name
@ -131,17 +154,6 @@ impl ServerContext {
}) })
} }
/// Constructs a new logged in context with the given information.
pub fn new_logged_in(user_name: String, login: ServerLoginInfo, rooms: RoomMap, users: UserMap) -> Self {
ServerLoggedOutContext {
user_name,
error: None,
}
.login(login),
Self::LoggedIn(
)
}
/// Attempts to record a successful login. Fails if we are already logged in. /// Attempts to record a successful login. Fails if we are already logged in.
pub fn login( pub fn login(
&mut self, &mut self,


Loading…
Cancel
Save