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