2018-04-13 03:18:42 +02:00
|
|
|
//! `ChatServer` is an actor. It maintains list of connection client session.
|
|
|
|
//! And manages available rooms. Peers send messages to other peers in same
|
|
|
|
//! room through `ChatServer`.
|
|
|
|
|
2018-05-08 20:08:43 +02:00
|
|
|
use actix::prelude::*;
|
2018-11-17 06:04:24 +01:00
|
|
|
use rand::{self, rngs::ThreadRng, Rng};
|
2018-04-13 03:18:42 +02:00
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
|
2018-05-11 23:35:43 +02:00
|
|
|
/// Chat server sends this messages to session
|
|
|
|
#[derive(Message)]
|
|
|
|
pub struct Message(pub String);
|
2018-04-13 03:18:42 +02:00
|
|
|
|
|
|
|
/// Message for chat server communications
|
|
|
|
|
|
|
|
/// New chat session is created
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(usize)]
|
|
|
|
pub struct Connect {
|
2018-07-16 08:36:53 +02:00
|
|
|
pub addr: Recipient<Message>,
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Session is disconnected
|
|
|
|
#[derive(Message)]
|
|
|
|
pub struct Disconnect {
|
|
|
|
pub id: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send message to specific room
|
|
|
|
#[derive(Message)]
|
2018-05-11 23:35:43 +02:00
|
|
|
pub struct ClientMessage {
|
2018-04-13 03:18:42 +02:00
|
|
|
/// Id of the client session
|
|
|
|
pub id: usize,
|
|
|
|
/// Peer message
|
|
|
|
pub msg: String,
|
|
|
|
/// Room name
|
|
|
|
pub room: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List of available rooms
|
|
|
|
pub struct ListRooms;
|
|
|
|
|
|
|
|
impl actix::Message for ListRooms {
|
|
|
|
type Result = Vec<String>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Join room, if room does not exists create new one.
|
|
|
|
#[derive(Message)]
|
|
|
|
pub struct Join {
|
|
|
|
/// Client id
|
|
|
|
pub id: usize,
|
|
|
|
/// Room name
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2018-05-08 20:08:43 +02:00
|
|
|
/// `ChatServer` manages chat rooms and responsible for coordinating chat
|
|
|
|
/// session. implementation is super primitive
|
2018-04-13 03:18:42 +02:00
|
|
|
pub struct ChatServer {
|
2018-07-16 08:36:53 +02:00
|
|
|
sessions: HashMap<usize, Recipient<Message>>,
|
2018-04-13 03:18:42 +02:00
|
|
|
rooms: HashMap<String, HashSet<usize>>,
|
2018-11-17 06:04:24 +01:00
|
|
|
rng: ThreadRng,
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ChatServer {
|
|
|
|
fn default() -> ChatServer {
|
|
|
|
// default room
|
|
|
|
let mut rooms = HashMap::new();
|
|
|
|
rooms.insert("Main".to_owned(), HashSet::new());
|
|
|
|
|
|
|
|
ChatServer {
|
|
|
|
sessions: HashMap::new(),
|
|
|
|
rooms: rooms,
|
2018-11-17 06:04:24 +01:00
|
|
|
rng: rand::thread_rng(),
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChatServer {
|
|
|
|
/// Send message to all users in the room
|
|
|
|
fn send_message(&self, room: &str, message: &str, skip_id: usize) {
|
|
|
|
if let Some(sessions) = self.rooms.get(room) {
|
|
|
|
for id in sessions {
|
|
|
|
if *id != skip_id {
|
|
|
|
if let Some(addr) = self.sessions.get(id) {
|
2018-05-11 23:35:43 +02:00
|
|
|
let _ = addr.do_send(Message(message.to_owned()));
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Make actor from `ChatServer`
|
|
|
|
impl Actor for ChatServer {
|
|
|
|
/// We are going to use simple Context, we just need ability to communicate
|
|
|
|
/// with other actors.
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handler for Connect message.
|
|
|
|
///
|
|
|
|
/// Register new session and assign unique id to this session
|
|
|
|
impl Handler<Connect> for ChatServer {
|
|
|
|
type Result = usize;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: Connect, _: &mut Context<Self>) -> Self::Result {
|
|
|
|
println!("Someone joined");
|
|
|
|
|
|
|
|
// notify all users in same room
|
|
|
|
self.send_message(&"Main".to_owned(), "Someone joined", 0);
|
|
|
|
|
|
|
|
// register session with random id
|
2018-11-17 06:04:24 +01:00
|
|
|
let id = self.rng.gen::<usize>();
|
2018-04-13 03:18:42 +02:00
|
|
|
self.sessions.insert(id, msg.addr);
|
|
|
|
|
|
|
|
// auto join session to Main room
|
2018-05-21 06:03:29 +02:00
|
|
|
self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id);
|
2018-04-13 03:18:42 +02:00
|
|
|
|
|
|
|
// send id back
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handler for Disconnect message.
|
|
|
|
impl Handler<Disconnect> for ChatServer {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
|
|
|
|
println!("Someone disconnected");
|
|
|
|
|
|
|
|
let mut rooms: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
// remove address
|
|
|
|
if self.sessions.remove(&msg.id).is_some() {
|
|
|
|
// remove session from all rooms
|
|
|
|
for (name, sessions) in &mut self.rooms {
|
|
|
|
if sessions.remove(&msg.id) {
|
|
|
|
rooms.push(name.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// send message to other users
|
|
|
|
for room in rooms {
|
|
|
|
self.send_message(&room, "Someone disconnected", 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handler for Message message.
|
2018-05-11 23:35:43 +02:00
|
|
|
impl Handler<ClientMessage> for ChatServer {
|
2018-04-13 03:18:42 +02:00
|
|
|
type Result = ();
|
|
|
|
|
2018-05-11 23:35:43 +02:00
|
|
|
fn handle(&mut self, msg: ClientMessage, _: &mut Context<Self>) {
|
2018-04-13 03:18:42 +02:00
|
|
|
self.send_message(&msg.room, msg.msg.as_str(), msg.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handler for `ListRooms` message.
|
|
|
|
impl Handler<ListRooms> for ChatServer {
|
|
|
|
type Result = MessageResult<ListRooms>;
|
|
|
|
|
|
|
|
fn handle(&mut self, _: ListRooms, _: &mut Context<Self>) -> Self::Result {
|
|
|
|
let mut rooms = Vec::new();
|
|
|
|
|
|
|
|
for key in self.rooms.keys() {
|
|
|
|
rooms.push(key.to_owned())
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageResult(rooms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Join room, send disconnect message to old room
|
|
|
|
/// send join message to new room
|
|
|
|
impl Handler<Join> for ChatServer {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: Join, _: &mut Context<Self>) {
|
2018-05-08 20:08:43 +02:00
|
|
|
let Join { id, name } = msg;
|
2018-04-13 03:18:42 +02:00
|
|
|
let mut rooms = Vec::new();
|
|
|
|
|
|
|
|
// remove session from all rooms
|
|
|
|
for (n, sessions) in &mut self.rooms {
|
|
|
|
if sessions.remove(&id) {
|
|
|
|
rooms.push(n.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// send message to other users
|
|
|
|
for room in rooms {
|
|
|
|
self.send_message(&room, "Someone disconnected", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.rooms.get_mut(&name).is_none() {
|
|
|
|
self.rooms.insert(name.clone(), HashSet::new());
|
|
|
|
}
|
|
|
|
self.send_message(&name, "Someone connected", id);
|
|
|
|
self.rooms.get_mut(&name).unwrap().insert(id);
|
|
|
|
}
|
|
|
|
}
|