razer_ws/factory.rs
1use communication::Sender;
2use handler::Handler;
3
4/// A trait for creating new WebSocket handlers.
5pub trait Factory {
6 type Handler: Handler;
7
8 /// Called when a TCP connection is made.
9 fn connection_made(&mut self, _: Sender) -> Self::Handler;
10
11 /// Called when the WebSocket is shutting down.
12 #[inline]
13 fn on_shutdown(&mut self) {
14 debug!("Factory received WebSocket shutdown request.");
15 }
16
17 /// Called when a new connection is established for a client endpoint.
18 /// This method can be used to differentiate a client aspect for a handler.
19 ///
20 /// ```
21 /// use ws::{Sender, Factory, Handler};
22 ///
23 /// struct MyHandler {
24 /// ws: Sender,
25 /// is_client: bool,
26 /// }
27 ///
28 /// impl Handler for MyHandler {}
29 ///
30 /// struct MyFactory;
31 ///
32 /// impl Factory for MyFactory {
33 /// type Handler = MyHandler;
34 ///
35 /// fn connection_made(&mut self, ws: Sender) -> MyHandler {
36 /// MyHandler {
37 /// ws: ws,
38 /// // default to server
39 /// is_client: false,
40 /// }
41 /// }
42 ///
43 /// fn client_connected(&mut self, ws: Sender) -> MyHandler {
44 /// MyHandler {
45 /// ws: ws,
46 /// is_client: true,
47 /// }
48 /// }
49 /// }
50 /// ```
51 #[inline]
52 fn client_connected(&mut self, ws: Sender) -> Self::Handler {
53 self.connection_made(ws)
54 }
55
56 /// Called when a new connection is established for a server endpoint.
57 /// This method can be used to differentiate a server aspect for a handler.
58 ///
59 /// ```
60 /// use ws::{Sender, Factory, Handler};
61 ///
62 /// struct MyHandler {
63 /// ws: Sender,
64 /// is_server: bool,
65 /// }
66 ///
67 /// impl Handler for MyHandler {}
68 ///
69 /// struct MyFactory;
70 ///
71 /// impl Factory for MyFactory {
72 /// type Handler = MyHandler;
73 ///
74 /// fn connection_made(&mut self, ws: Sender) -> MyHandler {
75 /// MyHandler {
76 /// ws: ws,
77 /// // default to client
78 /// is_server: false,
79 /// }
80 /// }
81 ///
82 /// fn server_connected(&mut self, ws: Sender) -> MyHandler {
83 /// MyHandler {
84 /// ws: ws,
85 /// is_server: true,
86 /// }
87 /// }
88 /// }
89 #[inline]
90 fn server_connected(&mut self, ws: Sender) -> Self::Handler {
91 self.connection_made(ws)
92 }
93
94 /// Called when a TCP connection is lost with the handler that was
95 /// setup for that connection.
96 ///
97 /// The default implementation is a noop that simply drops the handler.
98 /// You can use this to track connections being destroyed or to finalize
99 /// state that was not internally tracked by the handler.
100 #[inline]
101 fn connection_lost(&mut self, _: Self::Handler) {}
102}
103
104impl<F, H> Factory for F
105where
106 H: Handler,
107 F: FnMut(Sender) -> H,
108{
109 type Handler = H;
110
111 fn connection_made(&mut self, out: Sender) -> H {
112 self(out)
113 }
114}
115
116mod test {
117 #![allow(unused_imports, unused_variables, dead_code)]
118 use super::*;
119 use communication::{Command, Sender};
120 use frame;
121 use handler::Handler;
122 use handshake::{Handshake, Request, Response};
123 use message;
124 use mio;
125 use protocol::CloseCode;
126 use result::Result;
127
128 #[derive(Debug, Eq, PartialEq)]
129 struct M;
130 impl Handler for M {
131 fn on_message(&mut self, _: message::Message) -> Result<()> {
132 println!("test");
133 Ok(())
134 }
135
136 fn on_frame(&mut self, f: frame::Frame) -> Result<Option<frame::Frame>> {
137 Ok(None)
138 }
139 }
140
141 #[test]
142 fn impl_factory() {
143 struct X;
144
145 impl Factory for X {
146 type Handler = M;
147 fn connection_made(&mut self, _: Sender) -> M {
148 M
149 }
150 }
151
152 let (chn, _) = mio::channel::sync_channel(42);
153
154 let mut x = X;
155 let m = x.connection_made(Sender::new(mio::Token(0), chn, 0));
156 assert_eq!(m, M);
157 }
158
159 #[test]
160 fn closure_factory() {
161 let (chn, _) = mio::channel::sync_channel(42);
162
163 let mut factory = |_| |_| Ok(());
164
165 factory.connection_made(Sender::new(mio::Token(0), chn, 0));
166 }
167
168 #[test]
169 fn connection_lost() {
170 struct X;
171
172 impl Factory for X {
173 type Handler = M;
174 fn connection_made(&mut self, _: Sender) -> M {
175 M
176 }
177 fn connection_lost(&mut self, handler: M) {
178 assert_eq!(handler, M);
179 }
180 }
181
182 let (chn, _) = mio::channel::sync_channel(42);
183
184 let mut x = X;
185 let m = x.connection_made(Sender::new(mio::Token(0), chn, 0));
186 x.connection_lost(m);
187 }
188}