Browse Source

Add {Connect, Disconnect}Notification.

wip
Titouan Rigoudy 9 years ago
parent
commit
4eca84f468
3 changed files with 21 additions and 2 deletions
  1. +6
    -0
      src/client.rs
  2. +13
    -2
      src/control/controller.rs
  3. +2
    -0
      src/control/request.rs

+ 6
- 0
src/client.rs View File

@ -100,6 +100,12 @@ impl Client {
fn handle_control_request(&mut self, request: control::Request) { fn handle_control_request(&mut self, request: control::Request) {
match request { match request {
control::Request::ConnectNotification =>
info!("Controller client connected"),
control::Request::DisconnectNotification =>
info!("Controller client disconnected"),
control::Request::LoginStatusRequest => control::Request::LoginStatusRequest =>
self.handle_login_status_request(), self.handle_login_status_request(),


+ 13
- 2
src/control/controller.rs View File

@ -119,21 +119,32 @@ impl Controller {
continue; 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, receiver) = client.split();
let (sender_tx, sender_rx) = mpsc::channel(); 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 tx = self.client_tx.clone();
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
Self::receiver_loop(receiver, tx, sender_tx); 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); Self::sender_loop(sender, &mut self.client_rx, sender_rx);
// Sender loop has terminated, wait for receiver loop too.
handle.join(); handle.join();
info!("Controller client disconnected");
// Notify client that the controller has disconnected.
self.client_tx.send(Request::DisconnectNotification).unwrap();
} }
} }


+ 2
- 0
src/control/request.rs View File

@ -1,5 +1,7 @@
#[derive(Debug, RustcDecodable, RustcEncodable)] #[derive(Debug, RustcDecodable, RustcEncodable)]
pub enum Request { pub enum Request {
ConnectNotification,
DisconnectNotification,
JoinRoomRequest(String), JoinRoomRequest(String),
LoginStatusRequest, LoginStatusRequest,
RoomListRequest, RoomListRequest,


Loading…
Cancel
Save