Skip to content

Commit

Permalink
Add getRemoteHostAndPort and getLocalHostAndPort method
Browse files Browse the repository at this point in the history
  • Loading branch information
jjz921024 committed Aug 9, 2024
1 parent 9b76308 commit 08aaa23
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
71 changes: 32 additions & 39 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/redis/clients/jedis/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

}

0 comments on commit 08aaa23

Please sign in to comment.