1 use slab; |
1 use slab; |
2 use mio::net::*; |
|
3 use mio::*; |
|
4 use std::io; |
|
5 |
|
6 use utils; |
2 use utils; |
7 use super::client::*; |
3 use super::{ |
8 use super::room::*; |
4 client::*, room::*, actions, handlers |
9 use super::actions; |
5 }; |
10 use protocol::messages::*; |
6 use protocol::messages::*; |
11 use super::handlers; |
|
12 |
7 |
13 type Slab<T> = slab::Slab<T>; |
8 type Slab<T> = slab::Slab<T>; |
14 |
9 |
|
10 #[derive(Debug)] |
15 pub enum Destination { |
11 pub enum Destination { |
|
12 ToAll, |
16 ToSelf(ClientId), |
13 ToSelf(ClientId), |
17 ToOthers(ClientId) |
14 ToOthers(ClientId), |
|
15 ToSelected(Vec<ClientId>) |
18 } |
16 } |
19 |
17 |
20 pub struct PendingMessage(pub Destination, pub HWServerMessage); |
18 pub struct PendingMessage(pub Destination, pub HWServerMessage); |
21 |
19 |
22 pub struct HWServer { |
20 pub struct HWServer { |
65 entry.insert(room); |
63 entry.insert(room); |
66 key |
64 key |
67 } |
65 } |
68 |
66 |
69 pub fn handle_msg(&mut self, client_id: ClientId, msg: HWProtocolMessage) { |
67 pub fn handle_msg(&mut self, client_id: ClientId, msg: HWProtocolMessage) { |
|
68 debug!("Handling message {:?} for client {}", msg, client_id); |
70 handlers::handle(self, client_id, msg); |
69 handlers::handle(self, client_id, msg); |
|
70 } |
|
71 |
|
72 pub fn send_all(&mut self, msg: HWServerMessage) { |
|
73 self.output.push(PendingMessage( |
|
74 Destination::ToAll, msg)); |
71 } |
75 } |
72 |
76 |
73 pub fn send_self(&mut self, client_id: ClientId, msg: HWServerMessage) { |
77 pub fn send_self(&mut self, client_id: ClientId, msg: HWServerMessage) { |
74 self.output.push(PendingMessage( |
78 self.output.push(PendingMessage( |
75 Destination::ToSelf(client_id), msg)); |
79 Destination::ToSelf(client_id), msg)); |
78 pub fn send_others(&mut self, client_id: ClientId, msg: HWServerMessage) { |
82 pub fn send_others(&mut self, client_id: ClientId, msg: HWServerMessage) { |
79 self.output.push(PendingMessage( |
83 self.output.push(PendingMessage( |
80 Destination::ToOthers(client_id), msg)); |
84 Destination::ToOthers(client_id), msg)); |
81 } |
85 } |
82 |
86 |
|
87 pub fn send_to_selected(&mut self, client_ids: Vec<ClientId>, msg: HWServerMessage) { |
|
88 self.output.push(PendingMessage( |
|
89 Destination::ToSelected(client_ids), msg)); |
|
90 } |
|
91 |
83 pub fn react(&mut self, client_id: ClientId, actions: Vec<actions::Action>) { |
92 pub fn react(&mut self, client_id: ClientId, actions: Vec<actions::Action>) { |
84 for action in actions { |
93 for action in actions { |
85 actions::run_action(self, client_id, action); |
94 actions::run_action(self, client_id, action); |
86 } |
95 } |
87 } |
96 } |
|
97 |
|
98 pub fn has_room(&self, name: &str) -> bool { |
|
99 self.rooms.iter().any(|(_, r)| r.name == name) |
|
100 } |
|
101 |
|
102 pub fn find_room(&self, name: &str) -> Option<&HWRoom> { |
|
103 self.rooms.iter().find(|(_, r)| r.name == name).map(|(_, r)| r) |
|
104 } |
|
105 |
|
106 pub fn find_room_mut(&mut self, name: &str) -> Option<&mut HWRoom> { |
|
107 self.rooms.iter_mut().find(|(_, r)| r.name == name).map(|(_, r)| r) |
|
108 } |
|
109 |
|
110 pub fn select_clients<F>(&self, f: F) -> Vec<ClientId> |
|
111 where F: Fn(&(usize, &HWClient)) -> bool { |
|
112 self.clients.iter().filter(f) |
|
113 .map(|(_, c)| c.id).collect() |
|
114 } |
|
115 |
|
116 pub fn room_clients(&self, room_id: RoomId) -> Vec<ClientId> { |
|
117 self.select_clients(|(_, c)| c.room_id == Some(room_id)) |
|
118 } |
|
119 |
|
120 pub fn protocol_clients(&self, protocol: u32) -> Vec<ClientId> { |
|
121 self.select_clients(|(_, c)| c.protocol_number == protocol) |
|
122 } |
|
123 |
|
124 pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> { |
|
125 let room_id = self.clients[self_id].room_id; |
|
126 self.select_clients(|(id, c)| *id != self_id && c.room_id == room_id ) |
|
127 } |
|
128 |
|
129 pub fn client_and_room(&mut self, client_id: ClientId) -> (&mut HWClient, Option<&mut HWRoom>) { |
|
130 let c = &mut self.clients[client_id]; |
|
131 if let Some(room_id) = c.room_id { |
|
132 (c, Some(&mut self.rooms[room_id])) |
|
133 } else { |
|
134 (c, None) |
|
135 } |
|
136 } |
88 } |
137 } |