Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/nio/SelectorManager.java @ 943:96f60ce98949
remove Timeout
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 11 Oct 2016 02:02:48 -0600 |
parents | c157a786ed0b |
children | 1d24b6e422fa |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.io.nio; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.nio.channels.CancelledKeyException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.nio.channels.Channel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import java.nio.channels.ClosedSelectorException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import java.nio.channels.SelectableChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import java.nio.channels.SelectionKey; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import java.nio.channels.Selector; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import java.nio.channels.ServerSocketChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import java.nio.channels.SocketChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import java.util.ArrayList; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import java.util.List; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import java.util.Set; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import java.util.concurrent.ConcurrentHashMap; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import java.util.concurrent.ConcurrentLinkedQueue; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import java.util.concurrent.ConcurrentMap; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 import java.util.concurrent.CountDownLatch; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 import java.util.concurrent.TimeUnit; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 import org.eclipse.jetty.io.AsyncEndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 import org.eclipse.jetty.io.ConnectedEndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 import org.eclipse.jetty.io.Connection; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 import org.eclipse.jetty.io.EndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 import org.eclipse.jetty.util.TypeUtil; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 import org.eclipse.jetty.util.component.AbstractLifeCycle; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 import org.eclipse.jetty.util.component.AggregateLifeCycle; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 import org.eclipse.jetty.util.component.Dumpable; |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
47 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
48 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 /* ------------------------------------------------------------ */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 /** |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 * The Selector Manager manages and number of SelectSets to allow |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 * NIO scheduling to scale to large numbers of connections. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 * <p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
56 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 public abstract class SelectorManager extends AbstractLifeCycle implements Dumpable |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 { |
865 | 59 public static final Logger LOG=LoggerFactory.getLogger("org.eclipse.jetty.io.nio"); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
60 |
865 | 61 private static final int __MONITOR_PERIOD=Integer.getInteger("org.eclipse.jetty.io.nio.MONITOR_PERIOD",1000).intValue(); |
62 private static final int __MAX_SELECTS=Integer.getInteger("org.eclipse.jetty.io.nio.MAX_SELECTS",100000).intValue(); | |
63 private static final int __BUSY_PAUSE=Integer.getInteger("org.eclipse.jetty.io.nio.BUSY_PAUSE",50).intValue(); | |
64 private static final int __IDLE_TICK=Integer.getInteger("org.eclipse.jetty.io.nio.IDLE_TICK",400).intValue(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
65 |
865 | 66 private int _maxIdleTime; |
67 private long _lowResourcesConnections; | |
68 private SelectSet[] _selectSet; | |
69 private int _selectSets=1; | |
70 private volatile int _set=0; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 |
865 | 72 /* ------------------------------------------------------------ */ |
73 /** | |
74 * @param maxIdleTime The maximum period in milli seconds that a connection may be idle before it is closed. | |
75 * @see #setLowResourcesMaxIdleTime(long) | |
76 */ | |
77 public void setMaxIdleTime(long maxIdleTime) | |
78 { | |
79 _maxIdleTime=(int)maxIdleTime; | |
80 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
81 |
865 | 82 /* ------------------------------------------------------------ */ |
83 /** | |
84 * @param selectSets number of select sets to create | |
85 */ | |
86 public void setSelectSets(int selectSets) | |
87 { | |
88 long lrc = _lowResourcesConnections * _selectSets; | |
89 _selectSets=selectSets; | |
90 _lowResourcesConnections=lrc/_selectSets; | |
91 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
92 |
865 | 93 /* ------------------------------------------------------------ */ |
94 /** | |
95 * @return the max idle time | |
96 */ | |
97 public long getMaxIdleTime() | |
98 { | |
99 return _maxIdleTime; | |
100 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
101 |
865 | 102 /* ------------------------------------------------------------ */ |
103 /** | |
104 * @return the number of select sets in use | |
105 */ | |
106 public int getSelectSets() | |
107 { | |
108 return _selectSets; | |
109 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
110 |
865 | 111 /* ------------------------------------------------------------ */ |
112 /** | |
113 * @param i | |
114 * @return The select set | |
115 */ | |
116 public SelectSet getSelectSet(int i) | |
117 { | |
118 return _selectSet[i]; | |
119 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
120 |
865 | 121 /* ------------------------------------------------------------ */ |
122 /** Register a channel | |
123 * @param channel | |
124 * @param att Attached Object | |
125 */ | |
126 public void register(SocketChannel channel, Object att) | |
127 { | |
128 // The ++ increment here is not atomic, but it does not matter. | |
129 // so long as the value changes sometimes, then connections will | |
130 // be distributed over the available sets. | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
131 |
865 | 132 int s=_set++; |
133 if (s<0) | |
134 s=-s; | |
135 s=s%_selectSets; | |
136 SelectSet[] sets=_selectSet; | |
137 if (sets!=null) | |
138 { | |
139 SelectSet set=sets[s]; | |
140 set.addChange(channel,att); | |
141 set.wakeup(); | |
142 } | |
143 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
144 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
145 |
865 | 146 /* ------------------------------------------------------------ */ |
147 /** Register a channel | |
148 * @param channel | |
149 */ | |
150 public void register(SocketChannel channel) | |
151 { | |
152 // The ++ increment here is not atomic, but it does not matter. | |
153 // so long as the value changes sometimes, then connections will | |
154 // be distributed over the available sets. | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
155 |
865 | 156 int s=_set++; |
157 if (s<0) | |
158 s=-s; | |
159 s=s%_selectSets; | |
160 SelectSet[] sets=_selectSet; | |
161 if (sets!=null) | |
162 { | |
163 SelectSet set=sets[s]; | |
164 set.addChange(channel); | |
165 set.wakeup(); | |
166 } | |
167 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
168 |
865 | 169 /* ------------------------------------------------------------ */ |
170 /** Register a {@link ServerSocketChannel} | |
171 * @param acceptChannel | |
172 */ | |
173 public void register(ServerSocketChannel acceptChannel) | |
174 { | |
175 int s=_set++; | |
176 if (s<0) | |
177 s=-s; | |
178 s=s%_selectSets; | |
179 SelectSet set=_selectSet[s]; | |
180 set.addChange(acceptChannel); | |
181 set.wakeup(); | |
182 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
183 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
184 |
865 | 185 /* ------------------------------------------------------------ */ |
186 /** | |
187 * @return the lowResourcesConnections | |
188 */ | |
189 public long getLowResourcesConnections() | |
190 { | |
191 return _lowResourcesConnections*_selectSets; | |
192 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
193 |
865 | 194 /* ------------------------------------------------------------ */ |
195 /** | |
196 * Set the number of connections, which if exceeded places this manager in low resources state. | |
197 * This is not an exact measure as the connection count is averaged over the select sets. | |
198 * @param lowResourcesConnections the number of connections | |
199 * @see #setLowResourcesMaxIdleTime(long) | |
200 */ | |
201 public void setLowResourcesConnections(long lowResourcesConnections) | |
202 { | |
203 _lowResourcesConnections=(lowResourcesConnections+_selectSets-1)/_selectSets; | |
204 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
205 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
206 |
865 | 207 public abstract void execute(Runnable task); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
208 |
865 | 209 /* ------------------------------------------------------------ */ |
210 /* (non-Javadoc) | |
211 * @see org.eclipse.component.AbstractLifeCycle#doStart() | |
212 */ | |
213 @Override | |
214 protected void doStart() throws Exception | |
215 { | |
216 _selectSet = new SelectSet[_selectSets]; | |
217 for (int i=0;i<_selectSet.length;i++) | |
218 _selectSet[i]= new SelectSet(i); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
219 |
865 | 220 super.doStart(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
221 |
865 | 222 // start a thread to Select |
223 for (int i=0;i<getSelectSets();i++) | |
224 { | |
225 final int id=i; | |
226 execute(new Runnable() | |
227 { | |
228 public void run() | |
229 { | |
230 String name=Thread.currentThread().getName(); | |
231 try | |
232 { | |
233 SelectSet[] sets=_selectSet; | |
234 if (sets==null) | |
235 return; | |
236 SelectSet set=sets[id]; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
237 |
865 | 238 Thread.currentThread().setName(name+" Selector"+id); |
239 LOG.debug("Starting {} on {}",Thread.currentThread(),this); | |
240 while (isRunning()) | |
241 { | |
242 try | |
243 { | |
244 set.doSelect(); | |
245 } | |
246 catch(IOException e) | |
247 { | |
248 LOG.trace("",e); | |
249 } | |
250 catch(Exception e) | |
251 { | |
252 LOG.warn("",e); | |
253 } | |
254 } | |
255 } | |
256 finally | |
257 { | |
258 LOG.debug("Stopped {} on {}",Thread.currentThread(),this); | |
259 Thread.currentThread().setName(name); | |
260 } | |
261 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
262 |
865 | 263 }); |
264 } | |
265 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
266 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
267 |
865 | 268 /* ------------------------------------------------------------------------------- */ |
269 @Override | |
270 protected void doStop() throws Exception | |
271 { | |
272 SelectSet[] sets= _selectSet; | |
273 _selectSet=null; | |
274 if (sets!=null) | |
275 { | |
276 for (SelectSet set : sets) | |
277 { | |
278 if (set!=null) | |
279 set.stop(); | |
280 } | |
281 } | |
282 super.doStop(); | |
283 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
284 |
865 | 285 /* ------------------------------------------------------------ */ |
286 /** | |
287 * @param endpoint | |
288 */ | |
289 protected abstract void endPointClosed(SelectChannelEndPoint endpoint); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
290 |
865 | 291 /* ------------------------------------------------------------------------------- */ |
292 public abstract AsyncConnection newConnection(SocketChannel channel, AsyncEndPoint endpoint, Object attachment); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
293 |
865 | 294 /* ------------------------------------------------------------ */ |
295 /** | |
296 * Create a new end point | |
297 * @param channel | |
298 * @param selectSet | |
299 * @param sKey the selection key | |
300 * @return the new endpoint {@link SelectChannelEndPoint} | |
301 * @throws IOException | |
302 */ | |
303 protected abstract SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectorManager.SelectSet selectSet, SelectionKey sKey) throws IOException; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
304 |
865 | 305 /* ------------------------------------------------------------------------------- */ |
306 protected void connectionFailed(SocketChannel channel,Throwable ex,Object attachment) | |
307 { | |
308 LOG.warn(ex+","+channel+","+attachment); | |
309 LOG.debug("",ex); | |
310 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
311 |
865 | 312 /* ------------------------------------------------------------ */ |
313 public String dump() | |
314 { | |
315 return AggregateLifeCycle.dump(this); | |
316 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
317 |
865 | 318 /* ------------------------------------------------------------ */ |
319 public void dump(Appendable out, String indent) throws IOException | |
320 { | |
321 AggregateLifeCycle.dumpObject(out,this); | |
322 AggregateLifeCycle.dump(out,indent,TypeUtil.asList(_selectSet)); | |
323 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
324 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
325 |
865 | 326 /* ------------------------------------------------------------------------------- */ |
327 /* ------------------------------------------------------------------------------- */ | |
328 /* ------------------------------------------------------------------------------- */ | |
329 public class SelectSet implements Dumpable | |
330 { | |
331 private final int _setID; | |
943 | 332 private volatile long _now = System.currentTimeMillis(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
333 |
865 | 334 private final ConcurrentLinkedQueue<Object> _changes = new ConcurrentLinkedQueue<Object>(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
335 |
865 | 336 private volatile Selector _selector; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
337 |
865 | 338 private volatile Thread _selecting; |
339 private int _busySelects; | |
340 private long _monitorNext; | |
341 private boolean _pausing; | |
342 private boolean _paused; | |
343 private volatile long _idleTick; | |
344 private ConcurrentMap<SelectChannelEndPoint,Object> _endPoints = new ConcurrentHashMap<SelectChannelEndPoint, Object>(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
345 |
865 | 346 /* ------------------------------------------------------------ */ |
347 SelectSet(int acceptorID) throws Exception | |
348 { | |
349 _setID=acceptorID; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
350 |
865 | 351 _idleTick = System.currentTimeMillis(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
352 |
865 | 353 // create a selector; |
354 _selector = Selector.open(); | |
355 _monitorNext=System.currentTimeMillis()+__MONITOR_PERIOD; | |
356 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
357 |
865 | 358 /* ------------------------------------------------------------ */ |
359 public void addChange(Object change) | |
360 { | |
361 _changes.add(change); | |
362 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
363 |
865 | 364 /* ------------------------------------------------------------ */ |
365 public void addChange(SelectableChannel channel, Object att) | |
366 { | |
367 if (att==null) | |
368 addChange(channel); | |
369 else if (att instanceof EndPoint) | |
370 addChange(att); | |
371 else | |
372 addChange(new ChannelAndAttachment(channel,att)); | |
373 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
374 |
865 | 375 /* ------------------------------------------------------------ */ |
376 /** | |
377 * Select and dispatch tasks found from changes and the selector. | |
378 * | |
379 * @throws IOException | |
380 */ | |
381 public void doSelect() throws IOException | |
382 { | |
383 try | |
384 { | |
385 _selecting=Thread.currentThread(); | |
386 final Selector selector=_selector; | |
387 // Stopped concurrently ? | |
388 if (selector == null) | |
389 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
390 |
865 | 391 // Make any key changes required |
392 Object change; | |
393 int changes=_changes.size(); | |
394 while (changes-->0 && (change=_changes.poll())!=null) | |
395 { | |
396 Channel ch=null; | |
397 SelectionKey key=null; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
398 |
865 | 399 try |
400 { | |
401 if (change instanceof EndPoint) | |
402 { | |
403 // Update the operations for a key. | |
404 SelectChannelEndPoint endpoint = (SelectChannelEndPoint)change; | |
405 ch=endpoint.getChannel(); | |
406 endpoint.doUpdateKey(); | |
407 } | |
408 else if (change instanceof ChannelAndAttachment) | |
409 { | |
410 // finish accepting/connecting this connection | |
411 final ChannelAndAttachment asc = (ChannelAndAttachment)change; | |
412 final SelectableChannel channel=asc._channel; | |
413 ch=channel; | |
414 final Object att = asc._attachment; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
415 |
865 | 416 if ((channel instanceof SocketChannel) && ((SocketChannel)channel).isConnected()) |
417 { | |
418 key = channel.register(selector,SelectionKey.OP_READ,att); | |
419 SelectChannelEndPoint endpoint = createEndPoint((SocketChannel)channel,key); | |
420 key.attach(endpoint); | |
421 endpoint.schedule(); | |
422 } | |
423 else if (channel.isOpen()) | |
424 { | |
425 key = channel.register(selector,SelectionKey.OP_CONNECT,att); | |
426 } | |
427 } | |
428 else if (change instanceof SocketChannel) | |
429 { | |
430 // Newly registered channel | |
431 final SocketChannel channel=(SocketChannel)change; | |
432 ch=channel; | |
433 key = channel.register(selector,SelectionKey.OP_READ,null); | |
434 SelectChannelEndPoint endpoint = createEndPoint(channel,key); | |
435 key.attach(endpoint); | |
436 endpoint.schedule(); | |
437 } | |
438 else if (change instanceof ChangeTask) | |
439 { | |
440 ((Runnable)change).run(); | |
441 } | |
442 else if (change instanceof Runnable) | |
443 { | |
444 execute((Runnable)change); | |
445 } | |
446 else | |
447 throw new IllegalArgumentException(change.toString()); | |
448 } | |
449 catch (CancelledKeyException e) | |
450 { | |
451 LOG.trace("",e); | |
452 } | |
453 catch (Throwable e) | |
454 { | |
455 if (isRunning()) | |
456 LOG.warn("",e); | |
457 else | |
458 LOG.debug("",e); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
459 |
865 | 460 try |
461 { | |
462 if (ch!=null) | |
463 ch.close(); | |
464 } | |
465 catch(IOException e2) | |
466 { | |
467 LOG.debug("",e2); | |
468 } | |
469 } | |
470 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
471 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
472 |
865 | 473 // Do and instant select to see if any connections can be handled. |
474 int selected=selector.selectNow(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
475 |
943 | 476 _now = System.currentTimeMillis(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
477 |
865 | 478 // if no immediate things to do |
479 if (selected==0 && selector.selectedKeys().isEmpty()) | |
480 { | |
481 // If we are in pausing mode | |
482 if (_pausing) | |
483 { | |
484 try | |
485 { | |
486 Thread.sleep(__BUSY_PAUSE); // pause to reduce impact of busy loop | |
487 } | |
488 catch(InterruptedException e) | |
489 { | |
490 LOG.trace("",e); | |
491 } | |
943 | 492 _now = System.currentTimeMillis(); |
865 | 493 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
494 |
865 | 495 // workout how long to wait in select |
496 long wait = _changes.size()==0?__IDLE_TICK:0L; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
497 |
865 | 498 // If we should wait with a select |
499 if (wait>0) | |
500 { | |
943 | 501 long before = _now; |
865 | 502 selector.select(wait); |
943 | 503 _now = System.currentTimeMillis(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
504 |
865 | 505 // If we are monitoring for busy selector |
506 // and this select did not wait more than 1ms | |
943 | 507 if (__MONITOR_PERIOD>0 && _now-before <=1) |
865 | 508 { |
509 // count this as a busy select and if there have been too many this monitor cycle | |
510 if (++_busySelects>__MAX_SELECTS) | |
511 { | |
512 // Start injecting pauses | |
513 _pausing=true; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
514 |
865 | 515 // if this is the first pause |
516 if (!_paused) | |
517 { | |
518 // Log and dump some status | |
519 _paused=true; | |
520 LOG.warn("Selector {} is too busy, pausing!",this); | |
521 } | |
522 } | |
523 } | |
524 } | |
525 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
526 |
865 | 527 // have we been destroyed while sleeping |
528 if (_selector==null || !selector.isOpen()) | |
529 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
530 |
865 | 531 // Look for things to do |
532 for (SelectionKey key: selector.selectedKeys()) | |
533 { | |
534 SocketChannel channel=null; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
535 |
865 | 536 try |
537 { | |
538 if (!key.isValid()) | |
539 { | |
540 key.cancel(); | |
541 SelectChannelEndPoint endpoint = (SelectChannelEndPoint)key.attachment(); | |
542 if (endpoint != null) | |
543 endpoint.doUpdateKey(); | |
544 continue; | |
545 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
546 |
865 | 547 Object att = key.attachment(); |
548 if (att instanceof SelectChannelEndPoint) | |
549 { | |
550 if (key.isReadable()||key.isWritable()) | |
551 ((SelectChannelEndPoint)att).schedule(); | |
552 } | |
553 else if (key.isConnectable()) | |
554 { | |
555 // Complete a connection of a registered channel | |
556 channel = (SocketChannel)key.channel(); | |
557 boolean connected=false; | |
558 try | |
559 { | |
560 connected=channel.finishConnect(); | |
561 } | |
562 catch(Exception e) | |
563 { | |
564 connectionFailed(channel,e,att); | |
565 } | |
566 finally | |
567 { | |
568 if (connected) | |
569 { | |
570 key.interestOps(SelectionKey.OP_READ); | |
571 SelectChannelEndPoint endpoint = createEndPoint(channel,key); | |
572 key.attach(endpoint); | |
573 endpoint.schedule(); | |
574 } | |
575 else | |
576 { | |
577 key.cancel(); | |
578 channel.close(); | |
579 } | |
580 } | |
581 } | |
582 else | |
583 { | |
584 // Wrap readable registered channel in an endpoint | |
585 channel = (SocketChannel)key.channel(); | |
586 SelectChannelEndPoint endpoint = createEndPoint(channel,key); | |
587 key.attach(endpoint); | |
588 if (key.isReadable()) | |
589 endpoint.schedule(); | |
590 } | |
591 key = null; | |
592 } | |
593 catch (CancelledKeyException e) | |
594 { | |
595 LOG.trace("",e); | |
596 } | |
597 catch (Exception e) | |
598 { | |
599 if (isRunning()) | |
600 LOG.warn("",e); | |
601 else | |
602 LOG.trace("",e); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
603 |
865 | 604 try |
605 { | |
606 if (channel!=null) | |
607 channel.close(); | |
608 } | |
609 catch(IOException e2) | |
610 { | |
611 LOG.debug("",e2); | |
612 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
613 |
865 | 614 if (key != null && !(key.channel() instanceof ServerSocketChannel) && key.isValid()) |
615 key.cancel(); | |
616 } | |
617 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
618 |
865 | 619 // Everything always handled |
620 selector.selectedKeys().clear(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
621 |
943 | 622 _now = System.currentTimeMillis(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
623 |
865 | 624 // Idle tick |
943 | 625 if (_now-_idleTick>__IDLE_TICK) |
865 | 626 { |
943 | 627 _idleTick = _now; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
628 |
865 | 629 final long idle_now=((_lowResourcesConnections>0 && selector.keys().size()>_lowResourcesConnections)) |
943 | 630 ?(_now+_maxIdleTime) |
631 :_now; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
632 |
865 | 633 execute(new Runnable() |
634 { | |
635 public void run() | |
636 { | |
637 for (SelectChannelEndPoint endp:_endPoints.keySet()) | |
638 { | |
639 endp.checkIdleTimestamp(idle_now); | |
640 } | |
641 } | |
642 public String toString() {return "Idle-"+super.toString();} | |
643 }); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
644 |
865 | 645 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
646 |
865 | 647 // Reset busy select monitor counts |
943 | 648 if (__MONITOR_PERIOD>0 && _now>_monitorNext) |
865 | 649 { |
650 _busySelects=0; | |
651 _pausing=false; | |
943 | 652 _monitorNext=_now+__MONITOR_PERIOD; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
653 |
865 | 654 } |
655 } | |
656 catch (ClosedSelectorException e) | |
657 { | |
658 if (isRunning()) | |
659 LOG.warn("",e); | |
660 else | |
661 LOG.trace("",e); | |
662 } | |
663 catch (CancelledKeyException e) | |
664 { | |
665 LOG.trace("",e); | |
666 } | |
667 finally | |
668 { | |
669 _selecting=null; | |
670 } | |
671 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
672 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
673 |
865 | 674 private void renewSelector() |
675 { | |
676 try | |
677 { | |
678 synchronized (this) | |
679 { | |
680 Selector selector=_selector; | |
681 if (selector==null) | |
682 return; | |
683 final Selector new_selector = Selector.open(); | |
684 for (SelectionKey k: selector.keys()) | |
685 { | |
686 if (!k.isValid() || k.interestOps()==0) | |
687 continue; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
688 |
865 | 689 final SelectableChannel channel = k.channel(); |
690 final Object attachment = k.attachment(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
691 |
865 | 692 if (attachment==null) |
693 addChange(channel); | |
694 else | |
695 addChange(channel,attachment); | |
696 } | |
697 _selector.close(); | |
698 _selector=new_selector; | |
699 } | |
700 } | |
701 catch(IOException e) | |
702 { | |
703 throw new RuntimeException("recreating selector",e); | |
704 } | |
705 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
706 |
865 | 707 public SelectorManager getManager() |
708 { | |
709 return SelectorManager.this; | |
710 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
711 |
865 | 712 public long getNow() |
713 { | |
943 | 714 return _now; |
865 | 715 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
716 |
865 | 717 public void wakeup() |
718 { | |
719 try | |
720 { | |
721 Selector selector = _selector; | |
722 if (selector!=null) | |
723 selector.wakeup(); | |
724 } | |
725 catch(Exception e) | |
726 { | |
727 addChange(new ChangeTask() | |
728 { | |
729 public void run() | |
730 { | |
731 renewSelector(); | |
732 } | |
733 }); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
734 |
865 | 735 renewSelector(); |
736 } | |
737 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
738 |
865 | 739 private SelectChannelEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException |
740 { | |
741 SelectChannelEndPoint endp = newEndPoint(channel,this,sKey); | |
742 LOG.debug("created {}",endp); | |
743 _endPoints.put(endp,this); | |
744 return endp; | |
745 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
746 |
865 | 747 public void destroyEndPoint(SelectChannelEndPoint endp) |
748 { | |
749 LOG.debug("destroyEndPoint {}",endp); | |
750 _endPoints.remove(endp); | |
751 endPointClosed(endp); | |
752 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
753 |
865 | 754 Selector getSelector() |
755 { | |
756 return _selector; | |
757 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
758 |
865 | 759 void stop() throws Exception |
760 { | |
761 // Spin for a while waiting for selector to complete | |
762 // to avoid unneccessary closed channel exceptions | |
763 try | |
764 { | |
765 for (int i=0;i<100 && _selecting!=null;i++) | |
766 { | |
767 wakeup(); | |
768 Thread.sleep(10); | |
769 } | |
770 } | |
771 catch(Exception e) | |
772 { | |
773 LOG.trace("",e); | |
774 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
775 |
865 | 776 // close endpoints and selector |
777 synchronized (this) | |
778 { | |
779 Selector selector=_selector; | |
780 for (SelectionKey key:selector.keys()) | |
781 { | |
782 if (key==null) | |
783 continue; | |
784 Object att=key.attachment(); | |
785 if (att instanceof EndPoint) | |
786 { | |
787 EndPoint endpoint = (EndPoint)att; | |
788 try | |
789 { | |
790 endpoint.close(); | |
791 } | |
792 catch(IOException e) | |
793 { | |
794 LOG.trace("",e); | |
795 } | |
796 } | |
797 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
798 |
865 | 799 try |
800 { | |
801 selector=_selector; | |
802 if (selector != null) | |
803 selector.close(); | |
804 } | |
805 catch (IOException e) | |
806 { | |
807 LOG.trace("",e); | |
808 } | |
809 _selector=null; | |
810 } | |
811 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
812 |
865 | 813 public String dump() |
814 { | |
815 return AggregateLifeCycle.dump(this); | |
816 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
817 |
865 | 818 public void dump(Appendable out, String indent) throws IOException |
819 { | |
820 out.append(String.valueOf(this)).append(" id=").append(String.valueOf(_setID)).append("\n"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
821 |
865 | 822 Thread selecting = _selecting; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
823 |
865 | 824 Object where = "not selecting"; |
825 StackTraceElement[] trace =selecting==null?null:selecting.getStackTrace(); | |
826 if (trace!=null) | |
827 { | |
828 for (StackTraceElement t:trace) | |
829 if (t.getClassName().startsWith("org.eclipse.jetty.")) | |
830 { | |
831 where=t; | |
832 break; | |
833 } | |
834 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
835 |
865 | 836 Selector selector=_selector; |
837 if (selector!=null) | |
838 { | |
839 final ArrayList<Object> dump = new ArrayList<Object>(selector.keys().size()*2); | |
840 dump.add(where); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
841 |
865 | 842 final CountDownLatch latch = new CountDownLatch(1); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
843 |
865 | 844 addChange(new ChangeTask() |
845 { | |
846 public void run() | |
847 { | |
848 dumpKeyState(dump); | |
849 latch.countDown(); | |
850 } | |
851 }); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
852 |
865 | 853 try |
854 { | |
855 latch.await(5,TimeUnit.SECONDS); | |
856 } | |
857 catch(InterruptedException e) | |
858 { | |
859 LOG.trace("",e); | |
860 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
861 |
865 | 862 AggregateLifeCycle.dump(out,indent,dump); |
863 } | |
864 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
865 |
865 | 866 public void dumpKeyState(List<Object> dumpto) |
867 { | |
868 Selector selector=_selector; | |
869 Set<SelectionKey> keys = selector.keys(); | |
870 dumpto.add(selector + " keys=" + keys.size()); | |
871 for (SelectionKey key: keys) | |
872 { | |
873 if (key.isValid()) | |
874 dumpto.add(key.attachment()+" iOps="+key.interestOps()+" rOps="+key.readyOps()); | |
875 else | |
876 dumpto.add(key.attachment()+" iOps=-1 rOps=-1"); | |
877 } | |
878 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
879 |
865 | 880 public String toString() |
881 { | |
882 Selector selector=_selector; | |
883 return String.format("%s keys=%d selected=%d", | |
884 super.toString(), | |
885 selector != null && selector.isOpen() ? selector.keys().size() : -1, | |
886 selector != null && selector.isOpen() ? selector.selectedKeys().size() : -1); | |
887 } | |
888 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
889 |
865 | 890 private static class ChannelAndAttachment |
891 { | |
892 final SelectableChannel _channel; | |
893 final Object _attachment; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
894 |
865 | 895 public ChannelAndAttachment(SelectableChannel channel, Object attachment) |
896 { | |
897 super(); | |
898 _channel = channel; | |
899 _attachment = attachment; | |
900 } | |
901 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
902 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
903 |
865 | 904 private interface ChangeTask extends Runnable |
905 {} | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
906 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
907 } |