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