tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cos...@apache.org
Subject svn commit: r365857 - in /tomcat/sandbox/java/org/apache/tomcat/util/net: ./ javaio/ nio/
Date Wed, 04 Jan 2006 07:48:25 GMT
Author: costin
Date: Tue Jan  3 23:48:20 2006
New Revision: 365857

URL: http://svn.apache.org/viewcvs?rev=365857&view=rev
Log:
Moving code around, so it can be used at the same time with the old one
( if needed ) and to make it easier to exclude or package. 

The old endpoint is in javaio, with the ssl abstractions that don't
fit APR nor NIO. 

Few more simplifications and fixes to nio to reduce the thread use (
same as in LeaderFollower I think )




Added:
    tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/
    tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/DefaultServerSocketFactory.java
      - copied, changed from r348654, tomcat/sandbox/java/org/apache/tomcat/util/net/DefaultServerSocketFactory.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/LeaderFollowerEndpoint.java
      - copied, changed from r348656, tomcat/sandbox/java/org/apache/tomcat/util/net/LeaderFollowerEndpoint.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/LeaderFollowerWorkerThread.java
      - copied, changed from r348662, tomcat/sandbox/java/org/apache/tomcat/util/net/LeaderFollowerWorkerThread.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/MasterSlaveEndpoint.java
      - copied, changed from r348656, tomcat/sandbox/java/org/apache/tomcat/util/net/MasterSlaveEndpoint.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/MasterSlaveWorkerThread.java
      - copied, changed from r348662, tomcat/sandbox/java/org/apache/tomcat/util/net/MasterSlaveWorkerThread.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/SSLImplementation.java
      - copied, changed from r348654, tomcat/sandbox/java/org/apache/tomcat/util/net/SSLImplementation.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/SSLSupport.java
      - copied, changed from r348654, tomcat/sandbox/java/org/apache/tomcat/util/net/SSLSupport.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/ServerSocketFactory.java
      - copied, changed from r348654, tomcat/sandbox/java/org/apache/tomcat/util/net/ServerSocketFactory.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/nio/
    tomcat/sandbox/java/org/apache/tomcat/util/net/nio/NioEndpoint.java
      - copied, changed from r349085, tomcat/sandbox/java/org/apache/tomcat/util/net/NioEndpoint.java
Removed:
    tomcat/sandbox/java/org/apache/tomcat/util/net/AprEndpoint.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/DefaultServerSocketFactory.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/LeaderFollowerEndpoint.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/LeaderFollowerWorkerThread.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/MasterSlaveEndpoint.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/MasterSlaveWorkerThread.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/NioEndpoint.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/SSLImplementation.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/SSLSupport.java
    tomcat/sandbox/java/org/apache/tomcat/util/net/ServerSocketFactory.java
Modified:
    tomcat/sandbox/java/org/apache/tomcat/util/net/PoolTcpEndpoint.java

Modified: tomcat/sandbox/java/org/apache/tomcat/util/net/PoolTcpEndpoint.java
URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/net/PoolTcpEndpoint.java?rev=365857&r1=365856&r2=365857&view=diff
==============================================================================
--- tomcat/sandbox/java/org/apache/tomcat/util/net/PoolTcpEndpoint.java (original)
+++ tomcat/sandbox/java/org/apache/tomcat/util/net/PoolTcpEndpoint.java Tue Jan  3 23:48:20
2006
@@ -26,8 +26,6 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.tomcat.util.res.StringManager;
-import org.apache.tomcat.util.threads.ThreadPool;
-import org.apache.tomcat.util.threads.ThreadPool.ThreadPoolListener;
 
 /* Similar with MPM module in Apache2.0. Handles all the details related with
    "tcp server" functionality - thread management, accept policy, etc.
@@ -54,7 +52,7 @@
  */
 public class PoolTcpEndpoint implements Runnable { // implements Endpoint {
 
-    static Log log=LogFactory.getLog(PoolTcpEndpoint.class );
+    protected static Log log=LogFactory.getLog(PoolTcpEndpoint.class );
 
     protected StringManager sm = 
         StringManager.getManager("org.apache.tomcat.util.net.res");
@@ -89,7 +87,7 @@
     protected int maxThreads = 20;
     protected int maxSpareThreads = 20;
     protected int minSpareThreads = 20;
-    protected String type;
+    protected String type = "default";
 
     protected String name = "EP"; // base name for threads
     
@@ -98,6 +96,8 @@
     protected boolean daemon = true;
 
     private ArrayList listeners = new ArrayList();
+
+    private boolean polling;
     
     public PoolTcpEndpoint() {
     }
@@ -105,16 +105,16 @@
     public static PoolTcpEndpoint getEndpoint(String type) {
         String cn = null;
         if( "apr".equals( type )) {
-            cn = "org.apache.tomcat.util.net.AprEndpoint";
+            cn = "org.apache.tomcat.util.net.apr.AprEndpoint";
         }
         if( "lf".equals( type )) {
-            cn = "org.apache.tomcat.util.net.LeaderFollowerEndpoint";
-        }
-        if( "acc".equals( type )) {
-            cn = "org.apache.tomcat.util.net.AcceptorEndpoint";
+            cn = "org.apache.tomcat.util.net.javaio.LeaderFollowerEndpoint";
         }
         if( "ms".equals( type )) {
-            cn = "org.apache.tomcat.util.net.MasterSlaveEndpoint";
+            cn = "org.apache.tomcat.util.net.javaio.MasterSlaveEndpoint";
+        }
+        if( "nio".equals( type )) {
+            cn = "org.apache.tomcat.util.net.nio.NioEndpoint";
         }
         PoolTcpEndpoint res = null; 
         if( cn != null ) {
@@ -288,7 +288,16 @@
     public int getCurrentThreadsBusy() {
         return curThreads;
     }
+
+    public boolean getPolling() {
+        return polling;
+    }
     
+    public void setPolling( boolean b ) {
+        polling = b;
+    }
+
+
     // -------------------- Public methods --------------------
 
     public void initEndpoint() throws IOException, InstantiationException {
@@ -306,7 +315,7 @@
     public void stopEndpoint() {
     }
 
-    protected void processSocket(Socket s, TcpConnection con, 
+    public void processSocket(Socket s, TcpConnection con, 
             Object[] threadData) {
     }
 
@@ -317,7 +326,19 @@
         
     }
 
-
+    /** If the endpoint supports polling, add the socket to the poll
+     * watch. A thread will be woked up when the socket has available data.
+     * 
+     * Use this for Keep-Alive, or for reading data from client in poll mode.
+     * 
+     * 
+     * @param s
+     * @param context will be made available to the thread.
+     */
+    public void addPolling(Socket s, Object context ) {
+        
+    }
+    
     // ---------------- Utils ----------------------
     
     protected void closeServerSocket() {
@@ -410,5 +431,30 @@
 
         public void threadEnd( PoolTcpEndpoint ep, Thread t);
 
+    }
+
+    /**
+     * The Request attribute key for the cipher suite.
+     */
+    public static final String CIPHER_SUITE_KEY = "javax.servlet.request.cipher_suite";
+
+    /**
+     * The Request attribute key for the key size.
+     */
+    public static final String KEY_SIZE_KEY = "javax.servlet.request.key_size";
+
+    /**
+     * The Request attribute key for the client certificate chain.
+     */
+    public static final String CERTIFICATE_KEY = "javax.servlet.request.X509Certificate";
+
+    /**
+     * The Request attribute key for the session id.
+     * This one is a Tomcat extension to the Servlet spec.
+     */
+    public static final String SESSION_ID_KEY = "javax.servlet.request.ssl_session";
+
+    public Object getSsl(String string) {
+        return null;
     }
 }

Copied: tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/DefaultServerSocketFactory.java
(from r348654, tomcat/sandbox/java/org/apache/tomcat/util/net/DefaultServerSocketFactory.java)
URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/DefaultServerSocketFactory.java?p2=tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/DefaultServerSocketFactory.java&p1=tomcat/sandbox/java/org/apache/tomcat/util/net/DefaultServerSocketFactory.java&r1=348654&r2=365857&rev=365857&view=diff
==============================================================================
--- tomcat/sandbox/java/org/apache/tomcat/util/net/DefaultServerSocketFactory.java (original)
+++ tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/DefaultServerSocketFactory.java
Tue Jan  3 23:48:20 2006
@@ -14,7 +14,7 @@
  *  limitations under the License.
  */
 
-package org.apache.tomcat.util.net;
+package org.apache.tomcat.util.net.javaio;
 
 import java.io.*;
 import java.net.*;

Copied: tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/LeaderFollowerEndpoint.java
(from r348656, tomcat/sandbox/java/org/apache/tomcat/util/net/LeaderFollowerEndpoint.java)
URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/LeaderFollowerEndpoint.java?p2=tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/LeaderFollowerEndpoint.java&p1=tomcat/sandbox/java/org/apache/tomcat/util/net/LeaderFollowerEndpoint.java&r1=348656&r2=365857&rev=365857&view=diff
==============================================================================
--- tomcat/sandbox/java/org/apache/tomcat/util/net/LeaderFollowerEndpoint.java (original)
+++ tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/LeaderFollowerEndpoint.java Tue
Jan  3 23:48:20 2006
@@ -14,7 +14,7 @@
  *  limitations under the License.
  */
 
-package org.apache.tomcat.util.net;
+package org.apache.tomcat.util.net.javaio;
 
 import java.io.IOException;
 import java.io.InterruptedIOException;
@@ -24,6 +24,9 @@
 import java.security.AccessControlException;
 import java.util.Stack;
 
+import org.apache.tomcat.util.net.PoolTcpEndpoint;
+import org.apache.tomcat.util.net.TcpConnection;
+import org.apache.tomcat.util.net.TcpConnectionHandler;
 import org.apache.tomcat.util.threads.ThreadPool;
 import org.apache.tomcat.util.threads.ThreadPoolRunnable;
 
@@ -303,7 +306,7 @@
     }
 
     
-    protected void processSocket(Socket s, TcpConnection con, Object[] threadData) {
+    public void processSocket(Socket s, TcpConnection con, Object[] threadData) {
         // Process the connection
         int step = 1;
         try {

Copied: tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/LeaderFollowerWorkerThread.java
(from r348662, tomcat/sandbox/java/org/apache/tomcat/util/net/LeaderFollowerWorkerThread.java)
URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/LeaderFollowerWorkerThread.java?p2=tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/LeaderFollowerWorkerThread.java&p1=tomcat/sandbox/java/org/apache/tomcat/util/net/LeaderFollowerWorkerThread.java&r1=348662&r2=365857&rev=365857&view=diff
==============================================================================
--- tomcat/sandbox/java/org/apache/tomcat/util/net/LeaderFollowerWorkerThread.java (original)
+++ tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/LeaderFollowerWorkerThread.java
Tue Jan  3 23:48:20 2006
@@ -14,9 +14,12 @@
  *  limitations under the License.
  */
 
-package org.apache.tomcat.util.net;
+package org.apache.tomcat.util.net.javaio;
 
 import java.net.Socket;
+
+import org.apache.tomcat.util.net.PoolTcpEndpoint;
+import org.apache.tomcat.util.net.TcpConnection;
 import org.apache.tomcat.util.threads.ThreadPoolRunnable;
 
 /*

Copied: tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/MasterSlaveEndpoint.java (from
r348656, tomcat/sandbox/java/org/apache/tomcat/util/net/MasterSlaveEndpoint.java)
URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/MasterSlaveEndpoint.java?p2=tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/MasterSlaveEndpoint.java&p1=tomcat/sandbox/java/org/apache/tomcat/util/net/MasterSlaveEndpoint.java&r1=348656&r2=365857&rev=365857&view=diff
==============================================================================
--- tomcat/sandbox/java/org/apache/tomcat/util/net/MasterSlaveEndpoint.java (original)
+++ tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/MasterSlaveEndpoint.java Tue Jan
 3 23:48:20 2006
@@ -14,7 +14,7 @@
  *  limitations under the License.
  */
 
-package org.apache.tomcat.util.net;
+package org.apache.tomcat.util.net.javaio;
 
 import java.io.IOException;
 import java.io.InterruptedIOException;
@@ -25,6 +25,9 @@
 import java.util.Stack;
 import java.util.Vector;
 
+import org.apache.tomcat.util.net.PoolTcpEndpoint;
+import org.apache.tomcat.util.net.TcpConnection;
+
 /* Similar with MPM module in Apache2.0. Handles all the details related with
    "tcp server" functionality - thread management, accept policy, etc.
    It should do nothing more - as soon as it get a socket ( and all socket options
@@ -255,7 +258,7 @@
     }
 
     
-    protected void processSocket(Socket s, TcpConnection con, Object[] threadData) {
+    public void processSocket(Socket s, TcpConnection con, Object[] threadData) {
         // Process the connection
         int step = 1;
         try {

Copied: tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/MasterSlaveWorkerThread.java
(from r348662, tomcat/sandbox/java/org/apache/tomcat/util/net/MasterSlaveWorkerThread.java)
URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/MasterSlaveWorkerThread.java?p2=tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/MasterSlaveWorkerThread.java&p1=tomcat/sandbox/java/org/apache/tomcat/util/net/MasterSlaveWorkerThread.java&r1=348662&r2=365857&rev=365857&view=diff
==============================================================================
--- tomcat/sandbox/java/org/apache/tomcat/util/net/MasterSlaveWorkerThread.java (original)
+++ tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/MasterSlaveWorkerThread.java Tue
Jan  3 23:48:20 2006
@@ -14,10 +14,12 @@
  *  limitations under the License.
  */
 
-package org.apache.tomcat.util.net;
+package org.apache.tomcat.util.net.javaio;
 
 import java.net.Socket;
 
+import org.apache.tomcat.util.net.PoolTcpEndpoint;
+import org.apache.tomcat.util.net.TcpConnection;
 import org.apache.tomcat.util.threads.ThreadWithAttributes;
 
 /**

Copied: tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/SSLImplementation.java (from
r348654, tomcat/sandbox/java/org/apache/tomcat/util/net/SSLImplementation.java)
URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/SSLImplementation.java?p2=tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/SSLImplementation.java&p1=tomcat/sandbox/java/org/apache/tomcat/util/net/SSLImplementation.java&r1=348654&r2=365857&rev=365857&view=diff
==============================================================================
--- tomcat/sandbox/java/org/apache/tomcat/util/net/SSLImplementation.java (original)
+++ tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/SSLImplementation.java Tue Jan 
3 23:48:20 2006
@@ -14,7 +14,7 @@
  *  limitations under the License.
  */
 
-package org.apache.tomcat.util.net;
+package org.apache.tomcat.util.net.javaio;
 
 import java.net.Socket;
 

Copied: tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/SSLSupport.java (from r348654,
tomcat/sandbox/java/org/apache/tomcat/util/net/SSLSupport.java)
URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/SSLSupport.java?p2=tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/SSLSupport.java&p1=tomcat/sandbox/java/org/apache/tomcat/util/net/SSLSupport.java&r1=348654&r2=365857&rev=365857&view=diff
==============================================================================
--- tomcat/sandbox/java/org/apache/tomcat/util/net/SSLSupport.java (original)
+++ tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/SSLSupport.java Tue Jan  3 23:48:20
2006
@@ -14,7 +14,7 @@
  *  limitations under the License.
  */
 
-package org.apache.tomcat.util.net;
+package org.apache.tomcat.util.net.javaio;
 
 import java.io.IOException;
 

Copied: tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/ServerSocketFactory.java (from
r348654, tomcat/sandbox/java/org/apache/tomcat/util/net/ServerSocketFactory.java)
URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/ServerSocketFactory.java?p2=tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/ServerSocketFactory.java&p1=tomcat/sandbox/java/org/apache/tomcat/util/net/ServerSocketFactory.java&r1=348654&r2=365857&rev=365857&view=diff
==============================================================================
--- tomcat/sandbox/java/org/apache/tomcat/util/net/ServerSocketFactory.java (original)
+++ tomcat/sandbox/java/org/apache/tomcat/util/net/javaio/ServerSocketFactory.java Tue Jan
 3 23:48:20 2006
@@ -14,7 +14,7 @@
  *  limitations under the License.
  */
 
-package org.apache.tomcat.util.net;
+package org.apache.tomcat.util.net.javaio;
 
 import java.io.IOException;
 import java.net.InetAddress;

Copied: tomcat/sandbox/java/org/apache/tomcat/util/net/nio/NioEndpoint.java (from r349085,
tomcat/sandbox/java/org/apache/tomcat/util/net/NioEndpoint.java)
URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/net/nio/NioEndpoint.java?p2=tomcat/sandbox/java/org/apache/tomcat/util/net/nio/NioEndpoint.java&p1=tomcat/sandbox/java/org/apache/tomcat/util/net/NioEndpoint.java&r1=349085&r2=365857&rev=365857&view=diff
==============================================================================
--- tomcat/sandbox/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/sandbox/java/org/apache/tomcat/util/net/nio/NioEndpoint.java Tue Jan  3 23:48:20
2006
@@ -14,7 +14,7 @@
  *  limitations under the License.
  */
 
-package org.apache.tomcat.util.net;
+package org.apache.tomcat.util.net.nio;
 
 import java.io.IOException;
 import java.net.BindException;
@@ -22,7 +22,6 @@
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketAddress;
-import java.nio.channels.ClosedChannelException;
 import java.nio.channels.SelectableChannel;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
@@ -31,9 +30,10 @@
 import java.util.Iterator;
 import java.util.Set;
 
+import org.apache.tomcat.util.net.SimpleEndpoint;
+import org.apache.tomcat.util.net.TcpConnection;
 import org.apache.tomcat.util.threads.ThreadPool;
 import org.apache.tomcat.util.threads.ThreadPoolRunnable;
-import org.apache.tomcat.util.threads.ThreadWithAttributes;
 
 
 /** All threads blocked in accept(). New thread created on demand.
@@ -42,23 +42,25 @@
  * 
  */
 public class NioEndpoint extends SimpleEndpoint { 
-
-    private final Object threadSync = new Object();
-
-    // active acceptors
-    private int acceptors=0;
-    
-    ThreadPool tp;
+    private ThreadPool tp;
     
     public NioEndpoint() {
         tp=new ThreadPool();
         tp.setMinSpareThreads(2);
         tp.setMaxSpareThreads(8);
+        type = "nio";
     }
 
     // -------------------- Configuration --------------------
     // -------------------- Thread pool --------------------
 
+    public ThreadPool getThreadPool() {
+        return tp;
+    }
+    
+    // wrappers to make JMX happier .
+    // TODO: jmx wrapper should be smarter, support delegates. 
+    
     public void setMaxThreads(int maxThreads) {
         if( maxThreads > 0)
             tp.setMaxThreads(maxThreads);
@@ -110,16 +112,6 @@
     public void setName(String name) {
         tp.setName(name);
     }
-
-    
-    // ---------------------- 
-    public String getStrategy() {
-        return "nio";
-    }
-    
-    public int getCurrentThreadsBusy() {
-        return curThreads;
-    }
     
     // -------------------- Public methods --------------------
     
@@ -165,17 +157,17 @@
         } catch (IOException e) {
             e.printStackTrace();
         }
-        addSocketAccept( serverSocket, new SocketDispatch());
-        Thread poller = new Thread( new PollerThread());
-        poller.start();
+
+        PollerThread acceptTask = new PollerThread();
+        
+        addSocketAccept( serverSocket, acceptTask);
+        
+        tp.runIt(acceptTask);
     }
 
 
     // -------------------------------------------------- Master Slave Methods
 
-    
-
-
     public boolean getPolling() {
         return true;
     }
@@ -207,80 +199,8 @@
      *  
      * @author Costin Manolache
      */
-    class PollerThread implements Runnable {
-
-        public PollerThread() {
-        }
-               
-        public void run() {
-            while( running ) {
-                
-                try {
-                    int selRes = selector.select();
-
-                    if( selRes == 0 ) {
-                        System.err.println("Select with 0 keys " + 
-                                selector.keys().size() );
-                        for( SelectionKey k : selector.keys() ) {
-                            System.err.println("K " + k.interestOps() +
-                                    " " + k.readyOps() + " " + k.toString() + " "
-                                    + k.isValid() );
-                        }
-                        continue;
-                    }
-                    
-                    Set selected = selector.selectedKeys();
-                    Iterator selI = selected.iterator();
-                    
-                    while( selI.hasNext() ) {
-                        SelectionKey sk = (SelectionKey)selI.next();
-                        selI.remove();
-                        Object skAt = sk.attachment();
-                        
-                        int readyOps = sk.readyOps();
-                        SelectableChannel sc = sk.channel();
-                        
-                        // TODO: use the attachment to decide what's to do.
-                        if( sk.isAcceptable() ) {
-                            ServerSocketChannel ssc=(ServerSocketChannel)sc;
-                            SocketChannel sockC = ssc.accept();
-                            
-                            
-                            // process the connection in the thread pool
-                            if( skAt instanceof ThreadPoolRunnable ) {
-                                tp.runIt( (ThreadPoolRunnable) skAt, sockC);
-                            }
-                            //sk.interestOps( sk.interestOps() | 
-                            //        SelectionKey.OP_ACCEPT );
-                            System.err.println( sk.interestOps() ); 
-
-                            continue;
-                        }
-
-                        // TODO: this is for keep alive
-                        if( sk.isReadable() ) {
-                            SocketChannel sockC = (SocketChannel)sc;
-                            
-                            // Incoming data on keep-alive connection.
-                            continue;
-                        }
-                        
-                        // dispatch the socket to a pool thread
-                        System.err.println("Select: " + readyOps);
-                    }
-                    
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-                
-            }
-            
-        }
+    class PollerThread implements ThreadPoolRunnable  {
         
-    }
-    
-    class SocketDispatch implements ThreadPoolRunnable {
-
         public Object[] getInitData() {
             // no synchronization overhead, but 2 array access 
             Object obj[]=new Object[2];
@@ -288,26 +208,63 @@
             obj[0]=new TcpConnection();
             return obj;
         }
-        
+
         public void runIt(Object perThrData[]) {
-            ThreadWithAttributes t=(ThreadWithAttributes)Thread.currentThread();
-            
-            SocketChannel sc=(SocketChannel)t.getParam(tp);
-            if (isRunning()) {
-                // Loop if endpoint is paused
-                while (isPaused()) {
-                    try {
-                        Thread.sleep(1000);
-                    } catch (InterruptedException e) {
-                        // Ignore
+            try {
+                int selRes = selector.select();
+
+                if( selRes == 0 ) {
+                    System.err.println("Select with 0 keys " + 
+                            selector.keys().size() );
+                    for( SelectionKey k : selector.keys() ) {
+                        System.err.println("K " + k.interestOps() +
+                                " " + k.readyOps() + " " + k.toString() + " "
+                                + k.isValid() );
                     }
+                    return;
                 }
+                
+                Set selected = selector.selectedKeys();
+                Iterator selI = selected.iterator();
+                
+                while( selI.hasNext() ) {
+                    SelectionKey sk = (SelectionKey)selI.next();
+                    selI.remove();
+                    //Object skAt = sk.attachment(); // == this
+                    
+                    int readyOps = sk.readyOps();
+                    SelectableChannel sc = sk.channel();
+                    
+                    // TODO: use the attachment to decide what's to do.
+                    if( sk.isAcceptable() ) {
+                        ServerSocketChannel ssc=(ServerSocketChannel)sc;
+                        SocketChannel sockC = ssc.accept();
+                        
+                        //  continue accepting on a different thread
+                        // Side effect: if pool is full, accept will happen
+                        // a bit later. 
+                        // TODO: customize this if needed
+                        tp.runIt( this ); 
+                        // now process the socket. 
+                        processSocket(sockC.socket(), (TcpConnection) perThrData[0], 
+                                     (Object[]) perThrData[1]);
+                        continue;
+                    }
 
-                if (null != sc) {
-                    processSocket(sc.socket(), (TcpConnection) perThrData[0], 
-                            (Object[]) perThrData[1]);
+                    // TODO: this is for keep alive
+                    if( sk.isReadable() ) {
+                        //SocketChannel sockC = (SocketChannel)sc;
+                        
+                        // Incoming data on keep-alive connection.
+                        continue;
+                    }
+                    
+                    // dispatch the socket to a pool thread
+                    System.err.println("Select: " + readyOps);
                 }
-
+                
+            } catch (IOException e) {
+                e.printStackTrace();
             }
         }
         



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message