Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/nio/SelectorManager.java @ 953:7db4a488fc82
simplify SelectorManager
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 12 Oct 2016 22:16:36 -0600 |
parents | 669769bcdf5c |
children | a021c4c9c244 |
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.ServerSocketChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import java.nio.channels.SocketChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import java.util.ArrayList; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import java.util.List; |
944
1d24b6e422fa
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
943
diff
changeset
|
31 import java.util.Collections; |
802
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 int _maxIdleTime; |
62 private long _lowResourcesConnections; | |
63 private SelectSet[] _selectSet; | |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
64 private int _selectSets = 1; |
865 | 65 private volatile int _set=0; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
66 |
865 | 67 /* ------------------------------------------------------------ */ |
68 /** | |
69 * @param maxIdleTime The maximum period in milli seconds that a connection may be idle before it is closed. | |
70 * @see #setLowResourcesMaxIdleTime(long) | |
71 */ | |
951
e542a9cc75ef
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
950
diff
changeset
|
72 public void setMaxIdleTime(int maxIdleTime) |
865 | 73 { |
951
e542a9cc75ef
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
950
diff
changeset
|
74 _maxIdleTime = maxIdleTime; |
865 | 75 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
76 |
865 | 77 /* ------------------------------------------------------------ */ |
78 /** | |
79 * @param selectSets number of select sets to create | |
80 */ | |
81 public void setSelectSets(int selectSets) | |
82 { | |
83 long lrc = _lowResourcesConnections * _selectSets; | |
84 _selectSets=selectSets; | |
85 _lowResourcesConnections=lrc/_selectSets; | |
86 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
87 |
865 | 88 /* ------------------------------------------------------------ */ |
89 /** Register a channel | |
90 * @param channel | |
91 */ | |
92 public void register(SocketChannel channel) | |
93 { | |
94 // The ++ increment here is not atomic, but it does not matter. | |
95 // so long as the value changes sometimes, then connections will | |
96 // be distributed over the available sets. | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
97 |
948
f5aefdc4a81a
simplify SelectChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents:
944
diff
changeset
|
98 int s = _set++; |
865 | 99 if (s<0) |
100 s=-s; | |
101 s=s%_selectSets; | |
948
f5aefdc4a81a
simplify SelectChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents:
944
diff
changeset
|
102 SelectSet[] sets = _selectSet; |
865 | 103 if (sets!=null) |
104 { | |
105 SelectSet set=sets[s]; | |
106 set.addChange(channel); | |
107 } | |
108 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
109 |
865 | 110 /* ------------------------------------------------------------ */ |
111 /** | |
112 * @return the lowResourcesConnections | |
113 */ | |
114 public long getLowResourcesConnections() | |
115 { | |
116 return _lowResourcesConnections*_selectSets; | |
117 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
118 |
865 | 119 /* ------------------------------------------------------------ */ |
120 /** | |
121 * Set the number of connections, which if exceeded places this manager in low resources state. | |
122 * This is not an exact measure as the connection count is averaged over the select sets. | |
123 * @param lowResourcesConnections the number of connections | |
124 * @see #setLowResourcesMaxIdleTime(long) | |
125 */ | |
126 public void setLowResourcesConnections(long lowResourcesConnections) | |
127 { | |
128 _lowResourcesConnections=(lowResourcesConnections+_selectSets-1)/_selectSets; | |
129 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
130 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
131 |
865 | 132 public abstract void execute(Runnable task); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
133 |
865 | 134 /* ------------------------------------------------------------ */ |
135 /* (non-Javadoc) | |
136 * @see org.eclipse.component.AbstractLifeCycle#doStart() | |
137 */ | |
138 @Override | |
139 protected void doStart() throws Exception | |
140 { | |
141 _selectSet = new SelectSet[_selectSets]; | |
142 for (int i=0;i<_selectSet.length;i++) | |
143 _selectSet[i]= new SelectSet(i); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
144 |
865 | 145 super.doStart(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
146 |
865 | 147 // start a thread to Select |
948
f5aefdc4a81a
simplify SelectChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents:
944
diff
changeset
|
148 for (int i=0;i<_selectSets;i++) |
865 | 149 { |
150 final int id=i; | |
151 execute(new Runnable() | |
152 { | |
153 public void run() | |
154 { | |
155 String name=Thread.currentThread().getName(); | |
156 try | |
157 { | |
158 SelectSet[] sets=_selectSet; | |
159 if (sets==null) | |
160 return; | |
161 SelectSet set=sets[id]; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
162 |
865 | 163 Thread.currentThread().setName(name+" Selector"+id); |
164 LOG.debug("Starting {} on {}",Thread.currentThread(),this); | |
165 while (isRunning()) | |
166 { | |
167 try | |
168 { | |
169 set.doSelect(); | |
170 } | |
171 catch(IOException e) | |
172 { | |
173 LOG.trace("",e); | |
174 } | |
175 catch(Exception e) | |
176 { | |
177 LOG.warn("",e); | |
178 } | |
179 } | |
180 } | |
181 finally | |
182 { | |
183 LOG.debug("Stopped {} on {}",Thread.currentThread(),this); | |
184 Thread.currentThread().setName(name); | |
185 } | |
186 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
187 |
865 | 188 }); |
189 } | |
190 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
191 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
192 |
865 | 193 /* ------------------------------------------------------------------------------- */ |
194 @Override | |
195 protected void doStop() throws Exception | |
196 { | |
197 SelectSet[] sets= _selectSet; | |
198 _selectSet=null; | |
199 if (sets!=null) | |
200 { | |
201 for (SelectSet set : sets) | |
202 { | |
203 if (set!=null) | |
204 set.stop(); | |
205 } | |
206 } | |
207 super.doStop(); | |
208 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
209 |
952
669769bcdf5c
simplify AsyncEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
951
diff
changeset
|
210 public abstract AsyncConnection newConnection(SocketChannel channel, SelectChannelEndPoint endpoint, Object attachment); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
211 |
865 | 212 public String dump() |
213 { | |
214 return AggregateLifeCycle.dump(this); | |
215 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
216 |
865 | 217 public void dump(Appendable out, String indent) throws IOException |
218 { | |
219 AggregateLifeCycle.dumpObject(out,this); | |
220 AggregateLifeCycle.dump(out,indent,TypeUtil.asList(_selectSet)); | |
221 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
222 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
223 |
865 | 224 public class SelectSet implements Dumpable |
225 { | |
226 private final int _setID; | |
943 | 227 private volatile long _now = System.currentTimeMillis(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
228 |
950 | 229 private volatile SaneSelector _selector; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
230 |
865 | 231 private volatile Thread _selecting; |
232 private ConcurrentMap<SelectChannelEndPoint,Object> _endPoints = new ConcurrentHashMap<SelectChannelEndPoint, Object>(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
233 |
865 | 234 SelectSet(int acceptorID) throws Exception |
235 { | |
236 _setID=acceptorID; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
237 |
865 | 238 // create a selector; |
950 | 239 _selector = new SaneSelector(); |
865 | 240 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
241 |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
242 private void addChange(SocketChannel channel) |
865 | 243 { |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
244 try { |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
245 //System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqq a"); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
246 // SelectionKey key = _selector.register(channel,SelectionKey.OP_READ,null); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
247 SelectionKey key = _selector.register(channel,0,null); |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
248 SelectChannelEndPoint endpoint = createEndPoint(channel,key); |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
249 key.attach(endpoint); |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
250 key.interestOps(SelectionKey.OP_READ); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
251 _selector.update(); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
252 //System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqq b"); |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
253 endpoint.schedule(); |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
254 } catch(IOException e) { |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
255 LOG.warn("",e); |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
256 try { |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
257 channel.close(); |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
258 } catch(IOException e2) { |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
259 LOG.warn("",e2); |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
260 } |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
261 } |
865 | 262 } |
263 /* ------------------------------------------------------------ */ | |
264 /** | |
265 * Select and dispatch tasks found from changes and the selector. | |
266 * | |
267 * @throws IOException | |
268 */ | |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
269 private void doSelect() throws IOException |
865 | 270 { |
271 try | |
272 { | |
273 _selecting=Thread.currentThread(); | |
950 | 274 final SaneSelector selector = _selector; |
865 | 275 // Stopped concurrently ? |
276 if (selector == null) | |
277 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
278 |
951
e542a9cc75ef
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
950
diff
changeset
|
279 selector.select(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
280 |
865 | 281 // Look for things to do |
282 for (SelectionKey key: selector.selectedKeys()) | |
283 { | |
284 try | |
285 { | |
286 if (!key.isValid()) | |
287 { | |
288 key.cancel(); | |
289 SelectChannelEndPoint endpoint = (SelectChannelEndPoint)key.attachment(); | |
290 if (endpoint != null) | |
291 endpoint.doUpdateKey(); | |
292 continue; | |
293 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
294 |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
295 if (key.isReadable()||key.isWritable()) { |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
296 SelectChannelEndPoint endpoint = (SelectChannelEndPoint)key.attachment(); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
297 endpoint.schedule(); |
865 | 298 } |
299 key = null; | |
300 } | |
301 catch (CancelledKeyException e) | |
302 { | |
303 LOG.trace("",e); | |
304 } | |
305 catch (Exception e) | |
306 { | |
307 if (isRunning()) | |
308 LOG.warn("",e); | |
309 else | |
310 LOG.trace("",e); | |
311 } | |
312 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
313 |
865 | 314 // Everything always handled |
315 selector.selectedKeys().clear(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
316 |
943 | 317 _now = System.currentTimeMillis(); |
865 | 318 } |
319 catch (ClosedSelectorException e) | |
320 { | |
321 if (isRunning()) | |
322 LOG.warn("",e); | |
323 else | |
324 LOG.trace("",e); | |
325 } | |
326 catch (CancelledKeyException e) | |
327 { | |
328 LOG.trace("",e); | |
329 } | |
330 finally | |
331 { | |
332 _selecting=null; | |
333 } | |
334 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
335 |
865 | 336 public SelectorManager getManager() |
337 { | |
338 return SelectorManager.this; | |
339 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
340 |
865 | 341 public long getNow() |
342 { | |
943 | 343 return _now; |
865 | 344 } |
951
e542a9cc75ef
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
950
diff
changeset
|
345 |
865 | 346 private SelectChannelEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException |
347 { | |
948
f5aefdc4a81a
simplify SelectChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents:
944
diff
changeset
|
348 SelectChannelEndPoint endp = new SelectChannelEndPoint(channel,this,sKey, _maxIdleTime); |
f5aefdc4a81a
simplify SelectChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents:
944
diff
changeset
|
349 endp.setConnection(getManager().newConnection(channel,endp, sKey.attachment())); |
865 | 350 LOG.debug("created {}",endp); |
351 _endPoints.put(endp,this); | |
352 return endp; | |
353 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
354 |
865 | 355 public void destroyEndPoint(SelectChannelEndPoint endp) |
356 { | |
357 LOG.debug("destroyEndPoint {}",endp); | |
358 _endPoints.remove(endp); | |
948
f5aefdc4a81a
simplify SelectChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents:
944
diff
changeset
|
359 endp.getConnection().onClose(); |
865 | 360 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
361 |
950 | 362 SaneSelector getSelector() |
865 | 363 { |
364 return _selector; | |
365 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
366 |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
367 synchronized void stop() throws Exception |
865 | 368 { |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
369 // close endpoints and selector |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
370 for (SelectionKey key : _selector.keys()) |
865 | 371 { |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
372 Object att=key.attachment(); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
373 if (att instanceof EndPoint) |
865 | 374 { |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
375 EndPoint endpoint = (EndPoint)att; |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
376 try |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
377 { |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
378 endpoint.close(); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
379 } |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
380 catch(IOException e) |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
381 { |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
382 LOG.trace("",e); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
383 } |
865 | 384 } |
385 } | |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
386 |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
387 try |
865 | 388 { |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
389 _selector.close(); |
865 | 390 } |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
391 catch (IOException e) |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
392 { |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
393 LOG.trace("",e); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
394 } |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
395 _selector = null; |
865 | 396 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
397 |
865 | 398 public String dump() |
399 { | |
400 return AggregateLifeCycle.dump(this); | |
401 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
402 |
865 | 403 public void dump(Appendable out, String indent) throws IOException |
404 { | |
405 out.append(String.valueOf(this)).append(" id=").append(String.valueOf(_setID)).append("\n"); | |
944
1d24b6e422fa
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
943
diff
changeset
|
406 AggregateLifeCycle.dump(out,indent,Collections.emptyList()); |
865 | 407 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
408 |
865 | 409 public String toString() |
410 { | |
950 | 411 SaneSelector selector=_selector; |
865 | 412 return String.format("%s keys=%d selected=%d", |
413 super.toString(), | |
414 selector != null && selector.isOpen() ? selector.keys().size() : -1, | |
415 selector != null && selector.isOpen() ? selector.selectedKeys().size() : -1); | |
416 } | |
417 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
418 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
419 } |