comparison src/org/eclipse/jetty/server/Connector.java @ 909:c60c1adfac3e

simplify connectors
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 08 Oct 2016 21:15:28 -0600
parents bc4e299de953
children 17f4fe8271de
comparison
equal deleted inserted replaced
908:e003b20780c4 909:c60c1adfac3e
20 20
21 import java.io.IOException; 21 import java.io.IOException;
22 import java.net.InetAddress; 22 import java.net.InetAddress;
23 import java.net.Socket; 23 import java.net.Socket;
24 import java.net.UnknownHostException; 24 import java.net.UnknownHostException;
25 import java.nio.channels.ServerSocketChannel;
25 import java.util.concurrent.ThreadPoolExecutor; 26 import java.util.concurrent.ThreadPoolExecutor;
26 import java.util.concurrent.atomic.AtomicLong; 27 import java.util.concurrent.atomic.AtomicLong;
27 28
28 import javax.servlet.ServletRequest; 29 import javax.servlet.ServletRequest;
29 30
68 protected int _lowResourceMaxIdleTime = -1; 69 protected int _lowResourceMaxIdleTime = -1;
69 protected int _soLingerTime = -1; 70 protected int _soLingerTime = -1;
70 71
71 protected final HttpBuffersImpl _buffers = new HttpBuffersImpl(); 72 protected final HttpBuffersImpl _buffers = new HttpBuffersImpl();
72 73
74 // from child classes
75 protected transient ServerSocketChannel _acceptChannel;
76
73 public Connector(Server server,int port) { 77 public Connector(Server server,int port) {
74 this.server = server; 78 this.server = server;
75 this.port = port; 79 this.port = port;
76 server.connectors.add(this); 80 server.connectors.add(this);
77 addBean(_buffers); 81 addBean(_buffers);
79 _buffers.setRequestHeaderType(Type.INDIRECT); 83 _buffers.setRequestHeaderType(Type.INDIRECT);
80 _buffers.setResponseBufferType(Type.DIRECT); 84 _buffers.setResponseBufferType(Type.DIRECT);
81 _buffers.setResponseHeaderType(Type.INDIRECT); 85 _buffers.setResponseHeaderType(Type.INDIRECT);
82 } 86 }
83 87
84 /* ------------------------------------------------------------ */
85 /**
86 */
87 public void setHost(String host) 88 public void setHost(String host)
88 { 89 {
89 _host = host; 90 _host = host;
90 } 91 }
91 92
92 /* ------------------------------------------------------------ */
93 /*
94 */
95 public String getHost() 93 public String getHost()
96 { 94 {
97 return _host; 95 return _host;
98 } 96 }
99 97
100 /* ------------------------------------------------------------ */
101 /**
102 * @return Returns the maxIdleTime.
103 */
104 public int getMaxIdleTime() 98 public int getMaxIdleTime()
105 { 99 {
106 return _maxIdleTime; 100 return _maxIdleTime;
107 } 101 }
108 102
211 public void setSoLingerTime(int soLingerTime) 205 public void setSoLingerTime(int soLingerTime)
212 { 206 {
213 _soLingerTime = soLingerTime; 207 _soLingerTime = soLingerTime;
214 } 208 }
215 209
216 /* ------------------------------------------------------------ */
217 @Override 210 @Override
218 protected void doStart() throws Exception 211 protected void doStart() throws Exception
219 { 212 {
220 // open listener port
221 open();
222
223 super.doStart(); 213 super.doStart();
224 214
225 // Start selector thread 215 // Start selector thread
226 ThreadPoolExecutor _threadPool = server.threadPool; 216 ThreadPoolExecutor _threadPool = server.threadPool;
227 for (int i = 0; i < _acceptors; i++) 217 for (int i = 0; i < _acceptors; i++)
272 262
273 public void persist(EndPoint endpoint) throws IOException 263 public void persist(EndPoint endpoint) throws IOException
274 { 264 {
275 } 265 }
276 266
277 /* ------------------------------------------------------------ */
278 /*
279 * @see org.eclipse.jetty.server.Connector#isConfidential(org.eclipse.jetty.server.Request)
280 */
281 public boolean isConfidential(Request request) 267 public boolean isConfidential(Request request)
282 { 268 {
283 return false; 269 return false;
284 } 270 }
285 271
286 /* ------------------------------------------------------------ */
287 protected abstract void accept() throws IOException, InterruptedException; 272 protected abstract void accept() throws IOException, InterruptedException;
288 273
289 /* ------------------------------------------------------------ */
290 public void stopAccept(int acceptorID) throws Exception 274 public void stopAccept(int acceptorID) throws Exception
291 { 275 {
292 } 276 }
293 277
294 public int getRequestBufferSize() 278 public int getRequestBufferSize()
374 public int getMaxBuffers() 358 public int getMaxBuffers()
375 { 359 {
376 return _buffers.getMaxBuffers(); 360 return _buffers.getMaxBuffers();
377 } 361 }
378 362
379 /* ------------------------------------------------------------ */
380 @Override 363 @Override
381 public String toString() 364 public String toString()
382 { 365 {
383 return String.format("%s@%s:%d", 366 return String.format("%s@%s:%d",
384 getClass().getSimpleName(), 367 getClass().getSimpleName(),
385 getHost()==null?"0.0.0.0":getHost(), 368 getHost()==null?"0.0.0.0":getHost(),
386 getLocalPort()<=0 ? port : getLocalPort()); 369 port);
387 } 370 }
388 371
389 /* ------------------------------------------------------------ */ 372
390 /* ------------------------------------------------------------ */
391 /* ------------------------------------------------------------ */
392 private class Acceptor implements Runnable 373 private class Acceptor implements Runnable
393 { 374 {
394 375
395 public void run() 376 public void run()
396 { 377 {
398 String name = current.getName(); 379 String name = current.getName();
399 current.setName(name + " Acceptor" + " " + Connector.this); 380 current.setName(name + " Acceptor" + " " + Connector.this);
400 381
401 try 382 try
402 { 383 {
403 while (isRunning() && getConnection() != null) 384 while (isRunning() && _acceptChannel != null)
404 { 385 {
405 try 386 try
406 { 387 {
407 accept(); 388 accept();
408 } 389 }
433 } 414 }
434 415
435 public String getName() 416 public String getName()
436 { 417 {
437 if (_name == null) 418 if (_name == null)
438 _name = (getHost() == null?"0.0.0.0":getHost()) + ":" + (getLocalPort() <= 0?port:getLocalPort()); 419 _name = (getHost() == null?"0.0.0.0":getHost()) + ":" + port;
439 return _name; 420 return _name;
440 } 421 }
441 422
442 /* ------------------------------------------------------------ */
443 public final boolean isLowResources() 423 public final boolean isLowResources()
444 { 424 {
445 return server.isLowOnThreads(); 425 return server.isLowOnThreads();
446 } 426 }
447 427
448 // from old interface 428 // from old interface
449 public abstract void open() throws IOException;
450 public abstract void close() throws IOException; 429 public abstract void close() throws IOException;
451 public abstract int getLocalPort();
452 public abstract Object getConnection();
453 430
454 431
455 // from AbstractNIOConnector 432 // from AbstractNIOConnector
456 433
457 /* ------------------------------------------------------------------------------- */ 434 /* ------------------------------------------------------------------------------- */
468 public void setUseDirectBuffers(boolean direct) 445 public void setUseDirectBuffers(boolean direct)
469 { 446 {
470 _buffers.setRequestBufferType(direct?Type.DIRECT:Type.INDIRECT); 447 _buffers.setRequestBufferType(direct?Type.DIRECT:Type.INDIRECT);
471 _buffers.setResponseBufferType(direct?Type.DIRECT:Type.INDIRECT); 448 _buffers.setResponseBufferType(direct?Type.DIRECT:Type.INDIRECT);
472 } 449 }
450
451
452 // from child classes
453
473 } 454 }