Mercurial Hosting > luan
comparison src/org/eclipse/jetty/server/nio/SelectChannelConnector.java @ 909:c60c1adfac3e
simplify connectors
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 08 Oct 2016 21:15:28 -0600 |
parents | bc4e299de953 |
children | 1d0c304e12b5 |
comparison
equal
deleted
inserted
replaced
908:e003b20780c4 | 909:c60c1adfac3e |
---|---|
65 * | 65 * |
66 * @org.apache.xbean.XBean element="nioConnector" description="Creates an NIO based socket connector" | 66 * @org.apache.xbean.XBean element="nioConnector" description="Creates an NIO based socket connector" |
67 */ | 67 */ |
68 public class SelectChannelConnector extends Connector | 68 public class SelectChannelConnector extends Connector |
69 { | 69 { |
70 private transient ServerSocketChannel _acceptChannel; | |
71 private int _lowResourcesConnections; | 70 private int _lowResourcesConnections; |
72 private int _lowResourcesMaxIdleTime; | 71 private int _lowResourcesMaxIdleTime; |
73 private int _localPort=-1; | |
74 | 72 |
75 private final SelectorManager _manager = new ConnectorSelectorManager(); | 73 private final SelectorManager _manager = new ConnectorSelectorManager(); |
76 | 74 |
77 /* ------------------------------------------------------------------------------- */ | |
78 /** | |
79 * Constructor. | |
80 * | |
81 */ | |
82 public SelectChannelConnector(Server server,int port) | 75 public SelectChannelConnector(Server server,int port) |
83 { | 76 { |
84 super(server,port); | 77 super(server,port); |
85 _manager.setMaxIdleTime(getMaxIdleTime()); | 78 _manager.setMaxIdleTime(getMaxIdleTime()); |
86 addBean(_manager,true); | 79 addBean(_manager,true); |
87 setAcceptors(Math.max(1,(Runtime.getRuntime().availableProcessors()+3)/4)); | 80 setAcceptors(Math.max(1,(Runtime.getRuntime().availableProcessors()+3)/4)); |
88 } | 81 } |
89 | 82 |
90 /* ------------------------------------------------------------ */ | |
91 @Override | 83 @Override |
92 public final void accept() throws IOException | 84 public final void accept() throws IOException |
93 { | 85 { |
94 ServerSocketChannel server = _acceptChannel; | 86 ServerSocketChannel server = _acceptChannel; |
95 | 87 |
101 configure(socket); | 93 configure(socket); |
102 _manager.register(channel); | 94 _manager.register(channel); |
103 } | 95 } |
104 } | 96 } |
105 | 97 |
106 /* ------------------------------------------------------------ */ | 98 public synchronized void close() throws IOException |
107 public void close() throws IOException | 99 { |
108 { | 100 if (_acceptChannel != null) |
109 synchronized(this) | 101 { |
110 { | 102 removeBean(_acceptChannel); |
111 if (_acceptChannel != null) | 103 if (_acceptChannel.isOpen()) |
112 { | 104 _acceptChannel.close(); |
113 removeBean(_acceptChannel); | 105 } |
114 if (_acceptChannel.isOpen()) | 106 _acceptChannel = null; |
115 _acceptChannel.close(); | 107 } |
116 } | 108 |
117 _acceptChannel = null; | |
118 _localPort=-2; | |
119 } | |
120 } | |
121 | |
122 /* ------------------------------------------------------------------------------- */ | |
123 @Override | 109 @Override |
124 public void customize(EndPoint endpoint, Request request) throws IOException | 110 public void customize(EndPoint endpoint, Request request) throws IOException |
125 { | 111 { |
126 request.setTimeStamp(System.currentTimeMillis()); | 112 request.setTimeStamp(System.currentTimeMillis()); |
127 endpoint.setMaxIdleTime(_maxIdleTime); | 113 endpoint.setMaxIdleTime(_maxIdleTime); |
128 super.customize(endpoint, request); | 114 super.customize(endpoint, request); |
129 } | 115 } |
130 | 116 |
131 /* ------------------------------------------------------------------------------- */ | |
132 @Override | 117 @Override |
133 public void persist(EndPoint endpoint) throws IOException | 118 public void persist(EndPoint endpoint) throws IOException |
134 { | 119 { |
135 AsyncEndPoint aEndp = ((AsyncEndPoint)endpoint); | 120 AsyncEndPoint aEndp = ((AsyncEndPoint)endpoint); |
136 aEndp.setCheckForIdle(true); | 121 aEndp.setCheckForIdle(true); |
137 super.persist(endpoint); | 122 super.persist(endpoint); |
138 } | 123 } |
139 | 124 |
140 /* ------------------------------------------------------------ */ | |
141 public SelectorManager getSelectorManager() | 125 public SelectorManager getSelectorManager() |
142 { | 126 { |
143 return _manager; | 127 return _manager; |
144 } | 128 } |
145 | 129 |
146 /* ------------------------------------------------------------ */ | |
147 public synchronized Object getConnection() | |
148 { | |
149 return _acceptChannel; | |
150 } | |
151 | |
152 /* ------------------------------------------------------------------------------- */ | |
153 public int getLocalPort() | |
154 { | |
155 synchronized(this) | |
156 { | |
157 return _localPort; | |
158 } | |
159 } | |
160 | |
161 /* ------------------------------------------------------------ */ | |
162 public void open() throws IOException | |
163 { | |
164 synchronized(this) | |
165 { | |
166 if (_acceptChannel == null) | |
167 { | |
168 // Create a new server socket | |
169 _acceptChannel = ServerSocketChannel.open(); | |
170 // Set to blocking mode | |
171 _acceptChannel.configureBlocking(true); | |
172 | |
173 // Bind the server socket to the local host and port | |
174 _acceptChannel.socket().setReuseAddress(true); | |
175 InetSocketAddress addr = getHost()==null?new InetSocketAddress(port):new InetSocketAddress(getHost(),port); | |
176 _acceptChannel.socket().bind(addr); | |
177 | |
178 _localPort=_acceptChannel.socket().getLocalPort(); | |
179 if (_localPort<=0) | |
180 throw new IOException("Server channel not bound"); | |
181 | |
182 addBean(_acceptChannel); | |
183 } | |
184 } | |
185 } | |
186 | |
187 /* ------------------------------------------------------------ */ | |
188 @Override | 130 @Override |
189 public void setMaxIdleTime(int maxIdleTime) | 131 public void setMaxIdleTime(int maxIdleTime) |
190 { | 132 { |
191 _manager.setMaxIdleTime(maxIdleTime); | 133 _manager.setMaxIdleTime(maxIdleTime); |
192 super.setMaxIdleTime(maxIdleTime); | 134 super.setMaxIdleTime(maxIdleTime); |
193 } | 135 } |
194 | 136 |
195 /* ------------------------------------------------------------ */ | |
196 /** | |
197 * @return the lowResourcesConnections | |
198 */ | |
199 public int getLowResourcesConnections() | 137 public int getLowResourcesConnections() |
200 { | 138 { |
201 return _lowResourcesConnections; | 139 return _lowResourcesConnections; |
202 } | 140 } |
203 | 141 |
237 _lowResourcesMaxIdleTime=lowResourcesMaxIdleTime; | 175 _lowResourcesMaxIdleTime=lowResourcesMaxIdleTime; |
238 super.setLowResourcesMaxIdleTime(lowResourcesMaxIdleTime); | 176 super.setLowResourcesMaxIdleTime(lowResourcesMaxIdleTime); |
239 } | 177 } |
240 | 178 |
241 | 179 |
242 /* ------------------------------------------------------------ */ | 180 @Override |
243 /* | 181 protected synchronized void doStart() throws Exception |
244 * @see org.eclipse.jetty.server.server.AbstractConnector#doStart() | |
245 */ | |
246 @Override | |
247 protected void doStart() throws Exception | |
248 { | 182 { |
249 _manager.setSelectSets(getAcceptors()); | 183 _manager.setSelectSets(getAcceptors()); |
250 _manager.setMaxIdleTime(getMaxIdleTime()); | 184 _manager.setMaxIdleTime(getMaxIdleTime()); |
251 _manager.setLowResourcesConnections(getLowResourcesConnections()); | 185 _manager.setLowResourcesConnections(getLowResourcesConnections()); |
252 _manager.setLowResourcesMaxIdleTime(getLowResourcesMaxIdleTime()); | 186 _manager.setLowResourcesMaxIdleTime(getLowResourcesMaxIdleTime()); |
253 | 187 |
188 if (_acceptChannel == null) | |
189 { | |
190 // Create a new server socket | |
191 _acceptChannel = ServerSocketChannel.open(); | |
192 // Set to blocking mode | |
193 _acceptChannel.configureBlocking(true); | |
194 | |
195 // Bind the server socket to the local host and port | |
196 _acceptChannel.socket().setReuseAddress(true); | |
197 InetSocketAddress addr = getHost()==null?new InetSocketAddress(port):new InetSocketAddress(getHost(),port); | |
198 _acceptChannel.bind(addr); | |
199 | |
200 if( _acceptChannel.socket().getLocalPort() != port) | |
201 throw new IOException("Server channel not bound"); | |
202 | |
203 addBean(_acceptChannel); | |
204 } | |
205 | |
254 super.doStart(); | 206 super.doStart(); |
255 } | 207 } |
256 | 208 |
257 /* ------------------------------------------------------------ */ | |
258 protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key) throws IOException | 209 protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key) throws IOException |
259 { | 210 { |
260 SelectChannelEndPoint endp= new SelectChannelEndPoint(channel,selectSet,key, SelectChannelConnector.this._maxIdleTime); | 211 SelectChannelEndPoint endp= new SelectChannelEndPoint(channel,selectSet,key, SelectChannelConnector.this._maxIdleTime); |
261 endp.setConnection(selectSet.getManager().newConnection(channel,endp, key.attachment())); | 212 endp.setConnection(selectSet.getManager().newConnection(channel,endp, key.attachment())); |
262 return endp; | 213 return endp; |
263 } | 214 } |
264 | 215 |
265 /* ------------------------------------------------------------------------------- */ | |
266 protected void endPointClosed(SelectChannelEndPoint endpoint) | 216 protected void endPointClosed(SelectChannelEndPoint endpoint) |
267 { | 217 { |
268 endpoint.getConnection().onClose(); | 218 endpoint.getConnection().onClose(); |
269 } | 219 } |
270 | 220 |
271 /* ------------------------------------------------------------------------------- */ | |
272 protected AsyncConnection newConnection(SocketChannel channel,final AsyncEndPoint endpoint) | 221 protected AsyncConnection newConnection(SocketChannel channel,final AsyncEndPoint endpoint) |
273 { | 222 { |
274 return new AsyncHttpConnection(SelectChannelConnector.this,endpoint,server); | 223 return new AsyncHttpConnection(SelectChannelConnector.this,endpoint,server); |
275 } | 224 } |
276 | 225 |
277 | 226 |
278 /* ------------------------------------------------------------ */ | |
279 /* ------------------------------------------------------------ */ | |
280 /* ------------------------------------------------------------ */ | |
281 private final class ConnectorSelectorManager extends SelectorManager | 227 private final class ConnectorSelectorManager extends SelectorManager |
282 { | 228 { |
283 @Override | 229 @Override |
284 public void execute(Runnable task) | 230 public void execute(Runnable task) |
285 { | 231 { |