feature: add socket connection pool
This commit is contained in:
@@ -8,6 +8,9 @@
|
||||
|
||||
package org.csource.fastdfs;
|
||||
|
||||
import org.csource.fastdfs.pool.ConnectionInfo;
|
||||
import org.csource.fastdfs.pool.ConnectionPool;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -21,93 +24,119 @@ import java.net.Socket;
|
||||
* @version Version 1.11
|
||||
*/
|
||||
public class TrackerServer {
|
||||
protected Socket sock;
|
||||
protected InetSocketAddress inetSockAddr;
|
||||
protected Socket sock;
|
||||
protected InetSocketAddress inetSockAddr;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param sock Socket of server
|
||||
* @param inetSockAddr the server info
|
||||
*/
|
||||
public TrackerServer(Socket sock, InetSocketAddress inetSockAddr) {
|
||||
this.sock = sock;
|
||||
this.inetSockAddr = inetSockAddr;
|
||||
}
|
||||
private Long lastAccessTime = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* get the connected socket
|
||||
*
|
||||
* @return the socket
|
||||
*/
|
||||
public Socket getSocket() throws IOException {
|
||||
if (this.sock == null) {
|
||||
this.sock = ClientGlobal.getSocket(this.inetSockAddr);
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param sock Socket of server
|
||||
* @param inetSockAddr the server info
|
||||
*/
|
||||
public TrackerServer(Socket sock, InetSocketAddress inetSockAddr) {
|
||||
this.sock = sock;
|
||||
this.inetSockAddr = inetSockAddr;
|
||||
}
|
||||
|
||||
return this.sock;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 void close() throws IOException {
|
||||
if (this.sock != null) {
|
||||
try {
|
||||
ProtoCommon.closeSocket(this.sock);
|
||||
} finally {
|
||||
this.sock = null;
|
||||
}
|
||||
public TrackerServer(InetSocketAddress inetSockAddr) throws IOException {
|
||||
this.inetSockAddr = inetSockAddr;
|
||||
this.sock = getSocket();
|
||||
}
|
||||
}
|
||||
|
||||
protected void finalize() throws Throwable {
|
||||
this.close();
|
||||
}
|
||||
/**
|
||||
* get the connected socket
|
||||
*
|
||||
* @return the socket
|
||||
*/
|
||||
public Socket getSocket() throws IOException {
|
||||
if (this.sock == null) {
|
||||
if (ClientGlobal.g_connection_pool_enabled) {
|
||||
ConnectionInfo connection = ConnectionPool.getConnection(this.inetSockAddr);
|
||||
this.sock = connection.getSocket();
|
||||
this.lastAccessTime = connection.getLastAccessTime();
|
||||
} else {
|
||||
this.sock = ClientGlobal.getSocket(this.inetSockAddr);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnected(){
|
||||
boolean isConnected = false;
|
||||
if (sock != null) {
|
||||
if (sock.isConnected()) {
|
||||
isConnected = true;
|
||||
}
|
||||
return this.sock;
|
||||
}
|
||||
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;
|
||||
/**
|
||||
* 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 void close() throws IOException {
|
||||
//if connection enabled get from connection pool
|
||||
if (ClientGlobal.g_connection_pool_enabled) {
|
||||
ConnectionPool.freeConnection(this);
|
||||
} else {
|
||||
if (this.sock != null) {
|
||||
try {
|
||||
ProtoCommon.closeSocket(this.sock);
|
||||
} finally {
|
||||
this.sock = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void finalize() throws Throwable {
|
||||
this.close();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Long getLastAccessTime() {
|
||||
return lastAccessTime;
|
||||
}
|
||||
|
||||
public void setLastAccessTime(Long lastAccessTime) {
|
||||
this.lastAccessTime = lastAccessTime;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user