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.SelectionKeyAttachment; |
43 | |
import java.nio.channels.ClosedChannelException; |
44 | |
import java.nio.channels.SelectableChannel; |
45 | |
import java.nio.channels.SelectionKey; |
46 | |
import java.nio.channels.Selector; |
47 | |
import java.util.Iterator; |
48 | |
import java.util.logging.Level; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public class DefaultSelectionKeyHandler extends BaseSelectionKeyHandler { |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 130 | protected long nextKeysExpiration = 0; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | 130 | protected long timeout = 30 * 1000L; |
74 | |
|
75 | |
|
76 | 127 | public DefaultSelectionKeyHandler() { |
77 | 127 | } |
78 | |
|
79 | |
|
80 | |
public DefaultSelectionKeyHandler(SelectorHandler selectorHandler) { |
81 | 3 | super(selectorHandler); |
82 | 3 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
@Override |
89 | |
public void copyTo(Copyable copy) { |
90 | 4 | super.copyTo(copy); |
91 | 4 | DefaultSelectionKeyHandler copyHandler = (DefaultSelectionKeyHandler) copy; |
92 | 4 | copyHandler.timeout = timeout; |
93 | 4 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
@Override |
99 | |
public void process(SelectionKey key) { |
100 | 114227 | super.process(key); |
101 | 114227 | removeExpirationStamp(key); |
102 | 114227 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
@Override |
108 | |
public void postProcess(SelectionKey key) { |
109 | 113225 | super.postProcess(key); |
110 | 113225 | addExpirationStamp(key); |
111 | 113225 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
@Override |
118 | |
public void register(Iterator<SelectionKey> keyIterator, int selectionKeyOps) { |
119 | 0 | long currentTime = System.currentTimeMillis(); |
120 | |
SelectionKey key; |
121 | 0 | while (keyIterator.hasNext()) { |
122 | 0 | key = keyIterator.next(); |
123 | 0 | keyIterator.remove(); |
124 | 0 | doRegisterKey(key, selectionKeyOps, currentTime); |
125 | |
} |
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
@Override |
133 | |
public void register(SelectionKey key, int selectionKeyOps) { |
134 | 376824 | doRegisterKey(key, selectionKeyOps, System.currentTimeMillis()); |
135 | 376824 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
protected void doRegisterKey(SelectionKey key, int selectionKeyOps, |
141 | |
long currentTime) { |
142 | 376826 | if (!key.isValid()) { |
143 | 0 | return; |
144 | |
} |
145 | |
|
146 | 376826 | key.interestOps(key.interestOps() | selectionKeyOps); |
147 | 376826 | Object attachment = key.attachment(); |
148 | |
|
149 | 376826 | if (attachment == null) { |
150 | 3525 | key.attach(currentTime); |
151 | |
} |
152 | 376826 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
@Override |
158 | |
public void register(SelectableChannel channel, int ops) |
159 | |
throws ClosedChannelException { |
160 | 32 | if (!channel.isOpen()) { |
161 | 0 | return; |
162 | |
} |
163 | |
|
164 | 32 | Selector selector = selectorHandler.getSelector(); |
165 | 32 | SelectionKey key = channel.keyFor(selector); |
166 | 32 | long time = System.currentTimeMillis(); |
167 | |
|
168 | 32 | if (key == null) { |
169 | 30 | key = channel.register(selector, ops, time); |
170 | |
} else { |
171 | 2 | doRegisterKey(key, ops, time); |
172 | |
} |
173 | 32 | } |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
@Override |
179 | |
@SuppressWarnings("empty-statement") |
180 | |
public void register(SelectionKey key, long currentTime){ |
181 | |
; |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
@Override |
189 | |
@SuppressWarnings("empty-statement") |
190 | |
public void expire(SelectionKey key, long currentTime) { |
191 | |
; |
192 | 0 | } |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
@Override |
199 | |
public void expire(Iterator<SelectionKey> iterator) { |
200 | 200267 | if (timeout <= 0) return; |
201 | |
|
202 | 200267 | long currentTime = System.currentTimeMillis(); |
203 | 200267 | if (currentTime < nextKeysExpiration) { |
204 | 200181 | return; |
205 | |
} |
206 | 86 | nextKeysExpiration = currentTime + timeout; |
207 | |
|
208 | |
|
209 | |
SelectionKey key; |
210 | 194 | while (iterator.hasNext()) { |
211 | 108 | key = iterator.next(); |
212 | |
|
213 | 108 | if (!key.isValid()){ |
214 | 0 | continue; |
215 | |
} |
216 | |
|
217 | |
|
218 | 108 | Long expire = getExpirationStamp(key); |
219 | 108 | if (expire != null){ |
220 | 29 | if (currentTime - expire >= timeout) { |
221 | 0 | cancel(key); |
222 | 29 | } else if (expire + timeout < nextKeysExpiration) { |
223 | 4 | nextKeysExpiration = expire + timeout; |
224 | |
} |
225 | |
} |
226 | 108 | } |
227 | 86 | } |
228 | |
|
229 | |
|
230 | |
public long getTimeout() { |
231 | 0 | return timeout; |
232 | |
} |
233 | |
|
234 | |
|
235 | |
public void setTimeout(long timeout) { |
236 | 0 | this.timeout = timeout; |
237 | 0 | } |
238 | |
|
239 | |
|
240 | |
@Override |
241 | |
protected Object clearKeyAttachment(SelectionKey key) { |
242 | 400 | Object attachment = super.clearKeyAttachment(key); |
243 | 400 | if (attachment instanceof SelectionKeyAttachment) { |
244 | 292 | ((SelectionKeyAttachment) attachment).release(key); |
245 | |
} |
246 | 400 | return attachment; |
247 | |
} |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
private void removeExpirationStamp(SelectionKey key) { |
257 | 114227 | Object attachment = key.attachment(); |
258 | 114227 | if (attachment != null) { |
259 | 114223 | if (attachment instanceof Long) { |
260 | 8723 | key.attach(null); |
261 | 105500 | } else if (attachment instanceof SelectionKeyAttachment) { |
262 | 105500 | ((SelectionKeyAttachment) attachment).setTimeout(null); |
263 | |
} |
264 | |
} |
265 | 114227 | } |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
private void addExpirationStamp(SelectionKey key) { |
274 | 113225 | long currentTime = System.currentTimeMillis(); |
275 | 113225 | Object attachment = key.attachment(); |
276 | 113225 | if (attachment == null) { |
277 | 5263 | key.attach(currentTime); |
278 | 107962 | } else if (attachment instanceof SelectionKeyAttachment) { |
279 | 105500 | ((SelectionKeyAttachment) attachment).setTimeout(currentTime); |
280 | |
} |
281 | 113225 | } |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
private Long getExpirationStamp(SelectionKey key) { |
290 | 108 | Object attachment = key.attachment(); |
291 | 108 | if (attachment != null) { |
292 | |
try { |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | 66 | if (attachment instanceof Long) { |
299 | 26 | return (Long) attachment; |
300 | 40 | } else if (attachment instanceof SelectionKeyAttachment) { |
301 | 40 | return ((SelectionKeyAttachment) attachment).getTimeout(); |
302 | |
} |
303 | 0 | } catch (ClassCastException ex) { |
304 | 0 | if (logger.isLoggable(Level.FINEST)) { |
305 | 0 | logger.log(Level.FINEST, |
306 | |
"Invalid SelectionKey attachment", ex); |
307 | |
} |
308 | 0 | } |
309 | |
} |
310 | |
|
311 | 42 | return null; |
312 | |
} |
313 | |
} |