Browse Source

Introduce MessageHandler, Dispatcher, Executor.

wip
Titouan Rigoudy 6 years ago
parent
commit
9aaf843856
7 changed files with 149 additions and 7 deletions
  1. +43
    -0
      Cargo.lock
  2. +1
    -0
      Cargo.toml
  3. +22
    -0
      src/context.rs
  4. +44
    -0
      src/dispatcher.rs
  5. +20
    -0
      src/executor.rs
  6. +11
    -7
      src/main.rs
  7. +8
    -0
      src/message_handler.rs

+ 43
- 0
Cargo.lock View File

@ -266,6 +266,14 @@ dependencies = [
"scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lock_api"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "log"
version = "0.3.9"
@ -414,6 +422,16 @@ dependencies = [
"parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lock_api 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot_core"
version = "0.4.0"
@ -426,6 +444,21 @@ dependencies = [
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot_core"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "percent-encoding"
version = "1.0.1"
@ -613,6 +646,11 @@ name = "scopeguard"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "scopeguard"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "semver"
version = "0.9.0"
@ -666,6 +704,7 @@ dependencies = [
"futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.6.17 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1023,6 +1062,7 @@ dependencies = [
"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
"checksum libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "42914d39aad277d9e176efbdad68acb1d5443ab65afe0e0e4f0d49352a950880"
"checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c"
"checksum lock_api 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ed946d4529956a20f2d63ebe1b69996d5a2137c91913fe3ebbeff957f5bca7ff"
"checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
@ -1039,7 +1079,9 @@ dependencies = [
"checksum num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a23f0ed30a54abaa0c7e83b1d2d87ada7c3c23078d1d87815af3e3b6385fbba"
"checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13"
"checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337"
"checksum parking_lot 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fa7767817701cce701d5585b9c4db3cdd02086398322c1d7e8bf5094a96a2ce7"
"checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9"
"checksum parking_lot_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cb88cb1cb3790baa6776844f968fea3be44956cf184fa1be5a03341f5491278c"
"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
"checksum rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)" = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
@ -1062,6 +1104,7 @@ dependencies = [
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
"checksum scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "332ffa32bf586782a3efaeb58f127980944bbc8c4d6913a86107ac2a5ab24b28"
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
"checksum sha1 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a307a40d5834140e4213a6952483b84e9ad53bdcab918b7335a6e305e505a53c"


+ 1
- 0
Cargo.toml View File

@ -13,6 +13,7 @@ env_logger = "^0.3.2"
futures = "^0.1"
log = "^0.3.5"
mio = "^0.6"
parking_lot = "^0.8"
rust-crypto = "^0.2.34"
rustc-serialize = "^0.3.17"
slab = "^0.2"


+ 22
- 0
src/context.rs View File

@ -0,0 +1,22 @@
//! This module provides a central `Context` type that ties together all the
//! different bits of client state.
use parking_lot::Mutex;
use crate::room::RoomMap;
use crate::user::UserMap;
/// Contains all the different bits of client state.
pub struct Context {
pub rooms: Mutex<RoomMap>,
pub users: Mutex<UserMap>,
}
impl Context {
pub fn new() -> Self {
Self {
rooms: Mutex::new(RoomMap::new()),
users: Mutex::new(UserMap::new()),
}
}
}

+ 44
- 0
src/dispatcher.rs View File

@ -0,0 +1,44 @@
//! This module defines the central message dispatcher to the client process.
use std::io;
use crate::context::Context;
use crate::executor::Execute;
use crate::message_handler::MessageHandler;
use crate::proto::server::ServerResponse;
/// The type of messages dispatched by a dispatcher.
enum Message {
ServerResponse(ServerResponse),
}
/// Pairs together a message and its handler as chosen by the dispatcher.
/// Implements Execute so as to be scheduled on an executor.
struct DispatchedMessage<M, H> {
message: M,
handler: H,
}
impl<M, H> DispatchedMessage<M, H> {
fn new(message: M, handler: H) -> Self {
Self { message, handler }
}
}
impl<M, H: MessageHandler<M>> Execute for DispatchedMessage<M, H> {
fn execute(self, context: &Context) -> io::Result<()> {
self.handler.run(context, self.message)
}
}
struct Dispatcher;
impl Dispatcher {
fn new() -> Self {
Self {}
}
fn dispatch(message: Message) -> Box<dyn Execute> {
panic!("Unimplemented")
}
}

+ 20
- 0
src/executor.rs View File

@ -0,0 +1,20 @@
use std::io;
use crate::context::Context;
/// The trait of objects that can be run by an Executor.
pub trait Execute {
fn execute(self, context: &Context) -> io::Result<()>;
}
pub struct Executor {
// TODO
}
impl Executor {
pub fn new() -> Self { Self {} }
pub fn enqueue(work: Box<dyn Execute>) {
// TODO
}
}

+ 11
- 7
src/main.rs View File

@ -1,10 +1,3 @@
mod client;
mod config;
mod control;
mod proto;
mod room;
mod user;
// Still no 2018 way of using the log crate without `use log::*` everywhere.
#[macro_use]
extern crate log;
@ -14,6 +7,17 @@ use std::thread;
use crossbeam_channel;
use env_logger;
mod client;
mod config;
mod context;
mod control;
mod dispatcher;
mod executor;
mod message_handler;
mod proto;
mod room;
mod user;
fn main() {
match env_logger::init() {
Ok(()) => (),


+ 8
- 0
src/message_handler.rs View File

@ -0,0 +1,8 @@
use std::io;
use crate::context::Context;
/// A trait for types that can handle reception of a message.
pub trait MessageHandler<Message> {
fn run(self, context: &Context, message: Message) -> io::Result<()>;
}

Loading…
Cancel
Save