|
|
|
@ -1,11 +1,3 @@
|
|
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2008 Happy Fish / YuQing
|
|
|
|
|
*
|
|
|
|
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
|
|
|
|
* General Public License (LGPL).
|
|
|
|
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
package org.csource.fastdfs;
|
|
|
|
|
|
|
|
|
|
import java.net.*;
|
|
|
|
@ -13,11 +5,6 @@ import java.io.*;
|
|
|
|
|
import java.net.*;
|
|
|
|
|
import org.csource.common.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Global variables
|
|
|
|
|
* @author Happy Fish / YuQing
|
|
|
|
|
* @version Version 1.11
|
|
|
|
|
*/
|
|
|
|
|
public class ClientGlobal
|
|
|
|
|
{
|
|
|
|
|
public static int g_connect_timeout; //millisecond
|
|
|
|
@ -27,18 +14,14 @@ public class ClientGlobal
|
|
|
|
|
public static boolean g_anti_steal_token; //if anti-steal token
|
|
|
|
|
public static String g_secret_key; //generage token secret key
|
|
|
|
|
public static TrackerGroup g_tracker_group;
|
|
|
|
|
|
|
|
|
|
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
|
|
|
|
|
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
|
|
|
|
|
|
|
|
|
|
private ClientGlobal()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* load global variables
|
|
|
|
|
* @param conf_filename config filename
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 初始化ClientGlobal
|
|
|
|
|
public static void init(String conf_filename) throws FileNotFoundException, IOException, MyException
|
|
|
|
|
{
|
|
|
|
|
IniFileReader iniReader;
|
|
|
|
@ -47,6 +30,7 @@ public class ClientGlobal
|
|
|
|
|
|
|
|
|
|
iniReader = new IniFileReader(conf_filename);
|
|
|
|
|
|
|
|
|
|
// 读取连接超时时间
|
|
|
|
|
g_connect_timeout = iniReader.getIntValue("connect_timeout", DEFAULT_CONNECT_TIMEOUT);
|
|
|
|
|
if (g_connect_timeout < 0)
|
|
|
|
|
{
|
|
|
|
@ -54,6 +38,7 @@ public class ClientGlobal
|
|
|
|
|
}
|
|
|
|
|
g_connect_timeout *= 1000; //millisecond
|
|
|
|
|
|
|
|
|
|
// 读取网络超时时间
|
|
|
|
|
g_network_timeout = iniReader.getIntValue("network_timeout", DEFAULT_NETWORK_TIMEOUT);
|
|
|
|
|
if (g_network_timeout < 0)
|
|
|
|
|
{
|
|
|
|
@ -61,18 +46,21 @@ public class ClientGlobal
|
|
|
|
|
}
|
|
|
|
|
g_network_timeout *= 1000; //millisecond
|
|
|
|
|
|
|
|
|
|
// 读取字符集
|
|
|
|
|
g_charset = iniReader.getStrValue("charset");
|
|
|
|
|
if (g_charset == null || g_charset.length() == 0)
|
|
|
|
|
{
|
|
|
|
|
g_charset = "ISO8859-1";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 读取tracker服务器地址
|
|
|
|
|
szTrackerServers = iniReader.getValues("tracker_server");
|
|
|
|
|
if (szTrackerServers == null)
|
|
|
|
|
{
|
|
|
|
|
throw new MyException("item \"tracker_server\" in " + conf_filename + " not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将tracker服务器地址转换为InetSocketAddress数组
|
|
|
|
|
InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
|
|
|
|
|
for (int i=0; i<szTrackerServers.length; i++)
|
|
|
|
|
{
|
|
|
|
@ -86,20 +74,18 @@ public class ClientGlobal
|
|
|
|
|
}
|
|
|
|
|
g_tracker_group = new TrackerGroup(tracker_servers);
|
|
|
|
|
|
|
|
|
|
// 读取tracker服务器HTTP端口
|
|
|
|
|
g_tracker_http_port = iniReader.getIntValue("http.tracker_http_port", 80);
|
|
|
|
|
// 读取是否启用反盗链
|
|
|
|
|
g_anti_steal_token = iniReader.getBoolValue("http.anti_steal_token", false);
|
|
|
|
|
if (g_anti_steal_token)
|
|
|
|
|
{
|
|
|
|
|
// 读取反盗链密钥
|
|
|
|
|
g_secret_key = iniReader.getStrValue("http.secret_key");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* construct Socket object
|
|
|
|
|
* @param ip_addr ip address or hostname
|
|
|
|
|
* @param port port number
|
|
|
|
|
* @return connected Socket object
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 根据IP地址和端口获取Socket
|
|
|
|
|
public static Socket getSocket(String ip_addr, int port) throws IOException
|
|
|
|
|
{
|
|
|
|
|
Socket sock = new Socket();
|
|
|
|
@ -107,12 +93,8 @@ public class ClientGlobal
|
|
|
|
|
sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
|
|
|
|
|
return sock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* construct Socket object
|
|
|
|
|
* @param addr InetSocketAddress object, including ip address and port
|
|
|
|
|
* @return connected Socket object
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 根据InetSocketAddress获取Socket
|
|
|
|
|
public static Socket getSocket(InetSocketAddress addr) throws IOException
|
|
|
|
|
{
|
|
|
|
|
Socket sock = new Socket();
|
|
|
|
@ -121,76 +103,91 @@ public class ClientGlobal
|
|
|
|
|
return sock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取连接超时时间
|
|
|
|
|
public static int getG_connect_timeout()
|
|
|
|
|
{
|
|
|
|
|
return g_connect_timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置连接超时时间
|
|
|
|
|
public static void setG_connect_timeout(int connect_timeout)
|
|
|
|
|
{
|
|
|
|
|
ClientGlobal.g_connect_timeout = connect_timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取网络超时时间
|
|
|
|
|
public static int getG_network_timeout()
|
|
|
|
|
{
|
|
|
|
|
return g_network_timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置网络超时时间
|
|
|
|
|
public static void setG_network_timeout(int network_timeout)
|
|
|
|
|
{
|
|
|
|
|
ClientGlobal.g_network_timeout = network_timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取字符集
|
|
|
|
|
public static String getG_charset()
|
|
|
|
|
{
|
|
|
|
|
return g_charset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置字符集
|
|
|
|
|
public static void setG_charset(String charset)
|
|
|
|
|
{
|
|
|
|
|
ClientGlobal.g_charset = charset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取tracker服务器HTTP端口
|
|
|
|
|
public static int getG_tracker_http_port()
|
|
|
|
|
{
|
|
|
|
|
return g_tracker_http_port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置tracker服务器HTTP端口
|
|
|
|
|
public static void setG_tracker_http_port(int tracker_http_port)
|
|
|
|
|
{
|
|
|
|
|
ClientGlobal.g_tracker_http_port = tracker_http_port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取是否启用反盗链
|
|
|
|
|
public static boolean getG_anti_steal_token()
|
|
|
|
|
{
|
|
|
|
|
return g_anti_steal_token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取是否启用反盗链
|
|
|
|
|
public static boolean isG_anti_steal_token()
|
|
|
|
|
{
|
|
|
|
|
return g_anti_steal_token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置是否启用反盗链
|
|
|
|
|
public static void setG_anti_steal_token(boolean anti_steal_token)
|
|
|
|
|
{
|
|
|
|
|
ClientGlobal.g_anti_steal_token = anti_steal_token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取反盗链密钥
|
|
|
|
|
public static String getG_secret_key()
|
|
|
|
|
{
|
|
|
|
|
return g_secret_key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置反盗链密钥
|
|
|
|
|
public static void setG_secret_key(String secret_key)
|
|
|
|
|
{
|
|
|
|
|
ClientGlobal.g_secret_key = secret_key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取tracker服务器组
|
|
|
|
|
public static TrackerGroup getG_tracker_group()
|
|
|
|
|
{
|
|
|
|
|
return g_tracker_group;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置tracker服务器组
|
|
|
|
|
public static void setG_tracker_group(TrackerGroup tracker_group)
|
|
|
|
|
{
|
|
|
|
|
ClientGlobal.g_tracker_group = tracker_group;
|
|
|
|
|