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 2007/11/29 17:38:14 UTC

svn commit: r599506 - in /jakarta/httpcomponents/httpcore/trunk: module-nio/src/main/java/org/apache/http/impl/nio/reactor/ module-nio/src/main/java/org/apache/http/nio/reactor/ module-nio/src/test/java/org/apache/http/impl/nio/reactor/ module-nio/src/...

Author: olegk
Date: Thu Nov 29 08:38:10 2007
New Revision: 599506

URL: http://svn.apache.org/viewvc?rev=599506&view=rev
Log:
HTTPCORE-127: Improved management of listener endpoints

Added:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointImpl.java   (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointQueue.java   (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListenerEndpoint.java   (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListeningIOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestBaseIOReactorSSL.java
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/mockup/TestHttpSSLServer.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java?rev=599506&r1=599505&r2=599506&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java Thu Nov 29 08:38:10 2007
@@ -41,6 +41,7 @@
 import java.util.Set;
 
 import org.apache.http.nio.reactor.IOReactorException;
+import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.nio.reactor.ListeningIOReactor;
 import org.apache.http.params.HttpParams;
 import org.apache.http.util.concurrent.ThreadFactory;
@@ -48,11 +49,14 @@
 public class DefaultListeningIOReactor extends AbstractMultiworkerIOReactor 
         implements ListeningIOReactor {
 
+    private final ListenerEndpointQueue requestQueue;
+    
     public DefaultListeningIOReactor(
             int workerCount, 
             final ThreadFactory threadFactory,
             final HttpParams params) throws IOReactorException {
         super(workerCount, threadFactory, params);
+        this.requestQueue = new ListenerEndpointQueue();
     }
 
     public DefaultListeningIOReactor(
@@ -62,6 +66,8 @@
     }
     
     protected void processEvents(int readyCount) throws IOReactorException {
+        processSessionRequests();
+
         if (readyCount > 0) {
             Set selectedKeys = this.selector.selectedKeys();
             for (Iterator it = selectedKeys.iterator(); it.hasNext(); ) {
@@ -108,17 +114,46 @@
         }
     }
 
-    public SocketAddress listen(
-            final SocketAddress address) throws IOException {
+    public ListenerEndpoint listen(final SocketAddress address) {
         if (this.status > ACTIVE) {
             throw new IllegalStateException("I/O reactor has been shut down");
         }
-        ServerSocketChannel serverChannel = ServerSocketChannel.open();
-        serverChannel.configureBlocking(false);
-        serverChannel.socket().bind(address);
-        SelectionKey key = serverChannel.register(this.selector, SelectionKey.OP_ACCEPT);
-        key.attach(null);
-        return serverChannel.socket().getLocalSocketAddress();
+        ListenerEndpointImpl request = new ListenerEndpointImpl(address);
+        this.requestQueue.push(request);
+        this.selector.wakeup();
+        return request;
     }
 
+    private void processSessionRequests() throws IOReactorException {
+        ListenerEndpointImpl request;
+        while ((request = this.requestQueue.pop()) != null) {
+            SocketAddress address = request.getAddress();
+            ServerSocketChannel serverChannel;
+            try {
+                serverChannel = ServerSocketChannel.open();
+                serverChannel.configureBlocking(false);
+            } catch (IOException ex) {
+                throw new IOReactorException("Failure opening server socket", ex);
+            }
+            try {
+                serverChannel.socket().bind(address);
+            } catch (IOException ex) {
+                request.failed(ex);
+                if (this.exceptionHandler == null || !this.exceptionHandler.handle(ex)) {
+                    throw new IOReactorException("Failure binding socket to address " 
+                            + address, ex);
+                }
+            }
+            try {
+                SelectionKey key = serverChannel.register(this.selector, SelectionKey.OP_ACCEPT);
+                key.attach(request);
+                request.setKey(key);
+            } catch (IOException ex) {
+                throw new IOReactorException("Failure registering channel " +
+                        "with the selector", ex);
+            }
+            request.completed(serverChannel.socket().getLocalSocketAddress());
+        }
+    }
+    
 }

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointImpl.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointImpl.java?rev=599506&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointImpl.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointImpl.java Thu Nov 29 08:38:10 2007
@@ -0,0 +1,130 @@
+/*
+ * $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 java.io.IOException;
+import java.net.SocketAddress;
+import java.nio.channels.SelectionKey;
+
+import org.apache.http.nio.reactor.ListenerEndpoint;
+
+public class ListenerEndpointImpl implements ListenerEndpoint {
+
+    private volatile boolean completed;
+    private volatile boolean closed;
+    private volatile SelectionKey key;
+    private SocketAddress address;
+
+    private IOException exception = null;
+    
+    public ListenerEndpointImpl(final SocketAddress address) {
+        super();
+        if (address == null) {
+            throw new IllegalArgumentException("Address may not be null");
+        }
+        this.address = address;
+    }
+    
+    public SocketAddress getAddress() {
+        synchronized (this) {
+            return this.address;
+        }
+    }
+    
+    public IOException getException() {
+        synchronized (this) {
+            return this.exception;
+        }
+    }
+    
+    public void waitFor() throws InterruptedException {
+        if (this.completed) {
+            return;
+        }
+        synchronized (this) {
+            while (!this.completed) {
+                wait();
+            }
+        }
+    }
+    
+    public void completed(final SocketAddress address) {
+        if (address == null) {
+            throw new IllegalArgumentException("Address may not be null");
+        }
+        if (this.completed) {
+            return;
+        }
+        this.completed = true;
+        synchronized (this) {
+            this.address = address;
+            notifyAll();
+        }
+    }
+ 
+    public void failed(final IOException exception) {
+        if (exception == null) {
+            return;
+        }
+        if (this.completed) {
+            return;
+        }
+        this.completed = true;
+        synchronized (this) {
+            this.exception = exception;
+            notifyAll();
+        }
+    }
+ 
+    protected void setKey(final SelectionKey key) {
+        this.key = key;
+    }
+
+    public boolean isClosed() {
+        return this.closed || (this.key != null && !this.key.isValid());
+    }
+
+    public void close() {
+        if (this.closed) {
+            return;
+        }
+        this.completed = true;
+        this.closed = true;
+        if (this.key != null) {
+            this.key.cancel();
+        }
+        synchronized (this) {
+            notifyAll();
+        }
+    }
+    
+}

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

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

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

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointQueue.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointQueue.java?rev=599506&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointQueue.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/ListenerEndpointQueue.java Thu Nov 29 08:38:10 2007
@@ -0,0 +1,60 @@
+/*
+ * $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 java.util.LinkedList;
+
+public class ListenerEndpointQueue {
+    
+    private final LinkedList list;
+    
+    public ListenerEndpointQueue() {
+        super();
+        this.list = new LinkedList();
+    }
+
+    public synchronized void push(final ListenerEndpointImpl entry) {
+        if (entry == null) {
+            return;
+        }
+        this.list.addLast(entry);
+    }
+
+    public synchronized ListenerEndpointImpl pop() {
+        if (!this.list.isEmpty()) {
+            return (ListenerEndpointImpl) this.list.removeFirst();
+        } else {
+            return null;
+        }
+    }
+    
+}

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

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

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

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListenerEndpoint.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListenerEndpoint.java?rev=599506&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListenerEndpoint.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListenerEndpoint.java Thu Nov 29 08:38:10 2007
@@ -0,0 +1,49 @@
+/*
+ * $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.nio.reactor;
+
+import java.io.IOException;
+import java.net.SocketAddress;
+
+public interface ListenerEndpoint {
+
+    SocketAddress getAddress();
+    
+    IOException getException();
+
+    void waitFor() throws InterruptedException;
+    
+    boolean isClosed();
+    
+    void close();
+    
+}

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

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

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

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListeningIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListeningIOReactor.java?rev=599506&r1=599505&r2=599506&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListeningIOReactor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListeningIOReactor.java Thu Nov 29 08:38:10 2007
@@ -31,12 +31,10 @@
 
 package org.apache.http.nio.reactor;
 
-import java.io.IOException;
 import java.net.SocketAddress;
 
 public interface ListeningIOReactor extends IOReactor {
 
-    SocketAddress listen(SocketAddress address) 
-        throws IOException;
+    ListenerEndpoint listen(SocketAddress address);
     
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java?rev=599506&r1=599505&r2=599506&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java Thu Nov 29 08:38:10 2007
@@ -50,6 +50,7 @@
 import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
 import org.apache.http.nio.reactor.IOReactor;
 import org.apache.http.nio.reactor.IOReactorExceptionHandler;
+import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.params.BasicHttpParams;
 import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.HttpContext;
@@ -168,7 +169,10 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < connNo; i++) {
             this.client.openConnection(
@@ -238,7 +242,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         this.client.openConnection(
                 new InetSocketAddress("localhost", serverAddress.getPort()), 
@@ -322,7 +328,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         this.client.openConnection(
                 new InetSocketAddress("localhost", serverAddress.getPort()), 
@@ -409,7 +417,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         this.client.openConnection(
                 new InetSocketAddress("localhost", serverAddress.getPort()), 

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java?rev=599506&r1=599505&r2=599506&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java Thu Nov 29 08:38:10 2007
@@ -39,6 +39,7 @@
 import org.apache.http.nio.NHttpServiceHandler;
 import org.apache.http.nio.reactor.IOEventDispatch;
 import org.apache.http.nio.reactor.IOReactorExceptionHandler;
+import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.params.HttpParams;
 
 /**
@@ -50,16 +51,14 @@
 
     private final DefaultListeningIOReactor ioReactor;
     private final HttpParams params;
-    private final Object socketMutex;
 
     private volatile IOReactorThread thread;
-    private volatile InetSocketAddress address;
+    private ListenerEndpoint endpoint;
     
     public TestHttpServer(final HttpParams params) throws IOException {
         super();
         this.ioReactor = new DefaultListeningIOReactor(2, params);
         this.params = params;
-        this.socketMutex = new Object();
     }
 
     public HttpParams getParams() {
@@ -71,12 +70,6 @@
     }
 
     private void execute(final NHttpServiceHandler serviceHandler) throws IOException {
-        synchronized (this.socketMutex) {
-            this.address = (InetSocketAddress) this.ioReactor.listen(
-                    new InetSocketAddress(0));
-            this.socketMutex.notifyAll();
-        }
-        
         IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(
                 serviceHandler, 
                 this.params);
@@ -84,16 +77,16 @@
         this.ioReactor.execute(ioEventDispatch);
     }
     
-    public InetSocketAddress getSocketAddress() throws InterruptedException {
-        synchronized (this.socketMutex) {
-            while (this.address == null) {
-                this.socketMutex.wait();
-            }
-        }
-        return this.address;
+    public ListenerEndpoint getListenerEndpoint() {
+        return this.endpoint;
+    }
+
+    public void setEndpoint(ListenerEndpoint endpoint) {
+        this.endpoint = endpoint;
     }
 
     public void start(final NHttpServiceHandler serviceHandler) {
+        this.endpoint = this.ioReactor.listen(new InetSocketAddress(0));
         this.thread = new IOReactorThread(serviceHandler);
         this.thread.start();
     }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java?rev=599506&r1=599505&r2=599506&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java Thu Nov 29 08:38:10 2007
@@ -65,6 +65,7 @@
 import org.apache.http.nio.NHttpClientHandler;
 import org.apache.http.nio.NHttpConnection;
 import org.apache.http.nio.NHttpServiceHandler;
+import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.params.BasicHttpParams;
 import org.apache.http.params.CoreConnectionPNames;
 import org.apache.http.params.CoreProtocolPNames;
@@ -293,7 +294,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < responseData.length; i++) {
             this.client.openConnection(
@@ -433,7 +436,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < responseData.length; i++) {
             this.client.openConnection(
@@ -573,7 +578,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < responseData.length; i++) {
             this.client.openConnection(
@@ -721,7 +728,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < responseData.length; i++) {
             this.client.openConnection(
@@ -864,7 +873,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < responseData.length; i++) {
             this.client.openConnection(
@@ -1012,7 +1023,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         this.client.openConnection(
                 new InetSocketAddress("localhost", serverAddress.getPort()), 
@@ -1133,7 +1146,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
 
         method[0] = "GET";
         

Modified: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestBaseIOReactorSSL.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestBaseIOReactorSSL.java?rev=599506&r1=599505&r2=599506&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestBaseIOReactorSSL.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestBaseIOReactorSSL.java Thu Nov 29 08:38:10 2007
@@ -32,6 +32,7 @@
 import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
+import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.net.URL;
 import java.security.KeyStore;
@@ -54,6 +55,7 @@
 import org.apache.http.nio.NHttpServiceHandler;
 import org.apache.http.nio.protocol.BufferingHttpServiceHandler;
 import org.apache.http.nio.protocol.EventListener;
+import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.params.BasicHttpParams;
 import org.apache.http.params.CoreConnectionPNames;
 import org.apache.http.params.HttpParams;
@@ -145,7 +147,11 @@
         SSLContext sslcontext = SSLContext.getInstance("TLS");
         sslcontext.init(null, trustmanagers, null);        
 
-        Socket socket = sslcontext.getSocketFactory().createSocket("localhost", server.getSocketAddress().getPort());
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
+        
+        Socket socket = sslcontext.getSocketFactory().createSocket("localhost", serverAddress.getPort());
         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
         //            123456789012345678901234567890
         writer.write("GET / HTTP/1.1\r\n");

Modified: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java?rev=599506&r1=599505&r2=599506&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java Thu Nov 29 08:38:10 2007
@@ -48,6 +48,7 @@
 import org.apache.http.nio.NHttpServiceHandler;
 import org.apache.http.nio.protocol.EventListener;
 import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
+import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.params.BasicHttpParams;
 import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.HttpContext;
@@ -164,7 +165,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < connNo; i++) {
             this.client.openConnection(

Modified: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java?rev=599506&r1=599505&r2=599506&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java Thu Nov 29 08:38:10 2007
@@ -58,6 +58,7 @@
 import org.apache.http.nio.NHttpConnection;
 import org.apache.http.nio.NHttpServiceHandler;
 import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
+import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.params.CoreProtocolPNames;
 import org.apache.http.protocol.ExecutionContext;
 import org.apache.http.protocol.HttpContext;
@@ -197,7 +198,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < responseData.length; i++) {
             this.client.openConnection(
@@ -339,7 +342,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < responseData.length; i++) {
             this.client.openConnection(
@@ -484,7 +489,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < responseData.length; i++) {
             this.client.openConnection(
@@ -634,7 +641,9 @@
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
         
-        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
         
         for (int i = 0; i < responseData.length; i++) {
             this.client.openConnection(

Modified: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/mockup/TestHttpSSLServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/mockup/TestHttpSSLServer.java?rev=599506&r1=599505&r2=599506&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/mockup/TestHttpSSLServer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/mockup/TestHttpSSLServer.java Thu Nov 29 08:38:10 2007
@@ -44,6 +44,7 @@
 import org.apache.http.impl.nio.reactor.SSLServerIOEventDispatch;
 import org.apache.http.nio.NHttpServiceHandler;
 import org.apache.http.nio.reactor.IOEventDispatch;
+import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.nio.reactor.ListeningIOReactor;
 import org.apache.http.params.HttpParams;
 
@@ -57,16 +58,14 @@
     private final SSLContext sslcontext;
     private final ListeningIOReactor ioReactor;
     private final HttpParams params;
-    private final Object socketMutex;
     
     private volatile IOReactorThread thread;
-    private volatile InetSocketAddress address;
+    private ListenerEndpoint endpoint;
     
     public TestHttpSSLServer(final HttpParams params) throws Exception {
         super();
         this.params = params;
         this.ioReactor = new DefaultListeningIOReactor(2, this.params);
-        this.socketMutex = new Object();
         
         ClassLoader cl = getClass().getClassLoader();
         URL url = cl.getResource("test.keystore");
@@ -85,12 +84,6 @@
     }
     
     private void execute(final NHttpServiceHandler serviceHandler) throws IOException {
-        synchronized (this.socketMutex) {
-            this.address = (InetSocketAddress) this.ioReactor.listen(
-                    new InetSocketAddress(0));
-            this.socketMutex.notifyAll();
-        }
-        
         IOEventDispatch ioEventDispatch = new SSLServerIOEventDispatch(
                 serviceHandler, 
                 this.sslcontext,
@@ -98,17 +91,13 @@
         
         this.ioReactor.execute(ioEventDispatch);
     }
-    
-    public InetSocketAddress getSocketAddress() throws InterruptedException {
-        synchronized (this.socketMutex) {
-            while (this.address == null) {
-                this.socketMutex.wait();
-            }
-        }
-        return this.address;
+
+    public ListenerEndpoint getListenerEndpoint() {
+        return this.endpoint;
     }
 
     public void start(final NHttpServiceHandler serviceHandler) {
+        this.endpoint = this.ioReactor.listen(new InetSocketAddress(0));
         this.thread = new IOReactorThread(serviceHandler);
         this.thread.start();
     }