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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::vec::Vec;
use std::rc::Rc;
use std::cell::RefCell;
use ws::{listen, Sender, Handler, Message, Handshake, CloseCode, Result};
use serde_json;
// use serde_json::{Value, Error};

#[derive(Serialize, Deserialize)]
struct MessageJSON {
    #[serde(rename = "type")]
    type_name: String,
    content: String,
    from: usize,
    to: usize,
}

type Connections = Rc<RefCell<Vec<Sender>>>;
struct Server {
    ws: Sender,
    connections: Connections,
}

impl Handler for Server {
    fn on_open(&mut self, _: Handshake) -> Result<()> {
        let open_message = MessageJSON {
            type_name: String::from("open"),
            content: format!(""),
            from: usize::from(self.ws.token()),
            to: usize::from(self.ws.token()),
        };

        match serde_json::to_string(&open_message) {
            Ok(s) => {
                let msg = Message::text(s);
                self.ws.send(msg)?;
            },
            Err(e) => println!("{:?}", e)
        };

        let mut connections = self.connections.borrow_mut();
        for connection in connections.iter() {
            let connected_message = MessageJSON {
                type_name: String::from("connected"),
                content: format!(""),
                from: usize::from(self.ws.token()),
                to: usize::from(connection.token()),
            };

            match serde_json::to_string(&connected_message) {
                Ok(s) => {
                    let msg = Message::text(s);
                    connection.send(msg)?;
                },
                Err(e) => println!("{:?}", e)
            };
        }

        connections.push(self.ws.clone());

        Ok(())
    }

    fn on_message(&mut self, msg: Message) -> Result<()> {
        println!("*********Server got message '{}'. ", msg);

        match serde_json::from_str::<MessageJSON>(&msg.into_text()?) {
            Ok(received_message) => {
                if received_message.type_name == String::from("signal") {
                    let connections = self.connections.borrow();
                    for connection in connections.iter() {
                        if usize::from(connection.token()) == received_message.to {
                            let signal_message = MessageJSON {
                                type_name: String::from("signal"),
                                content: received_message.content.to_owned(),
                                from: usize::from(self.ws.token()),
                                to: usize::from(connection.token()),
                            };

                            match serde_json::to_string(&signal_message) {
                                Ok(s) => {
                                    let msg = Message::text(s);
                                    connection.send(msg);
                                },
                                Err(e) => println!("{:?}", e)
                            };
                        }
                    }
                }
            },
            Err(e) => println!("{:?}", e)
        };

        Ok(())
    }

    fn on_close(&mut self, code: CloseCode, reason: &str) {
        let mut connections = self.connections.borrow_mut();
        let index = connections.iter().position(|i| i.token() == self.ws.token()).unwrap();
        connections.remove(index);

        for connection in connections.iter() {
            let disconnected_message = MessageJSON {
                type_name: String::from("disconnected"),
                content: format!(""),
                from: usize::from(self.ws.token()),
                to: usize::from(connection.token()),
            };

            match serde_json::to_string(&disconnected_message) {
                Ok(s) => {
                    let msg = Message::text(s);
                    connection.send(msg);
                },
                Err(e) => println!("{:?}", e)
            };
        }

        match code {
            CloseCode::Normal => println!("The client is done with the connection."),
            CloseCode::Away   => println!("The client is leaving the site."),
            _ => println!("The client encountered an error: {}", reason),
        }
    }
}

pub struct MultiVP {
    pub address: String,
    pub port: u16,
}

impl MultiVP {
    pub fn new(address: String, port: u16) -> MultiVP {
        MultiVP {
            address: address,
            port: port
        }
    }

    pub fn run_server(&self) {
        let connections = Connections::new(RefCell::new(Vec::with_capacity(10_000)));;
        listen(format!("{}:{}", self.address, self.port), move |out| {
            Server {
                ws: out,
                connections: connections.clone()
            }
        }).unwrap()
    }
}