Skip to content

Commit

Permalink
用NetEndpoint取代HostAndPort
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Mar 27, 2017
1 parent e36113d commit ff18dab
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 100 deletions.
Expand Up @@ -37,7 +37,7 @@
import org.lealone.db.value.Value;
import org.lealone.net.AsyncCallback;
import org.lealone.net.AsyncConnection;
import org.lealone.net.HostAndPort;
import org.lealone.net.NetEndpoint;
import org.lealone.net.NetFactory;
import org.lealone.net.Transfer;
import org.lealone.replication.ReplicationSession;
Expand Down Expand Up @@ -126,8 +126,8 @@ public Session connect(boolean first) {
for (int i = 0; i < size; i++) {
// 如果首次连接的节点就是复制节点之一,则复用它
if (isValid()) {
HostAndPort hostAndPort = new HostAndPort(servers[i]);
if (hostAndPort.inetSocketAddress.equals(this.inetSocketAddress)) {
NetEndpoint endpoint = NetEndpoint.createTCP(servers[i]);
if (endpoint.getInetSocketAddress().equals(this.inetSocketAddress)) {
sessions[i] = this;
continue;
}
Expand Down Expand Up @@ -173,9 +173,9 @@ private void connectServer() {
try {
for (int i = 0, len = servers.length; i < len; i++) {
String s = servers[random.nextInt(len)];
HostAndPort hostAndPort = new HostAndPort(s);
NetEndpoint endpoint = NetEndpoint.createTCP(s);
try {
transfer = initTransfer(ci, hostAndPort);
transfer = initTransfer(ci, endpoint);
break;
} catch (Exception e) {
if (i == len - 1) {
Expand Down Expand Up @@ -229,20 +229,20 @@ private void initTraceSystem() {
trace = traceSystem.getTrace(Trace.JDBC);
}

private Transfer initTransfer(ConnectionInfo ci, HostAndPort hostAndPort) throws Exception {
inetSocketAddress = hostAndPort.inetSocketAddress;
private Transfer initTransfer(ConnectionInfo ci, NetEndpoint endpoint) throws Exception {
inetSocketAddress = endpoint.getInetSocketAddress();
asyncConnection = asyncConnections.get(inetSocketAddress);
if (asyncConnection == null) {
synchronized (ClientSession.class) {
asyncConnection = asyncConnections.get(inetSocketAddress);
if (asyncConnection == null) {
CountDownLatch latch = new CountDownLatch(1);
client.connect(hostAndPort.port, hostAndPort.host, res -> {
client.connect(endpoint.getPort(), endpoint.getHost(), res -> {
try {
if (res.succeeded()) {
NetSocket socket = res.result();
asyncConnection = new AsyncConnection(socket, false);
asyncConnection.setHostAndPort(hostAndPort.value);
asyncConnection.setHostAndPort(endpoint.getHostAndPort());
asyncConnections.put(inetSocketAddress, asyncConnection);
socket.handler(asyncConnection);
} else {
Expand Down
88 changes: 0 additions & 88 deletions lealone-common/src/main/java/org/lealone/net/HostAndPort.java

This file was deleted.

14 changes: 13 additions & 1 deletion lealone-common/src/main/java/org/lealone/net/NetEndpoint.java
Expand Up @@ -36,6 +36,10 @@ public static NetEndpoint getLocalTcpEndpoint() {
return localTcpEndpoint;
}

public static String getLocalTcpHostAndPort() {
return localTcpEndpoint.getHostAndPort();
}

public static void setLocalP2pEndpoint(String host, int port) {
localP2pEndpoint = new NetEndpoint(host, port);
}
Expand All @@ -48,19 +52,22 @@ public static NetEndpoint getLocalP2pEndpoint() {
private final InetSocketAddress inetSocketAddress;
private final String host;
private final int port;
private final String hostAndPort; // host + ":" + port;

public NetEndpoint(String host, int port) {
this.host = host;
this.port = port;
inetSocketAddress = new InetSocketAddress(host, port);
inetAddress = inetSocketAddress.getAddress();
hostAndPort = host + ":" + port;
}

public NetEndpoint(InetAddress inetAddress, int port) {
this.inetAddress = inetAddress;
this.host = inetAddress.getHostAddress();
this.port = port;
inetSocketAddress = new InetSocketAddress(host, port);
hostAndPort = host + ":" + port;
}

public static NetEndpoint getByName(String str) throws UnknownHostException {
Expand Down Expand Up @@ -95,14 +102,15 @@ public NetEndpoint(String str, boolean p2p) {
this.port = port;
this.inetSocketAddress = new InetSocketAddress(this.host, this.port);
inetAddress = inetSocketAddress.getAddress();
hostAndPort = host + ":" + port;
}

public String getHostAddress() {
return inetAddress.getHostAddress(); // 不能用getHostName(),很慢
}

public String getHostAndPort() {
return host + ":" + port;
return hostAndPort;
}

public String getHost() {
Expand All @@ -121,6 +129,10 @@ public InetAddress geInetAddress() {
return inetAddress;
}

public InetSocketAddress getInetSocketAddress() {
return inetSocketAddress;
}

@Override
public String toString() {
return "[host=" + host + ", port=" + port + "]";
Expand Down
Expand Up @@ -19,7 +19,6 @@
import org.lealone.db.Constants;
import org.lealone.net.AsyncConnection;
import org.lealone.net.CommandHandler;
import org.lealone.net.HostAndPort;
import org.lealone.net.NetEndpoint;
import org.lealone.net.NetFactory;

Expand Down Expand Up @@ -71,7 +70,6 @@ public void init(Map<String, String> config) { // TODO 对于不支持的参数
}
}

HostAndPort.setLocalHostAndPort(host, port);
NetEndpoint.setLocalTcpEndpoint(host, port);
}

Expand Down

0 comments on commit ff18dab

Please sign in to comment.