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