Skip to content

Commit

Permalink
De-duplicate code in AsyncDnsRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowdalic committed Nov 26, 2018
1 parent 7cdf046 commit 0d53969
Showing 1 changed file with 19 additions and 26 deletions.
Expand Up @@ -14,6 +14,7 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectableChannel;
Expand Down Expand Up @@ -163,21 +164,31 @@ void startHandling() {
}
}

private void abortUdpRequestAndCleanup(DatagramChannel datagramChannel, String errorMessage, IOException exception) {
LOGGER.log(Level.SEVERE, errorMessage, exception);
private void abortRequestAndCleanup(Channel channel, String errorMessage, IOException exception) {
if (exception == null) {
// TODO: Can this case be removed? Is 'exception' ever null?
LOGGER.info("Exception was null in abortRequestAndCleanup()");
exception = new IOException(errorMessage);
}
LOGGER.log(Level.SEVERE, "Error connecting " + channel + ": " + errorMessage, exception);
addException(exception);

if (selectionKey != null) {
selectionKey.cancel();
}

try {
datagramChannel.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Exception closing datagram channel", e);
addException(e);
if (channel != null && channel.isOpen()) {
try {
channel.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Exception closing socket channel", e);
addException(e);
}
}
}

private void abortUdpRequestAndCleanup(DatagramChannel datagramChannel, String errorMessage, IOException exception) {
abortRequestAndCleanup(datagramChannel, errorMessage, exception);
startTcpRequest();
}

Expand Down Expand Up @@ -309,25 +320,7 @@ public void handleChannelSelectedAndNotCancelled(SelectableChannel channel, Sele
}

private void abortTcpRequestAndCleanup(SocketChannel socketChannel, String errorMessage, IOException exception) {
if (exception == null) {
exception = new IOException(errorMessage);
}
LOGGER.log(Level.SEVERE, errorMessage, exception);
addException(exception);

if (selectionKey != null) {
selectionKey.cancel();
}

if (socketChannel != null && socketChannel.isOpen()) {
try {
socketChannel.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Exception closing socket channel", e);
addException(e);
}
}

abortRequestAndCleanup(socketChannel, errorMessage, exception);
future.setException(MultipleIoException.toIOException(exceptions));
}

Expand Down

0 comments on commit 0d53969

Please sign in to comment.