diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index f2604b6b0b6..8fad80c1540 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -5,7 +5,6 @@ import java.io.Closeable; import java.io.IOException; import java.net.Socket; -import java.net.SocketAddress; import java.net.SocketException; import java.nio.ByteBuffer; import java.nio.CharBuffer; @@ -38,8 +37,8 @@ public class Connection implements Closeable { private int soTimeout = 0; private int infiniteSoTimeout = 0; private boolean broken = false; - private boolean strValActive; - private String strVal; + private HostAndPort remoteHostAndPort; + private HostAndPort localHostAndPort; public Connection() { this(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT); @@ -72,42 +71,7 @@ public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clie @Override public String toString() { - if (strValActive == broken && strVal != null) { - return strVal; - } - - int id = hashCode(); - SocketAddress remoteAddr = socket.getRemoteSocketAddress(); - SocketAddress localAddr = socket.getLocalSocketAddress(); - if (remoteAddr != null) { - StringBuilder buf = new StringBuilder(96) - .append("[id: 0x") - .append(id) - .append(", L:") - .append(localAddr) - .append(broken? " ! " : " - ") - .append("R:") - .append(remoteAddr) - .append(']'); - strVal = buf.toString(); - } else if (localAddr != null) { - StringBuilder buf = new StringBuilder(64) - .append("[id: 0x") - .append(id) - .append(", L:") - .append(localAddr) - .append(']'); - strVal = buf.toString(); - } else { - StringBuilder buf = new StringBuilder(16) - .append("[id: 0x") - .append(id) - .append(']'); - strVal = buf.toString(); - } - - strValActive = broken; - return strVal; + return "Connection{" + socketFactory + "}"; } public final RedisProtocol getRedisProtocol() { @@ -122,6 +86,35 @@ final HostAndPort getHostAndPort() { return ((DefaultJedisSocketFactory) socketFactory).getHostAndPort(); } + /** + * @return the remote host and port if socket connected or null + */ + public final HostAndPort getRemoteHostAndPort() { + if (!isConnected()) { + return null; + } + if (remoteHostAndPort != null) { + return remoteHostAndPort; + } + String remoteAddress = socket.getRemoteSocketAddress().toString(); + remoteHostAndPort = HostAndPort.from(remoteAddress.substring(1)); + return remoteHostAndPort; + } + + /** + * @return the local host and port if socket connected or null + */ + public final HostAndPort getLocalHostAndPort() { + if (!isConnected()) { + return null; + } + if (localHostAndPort != null) { + return localHostAndPort; + } + localHostAndPort = new HostAndPort(socket.getLocalAddress().getHostAddress(), socket.getLocalPort()); + return localHostAndPort; + } + public int getSoTimeout() { return soTimeout; } diff --git a/src/test/java/redis/clients/jedis/ConnectionTest.java b/src/test/java/redis/clients/jedis/ConnectionTest.java index 28eba8100cf..c091c408ef1 100644 --- a/src/test/java/redis/clients/jedis/ConnectionTest.java +++ b/src/test/java/redis/clients/jedis/ConnectionTest.java @@ -5,6 +5,10 @@ import redis.clients.jedis.exceptions.JedisConnectionException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + public class ConnectionTest { private Connection client; @@ -40,4 +44,22 @@ public void checkCloseable() { client.connect(); client.close(); } + + @Test + public void testConnectionPeerAddrInfo() { + client = new Connection("127.0.0.1", 6379); + HostAndPort remoteAddr = client.getRemoteHostAndPort(); + HostAndPort localAddr = client.getLocalHostAndPort(); + assertNull(remoteAddr); + assertNull(localAddr); + + client.connect(); + remoteAddr = client.getRemoteHostAndPort(); + localAddr = client.getLocalHostAndPort(); + assertEquals(remoteAddr, HostAndPort.from("127.0.0.1:6379")); + assertEquals(localAddr.getHost(), "127.0.0.1"); + assertTrue(localAddr.getPort() >= 0 && localAddr.getPort() < 65536); + client.close(); + } + }