diff src/org/eclipse/jetty/io/nio/SelectorManager.java @ 953:7db4a488fc82

simplify SelectorManager
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 12 Oct 2016 22:16:36 -0600
parents 669769bcdf5c
children a021c4c9c244
line wrap: on
line diff
--- a/src/org/eclipse/jetty/io/nio/SelectorManager.java	Wed Oct 12 19:47:45 2016 -0600
+++ b/src/org/eclipse/jetty/io/nio/SelectorManager.java	Wed Oct 12 22:16:36 2016 -0600
@@ -61,7 +61,7 @@
 	private int _maxIdleTime;
 	private long _lowResourcesConnections;
 	private SelectSet[] _selectSet;
-	private int _selectSets=1;
+	private int _selectSets = 1;
 	private volatile int _set=0;
 
 	/* ------------------------------------------------------------ */
@@ -242,9 +242,14 @@
 		private void addChange(SocketChannel channel)
 		{
 			try {
-				SelectionKey key = _selector.register(channel,SelectionKey.OP_READ,null);
+//System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqq a");
+//				SelectionKey key = _selector.register(channel,SelectionKey.OP_READ,null);
+				SelectionKey key = _selector.register(channel,0,null);
 				SelectChannelEndPoint endpoint = createEndPoint(channel,key);
 				key.attach(endpoint);
+				key.interestOps(SelectionKey.OP_READ);
+				_selector.update();
+//System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqq b");
 				endpoint.schedule();
 			} catch(IOException e) {
 				LOG.warn("",e);
@@ -261,7 +266,7 @@
 		 *
 		 * @throws IOException
 		 */
-		public void doSelect() throws IOException
+		private void doSelect() throws IOException
 		{
 			try
 			{
@@ -276,8 +281,6 @@
 				// Look for things to do
 				for (SelectionKey key: selector.selectedKeys())
 				{
-					SocketChannel channel=null;
-
 					try
 					{
 						if (!key.isValid())
@@ -289,50 +292,9 @@
 							continue;
 						}
 
-						Object att = key.attachment();
-						if (att instanceof SelectChannelEndPoint)
-						{
-							if (key.isReadable()||key.isWritable())
-								((SelectChannelEndPoint)att).schedule();
-						}
-						else if (key.isConnectable())
-						{
-							// Complete a connection of a registered channel
-							channel = (SocketChannel)key.channel();
-							boolean connected=false;
-							try
-							{
-								connected=channel.finishConnect();
-							}
-							catch(Exception e)
-							{
-								LOG.warn(e+","+channel+","+att);
-								LOG.debug("",e);
-							}
-							finally
-							{
-								if (connected)
-								{
-									key.interestOps(SelectionKey.OP_READ);
-									SelectChannelEndPoint endpoint = createEndPoint(channel,key);
-									key.attach(endpoint);
-									endpoint.schedule();
-								}
-								else
-								{
-									key.cancel();
-									channel.close();
-								}
-							}
-						}
-						else
-						{
-							// Wrap readable registered channel in an endpoint
-							channel = (SocketChannel)key.channel();
-							SelectChannelEndPoint endpoint = createEndPoint(channel,key);
-							key.attach(endpoint);
-							if (key.isReadable())
-								endpoint.schedule();
+						if (key.isReadable()||key.isWritable()) {
+							SelectChannelEndPoint endpoint = (SelectChannelEndPoint)key.attachment();
+							endpoint.schedule();
 						}
 						key = null;
 					}
@@ -346,19 +308,6 @@
 							LOG.warn("",e);
 						else
 							LOG.trace("",e);
-
-						try
-						{
-							if (channel!=null)
-								channel.close();
-						}
-						catch(IOException e2)
-						{
-							LOG.debug("",e2);
-						}
-
-						if (key != null && !(key.channel() instanceof ServerSocketChannel) && key.isValid())
-							key.cancel();
 					}
 				}
 
@@ -415,56 +364,35 @@
 			return _selector;
 		}
 
-		void stop() throws Exception
+		synchronized void stop() throws Exception
 		{
-			// Spin for a while waiting for selector to complete
-			// to avoid unneccessary closed channel exceptions
-/*
-			try
+			// close endpoints and selector
+			for (SelectionKey key : _selector.keys())
 			{
-				for (int i=0;i<100 && _selecting!=null;i++)
+				Object att=key.attachment();
+				if (att instanceof EndPoint)
 				{
-					_selector.wakeup();
-					Thread.sleep(10);
+					EndPoint endpoint = (EndPoint)att;
+					try
+					{
+						endpoint.close();
+					}
+					catch(IOException e)
+					{
+						LOG.trace("",e);
+					}
 				}
 			}
-			catch(Exception e)
-			{
-				LOG.warn("",e);
-			}
-*/
-			// close endpoints and selector
-			synchronized (this)
+
+			try
 			{
-				for (SelectionKey key : _selector.keys())
-				{
-					if (key==null)
-						continue;
-					Object att=key.attachment();
-					if (att instanceof EndPoint)
-					{
-						EndPoint endpoint = (EndPoint)att;
-						try
-						{
-							endpoint.close();
-						}
-						catch(IOException e)
-						{
-							LOG.trace("",e);
-						}
-					}
-				}
-
-				try
-				{
-					_selector.close();
-				}
-				catch (IOException e)
-				{
-					LOG.trace("",e);
-				}
-				_selector = null;
+				_selector.close();
 			}
+			catch (IOException e)
+			{
+				LOG.trace("",e);
+			}
+			_selector = null;
 		}
 
 		public String dump()