hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r613538 - in /httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor: DefaultListeningIOReactor.java ListenerEndpointClosedCallback.java ListenerEndpointImpl.java
Date Sun, 20 Jan 2008 11:49:17 GMT
Author: olegk
Date: Sun Jan 20 03:49:15 2008
New Revision: 613538

URL: http://svn.apache.org/viewvc?rev=613538&view=rev
Log:
* Code improvements in DefaultListeningIOReactor and ListenerEndpointImpl
* Worked-around the problem that appears to be a bug in the NIO implementation on MS Windows
platforms: for some inexplicable reason a key can be returned by the selector as valid and
acceptable even after it has been canceled. An attempt to accept a connection from that channel,
however, causes an ClosedChannelException

Added:
    httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointClosedCallback.java
  (with props)
Modified:
    httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java
    httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointImpl.java

Modified: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java?rev=613538&r1=613537&r2=613538&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java
(original)
+++ httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java
Sun Jan 20 03:49:15 2008
@@ -34,6 +34,7 @@
 import java.io.IOException;
 import java.net.SocketAddress;
 import java.nio.channels.CancelledKeyException;
+import java.nio.channels.ClosedChannelException;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
@@ -113,9 +114,16 @@
                 SocketChannel socketChannel = null;
                 try {
                     socketChannel = serverChannel.accept();
+                } catch (ClosedChannelException ex) {
+                    // On MS Windows for some inexplicable reason a key can be returned by

+                    // the selector as valid and acceptable even after it has been canceled.

+                    // An attempt to accept a connection from that channel, however, causes

+                    // an ClosedChannelException
+                    key.cancel();
+                    socketChannel = null;
                 } catch (IOException ex) {
-                    if (this.exceptionHandler == null 
-                            || !this.exceptionHandler.handle(ex)) {
+                    if (this.exceptionHandler == null || 
+                            !this.exceptionHandler.handle(ex)) {
                         throw new IOReactorException(
                                 "Failure accepting connection", ex);
                     }
@@ -137,17 +145,30 @@
             }
             
         } catch (CancelledKeyException ex) {
-            ListenerEndpointImpl endpoint = (ListenerEndpointImpl) key.attachment();
+            ListenerEndpoint endpoint = (ListenerEndpoint) key.attachment();
             this.endpoints.remove(endpoint);
             key.attach(null);
         }
     }
 
+    private ListenerEndpointImpl createEnppoint(final SocketAddress address) {
+        ListenerEndpointImpl endpoint = new ListenerEndpointImpl(
+                address,
+                new ListenerEndpointClosedCallback() {
+
+                    public void endpointClosed(final ListenerEndpoint endpoint) {
+                        endpoints.remove(endpoint);
+                    }
+                    
+                });
+        return endpoint;
+    }
+    
     public ListenerEndpoint listen(final SocketAddress address) {
         if (this.status.compareTo(IOReactorStatus.ACTIVE) > 0) {
             throw new IllegalStateException("I/O reactor has been shut down");
         }
-        ListenerEndpointImpl request = new ListenerEndpointImpl(address);
+        ListenerEndpointImpl request = createEnppoint(address);
         this.requestQueue.add(request);
         this.selector.wakeup();
         return request;
@@ -185,7 +206,7 @@
             }
             
             this.endpoints.add(request);
-            request.completed(serverChannel);
+            request.completed(serverChannel.socket().getLocalSocketAddress());
         }
     }
     
@@ -228,8 +249,8 @@
             return;
         }
         this.paused = false;
-        for (SocketAddress socketAddress: this.pausedEndpoints) {
-            ListenerEndpointImpl request = new ListenerEndpointImpl(socketAddress);
+        for (SocketAddress address: this.pausedEndpoints) {
+            ListenerEndpointImpl request = createEnppoint(address);
             this.requestQueue.add(request);
         }
         this.pausedEndpoints.clear();

Added: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointClosedCallback.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointClosedCallback.java?rev=613538&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointClosedCallback.java
(added)
+++ httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointClosedCallback.java
Sun Jan 20 03:49:15 2008
@@ -0,0 +1,40 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.nio.reactor;
+
+import org.apache.http.nio.reactor.ListenerEndpoint;
+
+public interface ListenerEndpointClosedCallback {
+
+    void endpointClosed(ListenerEndpoint endpoint);
+    
+}

Propchange: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointClosedCallback.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointClosedCallback.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointClosedCallback.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointImpl.java?rev=613538&r1=613537&r2=613538&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointImpl.java
(original)
+++ httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointImpl.java
Sun Jan 20 03:49:15 2008
@@ -33,8 +33,8 @@
 
 import java.io.IOException;
 import java.net.SocketAddress;
+import java.nio.channels.Channel;
 import java.nio.channels.SelectionKey;
-import java.nio.channels.ServerSocketChannel;
 
 import org.apache.http.nio.reactor.ListenerEndpoint;
 
@@ -44,22 +44,23 @@
     private volatile boolean closed;
     private volatile SelectionKey key;
     private volatile SocketAddress address;
-    private volatile ServerSocketChannel serverChannel = null;
+    private volatile IOException exception;
 
-    private IOException exception = null;
+    private final ListenerEndpointClosedCallback callback;
     
-    public ListenerEndpointImpl(final SocketAddress address) {
+    public ListenerEndpointImpl(
+            final SocketAddress address,
+            final ListenerEndpointClosedCallback callback) {
         super();
         if (address == null) {
             throw new IllegalArgumentException("Address may not be null");
         }
         this.address = address;
+        this.callback = callback;
     }
     
     public SocketAddress getAddress() {
-        synchronized (this) {
-            return this.address;
-        }
+        return this.address;
     }
     
     public boolean isCompleted() {
@@ -67,9 +68,7 @@
     }
     
     public IOException getException() {
-        synchronized (this) {
-            return this.exception;
-        }
+        return this.exception;
     }
     
     public void waitFor() throws InterruptedException {
@@ -83,7 +82,7 @@
         }
     }
     
-    public void completed(final ServerSocketChannel serverChannel) {
+    public void completed(final SocketAddress address) {
         if (address == null) {
             throw new IllegalArgumentException("Address may not be null");
         }
@@ -92,8 +91,7 @@
         }
         this.completed = true;
         synchronized (this) {
-            this.address = serverChannel.socket().getLocalSocketAddress();
-            this.serverChannel = serverChannel;
+            this.address = address;
             notifyAll();
         }
     }
@@ -137,13 +135,17 @@
         }
         this.completed = true;
         this.closed = true;
-        if (this.serverChannel != null && this.serverChannel.isOpen()) {
-            try {
-                this.serverChannel.close();
-            } catch (IOException ignore) {}
-        }
         if (this.key != null) {
             this.key.cancel();
+            Channel channel = this.key.channel();
+            if (channel.isOpen()) {
+                try {
+                    channel.close();
+                } catch (IOException ignore) {}
+            }
+        }
+        if (this.callback != null) {
+            this.callback.endpointClosed(this);
         }
         synchronized (this) {
             notifyAll();



Mime
View raw message