|
5 | 5 | import java.io.Closeable;
|
6 | 6 | import java.io.IOException;
|
7 | 7 | import java.net.Socket;
|
| 8 | +import java.net.SocketAddress; |
8 | 9 | import java.net.SocketException;
|
9 | 10 | import java.nio.ByteBuffer;
|
10 | 11 | import java.nio.CharBuffer;
|
@@ -37,6 +38,8 @@ public class Connection implements Closeable {
|
37 | 38 | private int soTimeout = 0;
|
38 | 39 | private int infiniteSoTimeout = 0;
|
39 | 40 | private boolean broken = false;
|
| 41 | + private boolean strValActive; |
| 42 | + private String strVal; |
40 | 43 |
|
41 | 44 | public Connection() {
|
42 | 45 | this(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT);
|
@@ -72,6 +75,63 @@ public String toString() {
|
72 | 75 | return "Connection{" + socketFactory + "}";
|
73 | 76 | }
|
74 | 77 |
|
| 78 | + public String toIdentityString() { |
| 79 | + if (strValActive == broken && strVal != null) { |
| 80 | + return strVal; |
| 81 | + } |
| 82 | + |
| 83 | + int id = hashCode(); |
| 84 | + String classInfo = getClass().toString(); |
| 85 | + |
| 86 | + if (socket == null) { |
| 87 | + StringBuilder buf = new StringBuilder(56) |
| 88 | + .append("[") |
| 89 | + .append(classInfo) |
| 90 | + .append(", id: 0x") |
| 91 | + .append(id) |
| 92 | + .append(']'); |
| 93 | + return buf.toString(); |
| 94 | + } |
| 95 | + |
| 96 | + SocketAddress remoteAddr = socket.getRemoteSocketAddress(); |
| 97 | + SocketAddress localAddr = socket.getLocalSocketAddress(); |
| 98 | + if (remoteAddr != null) { |
| 99 | + StringBuilder buf = new StringBuilder(101) |
| 100 | + .append("[") |
| 101 | + .append(classInfo) |
| 102 | + .append(", id: 0x") |
| 103 | + .append(id) |
| 104 | + .append(", L:") |
| 105 | + .append(localAddr) |
| 106 | + .append(broken? " ! " : " - ") |
| 107 | + .append("R:") |
| 108 | + .append(remoteAddr) |
| 109 | + .append(']'); |
| 110 | + strVal = buf.toString(); |
| 111 | + } else if (localAddr != null) { |
| 112 | + StringBuilder buf = new StringBuilder(64) |
| 113 | + .append("[") |
| 114 | + .append(classInfo) |
| 115 | + .append(", id: 0x") |
| 116 | + .append(id) |
| 117 | + .append(", L:") |
| 118 | + .append(localAddr) |
| 119 | + .append(']'); |
| 120 | + strVal = buf.toString(); |
| 121 | + } else { |
| 122 | + StringBuilder buf = new StringBuilder(56) |
| 123 | + .append("[") |
| 124 | + .append(classInfo) |
| 125 | + .append(", id: 0x") |
| 126 | + .append(id) |
| 127 | + .append(']'); |
| 128 | + strVal = buf.toString(); |
| 129 | + } |
| 130 | + |
| 131 | + strValActive = broken; |
| 132 | + return strVal; |
| 133 | + } |
| 134 | + |
75 | 135 | public final RedisProtocol getRedisProtocol() {
|
76 | 136 | return protocol;
|
77 | 137 | }
|
|
0 commit comments