diff 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
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/nio/SelectChannelConnector.java	Fri Oct 07 15:17:01 2016 -0600
+++ b/src/org/eclipse/jetty/server/nio/SelectChannelConnector.java	Sat Oct 08 21:15:28 2016 -0600
@@ -67,18 +67,11 @@
  */
 public class SelectChannelConnector extends Connector
 {
-	private transient ServerSocketChannel _acceptChannel;
 	private int _lowResourcesConnections;
 	private int _lowResourcesMaxIdleTime;
-	private int _localPort=-1;
 
 	private final SelectorManager _manager = new ConnectorSelectorManager();
 
-	/* ------------------------------------------------------------------------------- */
-	/**
-	 * Constructor.
-	 *
-	 */
 	public SelectChannelConnector(Server server,int port)
 	{
 		super(server,port);
@@ -87,7 +80,6 @@
 		setAcceptors(Math.max(1,(Runtime.getRuntime().availableProcessors()+3)/4));
 	}
 	
-	/* ------------------------------------------------------------ */
 	@Override
 	public final void accept() throws IOException
 	{
@@ -103,23 +95,17 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	public void close() throws IOException
+	public synchronized void close() throws IOException
 	{
-		synchronized(this)
+		if (_acceptChannel != null)
 		{
-			if (_acceptChannel != null)
-			{
-				removeBean(_acceptChannel);
-				if (_acceptChannel.isOpen())
-					_acceptChannel.close();
-			}
-			_acceptChannel = null;
-			_localPort=-2;
+			removeBean(_acceptChannel);
+			if (_acceptChannel.isOpen())
+				_acceptChannel.close();
 		}
+		_acceptChannel = null;
 	}
 
-	/* ------------------------------------------------------------------------------- */
 	@Override
 	public void customize(EndPoint endpoint, Request request) throws IOException
 	{
@@ -128,7 +114,6 @@
 		super.customize(endpoint, request);
 	}
 
-	/* ------------------------------------------------------------------------------- */
 	@Override
 	public void persist(EndPoint endpoint) throws IOException
 	{
@@ -137,54 +122,11 @@
 		super.persist(endpoint);
 	}
 
-	/* ------------------------------------------------------------ */
 	public SelectorManager getSelectorManager()
 	{
 		return _manager;
 	}
 
-	/* ------------------------------------------------------------ */
-	public synchronized Object getConnection()
-	{
-		return _acceptChannel;
-	}
-
-	/* ------------------------------------------------------------------------------- */
-	public int getLocalPort()
-	{
-		synchronized(this)
-		{
-			return _localPort;
-		}
-	}
-
-	/* ------------------------------------------------------------ */
-	public void open() throws IOException
-	{
-		synchronized(this)
-		{
-			if (_acceptChannel == null)
-			{
-				// Create a new server socket
-				_acceptChannel = ServerSocketChannel.open();
-				// Set to blocking mode
-				_acceptChannel.configureBlocking(true);
-
-				// Bind the server socket to the local host and port
-				_acceptChannel.socket().setReuseAddress(true);
-				InetSocketAddress addr = getHost()==null?new InetSocketAddress(port):new InetSocketAddress(getHost(),port);
-				_acceptChannel.socket().bind(addr);
-
-				_localPort=_acceptChannel.socket().getLocalPort();
-				if (_localPort<=0)
-					throw new IOException("Server channel not bound");
-
-				addBean(_acceptChannel);
-			}
-		}
-	}
-
-	/* ------------------------------------------------------------ */
 	@Override
 	public void setMaxIdleTime(int maxIdleTime)
 	{
@@ -192,10 +134,6 @@
 		super.setMaxIdleTime(maxIdleTime);
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return the lowResourcesConnections
-	 */
 	public int getLowResourcesConnections()
 	{
 		return _lowResourcesConnections;
@@ -239,22 +177,35 @@
 	}
 
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see org.eclipse.jetty.server.server.AbstractConnector#doStart()
-	 */
 	@Override
-	protected void doStart() throws Exception
+	protected synchronized void doStart() throws Exception
 	{
 		_manager.setSelectSets(getAcceptors());
 		_manager.setMaxIdleTime(getMaxIdleTime());
 		_manager.setLowResourcesConnections(getLowResourcesConnections());
 		_manager.setLowResourcesMaxIdleTime(getLowResourcesMaxIdleTime());
 
+		if (_acceptChannel == null)
+		{
+			// Create a new server socket
+			_acceptChannel = ServerSocketChannel.open();
+			// Set to blocking mode
+			_acceptChannel.configureBlocking(true);
+
+			// Bind the server socket to the local host and port
+			_acceptChannel.socket().setReuseAddress(true);
+			InetSocketAddress addr = getHost()==null?new InetSocketAddress(port):new InetSocketAddress(getHost(),port);
+			_acceptChannel.bind(addr);
+
+			if( _acceptChannel.socket().getLocalPort() != port)
+				throw new IOException("Server channel not bound");
+
+			addBean(_acceptChannel);
+		}
+
 		super.doStart();
 	}
 
-	/* ------------------------------------------------------------ */
 	protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key) throws IOException
 	{
 		SelectChannelEndPoint endp= new SelectChannelEndPoint(channel,selectSet,key, SelectChannelConnector.this._maxIdleTime);
@@ -262,22 +213,17 @@
 		return endp;
 	}
 
-	/* ------------------------------------------------------------------------------- */
 	protected void endPointClosed(SelectChannelEndPoint endpoint)
 	{
 		endpoint.getConnection().onClose();
 	}
 
-	/* ------------------------------------------------------------------------------- */
 	protected AsyncConnection newConnection(SocketChannel channel,final AsyncEndPoint endpoint)
 	{
 		return new AsyncHttpConnection(SelectChannelConnector.this,endpoint,server);
 	}
 
 
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
 	private final class ConnectorSelectorManager extends SelectorManager
 	{
 		@Override