feature:add connection pool v2

This commit is contained in:
tanyawen
2019-12-29 03:28:26 +08:00
parent 1336b2b07f
commit 32167310dd
24 changed files with 3451 additions and 3670 deletions

View File

@@ -0,0 +1,107 @@
package org.csource.fastdfs.pool;
import org.csource.fastdfs.ProtoCommon;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Connection {
private Socket sock;
private InetSocketAddress inetSockAddr;
private Long lastAccessTime = System.currentTimeMillis();
public Connection(Socket sock, InetSocketAddress inetSockAddr) {
this.sock = sock;
this.inetSockAddr = inetSockAddr;
}
/**
* get the server info
*
* @return the server info
*/
public InetSocketAddress getInetSocketAddress() {
return this.inetSockAddr;
}
public OutputStream getOutputStream() throws IOException {
return this.sock.getOutputStream();
}
public InputStream getInputStream() throws IOException {
return this.sock.getInputStream();
}
public Long getLastAccessTime() {
return lastAccessTime;
}
public void setLastAccessTime(Long lastAccessTime) {
this.lastAccessTime = lastAccessTime;
}
/**
* close direct if not create from pool or not open pool
*
* @throws IOException
*/
public void close() throws IOException {
if (this.sock != null) {
try {
ProtoCommon.closeSocket(this.sock);
} finally {
this.sock = null;
}
}
}
public boolean activeTest() throws IOException {
if (this.sock == null) {
return false;
}
return ProtoCommon.activeTest(this.sock);
}
public boolean isConnected() {
boolean isConnected = false;
if (sock != null) {
if (sock.isConnected()) {
isConnected = true;
}
}
return isConnected;
}
public boolean isAvaliable() {
if (isConnected()) {
if (sock.getPort() == 0) {
return false;
}
if (sock.getInetAddress() == null) {
return false;
}
if (sock.getRemoteSocketAddress() == null) {
return false;
}
if (sock.isInputShutdown()) {
return false;
}
if (sock.isOutputShutdown()) {
return false;
}
return true;
}
return false;
}
@Override
public String toString() {
return "TrackerServer{" +
"sock=" + sock +
", inetSockAddr=" + inetSockAddr +
'}';
}
}