summaryrefslogtreecommitdiff
path: root/webhogg/game_server/src/webhogg_group.rs
blob: 091f7f81c631ba642792e3d799aad266d6f62141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::group::{Group, GroupId};
use crate::server::{UserId, GameClient,
                    ClientSender, ClientReceiver,
                    GameServerError};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

pub struct WebhoggGroup {
    id: GroupId,
    name: String,
    senders: Arc<Mutex<HashMap<UserId, ClientSender>>>
}

impl Group for WebhoggGroup {
    fn id(&self) -> GroupId {
        self.id
    }

    fn group_type(&self) -> String {
        "webhogg".to_string()
    }

    fn name(&self) -> String {
        self.name.clone()
    }

    fn run(&mut self) {
        info!("a new group {}:'{}' runs now", self.id, self.name);
    }

    fn add_client(&mut self, id: UserId, client: GameClient) -> Result<(), GameServerError> {
        if self.senders.lock().unwrap().len() > 1 {
            return Err(GameServerError::GroupError(
                    format!("user {} was not able to join the {} group, {}",
                            "because the client limit has been exceeded",
                            id, self.name)));
        } 
        debug!("user {} joined the group {}:'{}'", id, self.id, self.name);
        let (sen, rec) = client.split();
        self.senders.lock().unwrap().insert(id, sen);
        let senders_mutex = self.senders.clone();
        let self_uid = id;
        std::thread::spawn(move || Self::broadcast_clients(self_uid, rec, senders_mutex));
        Ok(())
    }
}

impl WebhoggGroup {
    pub fn new(id: GroupId, name: String) -> Self {
        Self { id, name, senders: Arc::new(Mutex::new(HashMap::new())) }
    }

    fn broadcast_clients(self_uid: UserId, mut rec: ClientReceiver, senders_mutex: Arc<Mutex<HashMap<UserId, ClientSender>>>) {
        loop {
            let message = match rec.recv_message() {
                Ok(x) => x,
                _ => break
            };
            //trace!("got message: '{:?}'", message);
            let mut senders = senders_mutex.lock().unwrap();
            for (uid, sender) in senders.iter_mut() {
                if self_uid != *uid {
                    sender.send_message(&message)
                        .unwrap_or_else(|_| debug!("tried to send message to {}, but failed", *uid));
                }
            }
        }
        senders_mutex.lock().unwrap().remove(&self_uid);
        info!("client {} has left", self_uid);
    }
}