From 4eca84f468e86d0068404b2b4f3daf4fe2b85cb9 Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Sun, 27 Mar 2016 19:55:46 +0200 Subject: [PATCH] Add {Connect, Disconnect}Notification. --- src/client.rs | 6 ++++++ src/control/controller.rs | 15 +++++++++++++-- src/control/request.rs | 2 ++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index d141413..14edb4d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -100,6 +100,12 @@ impl Client { fn handle_control_request(&mut self, request: control::Request) { match request { + control::Request::ConnectNotification => + info!("Controller client connected"), + + control::Request::DisconnectNotification => + info!("Controller client disconnected"), + control::Request::LoginStatusRequest => self.handle_login_status_request(), diff --git a/src/control/controller.rs b/src/control/controller.rs index 384a8bf..aa7783c 100644 --- a/src/control/controller.rs +++ b/src/control/controller.rs @@ -119,21 +119,32 @@ impl Controller { continue; } }; - info!("Controller client connected"); + + // Empty client_rx of any messages that client has sent while + // no-one was connected. + while let Ok(_) = self.client_rx.try_recv() { /* continue */ } + + // Notify client that a controller is connected. + self.client_tx.send(Request::ConnectNotification).unwrap(); let (sender, receiver) = client.split(); let (sender_tx, sender_rx) = mpsc::channel(); + // Handle incoming messages from controller in a separate thread, + // and forward them to the client through client_tx. let tx = self.client_tx.clone(); let handle = thread::spawn(move || { Self::receiver_loop(receiver, tx, sender_tx); }); + // Handle messages from client and forward them to the controller. Self::sender_loop(sender, &mut self.client_rx, sender_rx); + // Sender loop has terminated, wait for receiver loop too. handle.join(); - info!("Controller client disconnected"); + // Notify client that the controller has disconnected. + self.client_tx.send(Request::DisconnectNotification).unwrap(); } } diff --git a/src/control/request.rs b/src/control/request.rs index c12ed84..725b542 100644 --- a/src/control/request.rs +++ b/src/control/request.rs @@ -1,5 +1,7 @@ #[derive(Debug, RustcDecodable, RustcEncodable)] pub enum Request { + ConnectNotification, + DisconnectNotification, JoinRoomRequest(String), LoginStatusRequest, RoomListRequest,