Skip to content

Commit

Permalink
Simplified connect Promise implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbordet committed Mar 25, 2013
1 parent 0742054 commit 6a8049f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import javax.net.ssl.SSLEngine;

Expand All @@ -61,7 +60,6 @@
import org.eclipse.jetty.io.SelectChannelEndPoint;
import org.eclipse.jetty.io.SelectorManager;
import org.eclipse.jetty.io.ssl.SslConnection;
import org.eclipse.jetty.util.FuturePromise;
import org.eclipse.jetty.util.Jetty;
import org.eclipse.jetty.util.Promise;
import org.eclipse.jetty.util.SocketAddressResolver;
Expand Down Expand Up @@ -502,8 +500,8 @@ public void succeeded(SocketAddress socketAddress)
channel.configureBlocking(false);
channel.connect(socketAddress);

Future<Connection> futureConnection = new ConnectionCallback(destination, promise);
selectorManager.connect(channel, futureConnection);
ConnectionCallback callback = new ConnectionCallback(destination, promise);
selectorManager.connect(channel, callback);
}
// Must catch all exceptions, since some like
// UnresolvedAddressException are not IOExceptions.
Expand Down Expand Up @@ -971,15 +969,15 @@ public org.eclipse.jetty.io.Connection newConnection(SocketChannel channel, EndP
HttpConnection connection = newHttpConnection(HttpClient.this, appEndPoint, destination);

appEndPoint.setConnection(connection);
callback.promise.succeeded(connection);
callback.succeeded(connection);

return sslConnection;
}
}
else
{
HttpConnection connection = newHttpConnection(HttpClient.this, endPoint, destination);
callback.promise.succeeded(connection);
callback.succeeded(connection);
return connection;
}
}
Expand All @@ -988,11 +986,11 @@ public org.eclipse.jetty.io.Connection newConnection(SocketChannel channel, EndP
protected void connectionFailed(SocketChannel channel, Throwable ex, Object attachment)
{
ConnectionCallback callback = (ConnectionCallback)attachment;
callback.promise.failed(ex);
callback.failed(ex);
}
}

private class ConnectionCallback extends FuturePromise<Connection>
private class ConnectionCallback implements Promise<Connection>
{
private final HttpDestination destination;
private final Promise<Connection> promise;
Expand All @@ -1002,6 +1000,18 @@ private ConnectionCallback(HttpDestination destination, Promise<Connection> prom
this.destination = destination;
this.promise = promise;
}

@Override
public void succeeded(Connection result)
{
promise.succeeded(result);
}

@Override
public void failed(Throwable x)
{
promise.failed(x);
}
}

private class ContentDecoderFactorySet implements Set<ContentDecoder.Factory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,22 @@ public int getPort()
{
return port;
}

@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Address that = (Address)obj;
return host.equals(that.host) && port == that.port;
}

@Override
public int hashCode()
{
int result = host.hashCode();
result = 31 * result + port;
return result;
}
}
}

0 comments on commit 6a8049f

Please sign in to comment.