Skip to content

Instantly share code, notes, and snippets.

@kelemen
Created September 23, 2012 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelemen/3773036 to your computer and use it in GitHub Desktop.
Save kelemen/3773036 to your computer and use it in GitHub Desktop.
A proposed fix for GRADLE-2495
/*
* Copyright 2010 the original author or authors.
*
* Licensed 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.
*/
package org.gradle.messaging.remote.internal.inet;
import org.gradle.messaging.remote.Address;
import org.gradle.messaging.remote.internal.ConnectException;
import org.gradle.messaging.remote.internal.Connection;
import org.gradle.messaging.remote.internal.MessageSerializer;
import org.gradle.messaging.remote.internal.OutgoingConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.SocketChannel;
import java.util.List;
import java.util.concurrent.CancellationException;
public class TcpOutgoingConnector<T> implements OutgoingConnector<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(TcpOutgoingConnector.class);
private final MessageSerializer<T> serializer;
public TcpOutgoingConnector(MessageSerializer<T> serializer) {
this.serializer = serializer;
}
public Connection<T> connect(Address destinationAddress) {
if (!(destinationAddress instanceof InetEndpoint)) {
throw new IllegalArgumentException(String.format("Cannot create a connection to address of unknown type: %s.", destinationAddress));
}
InetEndpoint address = (InetEndpoint) destinationAddress;
LOGGER.debug("Attempting to connect to {}.", address);
// Try each address in turn. Not all of them are necessarily reachable (eg when socket option IPV6_V6ONLY
// is on - the default for debian and others), so we will try each of them until we can connect
List<InetAddress> candidateAddresses = address.getCandidates();
// Now try each address
Exception failure = null;
for (InetAddress candidate : candidateAddresses) {
LOGGER.debug("Trying to connect to address {}.", candidate);
SocketChannel socketChannel;
try {
socketChannel = SocketChannel.open(new InetSocketAddress(candidate, address.getPort()));
} catch (ClosedByInterruptException ex) {
throw new CancellationException();
} catch (Exception e) {
LOGGER.debug("Cannot connect to address {}, skipping.", candidate);
if (failure == null) {
failure = e;
}
else {
// In Java7 add "e" as a suppressed exception to "failure".
LOGGER.debug("Suppressing exception.", e);
}
continue;
}
LOGGER.debug("Connected to address {}.", candidate);
return new SocketConnection<T>(socketChannel, serializer);
}
throw new ConnectException(String.format("Could not connect to server %s. Tried addresses: %s.",
destinationAddress, candidateAddresses), failure);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment