You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2009/03/22 11:06:52 UTC

svn commit: r757164 - in /httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor: AbstractIOReactor.java IOSessionImpl.java InterestOpEntry.java

Author: olegk
Date: Sun Mar 22 10:06:52 2009
New Revision: 757164

URL: http://svn.apache.org/viewvc?rev=757164&view=rev
Log:
HTTPCORE-155: Compatibility mode with IBM JRE and other JREs with naive (broken) implementation of SelectionKey.

* Eliminated the need for AbstractIOReactor to call protected methods of IOSessionImpl in order to apply event mask
* Implemented #hashCode and #equals for InterestOpEntry


Modified:
    httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java
    httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
    httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/InterestOpEntry.java

Modified: httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java?rev=757164&r1=757163&r2=757164&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java (original)
+++ httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java Sun Mar 22 10:06:52 2009
@@ -40,11 +40,9 @@
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
 import java.nio.channels.SocketChannel;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Queue;
 import java.util.Set;
 import java.util.concurrent.ConcurrentLinkedQueue;
@@ -73,7 +71,7 @@
     private final boolean interestOpsQueueing;
     private final Selector selector;
     private final Set<IOSession> sessions;
-    private final List<InterestOpEntry> interestOpsQueue;
+    private final Queue<InterestOpEntry> interestOpsQueue;
     private final Queue<IOSession> closedSessions;
     private final Queue<ChannelEntry> newChannels;
 
@@ -91,7 +89,7 @@
         this.selectTimeout = selectTimeout;
         this.interestOpsQueueing = interestOpsQueueing;
         this.sessions = Collections.synchronizedSet(new HashSet<IOSession>());
-        this.interestOpsQueue = new ArrayList<InterestOpEntry>();
+        this.interestOpsQueue = new ConcurrentLinkedQueue<InterestOpEntry>();
         this.closedSessions = new ConcurrentLinkedQueue<IOSession>();
         this.newChannels = new ConcurrentLinkedQueue<ChannelEntry>();
         try {
@@ -433,19 +431,16 @@
             return;
         }
         synchronized (this.interestOpsQueue) {
-            // determine this interestOps() queue's size
-            int size = this.interestOpsQueue.size();
-
-            for (int i = 0; i < size; i++) {
+            while (!this.interestOpsQueue.isEmpty()) {
                 // get the first queue element
-                InterestOpEntry queueElement = this.interestOpsQueue.remove(0);
+                InterestOpEntry entry = this.interestOpsQueue.remove();
 
                 // obtain the operation's details
-                IOSessionImpl ioSession = queueElement.getIoSession();
-                int eventMask = queueElement.getEventMask();
-
-                // perform the operation
-                ioSession.applyEventMask(eventMask);
+                SelectionKey key = entry.getSelectionKey();
+                int eventMask = entry.getEventMask();
+                if (key.isValid()) {
+                    key.interestOps(eventMask);
+                }
             }
         }
     }
@@ -585,9 +580,6 @@
         if (entry == null) {
             return false;
         }
-        if (entry.getIoSession() == null) {
-            return false;
-        }
 
         synchronized (this.interestOpsQueue) {
             // add this operation to the interestOps() queue

Modified: httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java?rev=757164&r1=757163&r2=757164&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java (original)
+++ httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java Sun Mar 22 10:06:52 2009
@@ -124,7 +124,7 @@
             this.currentEventMask = ops;
 
             // local variable
-            InterestOpEntry entry = new InterestOpEntry(this, this.currentEventMask);
+            InterestOpEntry entry = new InterestOpEntry(this.key, this.currentEventMask);
 
             // add this operation to the interestOps() queue
             this.abstractIOReactor.addInterestOpsQueueElement(entry);
@@ -143,7 +143,7 @@
             this.currentEventMask |= op;
 
             // local variable
-            InterestOpEntry entry = new InterestOpEntry(this, this.currentEventMask);
+            InterestOpEntry entry = new InterestOpEntry(this.key, this.currentEventMask);
 
             // add this operation to the interestOps() queue
             this.abstractIOReactor.addInterestOpsQueueElement(entry);
@@ -165,7 +165,7 @@
             this.currentEventMask &= ~op;
 
             // local variable
-            InterestOpEntry entry = new InterestOpEntry(this, this.currentEventMask);
+            InterestOpEntry entry = new InterestOpEntry(this.key, this.currentEventMask);
 
             // add this operation to the interestOps() queue
             this.abstractIOReactor.addInterestOpsQueueElement(entry);
@@ -178,13 +178,6 @@
         this.key.selector().wakeup();
     }
 
-    protected void applyEventMask(int ops) {
-        if (this.status == CLOSED) {
-            return;
-        }
-        this.key.interestOps(ops);
-    }
-    
     public int getSocketTimeout() {
         return this.socketTimeout;
     }

Modified: httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/InterestOpEntry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/InterestOpEntry.java?rev=757164&r1=757163&r2=757164&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/InterestOpEntry.java (original)
+++ httpcomponents/httpcore/branches/ibm_compat_branch/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/InterestOpEntry.java Sun Mar 22 10:06:52 2009
@@ -31,27 +31,42 @@
 
 package org.apache.http.impl.nio.reactor;
 
+import java.nio.channels.SelectionKey;
+
 /**
  * Helper class, representing an element on an {@link java.nio.channels.SelectionKey#interestOps(int) 
  * interestOps(int)} queue.
  */
 class InterestOpEntry {
 
-    private final IOSessionImpl ioSession;
+    private final SelectionKey key;
     private final int eventMask;
 
-    public InterestOpEntry(IOSessionImpl ioSession, int eventMask) {
-        // initialize instance members
-        this.ioSession = ioSession;
+    public InterestOpEntry(final SelectionKey key, int eventMask) {
+        super();
+        if (key == null) {
+            throw new IllegalArgumentException("Selection key may not be null");
+        }
+        this.key = key;
         this.eventMask = eventMask;
     }
 
-    public IOSessionImpl getIoSession() {
-        return ioSession;
+    public SelectionKey getSelectionKey() {
+        return this.key;
     }
 
     public int getEventMask() {
-        return eventMask;
+        return this.eventMask;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        return this.key.equals(obj);
+    }
+
+    @Override
+    public int hashCode() {
+        return this.key.hashCode();
     }
 
 }