You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2016/05/31 20:35:15 UTC

svn commit: r1746341 - in /tomcat/trunk/java/org/apache/tomcat/util/net: AprEndpoint.java Nio2Endpoint.java NioEndpoint.java SocketProcessorBase.java

Author: markt
Date: Tue May 31 20:35:15 2016
New Revision: 1746341

URL: http://svn.apache.org/viewvc?rev=1746341&view=rev
Log:
Introduce SocketProcessorBase and start to pull common SocketProcessor functionality into it.

Added:
    tomcat/trunk/java/org/apache/tomcat/util/net/SocketProcessorBase.java   (with props)
Modified:
    tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java?rev=1746341&r1=1746340&r2=1746341&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java Tue May 31 20:35:15 2016
@@ -2244,33 +2244,25 @@ public class AprEndpoint extends Abstrac
      * This class is the equivalent of the Worker, but will simply use in an
      * external Executor thread pool.
      */
-    protected class SocketProcessor implements Runnable {
-
-        private final SocketWrapperBase<Long> socket;
-        private final SocketEvent status;
+    protected class SocketProcessor extends  SocketProcessorBase<Long> {
 
         public SocketProcessor(SocketWrapperBase<Long> socket,
-                SocketEvent status) {
-            this.socket = socket;
-            if (status == null) {
-                // Should never happen
-                throw new NullPointerException();
-            }
-            this.status = status;
+                SocketEvent event) {
+            super(socket, event);
         }
 
         @Override
         public void run() {
-            synchronized (socket) {
+            synchronized (socketWrapper) {
                 // Process the request from this socket
-                if (socket.getSocket() == null) {
+                if (socketWrapper.getSocket() == null) {
                     // Closed in another thread
                     return;
                 }
-                SocketState state = getHandler().process(socket, status);
+                SocketState state = getHandler().process(socketWrapper, event);
                 if (state == Handler.SocketState.CLOSED) {
                     // Close socket and pool
-                    closeSocket(socket.getSocket().longValue());
+                    closeSocket(socketWrapper.getSocket().longValue());
                 }
             }
         }

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java?rev=1746341&r1=1746340&r2=1746341&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java Tue May 31 20:35:15 2016
@@ -1624,44 +1624,36 @@ public class Nio2Endpoint extends Abstra
      * This class is the equivalent of the Worker, but will simply use in an
      * external Executor thread pool.
      */
-    protected class SocketProcessor implements Runnable {
+    protected class SocketProcessor extends SocketProcessorBase<Nio2Channel> {
 
-        private SocketWrapperBase<Nio2Channel> socket = null;
-        private SocketEvent status = null;
-
-        public SocketProcessor(SocketWrapperBase<Nio2Channel> socket, SocketEvent status) {
-            reset(socket,status);
-        }
-
-        public void reset(SocketWrapperBase<Nio2Channel> socket, SocketEvent status) {
-            this.socket = socket;
-            this.status = status;
+        public SocketProcessor(SocketWrapperBase<Nio2Channel> socketWrapper, SocketEvent event) {
+            super(socketWrapper, event);
         }
 
         @Override
         public void run() {
-            synchronized (socket) {
-                if (SocketEvent.OPEN_WRITE != status) {
+            synchronized (socketWrapper) {
+                if (SocketEvent.OPEN_WRITE != event) {
                     // Anything other than OPEN_WRITE is a genuine read or an
                     // error condition so for all of those release the semaphore
-                    ((Nio2SocketWrapper) socket).releaseReadPending();
+                    ((Nio2SocketWrapper) socketWrapper).releaseReadPending();
                 }
                 boolean launch = false;
                 try {
                     int handshake = -1;
 
                     try {
-                        if (socket.getSocket() != null) {
+                        if (socketWrapper.getSocket() != null) {
                             // For STOP there is no point trying to handshake as the
                             // Poller has been stopped.
-                            if (!socket.getSocket().isHandshakeComplete() && status == SocketEvent.ERROR) {
+                            if (!socketWrapper.getSocket().isHandshakeComplete() && event == SocketEvent.ERROR) {
                                 handshake = -1;
-                            } else if (socket.getSocket().isHandshakeComplete() ||
-                                    status == SocketEvent.STOP ||
-                                    status == SocketEvent.ERROR) {
+                            } else if (socketWrapper.getSocket().isHandshakeComplete() ||
+                                    event == SocketEvent.STOP ||
+                                    event == SocketEvent.ERROR) {
                                 handshake = 0;
                             } else {
-                                handshake = socket.getSocket().handshake();
+                                handshake = socketWrapper.getSocket().handshake();
                                 // The handshake process reads/writes from/to the
                                 // socket. status may therefore be OPEN_WRITE once
                                 // the handshake completes. However, the handshake
@@ -1669,7 +1661,7 @@ public class Nio2Endpoint extends Abstra
                                 // must always be OPEN_READ after it completes. It
                                 // is OK to always set this as it is only used if
                                 // the handshake completes.
-                                status = SocketEvent.OPEN_READ;
+                                event = SocketEvent.OPEN_READ;
                             }
                         }
                     } catch (IOException x) {
@@ -1681,27 +1673,27 @@ public class Nio2Endpoint extends Abstra
                     if (handshake == 0) {
                         SocketState state = SocketState.OPEN;
                         // Process the request from this socket
-                        if (status == null) {
-                            state = getHandler().process(socket, SocketEvent.OPEN_READ);
+                        if (event == null) {
+                            state = getHandler().process(socketWrapper, SocketEvent.OPEN_READ);
                         } else {
-                            state = getHandler().process(socket, status);
+                            state = getHandler().process(socketWrapper, event);
                         }
                         if (state == SocketState.CLOSED) {
                             // Close socket and pool
-                            closeSocket(socket);
+                            closeSocket(socketWrapper);
                             if (running && !paused) {
-                                if (!nioChannels.push(socket.getSocket())) {
-                                    socket.getSocket().free();
+                                if (!nioChannels.push(socketWrapper.getSocket())) {
+                                    socketWrapper.getSocket().free();
                                 }
                             }
                         } else if (state == SocketState.UPGRADING) {
                             launch = true;
                         }
                     } else if (handshake == -1 ) {
-                        closeSocket(socket);
+                        closeSocket(socketWrapper);
                         if (running && !paused) {
-                            if (!nioChannels.push(socket.getSocket())) {
-                                socket.getSocket().free();
+                            if (!nioChannels.push(socketWrapper.getSocket())) {
+                                socketWrapper.getSocket().free();
                             }
                         }
                     }
@@ -1709,13 +1701,13 @@ public class Nio2Endpoint extends Abstra
                     ExceptionUtils.handleThrowable(vme);
                 } catch (Throwable t) {
                     log.error(sm.getString("endpoint.processing.fail"), t);
-                    if (socket != null) {
-                        closeSocket(socket);
+                    if (socketWrapper != null) {
+                        closeSocket(socketWrapper);
                     }
                 } finally {
                     if (launch) {
                         try {
-                            getExecutor().execute(new SocketProcessor(socket, SocketEvent.OPEN_READ));
+                            getExecutor().execute(new SocketProcessor(socketWrapper, SocketEvent.OPEN_READ));
                         } catch (NullPointerException npe) {
                             if (running) {
                                 log.error(sm.getString("endpoint.launch.fail"),
@@ -1723,8 +1715,8 @@ public class Nio2Endpoint extends Abstra
                             }
                         }
                     }
-                    socket = null;
-                    status = null;
+                    socketWrapper = null;
+                    event = null;
                     //return to cache
                     if (running && !paused) {
                         processorCache.push(this);

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=1746341&r1=1746340&r2=1746341&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Tue May 31 20:35:15 2016
@@ -1423,23 +1423,15 @@ public class NioEndpoint extends Abstrac
      * This class is the equivalent of the Worker, but will simply use in an
      * external Executor thread pool.
      */
-    protected class SocketProcessor implements Runnable {
+    protected class SocketProcessor extends SocketProcessorBase<NioChannel> {
 
-        private NioSocketWrapper ka = null;
-        private SocketEvent status = null;
-
-        public SocketProcessor(NioSocketWrapper ka, SocketEvent status) {
-            reset(ka, status);
-        }
-
-        public void reset(NioSocketWrapper ka, SocketEvent status) {
-            this.ka = ka;
-            this.status = status;
+        public SocketProcessor(SocketWrapperBase<NioChannel> ka, SocketEvent event) {
+            super(ka, event);
         }
 
         @Override
         public void run() {
-            NioChannel socket = ka.getSocket();
+            NioChannel socket = socketWrapper.getSocket();
             if (socket == null) {
                 return;
             }
@@ -1455,7 +1447,7 @@ public class NioEndpoint extends Abstrac
                             // For STOP there is no point trying to handshake as the
                             // Poller has been stopped.
                             if (socket.isHandshakeComplete() ||
-                                    status == SocketEvent.STOP) {
+                                    event == SocketEvent.STOP) {
                                 handshake = 0;
                             } else {
                                 handshake = socket.handshake(
@@ -1467,7 +1459,7 @@ public class NioEndpoint extends Abstrac
                                 // must always be OPEN_READ after it completes. It
                                 // is OK to always set this as it is only used if
                                 // the handshake completes.
-                                status = SocketEvent.OPEN_READ;
+                                event = SocketEvent.OPEN_READ;
                             }
                         }
                     } catch (IOException x) {
@@ -1479,18 +1471,20 @@ public class NioEndpoint extends Abstrac
                     if (handshake == 0) {
                         SocketState state = SocketState.OPEN;
                         // Process the request from this socket
-                        if (status == null) {
-                            state = getHandler().process(ka, SocketEvent.OPEN_READ);
+                        if (event == null) {
+                            state = getHandler().process(socketWrapper, SocketEvent.OPEN_READ);
                         } else {
-                            state = getHandler().process(ka, status);
+                            state = getHandler().process(socketWrapper, event);
                         }
                         if (state == SocketState.CLOSED) {
                             close(socket, key);
                         }
                     } else if (handshake == -1 ) {
                         close(socket, key);
-                    } else {
-                        ka.getPoller().add(socket,handshake);
+                    } else if (handshake == SelectionKey.OP_READ){
+                        socketWrapper.registerReadInterest();
+                    } else if (handshake == SelectionKey.OP_WRITE){
+                        socketWrapper.registerWriteInterest();
                     }
                 } catch (CancelledKeyException cx) {
                     socket.getPoller().cancelledKey(key);
@@ -1500,8 +1494,8 @@ public class NioEndpoint extends Abstrac
                     log.error("", t);
                     socket.getPoller().cancelledKey(key);
                 } finally {
-                    ka = null;
-                    status = null;
+                    socketWrapper = null;
+                    event = null;
                     //return to cache
                     if (running && !paused) {
                         processorCache.push(this);

Added: tomcat/trunk/java/org/apache/tomcat/util/net/SocketProcessorBase.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/SocketProcessorBase.java?rev=1746341&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/SocketProcessorBase.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/SocketProcessorBase.java Tue May 31 20:35:15 2016
@@ -0,0 +1,39 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tomcat.util.net;
+
+
+public abstract class SocketProcessorBase<S> implements Runnable {
+
+    protected SocketWrapperBase<S> socketWrapper;
+    protected SocketEvent event;
+
+    public SocketProcessorBase(SocketWrapperBase<S> socketWrapper, SocketEvent event) {
+        reset(socketWrapper, event);
+    }
+
+
+    public void reset(SocketWrapperBase<S> socketWrapper, SocketEvent event) {
+        this.socketWrapper = socketWrapper;
+        if (event == null) {
+            // Should never happen
+            throw new NullPointerException();
+        } else {
+            this.event = event;
+        }
+    }
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/util/net/SocketProcessorBase.java
------------------------------------------------------------------------------
    svn:eol-style = native



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