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 | |
package com.sun.grizzly; |
40 | |
|
41 | |
import com.sun.grizzly.util.Copyable; |
42 | |
import com.sun.grizzly.util.SelectionKeyActionAttachment; |
43 | |
import java.io.IOException; |
44 | |
import java.net.Socket; |
45 | |
import java.nio.channels.ClosedChannelException; |
46 | |
import java.nio.channels.SelectableChannel; |
47 | |
import java.nio.channels.SelectionKey; |
48 | |
import java.nio.channels.Selector; |
49 | |
import java.nio.channels.SocketChannel; |
50 | |
import java.util.Iterator; |
51 | |
import java.util.logging.Level; |
52 | |
import java.util.logging.Logger; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public class BaseSelectionKeyHandler implements SelectionKeyHandler { |
66 | |
|
67 | 135 | protected Logger logger = Controller.logger(); |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
protected SelectorHandler selectorHandler; |
74 | |
|
75 | |
|
76 | 132 | public BaseSelectionKeyHandler() { |
77 | 132 | } |
78 | |
|
79 | |
|
80 | 3 | public BaseSelectionKeyHandler(SelectorHandler selectorHandler) { |
81 | 3 | this.selectorHandler = selectorHandler; |
82 | 3 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public SelectorHandler getSelectorHandler() { |
89 | 0 | return selectorHandler; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public void setSelectorHandler(SelectorHandler selectorHandler) { |
97 | 260 | this.selectorHandler = selectorHandler; |
98 | 260 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public void process(SelectionKey key) { |
105 | 144142 | Object attachment = key.attachment(); |
106 | |
|
107 | |
|
108 | |
|
109 | 144142 | if (attachment instanceof SelectionKeyActionAttachment) { |
110 | 115399 | ((SelectionKeyActionAttachment) attachment).process(key); |
111 | |
} |
112 | 144142 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public void postProcess(SelectionKey key) { |
119 | 143140 | Object attachment = key.attachment(); |
120 | |
|
121 | |
|
122 | |
|
123 | 143140 | if (attachment instanceof SelectionKeyActionAttachment) { |
124 | 115403 | ((SelectionKeyActionAttachment) attachment).postProcess(key); |
125 | |
} |
126 | 143139 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public void register(SelectionKey key, long currentTime) { |
133 | 0 | throw new UnsupportedOperationException(getClass().getName() + |
134 | |
" use of this API not allowed."); |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void register(SelectionKey key, int selectionKeyOps) { |
142 | 49458 | doRegisterKey(key, selectionKeyOps); |
143 | 49458 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
protected void doRegisterKey(SelectionKey key, int selectionKeyOps) { |
150 | 49458 | if (!key.isValid()) { |
151 | 0 | return; |
152 | |
} |
153 | 49458 | key.interestOps(key.interestOps() | selectionKeyOps); |
154 | 49458 | } |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public void register(SelectableChannel channel, int selectionKeyOps) throws ClosedChannelException { |
161 | 0 | if (!channel.isOpen()) { |
162 | 0 | return; |
163 | |
} |
164 | 0 | Selector selector = selectorHandler.getSelector(); |
165 | 0 | SelectionKey key = channel.keyFor(selector); |
166 | 0 | if (key == null) { |
167 | 0 | channel.register(selector, selectionKeyOps); |
168 | |
} else { |
169 | 0 | doRegisterKey(key, selectionKeyOps); |
170 | |
} |
171 | 0 | } |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public void register(Iterator<SelectionKey> keyIterator, int selectionKeyOps) { |
178 | |
SelectionKey key; |
179 | 0 | while (keyIterator.hasNext()) { |
180 | 0 | key = keyIterator.next(); |
181 | 0 | keyIterator.remove(); |
182 | 0 | doRegisterKey(key, selectionKeyOps); |
183 | |
} |
184 | 0 | } |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public void expire(SelectionKey key, long currentTime) { |
191 | |
|
192 | 0 | } |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public void expire(Iterator<SelectionKey> keyIterator) { |
199 | |
|
200 | 48940 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
public void cancel(SelectionKey key) { |
208 | 433 | if (!keyIsValid(key)) { |
209 | 10 | return; |
210 | |
} |
211 | |
|
212 | 423 | closeChannel(key); |
213 | |
|
214 | 423 | clearKeyAttachment(key); |
215 | |
|
216 | 423 | cancelKey(key); |
217 | 423 | } |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
public void close(SelectionKey key) { |
224 | 191 | cancel(key); |
225 | 191 | } |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
public void copyTo(Copyable copy) { |
231 | 4 | BaseSelectionKeyHandler copyHandler = (BaseSelectionKeyHandler) copy; |
232 | 4 | copyHandler.selectorHandler = selectorHandler; |
233 | 4 | } |
234 | |
|
235 | |
|
236 | |
public Logger getLogger() { |
237 | 2 | return logger; |
238 | |
} |
239 | |
|
240 | |
|
241 | |
public void setLogger(Logger logger) { |
242 | 0 | this.logger = logger; |
243 | 0 | } |
244 | |
|
245 | |
|
246 | |
protected void cancelKey(SelectionKey key) { |
247 | 423 | key.cancel(); |
248 | 423 | key = null; |
249 | 423 | } |
250 | |
|
251 | |
|
252 | |
protected boolean keyIsValid(SelectionKey key) { |
253 | 433 | if (key != null && key.isValid()) { |
254 | 423 | return true; |
255 | |
} else { |
256 | 10 | return false; |
257 | |
} |
258 | |
} |
259 | |
|
260 | |
|
261 | |
protected void closeChannel(SelectionKey key) { |
262 | 423 | if (selectorHandler != null) { |
263 | 423 | selectorHandler.closeChannel(key.channel()); |
264 | |
} else { |
265 | 0 | closeChannel(key.channel()); |
266 | |
} |
267 | 423 | } |
268 | |
|
269 | |
|
270 | |
protected Object clearKeyAttachment(SelectionKey key) { |
271 | 423 | return key.attach(null); |
272 | |
} |
273 | |
|
274 | |
|
275 | |
protected void closeChannel(SelectableChannel channel) { |
276 | |
try { |
277 | 0 | if (channel instanceof SocketChannel) { |
278 | 0 | Socket socket = ((SocketChannel) channel).socket(); |
279 | |
try { |
280 | 0 | if (!socket.isInputShutdown()) { |
281 | 0 | socket.shutdownInput(); |
282 | |
} |
283 | 0 | } catch (IOException ex) { |
284 | 0 | Logger.getLogger(BaseSelectionKeyHandler.class.getName()). |
285 | |
log(Level.FINE, null, ex); |
286 | 0 | } |
287 | |
try { |
288 | 0 | if (!socket.isOutputShutdown()) { |
289 | 0 | socket.shutdownOutput(); |
290 | |
} |
291 | 0 | } catch (IOException ex) { |
292 | 0 | Logger.getLogger(BaseSelectionKeyHandler.class.getName()). |
293 | |
log(Level.FINE, null, ex); |
294 | 0 | } |
295 | |
try { |
296 | 0 | socket.close(); |
297 | 0 | } catch (IOException ex) { |
298 | 0 | Logger.getLogger(BaseSelectionKeyHandler.class.getName()). |
299 | |
log(Level.FINE, null, ex); |
300 | 0 | } |
301 | |
} |
302 | 0 | channel.close(); |
303 | 0 | } catch (IOException ex) { |
304 | 0 | Logger.getLogger(BaseSelectionKeyHandler.class.getName()). |
305 | |
log(Level.FINE, null, ex); |
306 | 0 | } |
307 | 0 | } |
308 | |
} |