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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use communication::Sender;
use handler::Handler;

/// A trait for creating new WebSocket handlers.
pub trait Factory {
    type Handler: Handler;

    /// Called when a TCP connection is made.
    fn connection_made(&mut self, _: Sender) -> Self::Handler;

    /// Called when the WebSocket is shutting down.
    #[inline]
    fn on_shutdown(&mut self) {
        debug!("Factory received WebSocket shutdown request.");
    }

    /// Called when a new connection is established for a client endpoint.
    /// This method can be used to differentiate a client aspect for a handler.
    ///
    /// ```
    /// use ws::{Sender, Factory, Handler};
    ///
    /// struct MyHandler {
    ///     ws: Sender,
    ///     is_client: bool,
    /// }
    ///
    /// impl Handler for MyHandler {}
    ///
    /// struct MyFactory;
    ///
    /// impl Factory for MyFactory {
    ///     type Handler = MyHandler;
    ///
    ///     fn connection_made(&mut self, ws: Sender) -> MyHandler {
    ///         MyHandler {
    ///             ws: ws,
    ///             // default to server
    ///             is_client: false,
    ///         }
    ///     }
    ///
    ///     fn client_connected(&mut self, ws: Sender) -> MyHandler {
    ///         MyHandler {
    ///             ws: ws,
    ///             is_client: true,
    ///         }
    ///     }
    /// }
    /// ```
    #[inline]
    fn client_connected(&mut self, ws: Sender) -> Self::Handler {
        self.connection_made(ws)
    }

    /// Called when a new connection is established for a server endpoint.
    /// This method can be used to differentiate a server aspect for a handler.
    ///
    /// ```
    /// use ws::{Sender, Factory, Handler};
    ///
    /// struct MyHandler {
    ///     ws: Sender,
    ///     is_server: bool,
    /// }
    ///
    /// impl Handler for MyHandler {}
    ///
    /// struct MyFactory;
    ///
    /// impl Factory for MyFactory {
    ///     type Handler = MyHandler;
    ///
    ///     fn connection_made(&mut self, ws: Sender) -> MyHandler {
    ///         MyHandler {
    ///             ws: ws,
    ///             // default to client
    ///             is_server: false,
    ///         }
    ///     }
    ///
    ///     fn server_connected(&mut self, ws: Sender) -> MyHandler {
    ///         MyHandler {
    ///             ws: ws,
    ///             is_server: true,
    ///         }
    ///     }
    /// }
    #[inline]
    fn server_connected(&mut self, ws: Sender) -> Self::Handler {
        self.connection_made(ws)
    }

    /// Called when a TCP connection is lost with the handler that was
    /// setup for that connection.
    ///
    /// The default implementation is a noop that simply drops the handler.
    /// You can use this to track connections being destroyed or to finalize
    /// state that was not internally tracked by the handler.
    #[inline]
    fn connection_lost(&mut self, _: Self::Handler) {}
}

impl<F, H> Factory for F
where
    H: Handler,
    F: FnMut(Sender) -> H,
{
    type Handler = H;

    fn connection_made(&mut self, out: Sender) -> H {
        self(out)
    }
}

mod test {
    #![allow(unused_imports, unused_variables, dead_code)]
    use super::*;
    use communication::{Command, Sender};
    use frame;
    use handler::Handler;
    use handshake::{Handshake, Request, Response};
    use message;
    use mio;
    use protocol::CloseCode;
    use result::Result;

    #[derive(Debug, Eq, PartialEq)]
    struct M;
    impl Handler for M {
        fn on_message(&mut self, _: message::Message) -> Result<()> {
            println!("test");
            Ok(())
        }

        fn on_frame(&mut self, f: frame::Frame) -> Result<Option<frame::Frame>> {
            Ok(None)
        }
    }

    #[test]
    fn impl_factory() {
        struct X;

        impl Factory for X {
            type Handler = M;
            fn connection_made(&mut self, _: Sender) -> M {
                M
            }
        }

        let (chn, _) = mio::channel::sync_channel(42);

        let mut x = X;
        let m = x.connection_made(Sender::new(mio::Token(0), chn, 0));
        assert_eq!(m, M);
    }

    #[test]
    fn closure_factory() {
        let (chn, _) = mio::channel::sync_channel(42);

        let mut factory = |_| |_| Ok(());

        factory.connection_made(Sender::new(mio::Token(0), chn, 0));
    }

    #[test]
    fn connection_lost() {
        struct X;

        impl Factory for X {
            type Handler = M;
            fn connection_made(&mut self, _: Sender) -> M {
                M
            }
            fn connection_lost(&mut self, handler: M) {
                assert_eq!(handler, M);
            }
        }

        let (chn, _) = mio::channel::sync_channel(42);

        let mut x = X;
        let m = x.connection_made(Sender::new(mio::Token(0), chn, 0));
        x.connection_lost(m);
    }
}