1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.server;
20
21 import java.io.IOException;
22 import java.net.InetSocketAddress;
23 import java.net.ServerSocket;
24 import java.net.Socket;
25 import java.net.SocketException;
26 import java.nio.channels.Channel;
27 import java.nio.channels.SelectionKey;
28 import java.nio.channels.Selector;
29 import java.nio.channels.ServerSocketChannel;
30 import java.nio.channels.SocketChannel;
31 import java.util.concurrent.Executor;
32 import java.util.concurrent.Future;
33
34 import org.eclipse.jetty.io.ByteBufferPool;
35 import org.eclipse.jetty.io.Connection;
36 import org.eclipse.jetty.io.EndPoint;
37 import org.eclipse.jetty.io.SelectChannelEndPoint;
38 import org.eclipse.jetty.io.SelectorManager;
39 import org.eclipse.jetty.io.SelectorManager.ManagedSelector;
40 import org.eclipse.jetty.util.Callback;
41 import org.eclipse.jetty.util.annotation.ManagedAttribute;
42 import org.eclipse.jetty.util.annotation.ManagedObject;
43 import org.eclipse.jetty.util.annotation.Name;
44 import org.eclipse.jetty.util.ssl.SslContextFactory;
45 import org.eclipse.jetty.util.thread.Scheduler;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 @ManagedObject("HTTP connector using NIO ByteChannels and Selectors")
80 public class ServerConnector extends AbstractNetworkConnector
81 {
82 private final SelectorManager _manager;
83 private volatile ServerSocketChannel _acceptChannel;
84 private volatile boolean _inheritChannel = false;
85 private volatile int _localPort = -1;
86 private volatile int _acceptQueueSize = 0;
87 private volatile boolean _reuseAddress = true;
88 private volatile int _lingerTime = -1;
89
90
91
92
93
94
95
96 public ServerConnector(
97 @Name("server") Server server)
98 {
99 this(server,null,null,null,0,0,new HttpConnectionFactory());
100 }
101
102
103
104
105
106
107
108
109
110
111 public ServerConnector(
112 @Name("server") Server server,
113 @Name("acceptors") int acceptors,
114 @Name("selectors") int selectors)
115 {
116 this(server,null,null,null,acceptors,selectors,new HttpConnectionFactory());
117 }
118
119
120
121
122
123
124
125 public ServerConnector(
126 @Name("server") Server server,
127 @Name("factories") ConnectionFactory... factories)
128 {
129 this(server,null,null,null,0,0,factories);
130 }
131
132
133
134
135
136
137
138
139 public ServerConnector(
140 @Name("server") Server server,
141 @Name("sslContextFactory") SslContextFactory sslContextFactory)
142 {
143 this(server,null,null,null,0,0,AbstractConnectionFactory.getFactories(sslContextFactory,new HttpConnectionFactory()));
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157 public ServerConnector(
158 @Name("server") Server server,
159 @Name("acceptors") int acceptors,
160 @Name("selectors") int selectors,
161 @Name("sslContextFactory") SslContextFactory sslContextFactory)
162 {
163 this(server,null,null,null,acceptors,selectors,AbstractConnectionFactory.getFactories(sslContextFactory,new HttpConnectionFactory()));
164 }
165
166
167
168
169
170
171
172
173 public ServerConnector(
174 @Name("server") Server server,
175 @Name("sslContextFactory") SslContextFactory sslContextFactory,
176 @Name("factories") ConnectionFactory... factories)
177 {
178 this(server,null,null,null,0,0,AbstractConnectionFactory.getFactories(sslContextFactory,factories));
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public ServerConnector(
199 @Name("server") Server server,
200 @Name("executor") Executor executor,
201 @Name("scheduler") Scheduler scheduler,
202 @Name("bufferPool") ByteBufferPool bufferPool,
203 @Name("acceptors") int acceptors,
204 @Name("selectors") int selectors,
205 @Name("factories") ConnectionFactory... factories)
206 {
207 super(server,executor,scheduler,bufferPool,acceptors,factories);
208 _manager = new ServerConnectorManager(getExecutor(), getScheduler(), selectors > 0 ? selectors : Runtime.getRuntime().availableProcessors());
209 addBean(_manager, true);
210 }
211
212 @Override
213 public boolean isOpen()
214 {
215 ServerSocketChannel channel = _acceptChannel;
216 return channel!=null && channel.isOpen();
217 }
218
219
220
221
222
223 public boolean isInheritChannel()
224 {
225 return _inheritChannel;
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239 public void setInheritChannel(boolean inheritChannel)
240 {
241 _inheritChannel = inheritChannel;
242 }
243
244 @Override
245 public void open() throws IOException
246 {
247 if (_acceptChannel == null)
248 {
249 ServerSocketChannel serverChannel = null;
250 if (isInheritChannel())
251 {
252 Channel channel = System.inheritedChannel();
253 if (channel instanceof ServerSocketChannel)
254 serverChannel = (ServerSocketChannel)channel;
255 else
256 LOG.warn("Unable to use System.inheritedChannel() [{}]. Trying a new ServerSocketChannel at {}:{}", channel, getHost(), getPort());
257 }
258
259 if (serverChannel == null)
260 {
261 serverChannel = ServerSocketChannel.open();
262
263 InetSocketAddress bindAddress = getHost() == null ? new InetSocketAddress(getPort()) : new InetSocketAddress(getHost(), getPort());
264 serverChannel.socket().bind(bindAddress, getAcceptQueueSize());
265 serverChannel.socket().setReuseAddress(getReuseAddress());
266
267 _localPort = serverChannel.socket().getLocalPort();
268 if (_localPort <= 0)
269 throw new IOException("Server channel not bound");
270
271 addBean(serverChannel);
272 }
273
274 serverChannel.configureBlocking(true);
275 addBean(serverChannel);
276
277 _acceptChannel = serverChannel;
278 }
279 }
280
281 @Override
282 public Future<Void> shutdown()
283 {
284
285 return super.shutdown();
286 }
287
288 @Override
289 public void close()
290 {
291 ServerSocketChannel serverChannel = _acceptChannel;
292 _acceptChannel = null;
293
294 if (serverChannel != null)
295 {
296 removeBean(serverChannel);
297
298
299 if (serverChannel.isOpen())
300 {
301 try
302 {
303 serverChannel.close();
304 }
305 catch (IOException e)
306 {
307 LOG.warn(e);
308 }
309 }
310 }
311
312 _localPort = -2;
313 }
314
315 @Override
316 public void accept(int acceptorID) throws IOException
317 {
318 ServerSocketChannel serverChannel = _acceptChannel;
319 if (serverChannel != null && serverChannel.isOpen())
320 {
321 SocketChannel channel = serverChannel.accept();
322 channel.configureBlocking(false);
323 Socket socket = channel.socket();
324 configure(socket);
325 _manager.accept(channel);
326 }
327 }
328
329 protected void configure(Socket socket)
330 {
331 try
332 {
333 socket.setTcpNoDelay(true);
334 if (_lingerTime >= 0)
335 socket.setSoLinger(true, _lingerTime / 1000);
336 else
337 socket.setSoLinger(false, 0);
338 }
339 catch (SocketException e)
340 {
341 LOG.ignore(e);
342 }
343 }
344
345 public SelectorManager getSelectorManager()
346 {
347 return _manager;
348 }
349
350 @Override
351 public Object getTransport()
352 {
353 return _acceptChannel;
354 }
355
356 @Override
357 @ManagedAttribute("local port")
358 public int getLocalPort()
359 {
360 return _localPort;
361 }
362
363 protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException
364 {
365 return new SelectChannelEndPoint(channel, selectSet, key, getScheduler(), getIdleTimeout());
366 }
367
368
369
370
371
372 @ManagedAttribute("TCP/IP solinger time or -1 to disable")
373 public int getSoLingerTime()
374 {
375 return _lingerTime;
376 }
377
378
379
380
381
382 public void setSoLingerTime(int lingerTime)
383 {
384 _lingerTime = lingerTime;
385 }
386
387
388
389
390 @ManagedAttribute("Accept Queue size")
391 public int getAcceptQueueSize()
392 {
393 return _acceptQueueSize;
394 }
395
396
397
398
399 public void setAcceptQueueSize(int acceptQueueSize)
400 {
401 _acceptQueueSize = acceptQueueSize;
402 }
403
404
405
406
407
408 public boolean getReuseAddress()
409 {
410 return _reuseAddress;
411 }
412
413
414
415
416
417 public void setReuseAddress(boolean reuseAddress)
418 {
419 _reuseAddress = reuseAddress;
420 }
421
422 private final class ServerConnectorManager extends SelectorManager
423 {
424 private ServerConnectorManager(Executor executor, Scheduler scheduler, int selectors)
425 {
426 super(executor, scheduler, selectors);
427 }
428
429 @Override
430 protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey selectionKey) throws IOException
431 {
432 return ServerConnector.this.newEndPoint(channel, selectSet, selectionKey);
433 }
434
435 @Override
436 public Connection newConnection(SocketChannel channel, EndPoint endpoint, Object attachment) throws IOException
437 {
438 return getDefaultConnectionFactory().newConnection(ServerConnector.this, endpoint);
439 }
440
441 @Override
442 protected void endPointOpened(EndPoint endpoint)
443 {
444 super.endPointOpened(endpoint);
445 onEndPointOpened(endpoint);
446 }
447
448 @Override
449 protected void endPointClosed(EndPoint endpoint)
450 {
451 onEndPointClosed(endpoint);
452 super.endPointClosed(endpoint);
453 }
454
455
456 }
457 }