Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 5 additions & 37 deletions src/main/java/zmq/io/net/tcp/TcpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,13 @@
import java.nio.channels.SelectableChannel;
import java.nio.channels.SocketChannel;

import jdk.net.ExtendedSocketOptions;
import zmq.ZError;
import zmq.io.net.Address;
import zmq.util.Utils;

public class TcpUtils
{
public static final boolean WITH_EXTENDED_KEEPALIVE = SocketOptionsProvider.WITH_EXTENDED_KEEPALIVE;

@SuppressWarnings("unchecked")
private static final class SocketOptionsProvider
{
// Wrapped in an inner class, to avoid the @SuppressWarnings for the whole class
private static final SocketOption<Integer> TCP_KEEPCOUNT;
private static final SocketOption<Integer> TCP_KEEPIDLE;
private static final SocketOption<Integer> TCP_KEEPINTERVAL;
private static final boolean WITH_EXTENDED_KEEPALIVE;
static {
SocketOption<Integer> tryCount = null;
SocketOption<Integer> tryIdle = null;
SocketOption<Integer> tryInterval = null;

boolean extendedKeepAlive = false;
try {
Class<?> eso = TcpUtils.class.getClassLoader().loadClass("jdk.net.ExtendedSocketOptions");
tryCount = (SocketOption<Integer>) eso.getField("TCP_KEEPCOUNT").get(null);
tryIdle = (SocketOption<Integer>) eso.getField("TCP_KEEPIDLE").get(null);
tryInterval = (SocketOption<Integer>) eso.getField("TCP_KEEPINTERVAL").get(null);
extendedKeepAlive = true;
}
catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
// If failing, will keep extendedKeepAlive to false
}
TCP_KEEPCOUNT = tryCount;
TCP_KEEPIDLE = tryIdle;
TCP_KEEPINTERVAL = tryInterval;
WITH_EXTENDED_KEEPALIVE = extendedKeepAlive;
}
}

private TcpUtils()
{
}
Expand All @@ -67,15 +35,15 @@ public static void tuneTcpKeepalives(Channel channel, int tcpKeepAlive, int tcpK
if (channel instanceof SocketChannel) {
setOption(channel, StandardSocketOptions.SO_KEEPALIVE, tcpKeepAlive == 1);
}
if (WITH_EXTENDED_KEEPALIVE && tcpKeepAlive == 1) {
if (tcpKeepAlive == 1) {
if (tcpKeepAliveCnt > 0) {
setOption(channel, SocketOptionsProvider.TCP_KEEPCOUNT, tcpKeepAliveCnt);
setOption(channel, ExtendedSocketOptions.TCP_KEEPCOUNT, tcpKeepAliveCnt);
}
if (tcpKeepAliveIdle > 0) {
setOption(channel, SocketOptionsProvider.TCP_KEEPIDLE, tcpKeepAliveIdle);
setOption(channel, ExtendedSocketOptions.TCP_KEEPIDLE, tcpKeepAliveIdle);
}
if (tcpKeepAliveIntvl > 0) {
setOption(channel, SocketOptionsProvider.TCP_KEEPINTERVAL, tcpKeepAliveIntvl);
setOption(channel, ExtendedSocketOptions.TCP_KEEPINTERVAL, tcpKeepAliveIntvl);
}
}
}
Expand Down
51 changes: 14 additions & 37 deletions src/test/java/zmq/io/net/tcp/KeepAliveTest.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,33 @@
package zmq.io.net.tcp;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketOption;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import org.junit.Assert;
import org.junit.Test;

import zmq.Options;
import jdk.net.ExtendedSocketOptions;

public class KeepAliveTest
{
@Test
public void testConsistent() throws IOException
{
try {
int javaspec = Integer.parseInt(System.getProperty("java.specification.version"));
// Test fails for jvm lest that 13
if (javaspec >= 13) {
Class<?> eso = Options.class.getClassLoader().loadClass("jdk.net.ExtendedSocketOptions");
SocketOption<Integer> count = (SocketOption<Integer>) eso.getField("TCP_KEEPCOUNT").get(null);
SocketOption<Integer> idle = (SocketOption<Integer>) eso.getField("TCP_KEEPIDLE").get(null);
SocketOption<Integer> interval = (SocketOption<Integer>) eso.getField("TCP_KEEPINTERVAL").get(null);
Method socketgetOption = Socket.class.getMethod("getOption", SocketOption.class);
Method serversocketgetOption = ServerSocket.class.getMethod("getOption", SocketOption.class);
Assert.assertTrue(TcpUtils.WITH_EXTENDED_KEEPALIVE);
int javaspec = Integer.parseInt(System.getProperty("java.specification.version"));
// Test fails for jvm less than 13
if (javaspec >= 13) {
SocketChannel sc = SocketChannel.open();
TcpUtils.tuneTcpKeepalives(sc, 1, 2, 3, 4);
Assert.assertEquals(2, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPCOUNT));
Assert.assertEquals(3, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPIDLE));
Assert.assertEquals(4, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL));

SocketChannel sc = SocketChannel.open();
TcpUtils.tuneTcpKeepalives(sc, 1, 2, 3, 4);
Assert.assertEquals(2, socketgetOption.invoke(sc.socket(), count));
Assert.assertEquals(3, socketgetOption.invoke(sc.socket(), idle));
Assert.assertEquals(4, socketgetOption.invoke(sc.socket(), interval));

ServerSocketChannel ssc = ServerSocketChannel.open();
TcpUtils.tuneTcpKeepalives(ssc, 1, 2, 3, 4);
Assert.assertEquals(2, serversocketgetOption.invoke(ssc.socket(), count));
Assert.assertEquals(3, serversocketgetOption.invoke(ssc.socket(), idle));
Assert.assertEquals(4, serversocketgetOption.invoke(ssc.socket(), interval));
}
}
catch (NumberFormatException ex) {
// java 1.8, not a problem as keepalive is not handled
ServerSocketChannel ssc = ServerSocketChannel.open();
TcpUtils.tuneTcpKeepalives(ssc, 1, 2, 3, 4);
Assert.assertEquals(2, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPCOUNT));
Assert.assertEquals(3, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPIDLE));
Assert.assertEquals(4, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL));
}
catch (NoSuchMethodException | ClassNotFoundException | NoSuchFieldException | IllegalAccessException |
InvocationTargetException e) {
// Not defined, just skip the test
}

}
}