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