Added in the registration of comet interest operations
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 29 May 2007 10:29:25 +0000 (10:29 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 29 May 2007 10:29:25 +0000 (10:29 +0000)
Added in PollerInterest enumeration to satisfy different socket implementations and to decouple org.apache.tomcat from org.apache.catalina

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@542479 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/connector/CometEventImpl.java
java/org/apache/coyote/http11/Http11NioProcessor.java
java/org/apache/tomcat/util/net/NioEndpoint.java
java/org/apache/tomcat/util/net/PollerInterest.java [new file with mode: 0644]

index df76d56..f2d6d06 100644 (file)
@@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletResponse;
 import org.apache.catalina.CometEvent;
 import org.apache.catalina.util.StringManager;
 import org.apache.coyote.ActionCode;
+import org.apache.tomcat.util.net.PollerInterest;
 
 public class CometEventImpl implements CometEvent {
 
@@ -172,7 +173,7 @@ public class CometEventImpl implements CometEvent {
             for (CometEvent.CometOperation co : operations) {
                 if (!cometOperations.contains(co)) {
                     cometOperations.add(co);
-                    request.action(ActionCode.ACTION_COMET_REGISTER, co);
+                    request.action(ActionCode.ACTION_COMET_REGISTER, translate(co));
                 }
             }
         }
@@ -185,7 +186,7 @@ public class CometEventImpl implements CometEvent {
             for (CometEvent.CometOperation co : operations) {
                 if (cometOperations.contains(co)) {
                     cometOperations.remove(co);
-                    request.action(ActionCode.ACTION_COMET_UNREGISTER, co);
+                    request.action(ActionCode.ACTION_COMET_UNREGISTER, translate(co));
                 }
             }
         }
@@ -223,9 +224,22 @@ public class CometEventImpl implements CometEvent {
             throw new IllegalStateException("The operation can only be performed when invoked by a Tomcat worker thread.");
     }
     
+    protected PollerInterest translate(CometOperation op) {
+        if ( op == CometEvent.CometOperation.OP_READ )
+            return PollerInterest.READ;
+        else if ( op == CometEvent.CometOperation.OP_WRITE )
+            return PollerInterest.WRITE;
+        else if ( op == CometEvent.CometOperation.OP_CALLBACK )
+            return PollerInterest.CALLBACK;
+        else 
+            throw new IllegalArgumentException(op!=null?op.toString():"null");
+    }
+    
     //inner class used to keep track if the current thread is a worker thread.
     private static class WorkerThreadCheck extends ThreadLocal {
         
     }
+    
+    
 
 }
index 13bf343..84be8a4 100644 (file)
@@ -53,6 +53,7 @@ import org.apache.tomcat.util.net.SocketStatus;
 import org.apache.tomcat.util.net.NioEndpoint.Handler.SocketState;
 import org.apache.tomcat.util.res.StringManager;
 import org.apache.tomcat.util.net.NioEndpoint.KeyAttachment;
+import org.apache.tomcat.util.net.PollerInterest;
 
 
 /**
@@ -1221,10 +1222,37 @@ public class Http11NioProcessor implements ActionHook {
             attach.setTimeout(to.longValue());
         } else if (actionCode == ActionCode.ACTION_COMET_END) {
             comet = false;
+        } else if (actionCode == ActionCode.ACTION_COMET_REGISTER) {
+            int interest = getPollerInterest(param);
+            NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socket.getAttachment(false);
+            attach.setCometOps(attach.getCometOps()|interest);
+            attach.getPoller().cometInterest(socket);
+        } else if (actionCode == ActionCode.ACTION_COMET_UNREGISTER) {
+            int interest = getPollerInterest(param);
+            NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socket.getAttachment(false);
+            attach.setCometOps(attach.getCometOps()& (~interest));
+            attach.getPoller().cometInterest(socket);
+        } else if (actionCode == ActionCode.ACTION_COMET_CONFIGURE) {
         }
 
     }
 
+    private int getPollerInterest(Object param) throws IllegalArgumentException {
+        if ( param == null || (!(param instanceof PollerInterest)) )
+            throw new IllegalArgumentException("Action parameter must be a PollerInterest object.");
+        int interest = 0;
+        PollerInterest pi = (PollerInterest)param;
+        if ( pi == PollerInterest.CALLBACK )
+            interest = NioEndpoint.OP_CALLBACK;
+        else if ( pi == PollerInterest.READ ) 
+            interest  = SelectionKey.OP_READ;
+        else if ( pi == PollerInterest.WRITE ) 
+            interest = SelectionKey.OP_WRITE;
+        else
+            throw new IllegalArgumentException(pi!=null?pi.toString():"null");
+        return interest;
+    }
+
 
     // ------------------------------------------------------ Connector Methods
 
index 5aefdff..3f8fbd7 100644 (file)
@@ -103,7 +103,8 @@ public class NioEndpoint {
      */
     public static final String SESSION_ID_KEY = "javax.servlet.request.ssl_session";
 
-    public static final int OP_REGISTER = -1; //register interest op
+    public static final int OP_REGISTER = 0x100; //register interest op
+    public static final int OP_CALLBACK = 0x200; //callback interest op
     
     // ----------------------------------------------------------------- Fields
 
@@ -1312,6 +1313,14 @@ public class NioEndpoint {
             events.offer(event);
             if ( wakeupCounter.incrementAndGet() < 3 ) selector.wakeup();
         }
+        
+        public void cometInterest(NioChannel socket) {
+            throw new UnsupportedOperationException();
+        }
+        
+        public void wakeup() {
+            selector.wakeup();
+        }
 
         /**
          * Add specified socket and associated pool to the poller. The socket will
@@ -1648,6 +1657,8 @@ public class NioEndpoint {
         public void access(long access) { lastAccess = access; }
         public void setComet(boolean comet) { this.comet = comet; }
         public boolean getComet() { return comet; }
+        public void setCometOps(int ops) { this.cometOps = ops; }
+        public int getCometOps() { return cometOps; }
         public boolean getCurrentAccess() { return currentAccess; }
         public void setCurrentAccess(boolean access) { currentAccess = access; }
         public Object getMutex() {return mutex;}
@@ -1697,6 +1708,7 @@ public class NioEndpoint {
         protected long lastAccess = -1;
         protected boolean currentAccess = false;
         protected boolean comet = false;
+        protected int cometOps = 0;
         protected long timeout = -1;
         protected boolean error = false;
         protected NioChannel channel = null;
diff --git a/java/org/apache/tomcat/util/net/PollerInterest.java b/java/org/apache/tomcat/util/net/PollerInterest.java
new file mode 100644 (file)
index 0000000..3b47a3a
--- /dev/null
@@ -0,0 +1,26 @@
+/*\r
+ *  Licensed to the Apache Software Foundation (ASF) under one or more\r
+ *  contributor license agreements.  See the NOTICE file distributed with\r
+ *  this work for additional information regarding copyright ownership.\r
+ *  The ASF licenses this file to You under the Apache License, Version 2.0\r
+ *  (the "License"); you may not use this file except in compliance with\r
+ *  the License.  You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ */\r
+\r
+package org.apache.tomcat.util.net;\r
+\r
+/**\r
+ * Different poller inter\r
+ * @author fhanik\r
+ */\r
+public enum PollerInterest {\r
+    READ, WRITE, CALLBACK;\r
+}\r