Browse Source

Introduce PeerEventHandler.

main
Titouan Rigoudy 2 years ago
parent
commit
6a57be5475
5 changed files with 80 additions and 11 deletions
  1. +27
    -3
      client/src/dispatcher.rs
  2. +36
    -0
      client/src/peer/event.rs
  3. +8
    -0
      client/src/peer/map.rs
  4. +9
    -0
      client/src/peer/mod.rs
  5. +0
    -8
      client/src/peer/state.rs

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

@ -8,11 +8,15 @@ use crate::control::Request as ControlRequest;
use crate::event::{Event, EventHandler}; use crate::event::{Event, EventHandler};
use crate::handlers::*; use crate::handlers::*;
use crate::message_handler::MessageHandler; use crate::message_handler::MessageHandler;
use crate::peer::{PeerEvent, PeerEventHandler};
use crate::room::{RoomEvent, RoomEventHandler}; use crate::room::{RoomEvent, RoomEventHandler};
use crate::user::{UserEvent, UserEventHandler}; use crate::user::{UserEvent, UserEventHandler};
/// Subsystem event handlers to which the `Dispatcher` dispatches events. /// Subsystem event handlers to which the `Dispatcher` dispatches events.
pub trait DispatcherHandlers { pub trait DispatcherHandlers {
type PeerEventHandler: EventHandler<Event = PeerEvent>;
fn peer_event_handler(&mut self) -> &mut Self::PeerEventHandler;
type RoomEventHandler: EventHandler<Event = RoomEvent>; type RoomEventHandler: EventHandler<Event = RoomEvent>;
fn room_event_handler(&mut self) -> &mut Self::RoomEventHandler; fn room_event_handler(&mut self) -> &mut Self::RoomEventHandler;
@ -23,11 +27,17 @@ pub trait DispatcherHandlers {
/// Default event handlers implementing real behavior. /// Default event handlers implementing real behavior.
#[derive(Default)] #[derive(Default)]
pub struct DefaultDispatcherHandlers { pub struct DefaultDispatcherHandlers {
peer_event_handler: PeerEventHandler,
room_event_handler: RoomEventHandler, room_event_handler: RoomEventHandler,
user_event_handler: UserEventHandler, user_event_handler: UserEventHandler,
} }
impl DispatcherHandlers for DefaultDispatcherHandlers { impl DispatcherHandlers for DefaultDispatcherHandlers {
type PeerEventHandler = PeerEventHandler;
fn peer_event_handler(&mut self) -> &mut Self::PeerEventHandler {
&mut self.peer_event_handler
}
type RoomEventHandler = RoomEventHandler; type RoomEventHandler = RoomEventHandler;
fn room_event_handler(&mut self) -> &mut Self::RoomEventHandler { fn room_event_handler(&mut self) -> &mut Self::RoomEventHandler {
&mut self.room_event_handler &mut self.room_event_handler
@ -74,7 +84,10 @@ impl<H: DispatcherHandlers> Dispatcher<H> {
fn dispatch_internal(&mut self, event: Event) -> anyhow::Result<()> { fn dispatch_internal(&mut self, event: Event) -> anyhow::Result<()> {
match event { match event {
Event::ServerResponse(ServerResponse::PeerAddressResponse(response)) => { Event::ServerResponse(ServerResponse::PeerAddressResponse(response)) => {
PeerAddressResponseHandler::default().run(&mut self.context, &response)
self
.handlers
.peer_event_handler()
.handle(&mut self.context, PeerEvent::AddressResponse(response))
} }
Event::ServerResponse(ServerResponse::PrivilegedUsersResponse( Event::ServerResponse(ServerResponse::PrivilegedUsersResponse(
response, response,
@ -104,7 +117,10 @@ impl<H: DispatcherHandlers> Dispatcher<H> {
LoginStatusRequestHandler::default().run(&mut self.context, &()) LoginStatusRequestHandler::default().run(&mut self.context, &())
} }
Event::ControlRequest(ControlRequest::PeerConnectRequest(request)) => { Event::ControlRequest(ControlRequest::PeerConnectRequest(request)) => {
PeerConnectRequestHandler::default().run(&mut self.context, &request)
self
.handlers
.peer_event_handler()
.handle(&mut self.context, PeerEvent::ConnectRequest(request))
} }
Event::ControlRequest(ControlRequest::RoomListRequest) => self Event::ControlRequest(ControlRequest::RoomListRequest) => self
.handlers .handlers
@ -149,6 +165,7 @@ mod tests {
use crate::context::{Context, ContextBundle}; use crate::context::{Context, ContextBundle};
use crate::control; use crate::control;
use crate::event::{Event, EventHandler}; use crate::event::{Event, EventHandler};
use crate::peer::PeerEvent;
use crate::room::RoomEvent; use crate::room::RoomEvent;
use crate::user::UserEvent; use crate::user::UserEvent;
@ -184,6 +201,7 @@ mod tests {
#[derive(Default)] #[derive(Default)]
struct FakeDispatcherHandlers { struct FakeDispatcherHandlers {
peer_event_handler: FakeEventHandler<PeerEvent>,
room_event_handler: FakeEventHandler<RoomEvent>, room_event_handler: FakeEventHandler<RoomEvent>,
user_event_handler: FakeEventHandler<UserEvent>, user_event_handler: FakeEventHandler<UserEvent>,
} }
@ -191,12 +209,18 @@ mod tests {
impl FakeDispatcherHandlers { impl FakeDispatcherHandlers {
// Defined here for better maintainability as new handlers are added. // Defined here for better maintainability as new handlers are added.
fn has_events(&self) -> bool { fn has_events(&self) -> bool {
!self.room_event_handler.events.is_empty()
!self.peer_event_handler.events.is_empty()
|| !self.room_event_handler.events.is_empty()
|| !self.user_event_handler.events.is_empty() || !self.user_event_handler.events.is_empty()
} }
} }
impl DispatcherHandlers for FakeDispatcherHandlers { impl DispatcherHandlers for FakeDispatcherHandlers {
type PeerEventHandler = FakeEventHandler<PeerEvent>;
fn peer_event_handler(&mut self) -> &mut Self::PeerEventHandler {
&mut self.peer_event_handler
}
type RoomEventHandler = FakeEventHandler<RoomEvent>; type RoomEventHandler = FakeEventHandler<RoomEvent>;
fn room_event_handler(&mut self) -> &mut Self::RoomEventHandler { fn room_event_handler(&mut self) -> &mut Self::RoomEventHandler {
&mut self.room_event_handler &mut self.room_event_handler


+ 36
- 0
client/src/peer/event.rs View File

@ -0,0 +1,36 @@
//! This module defines events affecting the peer module and their handling.
use solstice_proto::server;
use crate::context::Context;
use crate::event::EventHandler;
use crate::handlers::{PeerAddressResponseHandler, PeerConnectRequestHandler};
use crate::message_handler::MessageHandler;
/// The type of events that affect the peer module.
pub enum PeerEvent {
AddressResponse(server::PeerAddressResponse),
ConnectRequest(String),
}
#[derive(Default)]
pub struct PeerEventHandler;
impl EventHandler for PeerEventHandler {
type Event = PeerEvent;
fn handle(
&mut self,
context: &mut Context,
event: Self::Event,
) -> anyhow::Result<()> {
match event {
PeerEvent::AddressResponse(response) => {
PeerAddressResponseHandler::default().run(context, &response)
}
PeerEvent::ConnectRequest(peer_name) => {
PeerConnectRequestHandler::default().run(context, &peer_name)
}
}
}
}

+ 8
- 0
client/src/peer/map.rs View File

@ -0,0 +1,8 @@
use std::collections::HashMap;
use crate::peer::PeerState;
#[derive(Debug, Default)]
pub struct PeerMap {
pub peers: HashMap<String, PeerState>,
}

+ 9
- 0
client/src/peer/mod.rs View File

@ -0,0 +1,9 @@
//! This module deals with peers, peer connections and data transfer.
mod event;
mod map;
mod state;
pub use event::{PeerEvent, PeerEventHandler};
pub use map::PeerMap;
pub use state::PeerState;

client/src/peer.rs → client/src/peer/state.rs View File

@ -1,6 +1,3 @@
//! This module provides abstractions for interacting with a set of peers.
use std::collections::HashMap;
use std::net::SocketAddr; use std::net::SocketAddr;
// Peer states: // Peer states:
@ -62,8 +59,3 @@ pub enum PeerState {
} }
*/ */
} }
#[derive(Debug, Default)]
pub struct PeerMap {
pub peers: HashMap<String, PeerState>,
}

Loading…
Cancel
Save