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 2017/01/04 14:20:52 UTC

svn commit: r1777321 - in /httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5: http/impl/nio/bootstrap/AsyncRequester.java reactor/ConnectionInitiator.java reactor/DefaultConnectingIOReactor.java

Author: olegk
Date: Wed Jan  4 14:20:52 2017
New Revision: 1777321

URL: http://svn.apache.org/viewvc?rev=1777321&view=rev
Log:
ConnectionInitializer to support multi-homed remote endpoints

Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/AsyncRequester.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/reactor/ConnectionInitiator.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/reactor/DefaultConnectingIOReactor.java

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/AsyncRequester.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/AsyncRequester.java?rev=1777321&r1=1777320&r2=1777321&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/AsyncRequester.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/AsyncRequester.java Wed Jan  4 14:20:52 2017
@@ -28,6 +28,7 @@
 package org.apache.hc.core5.http.impl.nio.bootstrap;
 
 import java.io.IOException;
+import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
@@ -74,20 +75,18 @@ public class AsyncRequester extends IORe
                 ioEventHandlerFactory, ioReactorConfig, threadFactory, sessionShutdownCallback);
     }
 
-    private NamedEndpoint toEndpoint(final HttpHost host) {
+    private InetSocketAddress toSocketAddress(final HttpHost host) {
         int port = host.getPort();
         if (port < 0) {
-            final String hostName = host.getHostName();
             final String scheme = host.getSchemeName();
             if ("http".equalsIgnoreCase(scheme)) {
                 port = 80;
             } else if ("https".equalsIgnoreCase(scheme)) {
                 port = 443;
             }
-            return new HttpHost(hostName, port, scheme);
-        } else {
-            return host;
         }
+        final String hostName = host.getHostName();
+        return new InetSocketAddress(hostName, port);
     }
 
     protected SessionRequest requestSession(
@@ -97,7 +96,7 @@ public class AsyncRequester extends IORe
             final SessionRequestCallback callback) {
         Args.notNull(host, "Host");
         Args.notNull(timeUnit, "Time unit");
-        final SessionRequest  sessionRequest = reactor().connect(toEndpoint(host), null, null, callback);
+        final SessionRequest  sessionRequest = reactor().connect(host, toSocketAddress(host), null, null, callback);
         sessionRequest.setConnectTimeout((int) timeUnit.toMillis(timeout));
         return sessionRequest;
     }
@@ -105,10 +104,11 @@ public class AsyncRequester extends IORe
     @Override
     public SessionRequest connect(
             final NamedEndpoint remoteEndpoint,
+            final SocketAddress remoteAddress,
             final SocketAddress localAddress,
             final Object attachment,
             final SessionRequestCallback callback) {
-        return reactor().connect(remoteEndpoint, localAddress, attachment, callback);
+        return reactor().connect(remoteEndpoint, remoteAddress, localAddress, attachment, callback);
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/reactor/ConnectionInitiator.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/reactor/ConnectionInitiator.java?rev=1777321&r1=1777320&r2=1777321&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/reactor/ConnectionInitiator.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/reactor/ConnectionInitiator.java Wed Jan  4 14:20:52 2017
@@ -64,6 +64,7 @@ public interface ConnectionInitiator {
      * timeout.
      *
      * @param remoteEndpoint name of the remote host.
+     * @param remoteAddress remote socket address.
      * @param localAddress local socket address. Can be {@code null},
      *    in which can the default local address and a random port will be used.
      * @param attachment the attachment object. Can be {@code null}.
@@ -72,6 +73,7 @@ public interface ConnectionInitiator {
      */
     SessionRequest connect(
             NamedEndpoint remoteEndpoint,
+            SocketAddress remoteAddress,
             SocketAddress localAddress,
             Object attachment,
             SessionRequestCallback callback);

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/reactor/DefaultConnectingIOReactor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/reactor/DefaultConnectingIOReactor.java?rev=1777321&r1=1777320&r2=1777321&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/reactor/DefaultConnectingIOReactor.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/reactor/DefaultConnectingIOReactor.java Wed Jan  4 14:20:52 2017
@@ -42,6 +42,7 @@ import java.util.concurrent.ThreadFactor
 
 import org.apache.hc.core5.function.Callback;
 import org.apache.hc.core5.net.NamedEndpoint;
+import org.apache.hc.core5.util.Args;
 import org.apache.hc.core5.util.Asserts;
 
 /**
@@ -181,14 +182,19 @@ public class DefaultConnectingIOReactor
     @Override
     public SessionRequest connect(
             final NamedEndpoint remoteEndpoint,
+            final SocketAddress remoteAddress,
             final SocketAddress localAddress,
             final Object attachment,
             final SessionRequestCallback callback) {
+        Args.notNull(remoteEndpoint, "Remote endpoint");
         final IOReactorStatus status = getStatus();
         Asserts.check(status == IOReactorStatus.INACTIVE || status == IOReactorStatus.ACTIVE, "I/O reactor has been shut down");
-        final InetSocketAddress remoteAddress = new InetSocketAddress(remoteEndpoint.getHostName(), remoteEndpoint.getPort());
         final SessionRequestImpl sessionRequest = new SessionRequestImpl(
-                remoteEndpoint, remoteAddress, localAddress, attachment, callback);
+                remoteEndpoint,
+                remoteAddress != null ? remoteAddress : new InetSocketAddress(remoteEndpoint.getHostName(), remoteEndpoint.getPort()),
+                localAddress,
+                attachment,
+                callback);
         sessionRequest.setConnectTimeout(this.reactorConfig.getConnectTimeout());
 
         this.requestQueue.add(sessionRequest);