From: fhanik Date: Tue, 29 May 2007 10:29:25 +0000 (+0000) Subject: Added in the registration of comet interest operations X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=e4adf30fcce1c400e68b24ec63650126f6a5867b;p=tomcat7.0 Added in the registration of comet interest operations 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 --- diff --git a/java/org/apache/catalina/connector/CometEventImpl.java b/java/org/apache/catalina/connector/CometEventImpl.java index df76d56ab..f2d6d0675 100644 --- a/java/org/apache/catalina/connector/CometEventImpl.java +++ b/java/org/apache/catalina/connector/CometEventImpl.java @@ -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 { } + + } diff --git a/java/org/apache/coyote/http11/Http11NioProcessor.java b/java/org/apache/coyote/http11/Http11NioProcessor.java index 13bf343d1..84be8a48b 100644 --- a/java/org/apache/coyote/http11/Http11NioProcessor.java +++ b/java/org/apache/coyote/http11/Http11NioProcessor.java @@ -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 diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java index 5aefdff18..3f8fbd744 100644 --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -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 index 000000000..3b47a3ab9 --- /dev/null +++ b/java/org/apache/tomcat/util/net/PollerInterest.java @@ -0,0 +1,26 @@ +/* + * 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; + +/** + * Different poller inter + * @author fhanik + */ +public enum PollerInterest { + READ, WRITE, CALLBACK; +}