Merge pull request #20 from veryben/master
格式化java代码、增加properties配置支持、修订README.md使用说明
This commit is contained in:
commit
db0e652f46
126
README.md
126
README.md
@ -1,3 +1,127 @@
|
|||||||
|
|
||||||
FastDFS java client SDK
|
# FastDFS java client SDK
|
||||||
|
|
||||||
|
FastDFS Java Client API may be copied only under the terms of the BSD license.
|
||||||
|
|
||||||
|
## 使用ant从源码构建
|
||||||
|
|
||||||
|
```
|
||||||
|
ant clean package
|
||||||
|
```
|
||||||
|
|
||||||
|
## 使用maven从源码安装
|
||||||
|
|
||||||
|
```
|
||||||
|
mvn clean install
|
||||||
|
```
|
||||||
|
|
||||||
|
## 使用maven从jar文件安装
|
||||||
|
```
|
||||||
|
mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=${version} -Dpackaging=jar -Dfile=fastdfs-client-java-${version}.jar
|
||||||
|
```
|
||||||
|
|
||||||
|
## 在您的maven项目pom.xml中添加依赖
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.csource</groupId>
|
||||||
|
<artifactId>fastdfs-client-java</artifactId>
|
||||||
|
<version>1.27-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
## .conf 配置文件、所在目录、加载优先顺序
|
||||||
|
|
||||||
|
配置文件名fdfs_client.conf(或使用其它文件名xxx_yyy.conf)
|
||||||
|
|
||||||
|
文件所在位置可以是项目classpath(或OS文件系统目录比如/opt/):
|
||||||
|
/opt/fdfs_client.conf
|
||||||
|
C:\Users\James\config\fdfs_client.conf
|
||||||
|
|
||||||
|
优先按OS文件系统路径读取,没有找到才查找项目classpath,尤其针对linux环境下的相对路径比如:
|
||||||
|
fdfs_client.conf
|
||||||
|
config/fdfs_client.conf
|
||||||
|
|
||||||
|
```
|
||||||
|
connect_timeout = 2
|
||||||
|
network_timeout = 30
|
||||||
|
charset = UTF-8
|
||||||
|
http.tracker_http_port = 80
|
||||||
|
http.anti_steal_token = no
|
||||||
|
http.secret_key = FastDFS1234567890
|
||||||
|
|
||||||
|
tracker_server = 10.0.11.247:22122
|
||||||
|
tracker_server = 10.0.11.248:22122
|
||||||
|
tracker_server = 10.0.11.249:22122
|
||||||
|
```
|
||||||
|
|
||||||
|
注1:tracker_server指向您自己IP地址和端口,1-n个
|
||||||
|
注2:除了tracker_server,其它配置项都是可选的
|
||||||
|
|
||||||
|
|
||||||
|
## .properties 配置文件、所在目录、加载优先顺序
|
||||||
|
|
||||||
|
配置文件名 fastdfs-client.properties(或使用其它文件名 xxx-yyy.properties)
|
||||||
|
|
||||||
|
文件所在位置可以是项目classpath(或OS文件系统目录比如/opt/):
|
||||||
|
/opt/fastdfs-client.properties
|
||||||
|
C:\Users\James\config\fastdfs-client.properties
|
||||||
|
|
||||||
|
优先按OS文件系统路径读取,没有找到才查找项目classpath,尤其针对linux环境下的相对路径比如:
|
||||||
|
fastdfs-client.properties
|
||||||
|
config/fastdfs-client.properties
|
||||||
|
|
||||||
|
```
|
||||||
|
fastdfs.connect_timeout_in_seconds = 5
|
||||||
|
fastdfs.network_timeout_in_seconds = 30
|
||||||
|
fastdfs.charset = UTF-8
|
||||||
|
fastdfs.http_anti_steal_token = false
|
||||||
|
fastdfs.http_secret_key = FastDFS1234567890
|
||||||
|
fastdfs.http_tracker_http_port = 80
|
||||||
|
|
||||||
|
fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
|
||||||
|
```
|
||||||
|
|
||||||
|
注1:properties 配置文件中属性名跟 conf 配置文件不尽相同,并且统一加前缀"fastdfs.",便于整合到用户项目配置文件
|
||||||
|
注2:fastdfs.tracker_servers 配置项不能重复属性名,多个 tracker_server 用逗号","隔开
|
||||||
|
注3:除了fastdfs.tracker_servers,其它配置项都是可选的
|
||||||
|
|
||||||
|
|
||||||
|
## 加载配置示例
|
||||||
|
|
||||||
|
加载原 conf 格式文件配置:
|
||||||
|
ClientGlobal.init("fdfs_client.conf");
|
||||||
|
ClientGlobal.init("config/fdfs_client.conf");
|
||||||
|
ClientGlobal.init("/opt/fdfs_client.conf");
|
||||||
|
ClientGlobal.init("C:\\Users\\James\\config\\fdfs_client.conf");
|
||||||
|
|
||||||
|
加载 properties 格式文件配置:
|
||||||
|
ClientGlobal.initByProperties("fastdfs-client.properties");
|
||||||
|
ClientGlobal.initByProperties("config/fastdfs-client.properties");
|
||||||
|
ClientGlobal.initByProperties("/opt/fastdfs-client.properties");
|
||||||
|
ClientGlobal.initByProperties("C:\\Users\\James\\config\\fastdfs-client.properties");
|
||||||
|
|
||||||
|
加载 Properties 对象配置:
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, "10.0.11.101:22122,10.0.11.102:22122");
|
||||||
|
ClientGlobal.initByProperties(props);
|
||||||
|
|
||||||
|
加载 trackerServers 字符串配置:
|
||||||
|
String trackerServers = "10.0.11.101:22122,10.0.11.102:22122";
|
||||||
|
ClientGlobal.initByTrackers(trackerServers);
|
||||||
|
|
||||||
|
|
||||||
|
## 检查加载配置结果:
|
||||||
|
|
||||||
|
System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo());
|
||||||
|
```
|
||||||
|
ClientGlobal.configInfo(): {
|
||||||
|
g_connect_timeout(ms) = 5000
|
||||||
|
g_network_timeout(ms) = 30000
|
||||||
|
g_charset = UTF-8
|
||||||
|
g_anti_steal_token = false
|
||||||
|
g_secret_key = FastDFS1234567890
|
||||||
|
g_tracker_http_port = 80
|
||||||
|
trackerServers = 10.0.11.101:22122,10.0.11.102:22122
|
||||||
|
}
|
||||||
|
```
|
||||||
|
13
fastdfs-client.properties
Normal file
13
fastdfs-client.properties
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
## fastdfs-client.properties
|
||||||
|
|
||||||
|
fastdfs.connect_timeout_in_seconds = 5
|
||||||
|
fastdfs.network_timeout_in_seconds = 30
|
||||||
|
|
||||||
|
fastdfs.charset = UTF-8
|
||||||
|
|
||||||
|
fastdfs.http_anti_steal_token = false
|
||||||
|
fastdfs.http_secret_key = FastDFS1234567890
|
||||||
|
fastdfs.http_tracker_http_port = 80
|
||||||
|
|
||||||
|
fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
|
||||||
|
|
@ -19,7 +19,7 @@ import java.io.IOException;
|
|||||||
* Optrak Distribution Software Ltd.
|
* Optrak Distribution Software Ltd.
|
||||||
* http://www.optrak.co.uk
|
* http://www.optrak.co.uk
|
||||||
* and Kevin Kelley's http://www.ruralnet.net/~kelley/java/Base64.java
|
* and Kevin Kelley's http://www.ruralnet.net/~kelley/java/Base64.java
|
||||||
*
|
* <p>
|
||||||
* Base64 is a way of encoding 8-bit characters using only ASCII printable
|
* Base64 is a way of encoding 8-bit characters using only ASCII printable
|
||||||
* characters similar to UUENCODE. UUENCODE includes a filename where BASE64 does not.
|
* characters similar to UUENCODE. UUENCODE includes a filename where BASE64 does not.
|
||||||
* The spec is described in RFC 2045. Base64 is a scheme where
|
* The spec is described in RFC 2045. Base64 is a scheme where
|
||||||
@ -34,452 +34,129 @@ import java.io.IOException;
|
|||||||
* If you don't like this code, there is another implementation at http://www.ruffboy.com/download.htm
|
* If you don't like this code, there is another implementation at http://www.ruffboy.com/download.htm
|
||||||
* Sun has an undocumented method called sun.misc.Base64Encoder.encode.
|
* Sun has an undocumented method called sun.misc.Base64Encoder.encode.
|
||||||
* You could use hex, simpler to code, but not as compact.
|
* You could use hex, simpler to code, but not as compact.
|
||||||
*
|
* <p>
|
||||||
* If you wanted to encode a giant file, you could do it in large chunks that
|
* If you wanted to encode a giant file, you could do it in large chunks that
|
||||||
* are even multiples of 3 bytes, except for the last chunk, and append the outputs.
|
* are even multiples of 3 bytes, except for the last chunk, and append the outputs.
|
||||||
*
|
* <p>
|
||||||
* To encode a string, rather than binary data java.net.URLEncoder may be better. See
|
* To encode a string, rather than binary data java.net.URLEncoder may be better. See
|
||||||
* printable characters in the Java glossary for a discussion of the differences.
|
* printable characters in the Java glossary for a discussion of the differences.
|
||||||
*
|
* <p>
|
||||||
* version 1.4 2002 February 15 -- correct bugs with uneven line lengths,
|
* version 1.4 2002 February 15 -- correct bugs with uneven line lengths,
|
||||||
* allow you to configure line separator.
|
* allow you to configure line separator.
|
||||||
* now need Base64 object and instance methods.
|
* now need Base64 object and instance methods.
|
||||||
* new mailing address.
|
* new mailing address.
|
||||||
* version 1.3 2000 September 12 -- fix problems with estimating output length in encode
|
* version 1.3 2000 September 12 -- fix problems with estimating output length in encode
|
||||||
* version 1.2 2000 September 09 -- now handles decode as well.
|
* version 1.2 2000 September 09 -- now handles decode as well.
|
||||||
* version 1.1 1999 December 04 -- more symmetrical encoding algorithm.
|
* version 1.1 1999 December 04 -- more symmetrical encoding algorithm.
|
||||||
* more accurate StringBuffer allocation size.
|
* more accurate StringBuffer allocation size.
|
||||||
* version 1.0 1999 December 03 -- posted in comp.lang.java.programmer.
|
* version 1.0 1999 December 03 -- posted in comp.lang.java.programmer.
|
||||||
* Futures Streams or files.
|
* Futures Streams or files.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class Base64
|
public class Base64 {
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* how we separate lines, e.g. \n, \r\n, \r etc.
|
* Marker value for chars we just ignore, e.g. \n \r high ascii
|
||||||
*/
|
*/
|
||||||
private String lineSeparator = System.getProperty( "line.separator" );
|
static final int IGNORE = -1;
|
||||||
|
/**
|
||||||
|
* Marker for = trailing pad
|
||||||
|
*/
|
||||||
|
static final int PAD = -2;
|
||||||
|
/**
|
||||||
|
* used to disable test driver
|
||||||
|
*/
|
||||||
|
private static final boolean debug = true;
|
||||||
|
/**
|
||||||
|
* how we separate lines, e.g. \n, \r\n, \r etc.
|
||||||
|
*/
|
||||||
|
private String lineSeparator = System.getProperty("line.separator");
|
||||||
|
/**
|
||||||
|
* max chars per line, excluding lineSeparator. A multiple of 4.
|
||||||
|
*/
|
||||||
|
private int lineLength = 72;
|
||||||
|
private char[] valueToChar = new char[64];
|
||||||
|
/**
|
||||||
|
* binary value encoded by a given letter of the alphabet 0..63
|
||||||
|
*/
|
||||||
|
private int[] charToValue = new int[256];
|
||||||
|
private int[] charToPad = new int[4];
|
||||||
|
|
||||||
/**
|
/* constructor */
|
||||||
* max chars per line, excluding lineSeparator. A multiple of 4.
|
public Base64() {
|
||||||
*/
|
this.init('+', '/', '=');
|
||||||
private int lineLength = 72;
|
}
|
||||||
|
|
||||||
private char[] valueToChar = new char[64];
|
/* constructor */
|
||||||
|
public Base64(char chPlus, char chSplash, char chPad, int lineLength) {
|
||||||
|
this.init(chPlus, chSplash, chPad);
|
||||||
|
this.lineLength = lineLength;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
public Base64(int lineLength) {
|
||||||
* binary value encoded by a given letter of the alphabet 0..63
|
this.lineLength = lineLength;
|
||||||
*/
|
}
|
||||||
private int[] charToValue = new int[256];
|
|
||||||
|
|
||||||
private int[] charToPad = new int[4];
|
|
||||||
|
|
||||||
/* constructor */
|
|
||||||
public Base64()
|
|
||||||
{
|
|
||||||
this.init('+', '/', '=');
|
|
||||||
}
|
|
||||||
|
|
||||||
/* constructor */
|
|
||||||
public Base64(char chPlus, char chSplash, char chPad, int lineLength)
|
|
||||||
{
|
|
||||||
this.init(chPlus, chSplash, chPad);
|
|
||||||
this.lineLength = lineLength;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Base64(int lineLength)
|
/**
|
||||||
{
|
* debug display array
|
||||||
this.lineLength = lineLength;
|
*/
|
||||||
}
|
public static void show(byte[] b) {
|
||||||
|
int count = 0;
|
||||||
|
int rows = 0;
|
||||||
|
|
||||||
/* initialise defaultValueToChar and defaultCharToValue tables */
|
|
||||||
private void init(char chPlus, char chSplash, char chPad)
|
for (int i = 0; i < b.length; i++) {
|
||||||
{
|
if (count == 8) {
|
||||||
int index = 0;
|
System.out.print(" ");
|
||||||
// build translate this.valueToChar table only once.
|
} else if (count == 16) {
|
||||||
// 0..25 -> 'A'..'Z'
|
System.out.println("");
|
||||||
for ( int i='A'; i<='Z'; i++) {
|
count = 0;
|
||||||
this.valueToChar[index++] = (char)i;
|
continue;
|
||||||
}
|
}
|
||||||
|
System.out.print(Integer.toHexString(b[i] & 0xFF).toUpperCase() + " ");
|
||||||
// 26..51 -> 'a'..'z'
|
count++;
|
||||||
for ( int i='a'; i<='z'; i++ ) {
|
|
||||||
this.valueToChar[index++] = (char)i;
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* debug display array
|
||||||
|
*/
|
||||||
|
public static void display(byte[] b) {
|
||||||
|
for (int i = 0; i < b.length; i++) {
|
||||||
|
System.out.print((char) b[i]);
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test driver
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
test();
|
||||||
|
System.exit(1);
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
try {
|
||||||
|
Base64 b64 = new Base64();
|
||||||
|
String str = "agfrtu¿¦etʲ1234¼Ù´óerty¿Õ234·¢¿¦2344ʲµÄ";
|
||||||
|
String str64 = "";
|
||||||
|
|
||||||
|
//encode
|
||||||
|
str64 = b64.encode(str.getBytes());
|
||||||
|
System.out.println(str64);
|
||||||
|
|
||||||
|
//decode
|
||||||
|
byte[] theBytes = b64.decode(str64);
|
||||||
|
show(theBytes);
|
||||||
|
String rst = new String(theBytes);
|
||||||
|
System.out.println(rst);
|
||||||
|
System.out.println(str);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
//getBytes(String charsetName);
|
||||||
// 52..61 -> '0'..'9'
|
|
||||||
for ( int i='0'; i<='9'; i++) {
|
|
||||||
this.valueToChar[index++] = (char)i;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.valueToChar[index++] = chPlus;
|
|
||||||
this.valueToChar[index++] = chSplash;
|
|
||||||
|
|
||||||
// build translate defaultCharToValue table only once.
|
|
||||||
for ( int i=0; i<256; i++ )
|
|
||||||
{
|
|
||||||
this.charToValue[i] = IGNORE; // default is to ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
for ( int i=0; i<64; i++ )
|
|
||||||
{
|
|
||||||
this.charToValue[this.valueToChar[i]] = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.charToValue[chPad] = PAD;
|
|
||||||
java.util.Arrays.fill(this.charToPad, chPad);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode an arbitrary array of bytes as Base64 printable ASCII.
|
|
||||||
* It will be broken into lines of 72 chars each. The last line is not
|
|
||||||
* terminated with a line separator.
|
|
||||||
* The output will always have an even multiple of data characters,
|
|
||||||
* exclusive of \n. It is padded out with =.
|
|
||||||
*/
|
|
||||||
public String encode(byte[] b) throws IOException
|
|
||||||
{
|
|
||||||
// Each group or partial group of 3 bytes becomes four chars
|
|
||||||
// covered quotient
|
|
||||||
int outputLength = ((b.length + 2) / 3) * 4;
|
|
||||||
|
|
||||||
// account for trailing newlines, on all but the very last line
|
|
||||||
if ( lineLength != 0 )
|
|
||||||
{
|
|
||||||
int lines = ( outputLength + lineLength -1 ) / lineLength - 1;
|
|
||||||
if ( lines > 0 )
|
|
||||||
{
|
|
||||||
outputLength += lines * lineSeparator.length();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// must be local for recursion to work.
|
|
||||||
StringBuffer sb = new StringBuffer( outputLength );
|
|
||||||
|
|
||||||
// must be local for recursion to work.
|
|
||||||
int linePos = 0;
|
|
||||||
|
|
||||||
// first deal with even multiples of 3 bytes.
|
|
||||||
int len = (b.length / 3) * 3;
|
|
||||||
int leftover = b.length - len;
|
|
||||||
for ( int i=0; i<len; i+=3 )
|
|
||||||
{
|
|
||||||
// Start a new line if next 4 chars won't fit on the current line
|
|
||||||
// We can't encapsulete the following code since the variable need to
|
|
||||||
// be local to this incarnation of encode.
|
|
||||||
linePos += 4;
|
|
||||||
if ( linePos > lineLength )
|
|
||||||
{
|
|
||||||
if ( lineLength != 0 )
|
|
||||||
{
|
|
||||||
sb.append(lineSeparator);
|
|
||||||
}
|
|
||||||
linePos = 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get next three bytes in unsigned form lined up,
|
|
||||||
// in big-endian order
|
|
||||||
int combined = b[i+0] & 0xff;
|
|
||||||
combined <<= 8;
|
|
||||||
combined |= b[i+1] & 0xff;
|
|
||||||
combined <<= 8;
|
|
||||||
combined |= b[i+2] & 0xff;
|
|
||||||
|
|
||||||
// break those 24 bits into a 4 groups of 6 bits,
|
|
||||||
// working LSB to MSB.
|
|
||||||
int c3 = combined & 0x3f;
|
|
||||||
combined >>>= 6;
|
|
||||||
int c2 = combined & 0x3f;
|
|
||||||
combined >>>= 6;
|
|
||||||
int c1 = combined & 0x3f;
|
|
||||||
combined >>>= 6;
|
|
||||||
int c0 = combined & 0x3f;
|
|
||||||
|
|
||||||
// Translate into the equivalent alpha character
|
|
||||||
// emitting them in big-endian order.
|
|
||||||
sb.append( valueToChar[c0]);
|
|
||||||
sb.append( valueToChar[c1]);
|
|
||||||
sb.append( valueToChar[c2]);
|
|
||||||
sb.append( valueToChar[c3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// deal with leftover bytes
|
|
||||||
switch ( leftover )
|
|
||||||
{
|
|
||||||
case 0:
|
|
||||||
default:
|
|
||||||
// nothing to do
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
// One leftover byte generates xx==
|
|
||||||
// Start a new line if next 4 chars won't fit on the current line
|
|
||||||
linePos += 4;
|
|
||||||
if ( linePos > lineLength )
|
|
||||||
{
|
|
||||||
|
|
||||||
if ( lineLength != 0 )
|
|
||||||
{
|
|
||||||
sb.append(lineSeparator);
|
|
||||||
}
|
|
||||||
linePos = 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle this recursively with a faked complete triple.
|
|
||||||
// Throw away last two chars and replace with ==
|
|
||||||
sb.append(encode(new byte[] {b[len], 0, 0}
|
|
||||||
).substring(0,2));
|
|
||||||
sb.append("==");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
// Two leftover bytes generates xxx=
|
|
||||||
// Start a new line if next 4 chars won't fit on the current line
|
|
||||||
linePos += 4;
|
|
||||||
if ( linePos > lineLength )
|
|
||||||
{
|
|
||||||
if ( lineLength != 0 )
|
|
||||||
{
|
|
||||||
sb.append(lineSeparator);
|
|
||||||
}
|
|
||||||
linePos = 4;
|
|
||||||
}
|
|
||||||
// Handle this recursively with a faked complete triple.
|
|
||||||
// Throw away last char and replace with =
|
|
||||||
sb.append(encode(new byte[] {b[len], b[len+1], 0}
|
|
||||||
).substring(0,3));
|
|
||||||
sb.append("=");
|
|
||||||
break;
|
|
||||||
|
|
||||||
} // end switch;
|
|
||||||
|
|
||||||
if ( outputLength != sb.length() )
|
|
||||||
{
|
|
||||||
System.out.println("oops: minor program flaw: output length mis-estimated");
|
|
||||||
System.out.println("estimate:" + outputLength);
|
|
||||||
System.out.println("actual:" + sb.length());
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}// end encode
|
|
||||||
|
|
||||||
/**
|
|
||||||
* decode a well-formed complete Base64 string back into an array of bytes.
|
|
||||||
* It must have an even multiple of 4 data characters (not counting \n),
|
|
||||||
* padded out with = as needed.
|
|
||||||
*/
|
|
||||||
public byte[] decodeAuto( String s) {
|
|
||||||
int nRemain = s.length() % 4;
|
|
||||||
if (nRemain == 0) {
|
|
||||||
return this.decode(s);
|
|
||||||
} else {
|
|
||||||
return this.decode(s + new String(this.charToPad, 0, 4 - nRemain));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* decode a well-formed complete Base64 string back into an array of bytes.
|
|
||||||
* It must have an even multiple of 4 data characters (not counting \n),
|
|
||||||
* padded out with = as needed.
|
|
||||||
*/
|
|
||||||
public byte[] decode( String s)
|
|
||||||
{
|
|
||||||
|
|
||||||
// estimate worst case size of output array, no embedded newlines.
|
|
||||||
byte[] b = new byte[(s.length() / 4) * 3];
|
|
||||||
|
|
||||||
// tracks where we are in a cycle of 4 input chars.
|
|
||||||
int cycle = 0;
|
|
||||||
|
|
||||||
// where we combine 4 groups of 6 bits and take apart as 3 groups of 8.
|
|
||||||
int combined = 0;
|
|
||||||
|
|
||||||
// how many bytes we have prepared.
|
|
||||||
int j = 0;
|
|
||||||
// will be an even multiple of 4 chars, plus some embedded \n
|
|
||||||
int len = s.length();
|
|
||||||
int dummies = 0;
|
|
||||||
for ( int i=0; i<len; i++ )
|
|
||||||
{
|
|
||||||
|
|
||||||
int c = s.charAt(i);
|
|
||||||
int value = (c <= 255) ? charToValue[c] : IGNORE;
|
|
||||||
// there are two magic values PAD (=) and IGNORE.
|
|
||||||
switch ( value )
|
|
||||||
{
|
|
||||||
case IGNORE:
|
|
||||||
// e.g. \n, just ignore it.
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PAD:
|
|
||||||
value = 0;
|
|
||||||
dummies++;
|
|
||||||
// fallthrough
|
|
||||||
default:
|
|
||||||
/* regular value character */
|
|
||||||
switch ( cycle )
|
|
||||||
{
|
|
||||||
case 0:
|
|
||||||
combined = value;
|
|
||||||
cycle = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
combined <<= 6;
|
|
||||||
combined |= value;
|
|
||||||
cycle = 2;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
combined <<= 6;
|
|
||||||
combined |= value;
|
|
||||||
cycle = 3;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 3:
|
|
||||||
combined <<= 6;
|
|
||||||
combined |= value;
|
|
||||||
// we have just completed a cycle of 4 chars.
|
|
||||||
// the four 6-bit values are in combined in big-endian order
|
|
||||||
// peel them off 8 bits at a time working lsb to msb
|
|
||||||
// to get our original 3 8-bit bytes back
|
|
||||||
|
|
||||||
b[j+2] = (byte)combined;
|
|
||||||
combined >>>= 8;
|
|
||||||
b[j+1] = (byte)combined;
|
|
||||||
combined >>>= 8;
|
|
||||||
b[j] = (byte)combined;
|
|
||||||
j += 3;
|
|
||||||
cycle = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} // end for
|
|
||||||
if ( cycle != 0 )
|
|
||||||
{
|
|
||||||
throw new ArrayIndexOutOfBoundsException ("Input to decode not an even multiple of 4 characters; pad with =.");
|
|
||||||
}
|
|
||||||
j -= dummies;
|
|
||||||
if ( b.length != j )
|
|
||||||
{
|
|
||||||
byte[] b2 = new byte[j];
|
|
||||||
System.arraycopy(b, 0, b2, 0, j);
|
|
||||||
b = b2;
|
|
||||||
}
|
|
||||||
return b;
|
|
||||||
|
|
||||||
}// end decode
|
|
||||||
|
|
||||||
/**
|
|
||||||
* determines how long the lines are that are generated by encode.
|
|
||||||
* Ignored by decode.
|
|
||||||
* @param length 0 means no newlines inserted. Must be a multiple of 4.
|
|
||||||
*/
|
|
||||||
public void setLineLength(int length)
|
|
||||||
{
|
|
||||||
this.lineLength = (length/4) * 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* How lines are separated.
|
|
||||||
* Ignored by decode.
|
|
||||||
* @param lineSeparator may be "" but not null.
|
|
||||||
* Usually contains only a combination of chars \n and \r.
|
|
||||||
* Could be any chars not in set A-Z a-z 0-9 + /.
|
|
||||||
*/
|
|
||||||
public void setLineSeparator(String lineSeparator)
|
|
||||||
{
|
|
||||||
this.lineSeparator = lineSeparator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marker value for chars we just ignore, e.g. \n \r high ascii
|
|
||||||
*/
|
|
||||||
static final int IGNORE = -1;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marker for = trailing pad
|
|
||||||
*/
|
|
||||||
static final int PAD = -2;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* used to disable test driver
|
|
||||||
*/
|
|
||||||
private static final boolean debug = true;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* debug display array
|
|
||||||
*/
|
|
||||||
public static void show (byte[] b)
|
|
||||||
{
|
|
||||||
int count = 0;
|
|
||||||
int rows = 0;
|
|
||||||
|
|
||||||
|
|
||||||
for ( int i=0; i<b.length; i++ )
|
|
||||||
{
|
|
||||||
if(count == 8)
|
|
||||||
{
|
|
||||||
System.out.print(" ");
|
|
||||||
}
|
|
||||||
else if(count == 16)
|
|
||||||
{
|
|
||||||
System.out.println("");
|
|
||||||
count = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
System.out.print( Integer.toHexString(b[i] & 0xFF).toUpperCase() + " ");
|
|
||||||
count ++;
|
|
||||||
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* debug display array
|
|
||||||
*/
|
|
||||||
public static void display (byte[] b)
|
|
||||||
{
|
|
||||||
for ( int i=0; i<b.length; i++ )
|
|
||||||
{
|
|
||||||
System.out.print( (char)b[i]);
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test driver
|
|
||||||
*/
|
|
||||||
public static void main(String[] args)
|
|
||||||
{
|
|
||||||
test();
|
|
||||||
System.exit(1);
|
|
||||||
|
|
||||||
if ( debug )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Base64 b64 = new Base64();
|
|
||||||
String str = "agfrtu¿¦etʲ1234¼Ù´óerty¿Õ234·¢¿¦2344ʲµÄ";
|
|
||||||
String str64 = "";
|
|
||||||
|
|
||||||
//encode
|
|
||||||
str64 = b64.encode(str.getBytes());
|
|
||||||
System.out.println(str64);
|
|
||||||
|
|
||||||
//decode
|
|
||||||
byte[] theBytes = b64.decode(str64);
|
|
||||||
show(theBytes);
|
|
||||||
String rst = new String(theBytes);
|
|
||||||
System.out.println(rst);
|
|
||||||
System.out.println(str);
|
|
||||||
}
|
|
||||||
catch(Exception e)
|
|
||||||
{
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
//getBytes(String charsetName);
|
|
||||||
/*
|
/*
|
||||||
byte[] a = { (byte)0xfc, (byte)0x0f, (byte)0xc0};
|
byte[] a = { (byte)0xfc, (byte)0x0f, (byte)0xc0};
|
||||||
byte[] b = { (byte)0x03, (byte)0xf0, (byte)0x3f};
|
byte[] b = { (byte)0x03, (byte)0xf0, (byte)0x3f};
|
||||||
@ -512,28 +189,303 @@ public class Base64
|
|||||||
b64.setLineLength(8);
|
b64.setLineLength(8);
|
||||||
show((b64.encode(h)).getBytes());
|
show((b64.encode(h)).getBytes());
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
}// end main
|
}// end main
|
||||||
|
|
||||||
public static void test()
|
public static void test() {
|
||||||
{
|
try {
|
||||||
try
|
Base64 b64 = new Base64();
|
||||||
{
|
|
||||||
Base64 b64 = new Base64();
|
|
||||||
|
|
||||||
//encode
|
//encode
|
||||||
//str64 = b64.encode(str.getBytes());
|
//str64 = b64.encode(str.getBytes());
|
||||||
//System.out.println(str64);
|
//System.out.println(str64);
|
||||||
|
|
||||||
String str64 = "CwUEFYoAAAADjQMC7ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267EI=";
|
String str64 = "CwUEFYoAAAADjQMC7ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267ELJiY6w05267EI=";
|
||||||
//decode
|
//decode
|
||||||
byte[] theBytes = b64.decode(str64);
|
byte[] theBytes = b64.decode(str64);
|
||||||
show(theBytes);
|
show(theBytes);
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch(Exception e)
|
e.printStackTrace();
|
||||||
{
|
}
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
|
||||||
}
|
/* initialise defaultValueToChar and defaultCharToValue tables */
|
||||||
|
private void init(char chPlus, char chSplash, char chPad) {
|
||||||
|
int index = 0;
|
||||||
|
// build translate this.valueToChar table only once.
|
||||||
|
// 0..25 -> 'A'..'Z'
|
||||||
|
for (int i = 'A'; i <= 'Z'; i++) {
|
||||||
|
this.valueToChar[index++] = (char) i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 26..51 -> 'a'..'z'
|
||||||
|
for (int i = 'a'; i <= 'z'; i++) {
|
||||||
|
this.valueToChar[index++] = (char) i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 52..61 -> '0'..'9'
|
||||||
|
for (int i = '0'; i <= '9'; i++) {
|
||||||
|
this.valueToChar[index++] = (char) i;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.valueToChar[index++] = chPlus;
|
||||||
|
this.valueToChar[index++] = chSplash;
|
||||||
|
|
||||||
|
// build translate defaultCharToValue table only once.
|
||||||
|
for (int i = 0; i < 256; i++) {
|
||||||
|
this.charToValue[i] = IGNORE; // default is to ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 64; i++) {
|
||||||
|
this.charToValue[this.valueToChar[i]] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.charToValue[chPad] = PAD;
|
||||||
|
java.util.Arrays.fill(this.charToPad, chPad);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode an arbitrary array of bytes as Base64 printable ASCII.
|
||||||
|
* It will be broken into lines of 72 chars each. The last line is not
|
||||||
|
* terminated with a line separator.
|
||||||
|
* The output will always have an even multiple of data characters,
|
||||||
|
* exclusive of \n. It is padded out with =.
|
||||||
|
*/
|
||||||
|
public String encode(byte[] b) throws IOException {
|
||||||
|
// Each group or partial group of 3 bytes becomes four chars
|
||||||
|
// covered quotient
|
||||||
|
int outputLength = ((b.length + 2) / 3) * 4;
|
||||||
|
|
||||||
|
// account for trailing newlines, on all but the very last line
|
||||||
|
if (lineLength != 0) {
|
||||||
|
int lines = (outputLength + lineLength - 1) / lineLength - 1;
|
||||||
|
if (lines > 0) {
|
||||||
|
outputLength += lines * lineSeparator.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// must be local for recursion to work.
|
||||||
|
StringBuffer sb = new StringBuffer(outputLength);
|
||||||
|
|
||||||
|
// must be local for recursion to work.
|
||||||
|
int linePos = 0;
|
||||||
|
|
||||||
|
// first deal with even multiples of 3 bytes.
|
||||||
|
int len = (b.length / 3) * 3;
|
||||||
|
int leftover = b.length - len;
|
||||||
|
for (int i = 0; i < len; i += 3) {
|
||||||
|
// Start a new line if next 4 chars won't fit on the current line
|
||||||
|
// We can't encapsulete the following code since the variable need to
|
||||||
|
// be local to this incarnation of encode.
|
||||||
|
linePos += 4;
|
||||||
|
if (linePos > lineLength) {
|
||||||
|
if (lineLength != 0) {
|
||||||
|
sb.append(lineSeparator);
|
||||||
|
}
|
||||||
|
linePos = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get next three bytes in unsigned form lined up,
|
||||||
|
// in big-endian order
|
||||||
|
int combined = b[i + 0] & 0xff;
|
||||||
|
combined <<= 8;
|
||||||
|
combined |= b[i + 1] & 0xff;
|
||||||
|
combined <<= 8;
|
||||||
|
combined |= b[i + 2] & 0xff;
|
||||||
|
|
||||||
|
// break those 24 bits into a 4 groups of 6 bits,
|
||||||
|
// working LSB to MSB.
|
||||||
|
int c3 = combined & 0x3f;
|
||||||
|
combined >>>= 6;
|
||||||
|
int c2 = combined & 0x3f;
|
||||||
|
combined >>>= 6;
|
||||||
|
int c1 = combined & 0x3f;
|
||||||
|
combined >>>= 6;
|
||||||
|
int c0 = combined & 0x3f;
|
||||||
|
|
||||||
|
// Translate into the equivalent alpha character
|
||||||
|
// emitting them in big-endian order.
|
||||||
|
sb.append(valueToChar[c0]);
|
||||||
|
sb.append(valueToChar[c1]);
|
||||||
|
sb.append(valueToChar[c2]);
|
||||||
|
sb.append(valueToChar[c3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// deal with leftover bytes
|
||||||
|
switch (leftover) {
|
||||||
|
case 0:
|
||||||
|
default:
|
||||||
|
// nothing to do
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
// One leftover byte generates xx==
|
||||||
|
// Start a new line if next 4 chars won't fit on the current line
|
||||||
|
linePos += 4;
|
||||||
|
if (linePos > lineLength) {
|
||||||
|
|
||||||
|
if (lineLength != 0) {
|
||||||
|
sb.append(lineSeparator);
|
||||||
|
}
|
||||||
|
linePos = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle this recursively with a faked complete triple.
|
||||||
|
// Throw away last two chars and replace with ==
|
||||||
|
sb.append(encode(new byte[]{b[len], 0, 0}
|
||||||
|
).substring(0, 2));
|
||||||
|
sb.append("==");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
// Two leftover bytes generates xxx=
|
||||||
|
// Start a new line if next 4 chars won't fit on the current line
|
||||||
|
linePos += 4;
|
||||||
|
if (linePos > lineLength) {
|
||||||
|
if (lineLength != 0) {
|
||||||
|
sb.append(lineSeparator);
|
||||||
|
}
|
||||||
|
linePos = 4;
|
||||||
|
}
|
||||||
|
// Handle this recursively with a faked complete triple.
|
||||||
|
// Throw away last char and replace with =
|
||||||
|
sb.append(encode(new byte[]{b[len], b[len + 1], 0}
|
||||||
|
).substring(0, 3));
|
||||||
|
sb.append("=");
|
||||||
|
break;
|
||||||
|
|
||||||
|
} // end switch;
|
||||||
|
|
||||||
|
if (outputLength != sb.length()) {
|
||||||
|
System.out.println("oops: minor program flaw: output length mis-estimated");
|
||||||
|
System.out.println("estimate:" + outputLength);
|
||||||
|
System.out.println("actual:" + sb.length());
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}// end encode
|
||||||
|
|
||||||
|
/**
|
||||||
|
* decode a well-formed complete Base64 string back into an array of bytes.
|
||||||
|
* It must have an even multiple of 4 data characters (not counting \n),
|
||||||
|
* padded out with = as needed.
|
||||||
|
*/
|
||||||
|
public byte[] decodeAuto(String s) {
|
||||||
|
int nRemain = s.length() % 4;
|
||||||
|
if (nRemain == 0) {
|
||||||
|
return this.decode(s);
|
||||||
|
} else {
|
||||||
|
return this.decode(s + new String(this.charToPad, 0, 4 - nRemain));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* decode a well-formed complete Base64 string back into an array of bytes.
|
||||||
|
* It must have an even multiple of 4 data characters (not counting \n),
|
||||||
|
* padded out with = as needed.
|
||||||
|
*/
|
||||||
|
public byte[] decode(String s) {
|
||||||
|
|
||||||
|
// estimate worst case size of output array, no embedded newlines.
|
||||||
|
byte[] b = new byte[(s.length() / 4) * 3];
|
||||||
|
|
||||||
|
// tracks where we are in a cycle of 4 input chars.
|
||||||
|
int cycle = 0;
|
||||||
|
|
||||||
|
// where we combine 4 groups of 6 bits and take apart as 3 groups of 8.
|
||||||
|
int combined = 0;
|
||||||
|
|
||||||
|
// how many bytes we have prepared.
|
||||||
|
int j = 0;
|
||||||
|
// will be an even multiple of 4 chars, plus some embedded \n
|
||||||
|
int len = s.length();
|
||||||
|
int dummies = 0;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
|
||||||
|
int c = s.charAt(i);
|
||||||
|
int value = (c <= 255) ? charToValue[c] : IGNORE;
|
||||||
|
// there are two magic values PAD (=) and IGNORE.
|
||||||
|
switch (value) {
|
||||||
|
case IGNORE:
|
||||||
|
// e.g. \n, just ignore it.
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PAD:
|
||||||
|
value = 0;
|
||||||
|
dummies++;
|
||||||
|
// fallthrough
|
||||||
|
default:
|
||||||
|
/* regular value character */
|
||||||
|
switch (cycle) {
|
||||||
|
case 0:
|
||||||
|
combined = value;
|
||||||
|
cycle = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
combined <<= 6;
|
||||||
|
combined |= value;
|
||||||
|
cycle = 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
combined <<= 6;
|
||||||
|
combined |= value;
|
||||||
|
cycle = 3;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
combined <<= 6;
|
||||||
|
combined |= value;
|
||||||
|
// we have just completed a cycle of 4 chars.
|
||||||
|
// the four 6-bit values are in combined in big-endian order
|
||||||
|
// peel them off 8 bits at a time working lsb to msb
|
||||||
|
// to get our original 3 8-bit bytes back
|
||||||
|
|
||||||
|
b[j + 2] = (byte) combined;
|
||||||
|
combined >>>= 8;
|
||||||
|
b[j + 1] = (byte) combined;
|
||||||
|
combined >>>= 8;
|
||||||
|
b[j] = (byte) combined;
|
||||||
|
j += 3;
|
||||||
|
cycle = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} // end for
|
||||||
|
if (cycle != 0) {
|
||||||
|
throw new ArrayIndexOutOfBoundsException("Input to decode not an even multiple of 4 characters; pad with =.");
|
||||||
|
}
|
||||||
|
j -= dummies;
|
||||||
|
if (b.length != j) {
|
||||||
|
byte[] b2 = new byte[j];
|
||||||
|
System.arraycopy(b, 0, b2, 0, j);
|
||||||
|
b = b2;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
|
||||||
|
}// end decode
|
||||||
|
|
||||||
|
/**
|
||||||
|
* determines how long the lines are that are generated by encode.
|
||||||
|
* Ignored by decode.
|
||||||
|
*
|
||||||
|
* @param length 0 means no newlines inserted. Must be a multiple of 4.
|
||||||
|
*/
|
||||||
|
public void setLineLength(int length) {
|
||||||
|
this.lineLength = (length / 4) * 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* How lines are separated.
|
||||||
|
* Ignored by decode.
|
||||||
|
*
|
||||||
|
* @param lineSeparator may be "" but not null.
|
||||||
|
* Usually contains only a combination of chars \n and \r.
|
||||||
|
* Could be any chars not in set A-Z a-z 0-9 + /.
|
||||||
|
*/
|
||||||
|
public void setLineSeparator(String lineSeparator) {
|
||||||
|
this.lineSeparator = lineSeparator;
|
||||||
|
}
|
||||||
} // end Base64
|
} // end Base64
|
||||||
|
|
||||||
|
@ -1,150 +1,160 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
package org.csource.common;
|
package org.csource.common;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
import org.csource.common.*;
|
import java.util.Hashtable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ini file reader / parser
|
* ini file reader / parser
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.0
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.0
|
||||||
public class IniFileReader
|
*/
|
||||||
{
|
public class IniFileReader {
|
||||||
private Hashtable paramTable;
|
private Hashtable paramTable;
|
||||||
private String conf_filename;
|
private String conf_filename;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param conf_filename config filename
|
* @param conf_filename config filename
|
||||||
*/
|
*/
|
||||||
public IniFileReader(String conf_filename) throws FileNotFoundException, IOException
|
public IniFileReader(String conf_filename) throws IOException {
|
||||||
{
|
this.conf_filename = conf_filename;
|
||||||
this.conf_filename = conf_filename;
|
loadFromFile(conf_filename);
|
||||||
loadFromFile(conf_filename);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public static ClassLoader classLoader() {
|
||||||
* get the config filename
|
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||||
* @return config filename
|
if (loader == null) {
|
||||||
*/
|
loader = ClassLoader.getSystemClassLoader();
|
||||||
public String getConfFilename()
|
}
|
||||||
{
|
return loader;
|
||||||
return this.conf_filename;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public static InputStream loadFromOsFileSystemOrClasspathAsStream(String filePath) {
|
||||||
* get string value from config file
|
|
||||||
* @param name item name in config file
|
|
||||||
* @return string value
|
|
||||||
*/
|
|
||||||
public String getStrValue(String name)
|
|
||||||
{
|
|
||||||
Object obj;
|
|
||||||
obj = this.paramTable.get(name);
|
|
||||||
if (obj == null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (obj instanceof String)
|
|
||||||
{
|
|
||||||
return (String)obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (String)((ArrayList)obj).get(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get int value from config file
|
|
||||||
* @param name item name in config file
|
|
||||||
* @param default_value the default value
|
|
||||||
* @return int value
|
|
||||||
*/
|
|
||||||
public int getIntValue(String name, int default_value)
|
|
||||||
{
|
|
||||||
String szValue = this.getStrValue(name);
|
|
||||||
if (szValue == null)
|
|
||||||
{
|
|
||||||
return default_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Integer.parseInt(szValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get boolean value from config file
|
|
||||||
* @param name item name in config file
|
|
||||||
* @param default_value the default value
|
|
||||||
* @return boolean value
|
|
||||||
*/
|
|
||||||
public boolean getBoolValue(String name, boolean default_value)
|
|
||||||
{
|
|
||||||
String szValue = this.getStrValue(name);
|
|
||||||
if (szValue == null)
|
|
||||||
{
|
|
||||||
return default_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") ||
|
|
||||||
szValue.equalsIgnoreCase("true") || szValue.equals("1");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get all values from config file
|
|
||||||
* @param name item name in config file
|
|
||||||
* @return string values (array)
|
|
||||||
*/
|
|
||||||
public String[] getValues(String name)
|
|
||||||
{
|
|
||||||
Object obj;
|
|
||||||
String[] values;
|
|
||||||
|
|
||||||
obj = this.paramTable.get(name);
|
|
||||||
if (obj == null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (obj instanceof String)
|
|
||||||
{
|
|
||||||
values = new String[1];
|
|
||||||
values[0] = (String)obj;
|
|
||||||
return values;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object[] objs = ((ArrayList)obj).toArray();
|
|
||||||
values = new String[objs.length];
|
|
||||||
System.arraycopy(objs, 0, values, 0, objs.length);
|
|
||||||
return values;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void loadFromFile(String confFilePath) throws IOException {
|
|
||||||
InputStream in = null;
|
InputStream in = null;
|
||||||
try {
|
try {
|
||||||
// 优先从文件系统路径加载
|
// 优先从文件系统路径加载
|
||||||
if (new File(confFilePath).exists()) {
|
if (new File(filePath).exists()) {
|
||||||
in = new FileInputStream(confFilePath);
|
in = new FileInputStream(filePath);
|
||||||
//System.out.println("loadFrom...file path done");
|
//System.out.println("loadFrom...file path done");
|
||||||
}
|
}
|
||||||
// 从类路径加载
|
// 从类路径加载
|
||||||
else {
|
else {
|
||||||
in = classLoader().getResourceAsStream(confFilePath);
|
in = classLoader().getResourceAsStream(filePath);
|
||||||
//System.out.println("loadFrom...class path done");
|
//System.out.println("loadFrom...class path done");
|
||||||
}
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the config filename
|
||||||
|
*
|
||||||
|
* @return config filename
|
||||||
|
*/
|
||||||
|
public String getConfFilename() {
|
||||||
|
return this.conf_filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get string value from config file
|
||||||
|
*
|
||||||
|
* @param name item name in config file
|
||||||
|
* @return string value
|
||||||
|
*/
|
||||||
|
public String getStrValue(String name) {
|
||||||
|
Object obj;
|
||||||
|
obj = this.paramTable.get(name);
|
||||||
|
if (obj == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj instanceof String) {
|
||||||
|
return (String) obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (String) ((ArrayList) obj).get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get int value from config file
|
||||||
|
*
|
||||||
|
* @param name item name in config file
|
||||||
|
* @param default_value the default value
|
||||||
|
* @return int value
|
||||||
|
*/
|
||||||
|
public int getIntValue(String name, int default_value) {
|
||||||
|
String szValue = this.getStrValue(name);
|
||||||
|
if (szValue == null) {
|
||||||
|
return default_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Integer.parseInt(szValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get boolean value from config file
|
||||||
|
*
|
||||||
|
* @param name item name in config file
|
||||||
|
* @param default_value the default value
|
||||||
|
* @return boolean value
|
||||||
|
*/
|
||||||
|
public boolean getBoolValue(String name, boolean default_value) {
|
||||||
|
String szValue = this.getStrValue(name);
|
||||||
|
if (szValue == null) {
|
||||||
|
return default_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") ||
|
||||||
|
szValue.equalsIgnoreCase("true") || szValue.equals("1");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get all values from config file
|
||||||
|
*
|
||||||
|
* @param name item name in config file
|
||||||
|
* @return string values (array)
|
||||||
|
*/
|
||||||
|
public String[] getValues(String name) {
|
||||||
|
Object obj;
|
||||||
|
String[] values;
|
||||||
|
|
||||||
|
obj = this.paramTable.get(name);
|
||||||
|
if (obj == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj instanceof String) {
|
||||||
|
values = new String[1];
|
||||||
|
values[0] = (String) obj;
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object[] objs = ((ArrayList) obj).toArray();
|
||||||
|
values = new String[objs.length];
|
||||||
|
System.arraycopy(objs, 0, values, 0, objs.length);
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadFromFile(String confFilePath) throws IOException {
|
||||||
|
InputStream in = loadFromOsFileSystemOrClasspathAsStream(confFilePath);
|
||||||
|
try {
|
||||||
readToParamTable(in);
|
readToParamTable(in);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if(in != null) in.close();
|
if (in != null) in.close();
|
||||||
//System.out.println("loadFrom...finally...in.close(); done");
|
//System.out.println("loadFrom...finally...in.close(); done");
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
@ -154,6 +164,7 @@ public class IniFileReader
|
|||||||
|
|
||||||
private void readToParamTable(InputStream in) throws IOException {
|
private void readToParamTable(InputStream in) throws IOException {
|
||||||
this.paramTable = new Hashtable();
|
this.paramTable = new Hashtable();
|
||||||
|
if (in == null) return;
|
||||||
String line;
|
String line;
|
||||||
String[] parts;
|
String[] parts;
|
||||||
String name;
|
String name;
|
||||||
@ -193,8 +204,8 @@ public class IniFileReader
|
|||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if(bufferedReader != null) bufferedReader.close();
|
if (bufferedReader != null) bufferedReader.close();
|
||||||
if(inReader != null) inReader.close();
|
if (inReader != null) inReader.close();
|
||||||
//System.out.println("readToParamTable...finally...bufferedReader.close();inReader.close(); done");
|
//System.out.println("readToParamTable...finally...bufferedReader.close();inReader.close(); done");
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
@ -202,12 +213,4 @@ public class IniFileReader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ClassLoader classLoader() {
|
|
||||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
|
||||||
if (loader == null) {
|
|
||||||
loader = ClassLoader.getSystemClassLoader();
|
|
||||||
}
|
|
||||||
return loader;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,18 +9,16 @@
|
|||||||
package org.csource.common;
|
package org.csource.common;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* My Exception
|
* My Exception
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.0
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.0
|
||||||
public class MyException extends Exception
|
*/
|
||||||
{
|
public class MyException extends Exception {
|
||||||
public MyException()
|
public MyException() {
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
public MyException(String message) {
|
||||||
public MyException(String message)
|
super(message);
|
||||||
{
|
}
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -9,47 +9,40 @@
|
|||||||
package org.csource.common;
|
package org.csource.common;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* name(key) and value pair model
|
* name(key) and value pair model
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.0
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.0
|
||||||
public class NameValuePair
|
*/
|
||||||
{
|
public class NameValuePair {
|
||||||
protected String name;
|
protected String name;
|
||||||
protected String value;
|
protected String value;
|
||||||
|
|
||||||
public NameValuePair()
|
public NameValuePair() {
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public NameValuePair(String name)
|
public NameValuePair(String name) {
|
||||||
{
|
this.name = name;
|
||||||
this.name = name;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public NameValuePair(String name, String value)
|
public NameValuePair(String name, String value) {
|
||||||
{
|
this.name = name;
|
||||||
this.name = name;
|
this.value = value;
|
||||||
this.value = value;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public String getName()
|
public String getName() {
|
||||||
{
|
return this.name;
|
||||||
return this.name;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public String getValue()
|
public void setName(String name) {
|
||||||
{
|
this.name = name;
|
||||||
return this.value;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name)
|
public String getValue() {
|
||||||
{
|
return this.value;
|
||||||
this.name = name;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(String value)
|
public void setValue(String value) {
|
||||||
{
|
this.value = value;
|
||||||
this.value = value;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,198 +1,305 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
import java.net.*;
|
import org.csource.common.IniFileReader;
|
||||||
import java.io.*;
|
import org.csource.common.MyException;
|
||||||
import java.net.*;
|
|
||||||
import org.csource.common.*;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global variables
|
* Global variables
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.11
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.11
|
||||||
public class ClientGlobal
|
*/
|
||||||
{
|
public class ClientGlobal {
|
||||||
public static int g_connect_timeout; //millisecond
|
|
||||||
public static int g_network_timeout; //millisecond
|
|
||||||
public static String g_charset;
|
|
||||||
public static int g_tracker_http_port;
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
public static void init(String conf_filename) throws FileNotFoundException, IOException, MyException
|
|
||||||
{
|
|
||||||
IniFileReader iniReader;
|
|
||||||
String[] szTrackerServers;
|
|
||||||
String[] parts;
|
|
||||||
|
|
||||||
iniReader = new IniFileReader(conf_filename);
|
|
||||||
|
|
||||||
g_connect_timeout = iniReader.getIntValue("connect_timeout", DEFAULT_CONNECT_TIMEOUT);
|
public static final String CONF_KEY_CONNECT_TIMEOUT = "connect_timeout";
|
||||||
if (g_connect_timeout < 0)
|
public static final String CONF_KEY_NETWORK_TIMEOUT = "network_timeout";
|
||||||
{
|
public static final String CONF_KEY_CHARSET = "charset";
|
||||||
g_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
|
public static final String CONF_KEY_HTTP_ANTI_STEAL_TOKEN = "http.anti_steal_token";
|
||||||
}
|
public static final String CONF_KEY_HTTP_SECRET_KEY = "http.secret_key";
|
||||||
g_connect_timeout *= 1000; //millisecond
|
public static final String CONF_KEY_HTTP_TRACKER_HTTP_PORT = "http.tracker_http_port";
|
||||||
|
public static final String CONF_KEY_TRACKER_SERVER = "tracker_server";
|
||||||
g_network_timeout = iniReader.getIntValue("network_timeout", DEFAULT_NETWORK_TIMEOUT);
|
|
||||||
if (g_network_timeout < 0)
|
public static final String PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS = "fastdfs.connect_timeout_in_seconds";
|
||||||
{
|
public static final String PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS = "fastdfs.network_timeout_in_seconds";
|
||||||
g_network_timeout = DEFAULT_NETWORK_TIMEOUT;
|
public static final String PROP_KEY_CHARSET = "fastdfs.charset";
|
||||||
}
|
public static final String PROP_KEY_HTTP_ANTI_STEAL_TOKEN = "fastdfs.http_anti_steal_token";
|
||||||
g_network_timeout *= 1000; //millisecond
|
public static final String PROP_KEY_HTTP_SECRET_KEY = "fastdfs.http_secret_key";
|
||||||
|
public static final String PROP_KEY_HTTP_TRACKER_HTTP_PORT = "fastdfs.http_tracker_http_port";
|
||||||
|
public static final String PROP_KEY_TRACKER_SERVERS = "fastdfs.tracker_servers";
|
||||||
|
|
||||||
|
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
|
||||||
|
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
|
||||||
|
public static final String DEFAULT_CHARSET = "UTF-8";
|
||||||
|
public static final boolean DEFAULT_HTTP_ANTI_STEAL_TOKEN = false;
|
||||||
|
public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890";
|
||||||
|
public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80;
|
||||||
|
|
||||||
|
public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond
|
||||||
|
public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond
|
||||||
|
public static String g_charset = DEFAULT_CHARSET;
|
||||||
|
public static boolean g_anti_steal_token = DEFAULT_HTTP_ANTI_STEAL_TOKEN; //if anti-steal token
|
||||||
|
public static String g_secret_key = DEFAULT_HTTP_SECRET_KEY; //generage token secret key
|
||||||
|
public static int g_tracker_http_port = DEFAULT_HTTP_TRACKER_HTTP_PORT;
|
||||||
|
|
||||||
|
public static TrackerGroup g_tracker_group;
|
||||||
|
|
||||||
|
private ClientGlobal() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* load global variables
|
||||||
|
*
|
||||||
|
* @param conf_filename config filename
|
||||||
|
*/
|
||||||
|
public static void init(String conf_filename) throws IOException, MyException {
|
||||||
|
IniFileReader iniReader;
|
||||||
|
String[] szTrackerServers;
|
||||||
|
String[] parts;
|
||||||
|
|
||||||
|
iniReader = new IniFileReader(conf_filename);
|
||||||
|
|
||||||
|
g_connect_timeout = iniReader.getIntValue("connect_timeout", DEFAULT_CONNECT_TIMEOUT);
|
||||||
|
if (g_connect_timeout < 0) {
|
||||||
|
g_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
|
||||||
|
}
|
||||||
|
g_connect_timeout *= 1000; //millisecond
|
||||||
|
|
||||||
|
g_network_timeout = iniReader.getIntValue("network_timeout", DEFAULT_NETWORK_TIMEOUT);
|
||||||
|
if (g_network_timeout < 0) {
|
||||||
|
g_network_timeout = DEFAULT_NETWORK_TIMEOUT;
|
||||||
|
}
|
||||||
|
g_network_timeout *= 1000; //millisecond
|
||||||
|
|
||||||
|
g_charset = iniReader.getStrValue("charset");
|
||||||
|
if (g_charset == null || g_charset.length() == 0) {
|
||||||
|
g_charset = "ISO8859-1";
|
||||||
|
}
|
||||||
|
|
||||||
|
szTrackerServers = iniReader.getValues("tracker_server");
|
||||||
|
if (szTrackerServers == null) {
|
||||||
|
throw new MyException("item \"tracker_server\" in " + conf_filename + " not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
|
||||||
|
for (int i = 0; i < szTrackerServers.length; i++) {
|
||||||
|
parts = szTrackerServers[i].split("\\:", 2);
|
||||||
|
if (parts.length != 2) {
|
||||||
|
throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
|
||||||
|
}
|
||||||
|
|
||||||
|
tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
|
||||||
|
}
|
||||||
|
g_tracker_group = new TrackerGroup(tracker_servers);
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* load from properties file
|
||||||
|
*
|
||||||
|
* @param propsFilePath properties file path, eg:
|
||||||
|
* "fastdfs-client.properties"
|
||||||
|
* "config/fastdfs-client.properties"
|
||||||
|
* "/opt/fastdfs-client.properties"
|
||||||
|
* "C:\\Users\\James\\config\\fastdfs-client.properties"
|
||||||
|
* properties文件至少包含一个配置项 fastdfs.tracker_servers 例如:
|
||||||
|
* fastdfs.tracker_servers = 10.0.11.245:22122,10.0.11.246:22122
|
||||||
|
* server的IP和端口用冒号':'分隔
|
||||||
|
* server之间用逗号','分隔
|
||||||
|
*/
|
||||||
|
public static void initByProperties(String propsFilePath) throws IOException, MyException {
|
||||||
|
Properties props = new Properties();
|
||||||
|
InputStream in = IniFileReader.loadFromOsFileSystemOrClasspathAsStream(propsFilePath);
|
||||||
|
if (in != null) {
|
||||||
|
props.load(in);
|
||||||
|
}
|
||||||
|
initByProperties(props);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initByProperties(Properties props) throws IOException, MyException {
|
||||||
|
String trackerServersConf = props.getProperty(PROP_KEY_TRACKER_SERVERS);
|
||||||
|
if (trackerServersConf == null || trackerServersConf.trim().length() == 0) {
|
||||||
|
throw new MyException(String.format("configure item %s is required", PROP_KEY_TRACKER_SERVERS));
|
||||||
|
}
|
||||||
|
initByTrackers(trackerServersConf.trim());
|
||||||
|
|
||||||
|
String connectTimeoutInSecondsConf = props.getProperty(PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS);
|
||||||
|
String networkTimeoutInSecondsConf = props.getProperty(PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS);
|
||||||
|
String charsetConf = props.getProperty(PROP_KEY_CHARSET);
|
||||||
|
String httpAntiStealTokenConf = props.getProperty(PROP_KEY_HTTP_ANTI_STEAL_TOKEN);
|
||||||
|
String httpSecretKeyConf = props.getProperty(PROP_KEY_HTTP_SECRET_KEY);
|
||||||
|
String httpTrackerHttpPortConf = props.getProperty(PROP_KEY_HTTP_TRACKER_HTTP_PORT);
|
||||||
|
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
|
||||||
|
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
|
||||||
|
}
|
||||||
|
if (networkTimeoutInSecondsConf != null && networkTimeoutInSecondsConf.trim().length() != 0) {
|
||||||
|
g_network_timeout = Integer.parseInt(networkTimeoutInSecondsConf.trim()) * 1000;
|
||||||
|
}
|
||||||
|
if (charsetConf != null && charsetConf.trim().length() != 0) {
|
||||||
|
g_charset = charsetConf.trim();
|
||||||
|
}
|
||||||
|
if (httpAntiStealTokenConf != null && httpAntiStealTokenConf.trim().length() != 0) {
|
||||||
|
g_anti_steal_token = Boolean.parseBoolean(httpAntiStealTokenConf);
|
||||||
|
}
|
||||||
|
if (httpSecretKeyConf != null && httpSecretKeyConf.trim().length() != 0) {
|
||||||
|
g_secret_key = httpSecretKeyConf.trim();
|
||||||
|
}
|
||||||
|
if (httpTrackerHttpPortConf != null && httpTrackerHttpPortConf.trim().length() != 0) {
|
||||||
|
g_tracker_http_port = Integer.parseInt(httpTrackerHttpPortConf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* load from properties file
|
||||||
|
*
|
||||||
|
* @param trackerServers 例如:"10.0.11.245:22122,10.0.11.246:22122"
|
||||||
|
* server的IP和端口用冒号':'分隔
|
||||||
|
* server之间用逗号','分隔
|
||||||
|
*/
|
||||||
|
public static void initByTrackers(String trackerServers) throws IOException, MyException {
|
||||||
|
List<InetSocketAddress> list = new ArrayList();
|
||||||
|
String spr1 = ",";
|
||||||
|
String spr2 = ":";
|
||||||
|
String[] arr1 = trackerServers.trim().split(spr1);
|
||||||
|
for (String addrStr : arr1) {
|
||||||
|
String[] arr2 = addrStr.trim().split(spr2);
|
||||||
|
String host = arr2[0].trim();
|
||||||
|
int port = Integer.parseInt(arr2[1].trim());
|
||||||
|
list.add(new InetSocketAddress(host, port));
|
||||||
|
}
|
||||||
|
InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]);
|
||||||
|
initByTrackers(trackerAddresses);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initByTrackers(InetSocketAddress[] trackerAddresses) throws IOException, MyException {
|
||||||
|
g_tracker_group = new TrackerGroup(trackerAddresses);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* construct Socket object
|
||||||
|
*
|
||||||
|
* @param ip_addr ip address or hostname
|
||||||
|
* @param port port number
|
||||||
|
* @return connected Socket object
|
||||||
|
*/
|
||||||
|
public static Socket getSocket(String ip_addr, int port) throws IOException {
|
||||||
|
Socket sock = new Socket();
|
||||||
|
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
public static Socket getSocket(InetSocketAddress addr) throws IOException {
|
||||||
|
Socket sock = new Socket();
|
||||||
|
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
||||||
|
sock.connect(addr, ClientGlobal.g_connect_timeout);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getG_tracker_http_port() {
|
||||||
|
return g_tracker_http_port;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TrackerGroup getG_tracker_group() {
|
||||||
|
return g_tracker_group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setG_tracker_group(TrackerGroup tracker_group) {
|
||||||
|
ClientGlobal.g_tracker_group = tracker_group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String configInfo() {
|
||||||
|
String trackerServers = "";
|
||||||
|
if (g_tracker_group != null) {
|
||||||
|
InetSocketAddress[] trackerAddresses = g_tracker_group.tracker_servers;
|
||||||
|
for (InetSocketAddress inetSocketAddress : trackerAddresses) {
|
||||||
|
if(trackerServers.length() > 0) trackerServers += ",";
|
||||||
|
trackerServers += inetSocketAddress.toString().substring(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "{"
|
||||||
|
+ "\n g_connect_timeout(ms) = " + g_connect_timeout
|
||||||
|
+ "\n g_network_timeout(ms) = " + g_network_timeout
|
||||||
|
+ "\n g_charset = " + g_charset
|
||||||
|
+ "\n g_anti_steal_token = " + g_anti_steal_token
|
||||||
|
+ "\n g_secret_key = " + g_secret_key
|
||||||
|
+ "\n g_tracker_http_port = " + g_tracker_http_port
|
||||||
|
+ "\n trackerServers = " + trackerServers
|
||||||
|
+ "\n}";
|
||||||
|
}
|
||||||
|
|
||||||
g_charset = iniReader.getStrValue("charset");
|
|
||||||
if (g_charset == null || g_charset.length() == 0)
|
|
||||||
{
|
|
||||||
g_charset = "ISO8859-1";
|
|
||||||
}
|
|
||||||
|
|
||||||
szTrackerServers = iniReader.getValues("tracker_server");
|
|
||||||
if (szTrackerServers == null)
|
|
||||||
{
|
|
||||||
throw new MyException("item \"tracker_server\" in " + conf_filename + " not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
|
|
||||||
for (int i=0; i<szTrackerServers.length; i++)
|
|
||||||
{
|
|
||||||
parts = szTrackerServers[i].split("\\:", 2);
|
|
||||||
if (parts.length != 2)
|
|
||||||
{
|
|
||||||
throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
|
|
||||||
}
|
|
||||||
|
|
||||||
tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
|
|
||||||
}
|
|
||||||
g_tracker_group = new TrackerGroup(tracker_servers);
|
|
||||||
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
public static Socket getSocket(String ip_addr, int port) throws IOException
|
|
||||||
{
|
|
||||||
Socket sock = new Socket();
|
|
||||||
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
public static Socket getSocket(InetSocketAddress addr) throws IOException
|
|
||||||
{
|
|
||||||
Socket sock = new Socket();
|
|
||||||
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
|
||||||
sock.connect(addr, ClientGlobal.g_connect_timeout);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getG_tracker_http_port()
|
|
||||||
{
|
|
||||||
return g_tracker_http_port;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TrackerGroup getG_tracker_group()
|
|
||||||
{
|
|
||||||
return g_tracker_group;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setG_tracker_group(TrackerGroup tracker_group)
|
|
||||||
{
|
|
||||||
ClientGlobal.g_tracker_group = tracker_group;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,31 +1,27 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.*;
|
|
||||||
import java.net.*;
|
|
||||||
import org.csource.common.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Download file callback interface
|
* Download file callback interface
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.4
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.4
|
||||||
public interface DownloadCallback
|
*/
|
||||||
{
|
public interface DownloadCallback {
|
||||||
/**
|
/**
|
||||||
* recv file content callback function, may be called more than once when the file downloaded
|
* recv file content callback function, may be called more than once when the file downloaded
|
||||||
* @param file_size file size
|
*
|
||||||
* @param data data buff
|
* @param file_size file size
|
||||||
* @param bytes data bytes
|
* @param data data buff
|
||||||
* @return 0 success, return none zero(errno) if fail
|
* @param bytes data bytes
|
||||||
*/
|
* @return 0 success, return none zero(errno) if fail
|
||||||
public int recv(long file_size, byte[] data, int bytes);
|
*/
|
||||||
|
public int recv(long file_size, byte[] data, int bytes);
|
||||||
}
|
}
|
||||||
|
@ -2,49 +2,43 @@ package org.csource.fastdfs;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import org.csource.fastdfs.DownloadCallback;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Download file by stream (download callback class)
|
* Download file by stream (download callback class)
|
||||||
* @author zhouzezhong & Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.11
|
* @author zhouzezhong & Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.11
|
||||||
public class DownloadStream implements DownloadCallback
|
*/
|
||||||
{
|
public class DownloadStream implements DownloadCallback {
|
||||||
private OutputStream out;
|
private OutputStream out;
|
||||||
private long currentBytes = 0;
|
private long currentBytes = 0;
|
||||||
|
|
||||||
public DownloadStream(OutputStream out)
|
|
||||||
{
|
|
||||||
super();
|
|
||||||
this.out = out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public DownloadStream(OutputStream out) {
|
||||||
* recv file content callback function, may be called more than once when the file downloaded
|
super();
|
||||||
* @param fileSize file size
|
this.out = out;
|
||||||
* @param data data buff
|
}
|
||||||
* @param bytes data bytes
|
|
||||||
* @return 0 success, return none zero(errno) if fail
|
/**
|
||||||
*/
|
* recv file content callback function, may be called more than once when the file downloaded
|
||||||
public int recv(long fileSize, byte[] data, int bytes)
|
*
|
||||||
{
|
* @param fileSize file size
|
||||||
try
|
* @param data data buff
|
||||||
{
|
* @param bytes data bytes
|
||||||
out.write(data, 0, bytes);
|
* @return 0 success, return none zero(errno) if fail
|
||||||
}
|
*/
|
||||||
catch(IOException ex)
|
public int recv(long fileSize, byte[] data, int bytes) {
|
||||||
{
|
try {
|
||||||
ex.printStackTrace();
|
out.write(data, 0, bytes);
|
||||||
return -1;
|
} catch (IOException ex) {
|
||||||
}
|
ex.printStackTrace();
|
||||||
|
return -1;
|
||||||
currentBytes += bytes;
|
}
|
||||||
if (this.currentBytes == fileSize)
|
|
||||||
{
|
currentBytes += bytes;
|
||||||
this.currentBytes = 0;
|
if (this.currentBytes == fileSize) {
|
||||||
}
|
this.currentBytes = 0;
|
||||||
|
}
|
||||||
return 0;
|
|
||||||
}
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,125 +1,125 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Server Info
|
* Server Info
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.23
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.23
|
||||||
public class FileInfo
|
*/
|
||||||
{
|
public class FileInfo {
|
||||||
protected String source_ip_addr;
|
protected String source_ip_addr;
|
||||||
protected long file_size;
|
protected long file_size;
|
||||||
protected Date create_timestamp;
|
protected Date create_timestamp;
|
||||||
protected int crc32;
|
protected int crc32;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param file_size the file size
|
*
|
||||||
* @param create_timestamp create timestamp in seconds
|
* @param file_size the file size
|
||||||
* @param crc32 the crc32 signature
|
* @param create_timestamp create timestamp in seconds
|
||||||
* @param source_ip_addr the source storage ip address
|
* @param crc32 the crc32 signature
|
||||||
*/
|
* @param source_ip_addr the source storage ip address
|
||||||
public FileInfo(long file_size, int create_timestamp, int crc32, String source_ip_addr)
|
*/
|
||||||
{
|
public FileInfo(long file_size, int create_timestamp, int crc32, String source_ip_addr) {
|
||||||
this.file_size = file_size;
|
this.file_size = file_size;
|
||||||
this.create_timestamp = new Date(create_timestamp * 1000L);
|
this.create_timestamp = new Date(create_timestamp * 1000L);
|
||||||
this.crc32 = crc32;
|
this.crc32 = crc32;
|
||||||
this.source_ip_addr = source_ip_addr;
|
this.source_ip_addr = source_ip_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set the source ip address of the file uploaded to
|
* get the source ip address of the file uploaded to
|
||||||
* @param source_ip_addr the source ip address
|
*
|
||||||
*/
|
* @return the source ip address of the file uploaded to
|
||||||
public void setSourceIpAddr(String source_ip_addr)
|
*/
|
||||||
{
|
public String getSourceIpAddr() {
|
||||||
this.source_ip_addr = source_ip_addr;
|
return this.source_ip_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* get the source ip address of the file uploaded to
|
|
||||||
* @return the source ip address of the file uploaded to
|
|
||||||
*/
|
|
||||||
public String getSourceIpAddr()
|
|
||||||
{
|
|
||||||
return this.source_ip_addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set the file size
|
|
||||||
* @param file_size the file size
|
|
||||||
*/
|
|
||||||
public void setFileSize(long file_size)
|
|
||||||
{
|
|
||||||
this.file_size = file_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the file size
|
|
||||||
* @return the file size
|
|
||||||
*/
|
|
||||||
public long getFileSize()
|
|
||||||
{
|
|
||||||
return this.file_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set the create timestamp of the file
|
* set the source ip address of the file uploaded to
|
||||||
* @param create_timestamp create timestamp in seconds
|
*
|
||||||
*/
|
* @param source_ip_addr the source ip address
|
||||||
public void setCreateTimestamp(int create_timestamp)
|
*/
|
||||||
{
|
public void setSourceIpAddr(String source_ip_addr) {
|
||||||
this.create_timestamp = new Date(create_timestamp * 1000L);
|
this.source_ip_addr = source_ip_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* get the create timestamp of the file
|
|
||||||
* @return the create timestamp of the file
|
|
||||||
*/
|
|
||||||
public Date getCreateTimestamp()
|
|
||||||
{
|
|
||||||
return this.create_timestamp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set the create timestamp of the file
|
* get the file size
|
||||||
* @param crc32 the crc32 signature
|
*
|
||||||
*/
|
* @return the file size
|
||||||
public void setCrc32(int crc32)
|
*/
|
||||||
{
|
public long getFileSize() {
|
||||||
this.crc32 = crc32;
|
return this.file_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the file CRC32 signature
|
* set the file size
|
||||||
* @return the file CRC32 signature
|
*
|
||||||
*/
|
* @param file_size the file size
|
||||||
public long getCrc32()
|
*/
|
||||||
{
|
public void setFileSize(long file_size) {
|
||||||
return this.crc32;
|
this.file_size = file_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* to string
|
* get the create timestamp of the file
|
||||||
* @return string
|
*
|
||||||
*/
|
* @return the create timestamp of the file
|
||||||
public String toString()
|
*/
|
||||||
{
|
public Date getCreateTimestamp() {
|
||||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
return this.create_timestamp;
|
||||||
return "source_ip_addr = " + this.source_ip_addr + ", " +
|
}
|
||||||
"file_size = " + this.file_size + ", " +
|
|
||||||
"create_timestamp = " + df.format(this.create_timestamp) + ", " +
|
/**
|
||||||
"crc32 = " + this.crc32;
|
* set the create timestamp of the file
|
||||||
}
|
*
|
||||||
|
* @param create_timestamp create timestamp in seconds
|
||||||
|
*/
|
||||||
|
public void setCreateTimestamp(int create_timestamp) {
|
||||||
|
this.create_timestamp = new Date(create_timestamp * 1000L);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the file CRC32 signature
|
||||||
|
*
|
||||||
|
* @return the file CRC32 signature
|
||||||
|
*/
|
||||||
|
public long getCrc32() {
|
||||||
|
return this.crc32;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the create timestamp of the file
|
||||||
|
*
|
||||||
|
* @param crc32 the crc32 signature
|
||||||
|
*/
|
||||||
|
public void setCrc32(int crc32) {
|
||||||
|
this.crc32 = crc32;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* to string
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public String toString() {
|
||||||
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
return "source_ip_addr = " + this.source_ip_addr + ", " +
|
||||||
|
"file_size = " + this.file_size + ", " +
|
||||||
|
"create_timestamp = " + df.format(this.create_timestamp) + ", " +
|
||||||
|
"crc32 = " + this.crc32;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,55 +1,48 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
|
||||||
import java.net.*;
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import org.csource.common.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* C struct body decoder
|
* C struct body decoder
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.17
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.17
|
||||||
public class ProtoStructDecoder<T extends StructBase>
|
*/
|
||||||
{
|
public class ProtoStructDecoder<T extends StructBase> {
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
public ProtoStructDecoder()
|
public ProtoStructDecoder() {
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* decode byte buffer
|
||||||
* decode byte buffer
|
*/
|
||||||
*/
|
public T[] decode(byte[] bs, Class<T> clazz, int fieldsTotalSize) throws Exception {
|
||||||
public T[] decode(byte[] bs, Class<T> clazz, int fieldsTotalSize) throws Exception
|
if (bs.length % fieldsTotalSize != 0) {
|
||||||
{
|
throw new IOException("byte array length: " + bs.length + " is invalid!");
|
||||||
if (bs.length % fieldsTotalSize != 0)
|
}
|
||||||
{
|
|
||||||
throw new IOException("byte array length: " + bs.length + " is invalid!");
|
int count = bs.length / fieldsTotalSize;
|
||||||
}
|
int offset;
|
||||||
|
T[] results = (T[]) Array.newInstance(clazz, count);
|
||||||
int count = bs.length / fieldsTotalSize;
|
|
||||||
int offset;
|
offset = 0;
|
||||||
T[] results = (T[])Array.newInstance(clazz, count);
|
for (int i = 0; i < results.length; i++) {
|
||||||
|
results[i] = clazz.newInstance();
|
||||||
offset = 0;
|
results[i].setFields(bs, offset);
|
||||||
for (int i=0; i<results.length; i++)
|
offset += fieldsTotalSize;
|
||||||
{
|
}
|
||||||
results[i] = clazz.newInstance();
|
|
||||||
results[i].setFields(bs, offset);
|
return results;
|
||||||
offset += fieldsTotalSize;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,67 +1,66 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.*;
|
import java.net.Socket;
|
||||||
import org.csource.common.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Server Info
|
* Server Info
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.7
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.7
|
||||||
public class ServerInfo
|
*/
|
||||||
{
|
public class ServerInfo {
|
||||||
protected String ip_addr;
|
protected String ip_addr;
|
||||||
protected int port;
|
protected int port;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param ip_addr address of the server
|
*
|
||||||
* @param port the port of the server
|
* @param ip_addr address of the server
|
||||||
*/
|
* @param port the port of the server
|
||||||
public ServerInfo(String ip_addr, int port)
|
*/
|
||||||
{
|
public ServerInfo(String ip_addr, int port) {
|
||||||
this.ip_addr = ip_addr;
|
this.ip_addr = ip_addr;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return the ip address
|
* return the ip address
|
||||||
* @return the ip address
|
*
|
||||||
*/
|
* @return the ip address
|
||||||
public String getIpAddr()
|
*/
|
||||||
{
|
public String getIpAddr() {
|
||||||
return this.ip_addr;
|
return this.ip_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return the port of the server
|
* return the port of the server
|
||||||
* @return the port of the server
|
*
|
||||||
*/
|
* @return the port of the server
|
||||||
public int getPort()
|
*/
|
||||||
{
|
public int getPort() {
|
||||||
return this.port;
|
return this.port;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* connect to server
|
* connect to server
|
||||||
* @return connected Socket object
|
*
|
||||||
*/
|
* @return connected Socket object
|
||||||
public Socket connect() throws IOException
|
*/
|
||||||
{
|
public Socket connect() throws IOException {
|
||||||
Socket sock = new Socket();
|
Socket sock = new Socket();
|
||||||
sock.setReuseAddress(true);
|
sock.setReuseAddress(true);
|
||||||
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
||||||
sock.connect(new InetSocketAddress(this.ip_addr, this.port), ClientGlobal.g_connect_timeout);
|
sock.connect(new InetSocketAddress(this.ip_addr, this.port), ClientGlobal.g_connect_timeout);
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,63 +1,57 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.*;
|
|
||||||
import org.csource.common.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Storage Server Info
|
* Storage Server Info
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.11
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.11
|
||||||
public class StorageServer extends TrackerServer
|
*/
|
||||||
{
|
public class StorageServer extends TrackerServer {
|
||||||
protected int store_path_index = 0;
|
protected int store_path_index = 0;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
* @param ip_addr the ip address of storage server
|
|
||||||
* @param port the port of storage server
|
|
||||||
* @param store_path the store path index on the storage server
|
|
||||||
*/
|
|
||||||
public StorageServer(String ip_addr, int port, int store_path) throws IOException
|
|
||||||
{
|
|
||||||
super(ClientGlobal.getSocket(ip_addr, port), new InetSocketAddress(ip_addr, port));
|
|
||||||
this.store_path_index = store_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param ip_addr the ip address of storage server
|
*
|
||||||
* @param port the port of storage server
|
* @param ip_addr the ip address of storage server
|
||||||
* @param store_path the store path index on the storage server
|
* @param port the port of storage server
|
||||||
*/
|
* @param store_path the store path index on the storage server
|
||||||
public StorageServer(String ip_addr, int port, byte store_path) throws IOException
|
*/
|
||||||
{
|
public StorageServer(String ip_addr, int port, int store_path) throws IOException {
|
||||||
super(ClientGlobal.getSocket(ip_addr, port), new InetSocketAddress(ip_addr, port));
|
super(ClientGlobal.getSocket(ip_addr, port), new InetSocketAddress(ip_addr, port));
|
||||||
if (store_path < 0)
|
this.store_path_index = store_path;
|
||||||
{
|
}
|
||||||
this.store_path_index = 256 + store_path;
|
|
||||||
}
|
/**
|
||||||
else
|
* Constructor
|
||||||
{
|
*
|
||||||
this.store_path_index = store_path;
|
* @param ip_addr the ip address of storage server
|
||||||
}
|
* @param port the port of storage server
|
||||||
}
|
* @param store_path the store path index on the storage server
|
||||||
|
*/
|
||||||
/**
|
public StorageServer(String ip_addr, int port, byte store_path) throws IOException {
|
||||||
* @return the store path index on the storage server
|
super(ClientGlobal.getSocket(ip_addr, port), new InetSocketAddress(ip_addr, port));
|
||||||
*/
|
if (store_path < 0) {
|
||||||
public int getStorePathIndex()
|
this.store_path_index = 256 + store_path;
|
||||||
{
|
} else {
|
||||||
return this.store_path_index;
|
this.store_path_index = store_path;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the store path index on the storage server
|
||||||
|
*/
|
||||||
|
public int getStorePathIndex() {
|
||||||
|
return this.store_path_index;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
@ -12,73 +12,62 @@ import java.io.UnsupportedEncodingException;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* C struct body decoder
|
* C struct body decoder
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.17
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.17
|
||||||
public abstract class StructBase
|
*/
|
||||||
{
|
public abstract class StructBase {
|
||||||
protected static class FieldInfo
|
/**
|
||||||
{
|
* set fields
|
||||||
protected String name;
|
*
|
||||||
protected int offset;
|
* @param bs byte array
|
||||||
protected int size;
|
* @param offset start offset
|
||||||
|
*/
|
||||||
public FieldInfo(String name, int offset, int size)
|
public abstract void setFields(byte[] bs, int offset);
|
||||||
{
|
|
||||||
this.name = name;
|
|
||||||
this.offset = offset;
|
|
||||||
this.size = size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set fields
|
|
||||||
* @param bs byte array
|
|
||||||
* @param offset start offset
|
|
||||||
*/
|
|
||||||
public abstract void setFields(byte[] bs, int offset);
|
|
||||||
|
|
||||||
protected String stringValue(byte[] bs, int offset, FieldInfo filedInfo)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return (new String(bs, offset + filedInfo.offset, filedInfo.size, ClientGlobal.g_charset)).trim();
|
|
||||||
}
|
|
||||||
catch(UnsupportedEncodingException ex)
|
|
||||||
{
|
|
||||||
ex.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected long longValue(byte[] bs, int offset, FieldInfo filedInfo)
|
|
||||||
{
|
|
||||||
return ProtoCommon.buff2long(bs, offset + filedInfo.offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected int intValue(byte[] bs, int offset, FieldInfo filedInfo)
|
|
||||||
{
|
|
||||||
return (int)ProtoCommon.buff2long(bs, offset + filedInfo.offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected int int32Value(byte[] bs, int offset, FieldInfo filedInfo)
|
|
||||||
{
|
|
||||||
return ProtoCommon.buff2int(bs, offset + filedInfo.offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected byte byteValue(byte[] bs, int offset, FieldInfo filedInfo)
|
protected String stringValue(byte[] bs, int offset, FieldInfo filedInfo) {
|
||||||
{
|
try {
|
||||||
return bs[offset + filedInfo.offset];
|
return (new String(bs, offset + filedInfo.offset, filedInfo.size, ClientGlobal.g_charset)).trim();
|
||||||
}
|
} catch (UnsupportedEncodingException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected boolean booleanValue(byte[] bs, int offset, FieldInfo filedInfo)
|
protected long longValue(byte[] bs, int offset, FieldInfo filedInfo) {
|
||||||
{
|
return ProtoCommon.buff2long(bs, offset + filedInfo.offset);
|
||||||
return bs[offset + filedInfo.offset] != 0;
|
}
|
||||||
}
|
|
||||||
|
protected int intValue(byte[] bs, int offset, FieldInfo filedInfo) {
|
||||||
protected Date dateValue(byte[] bs, int offset, FieldInfo filedInfo)
|
return (int) ProtoCommon.buff2long(bs, offset + filedInfo.offset);
|
||||||
{
|
}
|
||||||
return new Date(ProtoCommon.buff2long(bs, offset + filedInfo.offset) * 1000);
|
|
||||||
}
|
protected int int32Value(byte[] bs, int offset, FieldInfo filedInfo) {
|
||||||
|
return ProtoCommon.buff2int(bs, offset + filedInfo.offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected byte byteValue(byte[] bs, int offset, FieldInfo filedInfo) {
|
||||||
|
return bs[offset + filedInfo.offset];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean booleanValue(byte[] bs, int offset, FieldInfo filedInfo) {
|
||||||
|
return bs[offset + filedInfo.offset] != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Date dateValue(byte[] bs, int offset, FieldInfo filedInfo) {
|
||||||
|
return new Date(ProtoCommon.buff2long(bs, offset + filedInfo.offset) * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static class FieldInfo {
|
||||||
|
protected String name;
|
||||||
|
protected int offset;
|
||||||
|
protected int size;
|
||||||
|
|
||||||
|
public FieldInfo(String name, int offset, int size) {
|
||||||
|
this.name = name;
|
||||||
|
this.offset = offset;
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,226 +1,225 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* C struct body decoder
|
* C struct body decoder
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.18
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.18
|
||||||
public class StructGroupStat extends StructBase
|
*/
|
||||||
{
|
public class StructGroupStat extends StructBase {
|
||||||
protected static final int FIELD_INDEX_GROUP_NAME = 0;
|
protected static final int FIELD_INDEX_GROUP_NAME = 0;
|
||||||
protected static final int FIELD_INDEX_TOTAL_MB = 1;
|
protected static final int FIELD_INDEX_TOTAL_MB = 1;
|
||||||
protected static final int FIELD_INDEX_FREE_MB = 2;
|
protected static final int FIELD_INDEX_FREE_MB = 2;
|
||||||
protected static final int FIELD_INDEX_TRUNK_FREE_MB = 3;
|
protected static final int FIELD_INDEX_TRUNK_FREE_MB = 3;
|
||||||
protected static final int FIELD_INDEX_STORAGE_COUNT = 4;
|
protected static final int FIELD_INDEX_STORAGE_COUNT = 4;
|
||||||
protected static final int FIELD_INDEX_STORAGE_PORT = 5;
|
protected static final int FIELD_INDEX_STORAGE_PORT = 5;
|
||||||
protected static final int FIELD_INDEX_STORAGE_HTTP_PORT = 6;
|
protected static final int FIELD_INDEX_STORAGE_HTTP_PORT = 6;
|
||||||
protected static final int FIELD_INDEX_ACTIVE_COUNT = 7;
|
protected static final int FIELD_INDEX_ACTIVE_COUNT = 7;
|
||||||
protected static final int FIELD_INDEX_CURRENT_WRITE_SERVER = 8;
|
protected static final int FIELD_INDEX_CURRENT_WRITE_SERVER = 8;
|
||||||
protected static final int FIELD_INDEX_STORE_PATH_COUNT = 9;
|
protected static final int FIELD_INDEX_STORE_PATH_COUNT = 9;
|
||||||
protected static final int FIELD_INDEX_SUBDIR_COUNT_PER_PATH = 10;
|
protected static final int FIELD_INDEX_SUBDIR_COUNT_PER_PATH = 10;
|
||||||
protected static final int FIELD_INDEX_CURRENT_TRUNK_FILE_ID = 11;
|
protected static final int FIELD_INDEX_CURRENT_TRUNK_FILE_ID = 11;
|
||||||
|
|
||||||
protected static int fieldsTotalSize;
|
|
||||||
protected static StructBase.FieldInfo[] fieldsArray = new StructBase.FieldInfo[12];
|
|
||||||
|
|
||||||
static
|
|
||||||
{
|
|
||||||
int offset = 0;
|
|
||||||
fieldsArray[FIELD_INDEX_GROUP_NAME] = new StructBase.FieldInfo("groupName", offset, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1);
|
|
||||||
offset += ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1;
|
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_TOTAL_MB] = new StructBase.FieldInfo("totalMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_FREE_MB] = new StructBase.FieldInfo("freeMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_TRUNK_FREE_MB] = new StructBase.FieldInfo("trunkFreeMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_STORAGE_COUNT] = new StructBase.FieldInfo("storageCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
protected static int fieldsTotalSize;
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
protected static StructBase.FieldInfo[] fieldsArray = new StructBase.FieldInfo[12];
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_STORAGE_PORT] = new StructBase.FieldInfo("storagePort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT] = new StructBase.FieldInfo("storageHttpPort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_ACTIVE_COUNT] = new StructBase.FieldInfo("activeCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_CURRENT_WRITE_SERVER] = new StructBase.FieldInfo("currentWriteServer", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_STORE_PATH_COUNT] = new StructBase.FieldInfo("storePathCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH] = new StructBase.FieldInfo("subdirCountPerPath", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
|
||||||
|
|
||||||
fieldsArray[FIELD_INDEX_CURRENT_TRUNK_FILE_ID] = new StructBase.FieldInfo("currentTrunkFileId", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
static {
|
||||||
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
int offset = 0;
|
||||||
|
fieldsArray[FIELD_INDEX_GROUP_NAME] = new StructBase.FieldInfo("groupName", offset, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1);
|
||||||
fieldsTotalSize = offset;
|
offset += ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1;
|
||||||
}
|
|
||||||
|
fieldsArray[FIELD_INDEX_TOTAL_MB] = new StructBase.FieldInfo("totalMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
protected String groupName; //name of this group
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
protected long totalMB; //total disk storage in MB
|
|
||||||
protected long freeMB; //free disk space in MB
|
fieldsArray[FIELD_INDEX_FREE_MB] = new StructBase.FieldInfo("freeMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
protected long trunkFreeMB; //trunk free space in MB
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
protected int storageCount; //storage server count
|
|
||||||
|
fieldsArray[FIELD_INDEX_TRUNK_FREE_MB] = new StructBase.FieldInfo("trunkFreeMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
|
|
||||||
|
fieldsArray[FIELD_INDEX_STORAGE_COUNT] = new StructBase.FieldInfo("storageCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
|
|
||||||
|
fieldsArray[FIELD_INDEX_STORAGE_PORT] = new StructBase.FieldInfo("storagePort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
|
|
||||||
|
fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT] = new StructBase.FieldInfo("storageHttpPort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
|
|
||||||
|
fieldsArray[FIELD_INDEX_ACTIVE_COUNT] = new StructBase.FieldInfo("activeCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
|
|
||||||
|
fieldsArray[FIELD_INDEX_CURRENT_WRITE_SERVER] = new StructBase.FieldInfo("currentWriteServer", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
|
|
||||||
|
fieldsArray[FIELD_INDEX_STORE_PATH_COUNT] = new StructBase.FieldInfo("storePathCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
|
|
||||||
|
fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH] = new StructBase.FieldInfo("subdirCountPerPath", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
|
|
||||||
|
fieldsArray[FIELD_INDEX_CURRENT_TRUNK_FILE_ID] = new StructBase.FieldInfo("currentTrunkFileId", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
|
||||||
|
offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
|
||||||
|
|
||||||
|
fieldsTotalSize = offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String groupName; //name of this group
|
||||||
|
protected long totalMB; //total disk storage in MB
|
||||||
|
protected long freeMB; //free disk space in MB
|
||||||
|
protected long trunkFreeMB; //trunk free space in MB
|
||||||
|
protected int storageCount; //storage server count
|
||||||
protected int storagePort; //storage server port
|
protected int storagePort; //storage server port
|
||||||
protected int storageHttpPort; //storage server HTTP port
|
protected int storageHttpPort; //storage server HTTP port
|
||||||
protected int activeCount; //active storage server count
|
protected int activeCount; //active storage server count
|
||||||
protected int currentWriteServer; //current storage server index to upload file
|
protected int currentWriteServer; //current storage server index to upload file
|
||||||
protected int storePathCount; //store base path count of each storage server
|
protected int storePathCount; //store base path count of each storage server
|
||||||
protected int subdirCountPerPath; //sub dir count per store path
|
protected int subdirCountPerPath; //sub dir count per store path
|
||||||
protected int currentTrunkFileId; //current trunk file id
|
protected int currentTrunkFileId; //current trunk file id
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get group name
|
* get fields total size
|
||||||
* @return group name
|
*
|
||||||
*/
|
* @return fields total size
|
||||||
public String getGroupName()
|
*/
|
||||||
{
|
public static int getFieldsTotalSize() {
|
||||||
return this.groupName;
|
return fieldsTotalSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get total disk space in MB
|
* get group name
|
||||||
* @return total disk space in MB
|
*
|
||||||
*/
|
* @return group name
|
||||||
public long getTotalMB()
|
*/
|
||||||
{
|
public String getGroupName() {
|
||||||
return this.totalMB;
|
return this.groupName;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get free disk space in MB
|
|
||||||
* @return free disk space in MB
|
|
||||||
*/
|
|
||||||
public long getFreeMB()
|
|
||||||
{
|
|
||||||
return this.freeMB;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get trunk free space in MB
|
* get total disk space in MB
|
||||||
* @return trunk free space in MB
|
*
|
||||||
*/
|
* @return total disk space in MB
|
||||||
public long getTrunkFreeMB()
|
*/
|
||||||
{
|
public long getTotalMB() {
|
||||||
return this.trunkFreeMB;
|
return this.totalMB;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get storage server count in this group
|
* get free disk space in MB
|
||||||
* @return storage server count in this group
|
*
|
||||||
*/
|
* @return free disk space in MB
|
||||||
public int getStorageCount()
|
*/
|
||||||
{
|
public long getFreeMB() {
|
||||||
return this.storageCount;
|
return this.freeMB;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get active storage server count in this group
|
|
||||||
* @return active storage server count in this group
|
|
||||||
*/
|
|
||||||
public int getActiveCount()
|
|
||||||
{
|
|
||||||
return this.activeCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get storage server port
|
|
||||||
* @return storage server port
|
|
||||||
*/
|
|
||||||
public int getStoragePort()
|
|
||||||
{
|
|
||||||
return this.storagePort;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get storage server HTTP port
|
|
||||||
* @return storage server HTTP port
|
|
||||||
*/
|
|
||||||
public int getStorageHttpPort()
|
|
||||||
{
|
|
||||||
return this.storageHttpPort;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get current storage server index to upload file
|
* get trunk free space in MB
|
||||||
* @return current storage server index to upload file
|
*
|
||||||
*/
|
* @return trunk free space in MB
|
||||||
public int getCurrentWriteServer()
|
*/
|
||||||
{
|
public long getTrunkFreeMB() {
|
||||||
return this.currentWriteServer;
|
return this.trunkFreeMB;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* get store base path count of each storage server
|
|
||||||
* @return store base path count of each storage server
|
|
||||||
*/
|
|
||||||
public int getStorePathCount()
|
|
||||||
{
|
|
||||||
return this.storePathCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get sub dir count per store path
|
|
||||||
* @return sub dir count per store path
|
|
||||||
*/
|
|
||||||
public int getSubdirCountPerPath()
|
|
||||||
{
|
|
||||||
return this.subdirCountPerPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get current trunk file id
|
|
||||||
* @return current trunk file id
|
|
||||||
*/
|
|
||||||
public int getCurrentTrunkFileId()
|
|
||||||
{
|
|
||||||
return this.currentTrunkFileId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set fields
|
|
||||||
* @param bs byte array
|
|
||||||
* @param offset start offset
|
|
||||||
*/
|
|
||||||
public void setFields(byte[] bs, int offset)
|
|
||||||
{
|
|
||||||
this.groupName = stringValue(bs, offset, fieldsArray[FIELD_INDEX_GROUP_NAME]);
|
|
||||||
this.totalMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_MB]);
|
|
||||||
this.freeMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_FREE_MB]);
|
|
||||||
this.trunkFreeMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_TRUNK_FREE_MB]);
|
|
||||||
this.storageCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_COUNT]);
|
|
||||||
this.storagePort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_PORT]);
|
|
||||||
this.storageHttpPort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT]);
|
|
||||||
this.activeCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_ACTIVE_COUNT]);
|
|
||||||
this.currentWriteServer = intValue(bs, offset, fieldsArray[FIELD_INDEX_CURRENT_WRITE_SERVER]);
|
|
||||||
this.storePathCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORE_PATH_COUNT]);
|
|
||||||
this.subdirCountPerPath = intValue(bs, offset, fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH]);
|
|
||||||
this.currentTrunkFileId = intValue(bs, offset, fieldsArray[FIELD_INDEX_CURRENT_TRUNK_FILE_ID]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get fields total size
|
* get storage server count in this group
|
||||||
* @return fields total size
|
*
|
||||||
*/
|
* @return storage server count in this group
|
||||||
public static int getFieldsTotalSize()
|
*/
|
||||||
{
|
public int getStorageCount() {
|
||||||
return fieldsTotalSize;
|
return this.storageCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get active storage server count in this group
|
||||||
|
*
|
||||||
|
* @return active storage server count in this group
|
||||||
|
*/
|
||||||
|
public int getActiveCount() {
|
||||||
|
return this.activeCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get storage server port
|
||||||
|
*
|
||||||
|
* @return storage server port
|
||||||
|
*/
|
||||||
|
public int getStoragePort() {
|
||||||
|
return this.storagePort;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get storage server HTTP port
|
||||||
|
*
|
||||||
|
* @return storage server HTTP port
|
||||||
|
*/
|
||||||
|
public int getStorageHttpPort() {
|
||||||
|
return this.storageHttpPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get current storage server index to upload file
|
||||||
|
*
|
||||||
|
* @return current storage server index to upload file
|
||||||
|
*/
|
||||||
|
public int getCurrentWriteServer() {
|
||||||
|
return this.currentWriteServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get store base path count of each storage server
|
||||||
|
*
|
||||||
|
* @return store base path count of each storage server
|
||||||
|
*/
|
||||||
|
public int getStorePathCount() {
|
||||||
|
return this.storePathCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get sub dir count per store path
|
||||||
|
*
|
||||||
|
* @return sub dir count per store path
|
||||||
|
*/
|
||||||
|
public int getSubdirCountPerPath() {
|
||||||
|
return this.subdirCountPerPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get current trunk file id
|
||||||
|
*
|
||||||
|
* @return current trunk file id
|
||||||
|
*/
|
||||||
|
public int getCurrentTrunkFileId() {
|
||||||
|
return this.currentTrunkFileId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set fields
|
||||||
|
*
|
||||||
|
* @param bs byte array
|
||||||
|
* @param offset start offset
|
||||||
|
*/
|
||||||
|
public void setFields(byte[] bs, int offset) {
|
||||||
|
this.groupName = stringValue(bs, offset, fieldsArray[FIELD_INDEX_GROUP_NAME]);
|
||||||
|
this.totalMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_MB]);
|
||||||
|
this.freeMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_FREE_MB]);
|
||||||
|
this.trunkFreeMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_TRUNK_FREE_MB]);
|
||||||
|
this.storageCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_COUNT]);
|
||||||
|
this.storagePort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_PORT]);
|
||||||
|
this.storageHttpPort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT]);
|
||||||
|
this.activeCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_ACTIVE_COUNT]);
|
||||||
|
this.currentWriteServer = intValue(bs, offset, fieldsArray[FIELD_INDEX_CURRENT_WRITE_SERVER]);
|
||||||
|
this.storePathCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORE_PATH_COUNT]);
|
||||||
|
this.subdirCountPerPath = intValue(bs, offset, fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH]);
|
||||||
|
this.currentTrunkFileId = intValue(bs, offset, fieldsArray[FIELD_INDEX_CURRENT_TRUNK_FILE_ID]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,121 +1,106 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.*;
|
import java.net.Socket;
|
||||||
import org.csource.common.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tracker server group
|
* Tracker server group
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.17
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.17
|
||||||
public class TrackerGroup
|
*/
|
||||||
{
|
public class TrackerGroup {
|
||||||
protected Integer lock;
|
public int tracker_server_index;
|
||||||
public int tracker_server_index;
|
public InetSocketAddress[] tracker_servers;
|
||||||
public InetSocketAddress[] tracker_servers;
|
protected Integer lock;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
* @param tracker_servers tracker servers
|
|
||||||
*/
|
|
||||||
public TrackerGroup(InetSocketAddress[] tracker_servers)
|
|
||||||
{
|
|
||||||
this.tracker_servers = tracker_servers;
|
|
||||||
this.lock = new Integer(0);
|
|
||||||
this.tracker_server_index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return connected tracker server
|
* Constructor
|
||||||
* @return connected tracker server, null for fail
|
*
|
||||||
*/
|
* @param tracker_servers tracker servers
|
||||||
public TrackerServer getConnection(int serverIndex) throws IOException
|
*/
|
||||||
{
|
public TrackerGroup(InetSocketAddress[] tracker_servers) {
|
||||||
Socket sock = new Socket();
|
this.tracker_servers = tracker_servers;
|
||||||
sock.setReuseAddress(true);
|
this.lock = new Integer(0);
|
||||||
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
this.tracker_server_index = 0;
|
||||||
sock.connect(this.tracker_servers[serverIndex], ClientGlobal.g_connect_timeout);
|
}
|
||||||
return new TrackerServer(sock, this.tracker_servers[serverIndex]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* return connected tracker server
|
|
||||||
* @return connected tracker server, null for fail
|
|
||||||
*/
|
|
||||||
public TrackerServer getConnection() throws IOException
|
|
||||||
{
|
|
||||||
int current_index;
|
|
||||||
|
|
||||||
synchronized(this.lock)
|
|
||||||
{
|
|
||||||
this.tracker_server_index++;
|
|
||||||
if (this.tracker_server_index >= this.tracker_servers.length)
|
|
||||||
{
|
|
||||||
this.tracker_server_index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
current_index = this.tracker_server_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return this.getConnection(current_index);
|
|
||||||
}
|
|
||||||
catch(IOException ex)
|
|
||||||
{
|
|
||||||
System.err.println("connect to server " + this.tracker_servers[current_index].getAddress().getHostAddress() + ":" + this.tracker_servers[current_index].getPort() + " fail");
|
|
||||||
ex.printStackTrace(System.err);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i=0; i<this.tracker_servers.length; i++)
|
|
||||||
{
|
|
||||||
if (i == current_index)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
TrackerServer trackerServer = this.getConnection(i);
|
|
||||||
|
|
||||||
synchronized(this.lock)
|
|
||||||
{
|
|
||||||
if (this.tracker_server_index == current_index)
|
|
||||||
{
|
|
||||||
this.tracker_server_index = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return trackerServer;
|
|
||||||
}
|
|
||||||
catch(IOException ex)
|
|
||||||
{
|
|
||||||
System.err.println("connect to server " + this.tracker_servers[i].getAddress().getHostAddress() + ":" + this.tracker_servers[i].getPort() + " fail");
|
|
||||||
ex.printStackTrace(System.err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object clone()
|
/**
|
||||||
{
|
* return connected tracker server
|
||||||
InetSocketAddress[] trackerServers = new InetSocketAddress[this.tracker_servers.length];
|
*
|
||||||
for (int i=0; i<trackerServers.length; i++)
|
* @return connected tracker server, null for fail
|
||||||
{
|
*/
|
||||||
trackerServers[i] = new InetSocketAddress(this.tracker_servers[i].getAddress().getHostAddress(), this.tracker_servers[i].getPort());
|
public TrackerServer getConnection(int serverIndex) throws IOException {
|
||||||
}
|
Socket sock = new Socket();
|
||||||
|
sock.setReuseAddress(true);
|
||||||
return new TrackerGroup(trackerServers);
|
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
||||||
}
|
sock.connect(this.tracker_servers[serverIndex], ClientGlobal.g_connect_timeout);
|
||||||
|
return new TrackerServer(sock, this.tracker_servers[serverIndex]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return connected tracker server
|
||||||
|
*
|
||||||
|
* @return connected tracker server, null for fail
|
||||||
|
*/
|
||||||
|
public TrackerServer getConnection() throws IOException {
|
||||||
|
int current_index;
|
||||||
|
|
||||||
|
synchronized (this.lock) {
|
||||||
|
this.tracker_server_index++;
|
||||||
|
if (this.tracker_server_index >= this.tracker_servers.length) {
|
||||||
|
this.tracker_server_index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
current_index = this.tracker_server_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return this.getConnection(current_index);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
System.err.println("connect to server " + this.tracker_servers[current_index].getAddress().getHostAddress() + ":" + this.tracker_servers[current_index].getPort() + " fail");
|
||||||
|
ex.printStackTrace(System.err);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < this.tracker_servers.length; i++) {
|
||||||
|
if (i == current_index) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
TrackerServer trackerServer = this.getConnection(i);
|
||||||
|
|
||||||
|
synchronized (this.lock) {
|
||||||
|
if (this.tracker_server_index == current_index) {
|
||||||
|
this.tracker_server_index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return trackerServer;
|
||||||
|
} catch (IOException ex) {
|
||||||
|
System.err.println("connect to server " + this.tracker_servers[i].getAddress().getHostAddress() + ":" + this.tracker_servers[i].getPort() + " fail");
|
||||||
|
ex.printStackTrace(System.err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object clone() {
|
||||||
|
InetSocketAddress[] trackerServers = new InetSocketAddress[this.tracker_servers.length];
|
||||||
|
for (int i = 0; i < trackerServers.length; i++) {
|
||||||
|
trackerServers[i] = new InetSocketAddress(this.tracker_servers[i].getAddress().getHostAddress(), this.tracker_servers[i].getPort());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TrackerGroup(trackerServers);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,89 +1,81 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.io.InputStream;
|
||||||
import java.net.*;
|
import java.io.OutputStream;
|
||||||
import org.csource.common.*;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tracker Server Info
|
* Tracker Server Info
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.11
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.11
|
||||||
public class TrackerServer
|
*/
|
||||||
{
|
public class TrackerServer {
|
||||||
protected Socket sock;
|
protected Socket sock;
|
||||||
protected InetSocketAddress inetSockAddr;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the connected socket
|
|
||||||
* @return the socket
|
|
||||||
*/
|
|
||||||
public Socket getSocket() throws IOException
|
|
||||||
{
|
|
||||||
if (this.sock == null)
|
|
||||||
{
|
|
||||||
this.sock = ClientGlobal.getSocket(this.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
|
/**
|
||||||
{
|
* Constructor
|
||||||
if (this.sock != null)
|
*
|
||||||
{
|
* @param sock Socket of server
|
||||||
try
|
* @param inetSockAddr the server info
|
||||||
{
|
*/
|
||||||
ProtoCommon.closeSocket(this.sock);
|
public TrackerServer(Socket sock, InetSocketAddress inetSockAddr) {
|
||||||
}
|
this.sock = sock;
|
||||||
finally
|
this.inetSockAddr = inetSockAddr;
|
||||||
{
|
}
|
||||||
this.sock = null;
|
|
||||||
}
|
/**
|
||||||
}
|
* get the connected socket
|
||||||
}
|
*
|
||||||
|
* @return the socket
|
||||||
protected void finalize() throws Throwable
|
*/
|
||||||
{
|
public Socket getSocket() throws IOException {
|
||||||
this.close();
|
if (this.sock == null) {
|
||||||
}
|
this.sock = ClientGlobal.getSocket(this.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void finalize() throws Throwable {
|
||||||
|
this.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,28 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.io.OutputStream;
|
||||||
import java.net.*;
|
|
||||||
import org.csource.common.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* upload file callback interface
|
* upload file callback interface
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.0
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.0
|
||||||
public interface UploadCallback
|
*/
|
||||||
{
|
public interface UploadCallback {
|
||||||
/**
|
/**
|
||||||
* send file content callback function, be called only once when the file uploaded
|
* send file content callback function, be called only once when the file uploaded
|
||||||
* @param out output stream for writing file content
|
*
|
||||||
* @return 0 success, return none zero(errno) if fail
|
* @param out output stream for writing file content
|
||||||
*/
|
* @return 0 success, return none zero(errno) if fail
|
||||||
public int send(OutputStream out) throws IOException;
|
*/
|
||||||
|
public int send(OutputStream out) throws IOException;
|
||||||
}
|
}
|
||||||
|
@ -1,61 +1,55 @@
|
|||||||
package org.csource.fastdfs;
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import org.csource.fastdfs.UploadCallback;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Upload file by stream
|
* Upload file by stream
|
||||||
* @author zhouzezhong & Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.11
|
* @author zhouzezhong & Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.11
|
||||||
public class UploadStream implements UploadCallback
|
*/
|
||||||
{
|
public class UploadStream implements UploadCallback {
|
||||||
private InputStream inputStream; //input stream for reading
|
private InputStream inputStream; //input stream for reading
|
||||||
private long fileSize = 0; //size of the uploaded file
|
private long fileSize = 0; //size of the uploaded file
|
||||||
|
|
||||||
/**
|
|
||||||
* constructor
|
|
||||||
* @param inputStream input stream for uploading
|
|
||||||
* @param fileSize size of uploaded file
|
|
||||||
*/
|
|
||||||
public UploadStream(InputStream inputStream, long fileSize)
|
|
||||||
{
|
|
||||||
super();
|
|
||||||
this.inputStream = inputStream;
|
|
||||||
this.fileSize = fileSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send file content callback function, be called only once when the file uploaded
|
* constructor
|
||||||
* @param out output stream for writing file content
|
*
|
||||||
* @return 0 success, return none zero(errno) if fail
|
* @param inputStream input stream for uploading
|
||||||
*/
|
* @param fileSize size of uploaded file
|
||||||
public int send(OutputStream out) throws IOException
|
*/
|
||||||
{
|
public UploadStream(InputStream inputStream, long fileSize) {
|
||||||
long remainBytes = fileSize;
|
super();
|
||||||
byte[] buff = new byte[256 * 1024];
|
this.inputStream = inputStream;
|
||||||
int bytes;
|
this.fileSize = fileSize;
|
||||||
while(remainBytes > 0)
|
}
|
||||||
{
|
|
||||||
try
|
/**
|
||||||
{
|
* send file content callback function, be called only once when the file uploaded
|
||||||
if ((bytes=inputStream.read(buff, 0, remainBytes > buff.length ? buff.length : (int)remainBytes)) < 0)
|
*
|
||||||
{
|
* @param out output stream for writing file content
|
||||||
return -1;
|
* @return 0 success, return none zero(errno) if fail
|
||||||
}
|
*/
|
||||||
}
|
public int send(OutputStream out) throws IOException {
|
||||||
catch(IOException ex)
|
long remainBytes = fileSize;
|
||||||
{
|
byte[] buff = new byte[256 * 1024];
|
||||||
ex.printStackTrace();
|
int bytes;
|
||||||
return -1;
|
while (remainBytes > 0) {
|
||||||
}
|
try {
|
||||||
|
if ((bytes = inputStream.read(buff, 0, remainBytes > buff.length ? buff.length : (int) remainBytes)) < 0) {
|
||||||
out.write(buff, 0, bytes);
|
return -1;
|
||||||
remainBytes -= bytes;
|
}
|
||||||
}
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out.write(buff, 0, bytes);
|
||||||
|
remainBytes -= bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,69 +1,60 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs.test;
|
package org.csource.fastdfs.test;
|
||||||
|
|
||||||
import java.io.*;
|
import org.csource.fastdfs.DownloadCallback;
|
||||||
import java.util.*;
|
|
||||||
import java.net.*;
|
import java.io.FileOutputStream;
|
||||||
import org.csource.fastdfs.*;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DowloadCallback test
|
* DowloadCallback test
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.3
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.3
|
||||||
public class DownloadFileWriter implements DownloadCallback
|
*/
|
||||||
{
|
public class DownloadFileWriter implements DownloadCallback {
|
||||||
private String filename;
|
private String filename;
|
||||||
private FileOutputStream out = null;
|
private FileOutputStream out = null;
|
||||||
private long current_bytes = 0;
|
private long current_bytes = 0;
|
||||||
|
|
||||||
public DownloadFileWriter(String filename)
|
public DownloadFileWriter(String filename) {
|
||||||
{
|
this.filename = filename;
|
||||||
this.filename = filename;
|
}
|
||||||
}
|
|
||||||
|
public int recv(long file_size, byte[] data, int bytes) {
|
||||||
public int recv(long file_size, byte[] data, int bytes)
|
try {
|
||||||
{
|
if (this.out == null) {
|
||||||
try
|
this.out = new FileOutputStream(this.filename);
|
||||||
{
|
}
|
||||||
if (this.out == null)
|
|
||||||
{
|
this.out.write(data, 0, bytes);
|
||||||
this.out = new FileOutputStream(this.filename);
|
this.current_bytes += bytes;
|
||||||
}
|
|
||||||
|
if (this.current_bytes == file_size) {
|
||||||
this.out.write(data, 0, bytes);
|
this.out.close();
|
||||||
this.current_bytes += bytes;
|
this.out = null;
|
||||||
|
this.current_bytes = 0;
|
||||||
if (this.current_bytes == file_size)
|
}
|
||||||
{
|
} catch (IOException ex) {
|
||||||
this.out.close();
|
ex.printStackTrace();
|
||||||
this.out = null;
|
return -1;
|
||||||
this.current_bytes = 0;
|
}
|
||||||
}
|
|
||||||
}
|
return 0;
|
||||||
catch(IOException ex)
|
}
|
||||||
{
|
|
||||||
ex.printStackTrace();
|
protected void finalize() throws Throwable {
|
||||||
return -1;
|
if (this.out != null) {
|
||||||
}
|
this.out.close();
|
||||||
|
this.out = null;
|
||||||
return 0;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void finalize() throws Throwable
|
|
||||||
{
|
|
||||||
if (this.out != null)
|
|
||||||
{
|
|
||||||
this.out.close();
|
|
||||||
this.out = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,225 +1,203 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
package org.csource.fastdfs.test;
|
package org.csource.fastdfs.test;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import org.csource.common.*;
|
|
||||||
import org.csource.fastdfs.*;
|
import org.csource.fastdfs.*;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* load test class
|
* load test class
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.20
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.20
|
||||||
public class Monitor
|
*/
|
||||||
{
|
public class Monitor {
|
||||||
private Monitor()
|
private Monitor() {
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* entry point
|
||||||
* entry point
|
*
|
||||||
* @param args comand arguments
|
* @param args comand arguments
|
||||||
* <ul><li>args[0]: config filename</li></ul>
|
* <ul><li>args[0]: config filename</li></ul>
|
||||||
*/
|
*/
|
||||||
public static void main(String args[])
|
public static void main(String args[]) {
|
||||||
{
|
if (args.length < 1) {
|
||||||
if (args.length < 1)
|
System.out.println("Error: Must have 1 parameter: config filename");
|
||||||
{
|
return;
|
||||||
System.out.println("Error: Must have 1 parameter: config filename");
|
}
|
||||||
return;
|
|
||||||
}
|
System.out.println("java.version=" + System.getProperty("java.version"));
|
||||||
|
|
||||||
System.out.println("java.version=" + System.getProperty("java.version"));
|
try {
|
||||||
|
ClientGlobal.init(args[0]);
|
||||||
try
|
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
||||||
{
|
System.out.println("charset=" + ClientGlobal.g_charset);
|
||||||
ClientGlobal.init(args[0]);
|
|
||||||
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
TrackerClient tracker = new TrackerClient();
|
||||||
System.out.println("charset=" + ClientGlobal.g_charset);
|
|
||||||
|
|
||||||
TrackerClient tracker = new TrackerClient();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
System.out.println("delete storage return: " + tracker.deleteStorage("group1", "192.168.0.192"));
|
System.out.println("delete storage return: " + tracker.deleteStorage("group1", "192.168.0.192"));
|
||||||
System.out.println("delete storage errno: " + tracker.getErrorCode());
|
System.out.println("delete storage errno: " + tracker.getErrorCode());
|
||||||
*/
|
*/
|
||||||
|
|
||||||
TrackerServer trackerServer = tracker.getConnection();
|
|
||||||
if (trackerServer == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int count;
|
|
||||||
StructGroupStat[] groupStats = tracker.listGroups(trackerServer);
|
|
||||||
if (groupStats == null)
|
|
||||||
{
|
|
||||||
System.out.println("");
|
|
||||||
System.out.println("ERROR! list groups error, error no: " + tracker.getErrorCode());
|
|
||||||
System.out.println("");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("group count: " + groupStats.length);
|
|
||||||
|
|
||||||
count = 0;
|
|
||||||
for (StructGroupStat groupStat : groupStats)
|
|
||||||
{
|
|
||||||
count++;
|
|
||||||
System.out.println("Group " + count + ":");
|
|
||||||
System.out.println("group name = " + groupStat.getGroupName());
|
|
||||||
System.out.println("disk total space = " + groupStat.getTotalMB() + "MB");
|
|
||||||
System.out.println("disk free space = " + groupStat.getFreeMB() + " MB");
|
|
||||||
System.out.println("trunk free space = " + groupStat.getTrunkFreeMB() + " MB");
|
|
||||||
System.out.println("storage server count = " + groupStat.getStorageCount());
|
|
||||||
System.out.println("active server count = " + groupStat.getActiveCount());
|
|
||||||
System.out.println("storage server port = " + groupStat.getStoragePort());
|
|
||||||
System.out.println("storage HTTP port = " + groupStat.getStorageHttpPort());
|
|
||||||
System.out.println("store path count = " + groupStat.getStorePathCount());
|
|
||||||
System.out.println("subdir count per path = " + groupStat.getSubdirCountPerPath());
|
|
||||||
System.out.println("current write server index = " + groupStat.getCurrentWriteServer());
|
|
||||||
System.out.println("current trunk file id = " + groupStat.getCurrentTrunkFileId());
|
|
||||||
|
|
||||||
StructStorageStat[] storageStats = tracker.listStorages(trackerServer, groupStat.getGroupName());
|
|
||||||
if (storageStats == null)
|
|
||||||
{
|
|
||||||
System.out.println("");
|
|
||||||
System.out.println("ERROR! list storage error, error no: " + tracker.getErrorCode());
|
|
||||||
System.out.println("");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
||||||
int stroageCount = 0;
|
|
||||||
for (StructStorageStat storageStat : storageStats)
|
|
||||||
{
|
|
||||||
stroageCount++;
|
|
||||||
System.out.println("\tStorage " + stroageCount + ":");
|
|
||||||
System.out.println("\t\tstorage id = " + storageStat.getId());
|
|
||||||
System.out.println("\t\tip_addr = " + storageStat.getIpAddr() + " " + ProtoCommon.getStorageStatusCaption(storageStat.getStatus()));
|
|
||||||
System.out.println("\t\thttp domain = " + storageStat.getDomainName());
|
|
||||||
System.out.println("\t\tversion = " + storageStat.getVersion());
|
|
||||||
System.out.println("\t\tjoin time = " + df.format(storageStat.getJoinTime()));
|
|
||||||
System.out.println("\t\tup time = " + (storageStat.getUpTime().getTime() == 0 ? "" : df.format(storageStat.getUpTime())));
|
|
||||||
System.out.println("\t\ttotal storage = " + storageStat.getTotalMB() + "MB");
|
|
||||||
System.out.println("\t\tfree storage = " + storageStat.getFreeMB() + "MB");
|
|
||||||
System.out.println("\t\tupload priority = " + storageStat.getUploadPriority());
|
|
||||||
System.out.println("\t\tstore_path_count = " + storageStat.getStorePathCount());
|
|
||||||
System.out.println("\t\tsubdir_count_per_path = " + storageStat.getSubdirCountPerPath());
|
|
||||||
System.out.println("\t\tstorage_port = " + storageStat.getStoragePort());
|
|
||||||
System.out.println("\t\tstorage_http_port = " + storageStat.getStorageHttpPort());
|
|
||||||
System.out.println("\t\tcurrent_write_path = " + storageStat.getCurrentWritePath());
|
|
||||||
System.out.println("\t\tsource ip_addr = " + storageStat.getSrcIpAddr());
|
|
||||||
System.out.println("\t\tif_trunk_server = " + storageStat.isTrunkServer());
|
|
||||||
System.out.println("\t\tconntion.alloc_count = " + storageStat.getConnectionAllocCount());
|
|
||||||
System.out.println("\t\tconntion.current_count = " + storageStat.getConnectionCurrentCount());
|
|
||||||
System.out.println("\t\tconntion.max_count = " + storageStat.getConnectionMaxCount());
|
|
||||||
System.out.println("\t\ttotal_upload_count = " + storageStat.getTotalUploadCount());
|
|
||||||
System.out.println("\t\tsuccess_upload_count = " + storageStat.getSuccessUploadCount());
|
|
||||||
System.out.println("\t\ttotal_append_count = " + storageStat.getTotalAppendCount());
|
|
||||||
System.out.println("\t\tsuccess_append_count = " + storageStat.getSuccessAppendCount());
|
|
||||||
System.out.println("\t\ttotal_modify_count = " + storageStat.getTotalModifyCount());
|
|
||||||
System.out.println("\t\tsuccess_modify_count = " + storageStat.getSuccessModifyCount());
|
|
||||||
System.out.println("\t\ttotal_truncate_count = " + storageStat.getTotalTruncateCount());
|
|
||||||
System.out.println("\t\tsuccess_truncate_count = " + storageStat.getSuccessTruncateCount());
|
|
||||||
System.out.println("\t\ttotal_set_meta_count = " + storageStat.getTotalSetMetaCount());
|
|
||||||
System.out.println("\t\tsuccess_set_meta_count = " + storageStat.getSuccessSetMetaCount());
|
|
||||||
System.out.println("\t\ttotal_delete_count = " + storageStat.getTotalDeleteCount());
|
|
||||||
System.out.println("\t\tsuccess_delete_count = " + storageStat.getSuccessDeleteCount());
|
|
||||||
System.out.println("\t\ttotal_download_count = " + storageStat.getTotalDownloadCount());
|
|
||||||
System.out.println("\t\tsuccess_download_count = " + storageStat.getSuccessDownloadCount());
|
|
||||||
System.out.println("\t\ttotal_get_meta_count = " + storageStat.getTotalGetMetaCount());
|
|
||||||
System.out.println("\t\tsuccess_get_meta_count = " + storageStat.getSuccessGetMetaCount());
|
|
||||||
System.out.println("\t\ttotal_create_link_count = " + storageStat.getTotalCreateLinkCount());
|
|
||||||
System.out.println("\t\tsuccess_create_link_count = " + storageStat.getSuccessCreateLinkCount());
|
|
||||||
System.out.println("\t\ttotal_delete_link_count = " + storageStat.getTotalDeleteLinkCount());
|
|
||||||
System.out.println("\t\tsuccess_delete_link_count = " + storageStat.getSuccessDeleteLinkCount());
|
|
||||||
System.out.println("\t\ttotal_upload_bytes = " + storageStat.getTotalUploadBytes());
|
|
||||||
System.out.println("\t\tsuccess_upload_bytes = " + storageStat.getSuccessUploadBytes());
|
|
||||||
System.out.println("\t\ttotal_append_bytes = " + storageStat.getTotalAppendBytes());
|
|
||||||
System.out.println("\t\tsuccess_append_bytes = " + storageStat.getSuccessAppendBytes());
|
|
||||||
System.out.println("\t\ttotal_modify_bytes = " + storageStat.getTotalModifyBytes());
|
|
||||||
System.out.println("\t\tsuccess_modify_bytes = " + storageStat.getSuccessModifyBytes());
|
|
||||||
System.out.println("\t\ttotal_download_bytes = " + storageStat.getTotalDownloadloadBytes());
|
|
||||||
System.out.println("\t\tsuccess_download_bytes = " + storageStat.getSuccessDownloadloadBytes());
|
|
||||||
System.out.println("\t\ttotal_sync_in_bytes = " + storageStat.getTotalSyncInBytes());
|
|
||||||
System.out.println("\t\tsuccess_sync_in_bytes = " + storageStat.getSuccessSyncInBytes());
|
|
||||||
System.out.println("\t\ttotal_sync_out_bytes = " + storageStat.getTotalSyncOutBytes());
|
|
||||||
System.out.println("\t\tsuccess_sync_out_bytes = " + storageStat.getSuccessSyncOutBytes());
|
|
||||||
System.out.println("\t\ttotal_file_open_count = " + storageStat.getTotalFileOpenCount());
|
|
||||||
System.out.println("\t\tsuccess_file_open_count = " + storageStat.getSuccessFileOpenCount());
|
|
||||||
System.out.println("\t\ttotal_file_read_count = " + storageStat.getTotalFileReadCount());
|
|
||||||
System.out.println("\t\tsuccess_file_read_count = " + storageStat.getSuccessFileReadCount());
|
|
||||||
System.out.println("\t\ttotal_file_write_count = " + storageStat.getTotalFileWriteCount());
|
|
||||||
System.out.println("\t\tsuccess_file_write_count = " + storageStat.getSuccessFileWriteCount());
|
|
||||||
System.out.println("\t\tlast_heart_beat_time = " + df.format(storageStat.getLastHeartBeatTime()));
|
|
||||||
System.out.println("\t\tlast_source_update = " + df.format(storageStat.getLastSourceUpdate()));
|
|
||||||
System.out.println("\t\tlast_sync_update = " + df.format(storageStat.getLastSyncUpdate()));
|
|
||||||
System.out.println("\t\tlast_synced_timestamp = " + df.format(storageStat.getLastSyncedTimestamp()) + getSyncedDelayString(storageStats, storageStat));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trackerServer.close();
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static String getSyncedDelayString(StructStorageStat[] storageStats, StructStorageStat currentStorageStat)
|
|
||||||
{
|
|
||||||
long maxLastSourceUpdate = 0;
|
|
||||||
for (StructStorageStat storageStat : storageStats)
|
|
||||||
{
|
|
||||||
if (storageStat != currentStorageStat && storageStat.getLastSourceUpdate().getTime() > maxLastSourceUpdate)
|
|
||||||
{
|
|
||||||
maxLastSourceUpdate = storageStat.getLastSourceUpdate().getTime();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (maxLastSourceUpdate == 0)
|
|
||||||
{
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (currentStorageStat.getLastSyncedTimestamp().getTime() == 0)
|
TrackerServer trackerServer = tracker.getConnection();
|
||||||
{
|
if (trackerServer == null) {
|
||||||
return " (never synced)";
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int delaySeconds = (int)((maxLastSourceUpdate - currentStorageStat.getLastSyncedTimestamp().getTime()) / 1000);
|
int count;
|
||||||
int day = delaySeconds / (24 * 3600);
|
StructGroupStat[] groupStats = tracker.listGroups(trackerServer);
|
||||||
int remainSeconds = delaySeconds % (24 * 3600);
|
if (groupStats == null) {
|
||||||
int hour = remainSeconds / 3600;
|
System.out.println("");
|
||||||
remainSeconds %= 3600;
|
System.out.println("ERROR! list groups error, error no: " + tracker.getErrorCode());
|
||||||
int minute = remainSeconds / 60;
|
System.out.println("");
|
||||||
int second = remainSeconds % 60;
|
return;
|
||||||
String delayTimeStr;
|
}
|
||||||
if (day != 0)
|
|
||||||
{
|
System.out.println("group count: " + groupStats.length);
|
||||||
delayTimeStr = String.format("%1$d days %2$02dh:%3$02dm:%4$02ds", day, hour, minute, second);
|
|
||||||
}
|
count = 0;
|
||||||
else if (hour != 0)
|
for (StructGroupStat groupStat : groupStats) {
|
||||||
{
|
count++;
|
||||||
delayTimeStr = String.format("%1$02dh:%2$02dm:%3$02ds", hour, minute, second);
|
System.out.println("Group " + count + ":");
|
||||||
}
|
System.out.println("group name = " + groupStat.getGroupName());
|
||||||
else if (minute != 0)
|
System.out.println("disk total space = " + groupStat.getTotalMB() + "MB");
|
||||||
{
|
System.out.println("disk free space = " + groupStat.getFreeMB() + " MB");
|
||||||
delayTimeStr = String.format("%1$02dm:%2$02ds", minute, second);
|
System.out.println("trunk free space = " + groupStat.getTrunkFreeMB() + " MB");
|
||||||
}
|
System.out.println("storage server count = " + groupStat.getStorageCount());
|
||||||
else
|
System.out.println("active server count = " + groupStat.getActiveCount());
|
||||||
{
|
System.out.println("storage server port = " + groupStat.getStoragePort());
|
||||||
delayTimeStr = String.format("%1$ds", second);
|
System.out.println("storage HTTP port = " + groupStat.getStorageHttpPort());
|
||||||
}
|
System.out.println("store path count = " + groupStat.getStorePathCount());
|
||||||
|
System.out.println("subdir count per path = " + groupStat.getSubdirCountPerPath());
|
||||||
return " (" + delayTimeStr + " delay)";
|
System.out.println("current write server index = " + groupStat.getCurrentWriteServer());
|
||||||
}
|
System.out.println("current trunk file id = " + groupStat.getCurrentTrunkFileId());
|
||||||
|
|
||||||
|
StructStorageStat[] storageStats = tracker.listStorages(trackerServer, groupStat.getGroupName());
|
||||||
|
if (storageStats == null) {
|
||||||
|
System.out.println("");
|
||||||
|
System.out.println("ERROR! list storage error, error no: " + tracker.getErrorCode());
|
||||||
|
System.out.println("");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
int stroageCount = 0;
|
||||||
|
for (StructStorageStat storageStat : storageStats) {
|
||||||
|
stroageCount++;
|
||||||
|
System.out.println("\tStorage " + stroageCount + ":");
|
||||||
|
System.out.println("\t\tstorage id = " + storageStat.getId());
|
||||||
|
System.out.println("\t\tip_addr = " + storageStat.getIpAddr() + " " + ProtoCommon.getStorageStatusCaption(storageStat.getStatus()));
|
||||||
|
System.out.println("\t\thttp domain = " + storageStat.getDomainName());
|
||||||
|
System.out.println("\t\tversion = " + storageStat.getVersion());
|
||||||
|
System.out.println("\t\tjoin time = " + df.format(storageStat.getJoinTime()));
|
||||||
|
System.out.println("\t\tup time = " + (storageStat.getUpTime().getTime() == 0 ? "" : df.format(storageStat.getUpTime())));
|
||||||
|
System.out.println("\t\ttotal storage = " + storageStat.getTotalMB() + "MB");
|
||||||
|
System.out.println("\t\tfree storage = " + storageStat.getFreeMB() + "MB");
|
||||||
|
System.out.println("\t\tupload priority = " + storageStat.getUploadPriority());
|
||||||
|
System.out.println("\t\tstore_path_count = " + storageStat.getStorePathCount());
|
||||||
|
System.out.println("\t\tsubdir_count_per_path = " + storageStat.getSubdirCountPerPath());
|
||||||
|
System.out.println("\t\tstorage_port = " + storageStat.getStoragePort());
|
||||||
|
System.out.println("\t\tstorage_http_port = " + storageStat.getStorageHttpPort());
|
||||||
|
System.out.println("\t\tcurrent_write_path = " + storageStat.getCurrentWritePath());
|
||||||
|
System.out.println("\t\tsource ip_addr = " + storageStat.getSrcIpAddr());
|
||||||
|
System.out.println("\t\tif_trunk_server = " + storageStat.isTrunkServer());
|
||||||
|
System.out.println("\t\tconntion.alloc_count = " + storageStat.getConnectionAllocCount());
|
||||||
|
System.out.println("\t\tconntion.current_count = " + storageStat.getConnectionCurrentCount());
|
||||||
|
System.out.println("\t\tconntion.max_count = " + storageStat.getConnectionMaxCount());
|
||||||
|
System.out.println("\t\ttotal_upload_count = " + storageStat.getTotalUploadCount());
|
||||||
|
System.out.println("\t\tsuccess_upload_count = " + storageStat.getSuccessUploadCount());
|
||||||
|
System.out.println("\t\ttotal_append_count = " + storageStat.getTotalAppendCount());
|
||||||
|
System.out.println("\t\tsuccess_append_count = " + storageStat.getSuccessAppendCount());
|
||||||
|
System.out.println("\t\ttotal_modify_count = " + storageStat.getTotalModifyCount());
|
||||||
|
System.out.println("\t\tsuccess_modify_count = " + storageStat.getSuccessModifyCount());
|
||||||
|
System.out.println("\t\ttotal_truncate_count = " + storageStat.getTotalTruncateCount());
|
||||||
|
System.out.println("\t\tsuccess_truncate_count = " + storageStat.getSuccessTruncateCount());
|
||||||
|
System.out.println("\t\ttotal_set_meta_count = " + storageStat.getTotalSetMetaCount());
|
||||||
|
System.out.println("\t\tsuccess_set_meta_count = " + storageStat.getSuccessSetMetaCount());
|
||||||
|
System.out.println("\t\ttotal_delete_count = " + storageStat.getTotalDeleteCount());
|
||||||
|
System.out.println("\t\tsuccess_delete_count = " + storageStat.getSuccessDeleteCount());
|
||||||
|
System.out.println("\t\ttotal_download_count = " + storageStat.getTotalDownloadCount());
|
||||||
|
System.out.println("\t\tsuccess_download_count = " + storageStat.getSuccessDownloadCount());
|
||||||
|
System.out.println("\t\ttotal_get_meta_count = " + storageStat.getTotalGetMetaCount());
|
||||||
|
System.out.println("\t\tsuccess_get_meta_count = " + storageStat.getSuccessGetMetaCount());
|
||||||
|
System.out.println("\t\ttotal_create_link_count = " + storageStat.getTotalCreateLinkCount());
|
||||||
|
System.out.println("\t\tsuccess_create_link_count = " + storageStat.getSuccessCreateLinkCount());
|
||||||
|
System.out.println("\t\ttotal_delete_link_count = " + storageStat.getTotalDeleteLinkCount());
|
||||||
|
System.out.println("\t\tsuccess_delete_link_count = " + storageStat.getSuccessDeleteLinkCount());
|
||||||
|
System.out.println("\t\ttotal_upload_bytes = " + storageStat.getTotalUploadBytes());
|
||||||
|
System.out.println("\t\tsuccess_upload_bytes = " + storageStat.getSuccessUploadBytes());
|
||||||
|
System.out.println("\t\ttotal_append_bytes = " + storageStat.getTotalAppendBytes());
|
||||||
|
System.out.println("\t\tsuccess_append_bytes = " + storageStat.getSuccessAppendBytes());
|
||||||
|
System.out.println("\t\ttotal_modify_bytes = " + storageStat.getTotalModifyBytes());
|
||||||
|
System.out.println("\t\tsuccess_modify_bytes = " + storageStat.getSuccessModifyBytes());
|
||||||
|
System.out.println("\t\ttotal_download_bytes = " + storageStat.getTotalDownloadloadBytes());
|
||||||
|
System.out.println("\t\tsuccess_download_bytes = " + storageStat.getSuccessDownloadloadBytes());
|
||||||
|
System.out.println("\t\ttotal_sync_in_bytes = " + storageStat.getTotalSyncInBytes());
|
||||||
|
System.out.println("\t\tsuccess_sync_in_bytes = " + storageStat.getSuccessSyncInBytes());
|
||||||
|
System.out.println("\t\ttotal_sync_out_bytes = " + storageStat.getTotalSyncOutBytes());
|
||||||
|
System.out.println("\t\tsuccess_sync_out_bytes = " + storageStat.getSuccessSyncOutBytes());
|
||||||
|
System.out.println("\t\ttotal_file_open_count = " + storageStat.getTotalFileOpenCount());
|
||||||
|
System.out.println("\t\tsuccess_file_open_count = " + storageStat.getSuccessFileOpenCount());
|
||||||
|
System.out.println("\t\ttotal_file_read_count = " + storageStat.getTotalFileReadCount());
|
||||||
|
System.out.println("\t\tsuccess_file_read_count = " + storageStat.getSuccessFileReadCount());
|
||||||
|
System.out.println("\t\ttotal_file_write_count = " + storageStat.getTotalFileWriteCount());
|
||||||
|
System.out.println("\t\tsuccess_file_write_count = " + storageStat.getSuccessFileWriteCount());
|
||||||
|
System.out.println("\t\tlast_heart_beat_time = " + df.format(storageStat.getLastHeartBeatTime()));
|
||||||
|
System.out.println("\t\tlast_source_update = " + df.format(storageStat.getLastSourceUpdate()));
|
||||||
|
System.out.println("\t\tlast_sync_update = " + df.format(storageStat.getLastSyncUpdate()));
|
||||||
|
System.out.println("\t\tlast_synced_timestamp = " + df.format(storageStat.getLastSyncedTimestamp()) + getSyncedDelayString(storageStats, storageStat));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trackerServer.close();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static String getSyncedDelayString(StructStorageStat[] storageStats, StructStorageStat currentStorageStat) {
|
||||||
|
long maxLastSourceUpdate = 0;
|
||||||
|
for (StructStorageStat storageStat : storageStats) {
|
||||||
|
if (storageStat != currentStorageStat && storageStat.getLastSourceUpdate().getTime() > maxLastSourceUpdate) {
|
||||||
|
maxLastSourceUpdate = storageStat.getLastSourceUpdate().getTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maxLastSourceUpdate == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentStorageStat.getLastSyncedTimestamp().getTime() == 0) {
|
||||||
|
return " (never synced)";
|
||||||
|
}
|
||||||
|
|
||||||
|
int delaySeconds = (int) ((maxLastSourceUpdate - currentStorageStat.getLastSyncedTimestamp().getTime()) / 1000);
|
||||||
|
int day = delaySeconds / (24 * 3600);
|
||||||
|
int remainSeconds = delaySeconds % (24 * 3600);
|
||||||
|
int hour = remainSeconds / 3600;
|
||||||
|
remainSeconds %= 3600;
|
||||||
|
int minute = remainSeconds / 60;
|
||||||
|
int second = remainSeconds % 60;
|
||||||
|
String delayTimeStr;
|
||||||
|
if (day != 0) {
|
||||||
|
delayTimeStr = String.format("%1$d days %2$02dh:%3$02dm:%4$02ds", day, hour, minute, second);
|
||||||
|
} else if (hour != 0) {
|
||||||
|
delayTimeStr = String.format("%1$02dh:%2$02dm:%3$02ds", hour, minute, second);
|
||||||
|
} else if (minute != 0) {
|
||||||
|
delayTimeStr = String.format("%1$02dm:%2$02ds", minute, second);
|
||||||
|
} else {
|
||||||
|
delayTimeStr = String.format("%1$ds", second);
|
||||||
|
}
|
||||||
|
|
||||||
|
return " (" + delayTimeStr + " delay)";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,77 +1,69 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
package org.csource.fastdfs.test;
|
package org.csource.fastdfs.test;
|
||||||
|
|
||||||
import java.io.*;
|
import org.csource.common.NameValuePair;
|
||||||
import java.net.*;
|
|
||||||
import java.util.*;
|
|
||||||
import org.csource.common.*;
|
|
||||||
import org.csource.fastdfs.*;
|
import org.csource.fastdfs.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* client test
|
* client test
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.18
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.18
|
||||||
public class Test
|
*/
|
||||||
{
|
public class Test {
|
||||||
private Test()
|
private Test() {
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* entry point
|
|
||||||
* @param args comand arguments
|
|
||||||
* <ul><li>args[0]: config filename</li></ul>
|
|
||||||
* <ul><li>args[1]: local filename to upload</li></ul>
|
|
||||||
*/
|
|
||||||
public static void main(String args[])
|
|
||||||
{
|
|
||||||
if (args.length < 2)
|
|
||||||
{
|
|
||||||
System.out.println("Error: Must have 2 parameters, one is config filename, "
|
|
||||||
+ "the other is the local filename to upload");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("java.version=" + System.getProperty("java.version"));
|
|
||||||
|
|
||||||
String conf_filename = args[0];
|
|
||||||
String local_filename = args[1];
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ClientGlobal.init(conf_filename);
|
|
||||||
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
|
||||||
System.out.println("charset=" + ClientGlobal.g_charset);
|
|
||||||
|
|
||||||
TrackerClient tracker = new TrackerClient();
|
|
||||||
TrackerServer trackerServer = tracker.getConnection();
|
|
||||||
StorageServer storageServer = null;
|
|
||||||
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
|
||||||
|
|
||||||
NameValuePair[] metaList = new NameValuePair[1];
|
/**
|
||||||
metaList[0] = new NameValuePair("fileName", local_filename);
|
* entry point
|
||||||
String fileId = client.upload_file1(local_filename, null, metaList);
|
*
|
||||||
System.out.println("upload success. file id is: " + fileId);
|
* @param args comand arguments
|
||||||
|
* <ul><li>args[0]: config filename</li></ul>
|
||||||
|
* <ul><li>args[1]: local filename to upload</li></ul>
|
||||||
|
*/
|
||||||
|
public static void main(String args[]) {
|
||||||
|
if (args.length < 2) {
|
||||||
|
System.out.println("Error: Must have 2 parameters, one is config filename, "
|
||||||
|
+ "the other is the local filename to upload");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int i = 0;
|
System.out.println("java.version=" + System.getProperty("java.version"));
|
||||||
while (i++ < 10) {
|
|
||||||
byte[] result = client.download_file1(fileId);
|
String conf_filename = args[0];
|
||||||
System.out.println(i + ", download result is: " + result.length);
|
String local_filename = args[1];
|
||||||
}
|
|
||||||
|
try {
|
||||||
trackerServer.close();
|
ClientGlobal.init(conf_filename);
|
||||||
}
|
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
||||||
catch(Exception ex)
|
System.out.println("charset=" + ClientGlobal.g_charset);
|
||||||
{
|
|
||||||
ex.printStackTrace();
|
TrackerClient tracker = new TrackerClient();
|
||||||
}
|
TrackerServer trackerServer = tracker.getConnection();
|
||||||
|
StorageServer storageServer = null;
|
||||||
|
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
||||||
|
|
||||||
|
NameValuePair[] metaList = new NameValuePair[1];
|
||||||
|
metaList[0] = new NameValuePair("fileName", local_filename);
|
||||||
|
String fileId = client.upload_file1(local_filename, null, metaList);
|
||||||
|
System.out.println("upload success. file id is: " + fileId);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (i++ < 10) {
|
||||||
|
byte[] result = client.download_file1(fileId);
|
||||||
|
System.out.println(i + ", download result is: " + result.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
trackerServer.close();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,59 +1,47 @@
|
|||||||
|
|
||||||
package org.csource.fastdfs.test;
|
package org.csource.fastdfs.test;
|
||||||
|
|
||||||
import java.io.*;
|
import org.csource.common.NameValuePair;
|
||||||
import java.net.*;
|
|
||||||
import java.util.*;
|
|
||||||
import org.csource.common.*;
|
|
||||||
import org.csource.fastdfs.*;
|
import org.csource.fastdfs.*;
|
||||||
|
|
||||||
public class Test1
|
import java.net.InetSocketAddress;
|
||||||
{
|
|
||||||
public static void main(String args[])
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ClientGlobal.init("fdfs_client.conf");
|
|
||||||
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
|
||||||
System.out.println("charset=" + ClientGlobal.g_charset);
|
|
||||||
|
|
||||||
TrackerGroup tg = new TrackerGroup(new InetSocketAddress[]{new InetSocketAddress("10.0.11.243", 22122)});
|
public class Test1 {
|
||||||
TrackerClient tc = new TrackerClient(tg);
|
public static void main(String args[]) {
|
||||||
|
try {
|
||||||
|
ClientGlobal.init("fdfs_client.conf");
|
||||||
|
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
||||||
|
System.out.println("charset=" + ClientGlobal.g_charset);
|
||||||
|
|
||||||
TrackerServer ts = tc.getConnection();
|
TrackerGroup tg = new TrackerGroup(new InetSocketAddress[]{new InetSocketAddress("10.0.11.243", 22122)});
|
||||||
if (ts == null)
|
TrackerClient tc = new TrackerClient(tg);
|
||||||
{
|
|
||||||
System.out.println("getConnection return null");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
StorageServer ss = tc.getStoreStorage(ts);
|
TrackerServer ts = tc.getConnection();
|
||||||
if (ss == null)
|
if (ts == null) {
|
||||||
{
|
System.out.println("getConnection return null");
|
||||||
System.out.println("getStoreStorage return null");
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
StorageClient1 sc1 = new StorageClient1(ts, ss);
|
StorageServer ss = tc.getStoreStorage(ts);
|
||||||
|
if (ss == null) {
|
||||||
|
System.out.println("getStoreStorage return null");
|
||||||
|
}
|
||||||
|
|
||||||
NameValuePair[] meta_list = null; //new NameValuePair[0];
|
StorageClient1 sc1 = new StorageClient1(ts, ss);
|
||||||
String item;
|
|
||||||
String fileid;
|
|
||||||
if (System.getProperty("os.name").equalsIgnoreCase("windows"))
|
|
||||||
{
|
|
||||||
item = "c:/windows/system32/notepad.exe";
|
|
||||||
fileid = sc1.upload_file1(item, "exe", meta_list);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
item = "/etc/hosts";
|
|
||||||
fileid = sc1.upload_file1(item, "", meta_list);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Upload local file "+item+" ok, fileid="+fileid);
|
NameValuePair[] meta_list = null; //new NameValuePair[0];
|
||||||
}
|
String item;
|
||||||
catch(Exception ex)
|
String fileid;
|
||||||
{
|
if (System.getProperty("os.name").equalsIgnoreCase("windows")) {
|
||||||
ex.printStackTrace();
|
item = "c:/windows/system32/notepad.exe";
|
||||||
}
|
fileid = sc1.upload_file1(item, "exe", meta_list);
|
||||||
}
|
} else {
|
||||||
|
item = "/etc/hosts";
|
||||||
|
fileid = sc1.upload_file1(item, "", meta_list);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Upload local file " + item + " ok, fileid=" + fileid);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,67 +1,64 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
package org.csource.fastdfs.test;
|
package org.csource.fastdfs.test;
|
||||||
|
|
||||||
import java.io.*;
|
import org.csource.common.NameValuePair;
|
||||||
import java.net.*;
|
|
||||||
import java.util.*;
|
|
||||||
import org.csource.common.*;
|
|
||||||
import org.csource.fastdfs.*;
|
import org.csource.fastdfs.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* client test
|
* client test
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.20
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.20
|
||||||
public class TestAppender
|
*/
|
||||||
{
|
public class TestAppender {
|
||||||
private TestAppender()
|
private TestAppender() {
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* entry point
|
||||||
* entry point
|
*
|
||||||
* @param args comand arguments
|
* @param args comand arguments
|
||||||
* <ul><li>args[0]: config filename</li></ul>
|
* <ul><li>args[0]: config filename</li></ul>
|
||||||
* <ul><li>args[1]: local filename to upload</li></ul>
|
* <ul><li>args[1]: local filename to upload</li></ul>
|
||||||
*/
|
*/
|
||||||
public static void main(String args[])
|
public static void main(String args[]) {
|
||||||
{
|
if (args.length < 2) {
|
||||||
if (args.length < 2)
|
System.out.println("Error: Must have 2 parameters, one is config filename, "
|
||||||
{
|
+ "the other is the local filename to upload");
|
||||||
System.out.println("Error: Must have 2 parameters, one is config filename, "
|
return;
|
||||||
+ "the other is the local filename to upload");
|
}
|
||||||
return;
|
|
||||||
}
|
System.out.println("java.version=" + System.getProperty("java.version"));
|
||||||
|
|
||||||
System.out.println("java.version=" + System.getProperty("java.version"));
|
String conf_filename = args[0];
|
||||||
|
String local_filename = args[1];
|
||||||
String conf_filename = args[0];
|
|
||||||
String local_filename = args[1];
|
try {
|
||||||
|
ClientGlobal.init(conf_filename);
|
||||||
try
|
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
||||||
{
|
System.out.println("charset=" + ClientGlobal.g_charset);
|
||||||
ClientGlobal.init(conf_filename);
|
|
||||||
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
long startTime;
|
||||||
System.out.println("charset=" + ClientGlobal.g_charset);
|
String group_name;
|
||||||
|
String remote_filename;
|
||||||
long startTime;
|
ServerInfo[] servers;
|
||||||
String group_name;
|
TrackerClient tracker = new TrackerClient();
|
||||||
String remote_filename;
|
TrackerServer trackerServer = tracker.getConnection();
|
||||||
ServerInfo[] servers;
|
|
||||||
TrackerClient tracker = new TrackerClient();
|
StorageServer storageServer = null;
|
||||||
TrackerServer trackerServer = tracker.getConnection();
|
|
||||||
|
|
||||||
StorageServer storageServer = null;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
storageServer = tracker.getStoreStorage(trackerServer);
|
storageServer = tracker.getStoreStorage(trackerServer);
|
||||||
if (storageServer == null)
|
if (storageServer == null)
|
||||||
{
|
{
|
||||||
System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode());
|
System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode());
|
||||||
@ -69,290 +66,237 @@ public class TestAppender
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
StorageClient client = new StorageClient(trackerServer, storageServer);
|
StorageClient client = new StorageClient(trackerServer, storageServer);
|
||||||
byte[] file_buff;
|
byte[] file_buff;
|
||||||
NameValuePair[] meta_list;
|
NameValuePair[] meta_list;
|
||||||
String[] results;
|
String[] results;
|
||||||
String appender_filename;
|
String appender_filename;
|
||||||
String file_ext_name;
|
String file_ext_name;
|
||||||
int errno;
|
int errno;
|
||||||
|
|
||||||
meta_list = new NameValuePair[4];
|
meta_list = new NameValuePair[4];
|
||||||
meta_list[0] = new NameValuePair("width", "800");
|
meta_list[0] = new NameValuePair("width", "800");
|
||||||
meta_list[1] = new NameValuePair("heigth", "600");
|
meta_list[1] = new NameValuePair("heigth", "600");
|
||||||
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
|
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
|
||||||
meta_list[3] = new NameValuePair("author", "Mike");
|
meta_list[3] = new NameValuePair("author", "Mike");
|
||||||
|
|
||||||
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
|
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
|
||||||
System.out.println("file length: " + file_buff.length);
|
System.out.println("file length: " + file_buff.length);
|
||||||
|
|
||||||
|
group_name = null;
|
||||||
|
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
|
||||||
|
if (storageServers == null) {
|
||||||
|
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
|
||||||
|
} else {
|
||||||
|
System.err.println("store storage servers count: " + storageServers.length);
|
||||||
|
for (int k = 0; k < storageServers.length; k++) {
|
||||||
|
System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
|
||||||
|
}
|
||||||
|
System.err.println("");
|
||||||
|
}
|
||||||
|
|
||||||
|
startTime = System.currentTimeMillis();
|
||||||
|
results = client.upload_appender_file(file_buff, "txt", meta_list);
|
||||||
|
System.out.println("upload_appender_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
|
|
||||||
group_name = null;
|
|
||||||
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
|
|
||||||
if (storageServers == null)
|
|
||||||
{
|
|
||||||
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("store storage servers count: " + storageServers.length);
|
|
||||||
for (int k=0; k<storageServers.length; k++)
|
|
||||||
{
|
|
||||||
System.err.println((k+1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
|
|
||||||
}
|
|
||||||
System.err.println("");
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
results = client.upload_appender_file(file_buff, "txt", meta_list);
|
|
||||||
System.out.println("upload_appender_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
group_name = "";
|
group_name = "";
|
||||||
results = client.upload_appender_file(group_name, file_buff, "txt", meta_list);
|
results = client.upload_appender_file(group_name, file_buff, "txt", meta_list);
|
||||||
*/
|
*/
|
||||||
if (results == null)
|
if (results == null) {
|
||||||
{
|
System.err.println("upload file fail, error code: " + client.getErrorCode());
|
||||||
System.err.println("upload file fail, error code: " + client.getErrorCode());
|
return;
|
||||||
return;
|
} else {
|
||||||
}
|
group_name = results[0];
|
||||||
else
|
remote_filename = results[1];
|
||||||
{
|
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
|
||||||
group_name = results[0];
|
System.err.println(client.get_file_info(group_name, remote_filename));
|
||||||
remote_filename = results[1];
|
|
||||||
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
|
|
||||||
System.err.println(client.get_file_info(group_name, remote_filename));
|
|
||||||
|
|
||||||
servers = tracker.getFetchStorages(trackerServer, group_name, remote_filename);
|
servers = tracker.getFetchStorages(trackerServer, group_name, remote_filename);
|
||||||
if (servers == null)
|
if (servers == null) {
|
||||||
{
|
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
|
||||||
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
|
} else {
|
||||||
}
|
System.err.println("storage servers count: " + servers.length);
|
||||||
else
|
for (int k = 0; k < servers.length; k++) {
|
||||||
{
|
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
|
||||||
System.err.println("storage servers count: " + servers.length);
|
}
|
||||||
for (int k=0; k<servers.length; k++)
|
System.err.println("");
|
||||||
{
|
}
|
||||||
System.err.println((k+1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
|
|
||||||
}
|
meta_list = new NameValuePair[4];
|
||||||
System.err.println("");
|
meta_list[0] = new NameValuePair("width", "1024");
|
||||||
}
|
meta_list[1] = new NameValuePair("heigth", "768");
|
||||||
|
meta_list[2] = new NameValuePair("bgcolor", "#000000");
|
||||||
meta_list = new NameValuePair[4];
|
meta_list[3] = new NameValuePair("title", "Untitle");
|
||||||
meta_list[0] = new NameValuePair("width", "1024");
|
|
||||||
meta_list[1] = new NameValuePair("heigth", "768");
|
startTime = System.currentTimeMillis();
|
||||||
meta_list[2] = new NameValuePair("bgcolor", "#000000");
|
errno = client.set_metadata(group_name, remote_filename, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
|
||||||
meta_list[3] = new NameValuePair("title", "Untitle");
|
System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
|
if (errno == 0) {
|
||||||
startTime = System.currentTimeMillis();
|
System.err.println("set_metadata success");
|
||||||
errno=client.set_metadata(group_name, remote_filename, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
|
} else {
|
||||||
System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
System.err.println("set_metadata fail, error no: " + errno);
|
||||||
if (errno == 0)
|
}
|
||||||
{
|
|
||||||
System.err.println("set_metadata success");
|
meta_list = client.get_metadata(group_name, remote_filename);
|
||||||
}
|
if (meta_list != null) {
|
||||||
else
|
for (int i = 0; i < meta_list.length; i++) {
|
||||||
{
|
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
|
||||||
System.err.println("set_metadata fail, error no: " + errno);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
meta_list = client.get_metadata(group_name, remote_filename);
|
//Thread.sleep(30000);
|
||||||
if (meta_list != null)
|
|
||||||
{
|
startTime = System.currentTimeMillis();
|
||||||
for (int i=0; i<meta_list.length; i++)
|
file_buff = client.download_file(group_name, remote_filename);
|
||||||
{
|
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
|
|
||||||
}
|
if (file_buff != null) {
|
||||||
}
|
System.out.println("file length:" + file_buff.length);
|
||||||
|
System.out.println((new String(file_buff)));
|
||||||
//Thread.sleep(30000);
|
}
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset);
|
||||||
file_buff = client.download_file(group_name, remote_filename);
|
appender_filename = remote_filename;
|
||||||
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
file_ext_name = "txt";
|
||||||
|
startTime = System.currentTimeMillis();
|
||||||
if (file_buff != null)
|
errno = client.append_file(group_name, appender_filename, file_buff);
|
||||||
{
|
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
System.out.println("file length:" + file_buff.length);
|
if (errno == 0) {
|
||||||
System.out.println((new String(file_buff)));
|
System.err.println(client.get_file_info(group_name, appender_filename));
|
||||||
}
|
} else {
|
||||||
|
System.err.println("append file fail, error no: " + errno);
|
||||||
file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset);
|
}
|
||||||
appender_filename = remote_filename;
|
|
||||||
file_ext_name = "txt";
|
startTime = System.currentTimeMillis();
|
||||||
startTime = System.currentTimeMillis();
|
errno = client.delete_file(group_name, remote_filename);
|
||||||
errno = client.append_file(group_name, appender_filename, file_buff);
|
System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
if (errno == 0) {
|
||||||
if (errno == 0)
|
System.err.println("Delete file success");
|
||||||
{
|
} else {
|
||||||
System.err.println(client.get_file_info(group_name, appender_filename));
|
System.err.println("Delete file fail, error no: " + errno);
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
|
||||||
System.err.println("append file fail, error no: " + errno);
|
results = client.upload_appender_file(local_filename, null, meta_list);
|
||||||
}
|
if (results != null) {
|
||||||
|
String file_id;
|
||||||
startTime = System.currentTimeMillis();
|
int ts;
|
||||||
errno = client.delete_file(group_name, remote_filename);
|
String token;
|
||||||
System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
String file_url;
|
||||||
if (errno == 0)
|
InetSocketAddress inetSockAddr;
|
||||||
{
|
|
||||||
System.err.println("Delete file success");
|
group_name = results[0];
|
||||||
}
|
remote_filename = results[1];
|
||||||
else
|
file_id = group_name + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remote_filename;
|
||||||
{
|
|
||||||
System.err.println("Delete file fail, error no: " + errno);
|
inetSockAddr = trackerServer.getInetSocketAddress();
|
||||||
}
|
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
|
||||||
}
|
if (ClientGlobal.g_tracker_http_port != 80) {
|
||||||
|
file_url += ":" + ClientGlobal.g_tracker_http_port;
|
||||||
results = client.upload_appender_file(local_filename, null, meta_list);
|
}
|
||||||
if (results != null)
|
file_url += "/" + file_id;
|
||||||
{
|
if (ClientGlobal.g_anti_steal_token) {
|
||||||
String file_id;
|
ts = (int) (System.currentTimeMillis() / 1000);
|
||||||
int ts;
|
token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
|
||||||
String token;
|
file_url += "?token=" + token + "&ts=" + ts;
|
||||||
String file_url;
|
}
|
||||||
InetSocketAddress inetSockAddr;
|
|
||||||
|
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
|
||||||
group_name = results[0];
|
System.err.println(client.get_file_info(group_name, remote_filename));
|
||||||
remote_filename = results[1];
|
System.err.println("file url: " + file_url);
|
||||||
file_id = group_name + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remote_filename;
|
|
||||||
|
errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.replaceAll("/", "_"));
|
||||||
inetSockAddr = trackerServer.getInetSocketAddress();
|
if (errno == 0) {
|
||||||
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
|
System.err.println("Download file success");
|
||||||
if (ClientGlobal.g_tracker_http_port != 80)
|
} else {
|
||||||
{
|
System.err.println("Download file fail, error no: " + errno);
|
||||||
file_url += ":" + ClientGlobal.g_tracker_http_port;
|
}
|
||||||
}
|
|
||||||
file_url += "/" + file_id;
|
errno = client.download_file(group_name, remote_filename, 0, 0, new DownloadFileWriter("c:\\" + remote_filename.replaceAll("/", "-")));
|
||||||
if (ClientGlobal.g_anti_steal_token)
|
if (errno == 0) {
|
||||||
{
|
System.err.println("Download file success");
|
||||||
ts = (int)(System.currentTimeMillis() / 1000);
|
} else {
|
||||||
token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
|
System.err.println("Download file fail, error no: " + errno);
|
||||||
file_url += "?token=" + token + "&ts=" + ts;
|
}
|
||||||
}
|
|
||||||
|
appender_filename = remote_filename;
|
||||||
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
|
file_ext_name = null;
|
||||||
System.err.println(client.get_file_info(group_name, remote_filename));
|
startTime = System.currentTimeMillis();
|
||||||
System.err.println("file url: " + file_url);
|
errno = client.append_file(group_name, appender_filename, local_filename);
|
||||||
|
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.replaceAll("/", "_"));
|
if (errno == 0) {
|
||||||
if (errno == 0)
|
System.err.println(client.get_file_info(group_name, appender_filename));
|
||||||
{
|
} else {
|
||||||
System.err.println("Download file success");
|
System.err.println("append file fail, error no: " + errno);
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
|
||||||
System.err.println("Download file fail, error no: " + errno);
|
File f;
|
||||||
}
|
f = new File(local_filename);
|
||||||
|
int nPos = local_filename.lastIndexOf('.');
|
||||||
errno = client.download_file(group_name, remote_filename, 0, 0, new DownloadFileWriter("c:\\" + remote_filename.replaceAll("/", "-")));
|
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
|
||||||
if (errno == 0)
|
file_ext_name = local_filename.substring(nPos + 1);
|
||||||
{
|
} else {
|
||||||
System.err.println("Download file success");
|
file_ext_name = null;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
results = client.upload_appender_file(null, f.length(),
|
||||||
System.err.println("Download file fail, error no: " + errno);
|
new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
||||||
}
|
if (results != null) {
|
||||||
|
group_name = results[0];
|
||||||
appender_filename = remote_filename;
|
remote_filename = results[1];
|
||||||
file_ext_name = null;
|
|
||||||
startTime = System.currentTimeMillis();
|
System.out.println("group name: " + group_name + ", remote filename: " + remote_filename);
|
||||||
errno = client.append_file(group_name, appender_filename, local_filename);
|
System.out.println(client.get_file_info(group_name, remote_filename));
|
||||||
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
if (errno == 0)
|
appender_filename = remote_filename;
|
||||||
{
|
startTime = System.currentTimeMillis();
|
||||||
System.err.println(client.get_file_info(group_name, appender_filename));
|
errno = client.append_file(group_name, appender_filename, f.length(), new UploadLocalFileSender(local_filename));
|
||||||
}
|
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
else
|
if (errno == 0) {
|
||||||
{
|
System.err.println(client.get_file_info(group_name, appender_filename));
|
||||||
System.err.println("append file fail, error no: " + errno);
|
} else {
|
||||||
}
|
System.err.println("append file fail, error no: " + errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
File f;
|
startTime = System.currentTimeMillis();
|
||||||
f = new File(local_filename);
|
errno = client.modify_file(group_name, appender_filename, 0, f.length(), new UploadLocalFileSender(local_filename));
|
||||||
int nPos = local_filename.lastIndexOf('.');
|
System.out.println("modify_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1)
|
if (errno == 0) {
|
||||||
{
|
System.err.println(client.get_file_info(group_name, appender_filename));
|
||||||
file_ext_name = local_filename.substring(nPos+1);
|
} else {
|
||||||
}
|
System.err.println("modify file fail, error no: " + errno);
|
||||||
else
|
}
|
||||||
{
|
|
||||||
file_ext_name = null;
|
startTime = System.currentTimeMillis();
|
||||||
}
|
errno = client.truncate_file(group_name, appender_filename);
|
||||||
|
System.out.println("truncate_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
results = client.upload_appender_file(null, f.length(),
|
if (errno == 0) {
|
||||||
new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
System.err.println(client.get_file_info(group_name, appender_filename));
|
||||||
if (results != null)
|
} else {
|
||||||
{
|
System.err.println("truncate file fail, error no: " + errno);
|
||||||
group_name = results[0];
|
}
|
||||||
remote_filename = results[1];
|
} else {
|
||||||
|
System.err.println("Upload file fail, error no: " + errno);
|
||||||
System.out.println("group name: " + group_name + ", remote filename: " + remote_filename);
|
}
|
||||||
System.out.println(client.get_file_info(group_name, remote_filename));
|
|
||||||
|
storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename);
|
||||||
appender_filename = remote_filename;
|
if (storageServer == null) {
|
||||||
startTime = System.currentTimeMillis();
|
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
|
||||||
errno = client.append_file(group_name, appender_filename, f.length(), new UploadLocalFileSender(local_filename));
|
return;
|
||||||
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
}
|
||||||
if (errno == 0)
|
|
||||||
{
|
|
||||||
System.err.println(client.get_file_info(group_name, appender_filename));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("append file fail, error no: " + errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
errno = client.modify_file(group_name, appender_filename, 0, f.length(), new UploadLocalFileSender(local_filename));
|
|
||||||
System.out.println("modify_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
if (errno == 0)
|
|
||||||
{
|
|
||||||
System.err.println(client.get_file_info(group_name, appender_filename));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("modify file fail, error no: " + errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
errno = client.truncate_file(group_name, appender_filename);
|
|
||||||
System.out.println("truncate_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
if (errno == 0)
|
|
||||||
{
|
|
||||||
System.err.println(client.get_file_info(group_name, appender_filename));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("truncate file fail, error no: " + errno);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("Upload file fail, error no: " + errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename);
|
|
||||||
if (storageServer == null)
|
|
||||||
{
|
|
||||||
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/* for test only */
|
/* for test only */
|
||||||
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
|
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
|
||||||
|
|
||||||
storageServer.close();
|
storageServer.close();
|
||||||
|
|
||||||
/* for test only */
|
/* for test only */
|
||||||
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
|
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
|
||||||
|
|
||||||
trackerServer.close();
|
trackerServer.close();
|
||||||
}
|
} catch (Exception ex) {
|
||||||
catch(Exception ex)
|
ex.printStackTrace();
|
||||||
{
|
}
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,65 +1,62 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
package org.csource.fastdfs.test;
|
package org.csource.fastdfs.test;
|
||||||
|
|
||||||
import java.io.*;
|
import org.csource.common.NameValuePair;
|
||||||
import java.net.*;
|
|
||||||
import java.util.*;
|
|
||||||
import org.csource.common.*;
|
|
||||||
import org.csource.fastdfs.*;
|
import org.csource.fastdfs.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* client test
|
* client test
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.20
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.20
|
||||||
public class TestAppender1
|
*/
|
||||||
{
|
public class TestAppender1 {
|
||||||
private TestAppender1()
|
private TestAppender1() {
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* entry point
|
||||||
* entry point
|
*
|
||||||
* @param args comand arguments
|
* @param args comand arguments
|
||||||
* <ul><li>args[0]: config filename</li></ul>
|
* <ul><li>args[0]: config filename</li></ul>
|
||||||
* <ul><li>args[1]: local filename to upload</li></ul>
|
* <ul><li>args[1]: local filename to upload</li></ul>
|
||||||
*/
|
*/
|
||||||
public static void main(String args[])
|
public static void main(String args[]) {
|
||||||
{
|
if (args.length < 2) {
|
||||||
if (args.length < 2)
|
System.out.println("Error: Must have 2 parameters, one is config filename, "
|
||||||
{
|
+ "the other is the local filename to upload");
|
||||||
System.out.println("Error: Must have 2 parameters, one is config filename, "
|
return;
|
||||||
+ "the other is the local filename to upload");
|
}
|
||||||
return;
|
|
||||||
}
|
System.out.println("java.version=" + System.getProperty("java.version"));
|
||||||
|
|
||||||
System.out.println("java.version=" + System.getProperty("java.version"));
|
String conf_filename = args[0];
|
||||||
|
String local_filename = args[1];
|
||||||
String conf_filename = args[0];
|
|
||||||
String local_filename = args[1];
|
try {
|
||||||
|
ClientGlobal.init(conf_filename);
|
||||||
try
|
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
||||||
{
|
System.out.println("charset=" + ClientGlobal.g_charset);
|
||||||
ClientGlobal.init(conf_filename);
|
|
||||||
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
long startTime;
|
||||||
System.out.println("charset=" + ClientGlobal.g_charset);
|
ServerInfo[] servers;
|
||||||
|
TrackerClient tracker = new TrackerClient();
|
||||||
long startTime;
|
TrackerServer trackerServer = tracker.getConnection();
|
||||||
ServerInfo[] servers;
|
|
||||||
TrackerClient tracker = new TrackerClient();
|
StorageServer storageServer = null;
|
||||||
TrackerServer trackerServer = tracker.getConnection();
|
|
||||||
|
|
||||||
StorageServer storageServer = null;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
storageServer = tracker.getStoreStorage(trackerServer);
|
storageServer = tracker.getStoreStorage(trackerServer);
|
||||||
if (storageServer == null)
|
if (storageServer == null)
|
||||||
{
|
{
|
||||||
System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode());
|
System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode());
|
||||||
@ -67,272 +64,219 @@ public class TestAppender1
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
||||||
byte[] file_buff;
|
byte[] file_buff;
|
||||||
NameValuePair[] meta_list;
|
NameValuePair[] meta_list;
|
||||||
String group_name;
|
String group_name;
|
||||||
String appender_file_id;
|
String appender_file_id;
|
||||||
String file_ext_name;
|
String file_ext_name;
|
||||||
int errno;
|
int errno;
|
||||||
|
|
||||||
meta_list = new NameValuePair[4];
|
meta_list = new NameValuePair[4];
|
||||||
meta_list[0] = new NameValuePair("width", "800");
|
meta_list[0] = new NameValuePair("width", "800");
|
||||||
meta_list[1] = new NameValuePair("heigth", "600");
|
meta_list[1] = new NameValuePair("heigth", "600");
|
||||||
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
|
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
|
||||||
meta_list[3] = new NameValuePair("author", "Mike");
|
meta_list[3] = new NameValuePair("author", "Mike");
|
||||||
|
|
||||||
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
|
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
|
||||||
System.out.println("file length: " + file_buff.length);
|
System.out.println("file length: " + file_buff.length);
|
||||||
|
|
||||||
|
group_name = null;
|
||||||
|
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
|
||||||
|
if (storageServers == null) {
|
||||||
|
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
|
||||||
|
} else {
|
||||||
|
System.err.println("store storage servers count: " + storageServers.length);
|
||||||
|
for (int k = 0; k < storageServers.length; k++) {
|
||||||
|
System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
|
||||||
|
}
|
||||||
|
System.err.println("");
|
||||||
|
}
|
||||||
|
|
||||||
|
startTime = System.currentTimeMillis();
|
||||||
|
appender_file_id = client.upload_appender_file1(file_buff, "txt", meta_list);
|
||||||
|
System.out.println("upload_appender_file1 time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
|
|
||||||
group_name = null;
|
|
||||||
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
|
|
||||||
if (storageServers == null)
|
|
||||||
{
|
|
||||||
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("store storage servers count: " + storageServers.length);
|
|
||||||
for (int k=0; k<storageServers.length; k++)
|
|
||||||
{
|
|
||||||
System.err.println((k+1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
|
|
||||||
}
|
|
||||||
System.err.println("");
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
appender_file_id = client.upload_appender_file1(file_buff, "txt", meta_list);
|
|
||||||
System.out.println("upload_appender_file1 time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
group_name = "";
|
group_name = "";
|
||||||
appender_file_id = client.upload_appender_file1(group_name, file_buff, "txt", meta_list);
|
appender_file_id = client.upload_appender_file1(group_name, file_buff, "txt", meta_list);
|
||||||
*/
|
*/
|
||||||
if (appender_file_id == null)
|
if (appender_file_id == null) {
|
||||||
{
|
System.err.println("upload file fail, error code: " + client.getErrorCode());
|
||||||
System.err.println("upload file fail, error code: " + client.getErrorCode());
|
return;
|
||||||
return;
|
} else {
|
||||||
}
|
System.err.println(client.get_file_info1(appender_file_id));
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println(client.get_file_info1(appender_file_id));
|
|
||||||
|
|
||||||
servers = tracker.getFetchStorages1(trackerServer, appender_file_id);
|
servers = tracker.getFetchStorages1(trackerServer, appender_file_id);
|
||||||
if (servers == null)
|
if (servers == null) {
|
||||||
{
|
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
|
||||||
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
|
} else {
|
||||||
}
|
System.err.println("storage servers count: " + servers.length);
|
||||||
else
|
for (int k = 0; k < servers.length; k++) {
|
||||||
{
|
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
|
||||||
System.err.println("storage servers count: " + servers.length);
|
}
|
||||||
for (int k=0; k<servers.length; k++)
|
System.err.println("");
|
||||||
{
|
}
|
||||||
System.err.println((k+1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
|
|
||||||
}
|
meta_list = new NameValuePair[4];
|
||||||
System.err.println("");
|
meta_list[0] = new NameValuePair("width", "1024");
|
||||||
}
|
meta_list[1] = new NameValuePair("heigth", "768");
|
||||||
|
meta_list[2] = new NameValuePair("bgcolor", "#000000");
|
||||||
meta_list = new NameValuePair[4];
|
meta_list[3] = new NameValuePair("title", "Untitle");
|
||||||
meta_list[0] = new NameValuePair("width", "1024");
|
|
||||||
meta_list[1] = new NameValuePair("heigth", "768");
|
startTime = System.currentTimeMillis();
|
||||||
meta_list[2] = new NameValuePair("bgcolor", "#000000");
|
errno = client.set_metadata1(appender_file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
|
||||||
meta_list[3] = new NameValuePair("title", "Untitle");
|
System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
|
if (errno == 0) {
|
||||||
startTime = System.currentTimeMillis();
|
System.err.println("set_metadata success");
|
||||||
errno=client.set_metadata1(appender_file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
|
} else {
|
||||||
System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
System.err.println("set_metadata fail, error no: " + errno);
|
||||||
if (errno == 0)
|
}
|
||||||
{
|
|
||||||
System.err.println("set_metadata success");
|
meta_list = client.get_metadata1(appender_file_id);
|
||||||
}
|
if (meta_list != null) {
|
||||||
else
|
for (int i = 0; i < meta_list.length; i++) {
|
||||||
{
|
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
|
||||||
System.err.println("set_metadata fail, error no: " + errno);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
meta_list = client.get_metadata1(appender_file_id);
|
startTime = System.currentTimeMillis();
|
||||||
if (meta_list != null)
|
file_buff = client.download_file1(appender_file_id);
|
||||||
{
|
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
for (int i=0; i<meta_list.length; i++)
|
|
||||||
{
|
if (file_buff != null) {
|
||||||
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
|
System.out.println("file length:" + file_buff.length);
|
||||||
}
|
System.out.println((new String(file_buff)));
|
||||||
}
|
}
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset);
|
||||||
file_buff = client.download_file1(appender_file_id);
|
file_ext_name = "txt";
|
||||||
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
startTime = System.currentTimeMillis();
|
||||||
|
errno = client.append_file1(appender_file_id, file_buff);
|
||||||
if (file_buff != null)
|
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
{
|
if (errno == 0) {
|
||||||
System.out.println("file length:" + file_buff.length);
|
System.err.println(client.get_file_info1(appender_file_id));
|
||||||
System.out.println((new String(file_buff)));
|
} else {
|
||||||
}
|
System.err.println("append file fail, error no: " + errno);
|
||||||
|
}
|
||||||
file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset);
|
|
||||||
file_ext_name = "txt";
|
startTime = System.currentTimeMillis();
|
||||||
startTime = System.currentTimeMillis();
|
errno = client.delete_file1(appender_file_id);
|
||||||
errno = client.append_file1(appender_file_id, file_buff);
|
System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
if (errno == 0) {
|
||||||
if (errno == 0)
|
System.err.println("Delete file success");
|
||||||
{
|
} else {
|
||||||
System.err.println(client.get_file_info1(appender_file_id));
|
System.err.println("Delete file fail, error no: " + errno);
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
|
||||||
System.err.println("append file fail, error no: " + errno);
|
appender_file_id = client.upload_appender_file1(local_filename, null, meta_list);
|
||||||
}
|
if (appender_file_id != null) {
|
||||||
|
int ts;
|
||||||
startTime = System.currentTimeMillis();
|
String token;
|
||||||
errno = client.delete_file1(appender_file_id);
|
String file_url;
|
||||||
System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
InetSocketAddress inetSockAddr;
|
||||||
if (errno == 0)
|
|
||||||
{
|
inetSockAddr = trackerServer.getInetSocketAddress();
|
||||||
System.err.println("Delete file success");
|
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
|
||||||
}
|
if (ClientGlobal.g_tracker_http_port != 80) {
|
||||||
else
|
file_url += ":" + ClientGlobal.g_tracker_http_port;
|
||||||
{
|
}
|
||||||
System.err.println("Delete file fail, error no: " + errno);
|
file_url += "/" + appender_file_id;
|
||||||
}
|
if (ClientGlobal.g_anti_steal_token) {
|
||||||
}
|
ts = (int) (System.currentTimeMillis() / 1000);
|
||||||
|
token = ProtoCommon.getToken(appender_file_id, ts, ClientGlobal.g_secret_key);
|
||||||
appender_file_id = client.upload_appender_file1(local_filename, null, meta_list);
|
file_url += "?token=" + token + "&ts=" + ts;
|
||||||
if (appender_file_id != null)
|
}
|
||||||
{
|
|
||||||
int ts;
|
System.err.println(client.get_file_info1(appender_file_id));
|
||||||
String token;
|
System.err.println("file url: " + file_url);
|
||||||
String file_url;
|
|
||||||
InetSocketAddress inetSockAddr;
|
errno = client.download_file1(appender_file_id, 0, 0, "c:\\" + appender_file_id.replaceAll("/", "_"));
|
||||||
|
if (errno == 0) {
|
||||||
inetSockAddr = trackerServer.getInetSocketAddress();
|
System.err.println("Download file success");
|
||||||
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
|
} else {
|
||||||
if (ClientGlobal.g_tracker_http_port != 80)
|
System.err.println("Download file fail, error no: " + errno);
|
||||||
{
|
}
|
||||||
file_url += ":" + ClientGlobal.g_tracker_http_port;
|
|
||||||
}
|
errno = client.download_file1(appender_file_id, 0, 0, new DownloadFileWriter("c:\\" + appender_file_id.replaceAll("/", "-")));
|
||||||
file_url += "/" + appender_file_id;
|
if (errno == 0) {
|
||||||
if (ClientGlobal.g_anti_steal_token)
|
System.err.println("Download file success");
|
||||||
{
|
} else {
|
||||||
ts = (int)(System.currentTimeMillis() / 1000);
|
System.err.println("Download file fail, error no: " + errno);
|
||||||
token = ProtoCommon.getToken(appender_file_id, ts, ClientGlobal.g_secret_key);
|
}
|
||||||
file_url += "?token=" + token + "&ts=" + ts;
|
|
||||||
}
|
file_ext_name = null;
|
||||||
|
startTime = System.currentTimeMillis();
|
||||||
System.err.println(client.get_file_info1(appender_file_id));
|
errno = client.append_file1(appender_file_id, local_filename);
|
||||||
System.err.println("file url: " + file_url);
|
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
|
if (errno == 0) {
|
||||||
errno = client.download_file1(appender_file_id, 0, 0, "c:\\" + appender_file_id.replaceAll("/", "_"));
|
System.err.println(client.get_file_info1(appender_file_id));
|
||||||
if (errno == 0)
|
} else {
|
||||||
{
|
System.err.println("append file fail, error no: " + errno);
|
||||||
System.err.println("Download file success");
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
File f;
|
||||||
System.err.println("Download file fail, error no: " + errno);
|
f = new File(local_filename);
|
||||||
}
|
int nPos = local_filename.lastIndexOf('.');
|
||||||
|
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
|
||||||
errno = client.download_file1(appender_file_id, 0, 0, new DownloadFileWriter("c:\\" + appender_file_id.replaceAll("/", "-")));
|
file_ext_name = local_filename.substring(nPos + 1);
|
||||||
if (errno == 0)
|
} else {
|
||||||
{
|
file_ext_name = null;
|
||||||
System.err.println("Download file success");
|
}
|
||||||
}
|
|
||||||
else
|
appender_file_id = client.upload_appender_file1(null, f.length(),
|
||||||
{
|
new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
||||||
System.err.println("Download file fail, error no: " + errno);
|
if (appender_file_id != null) {
|
||||||
}
|
System.out.println(client.get_file_info1(appender_file_id));
|
||||||
|
|
||||||
file_ext_name = null;
|
startTime = System.currentTimeMillis();
|
||||||
startTime = System.currentTimeMillis();
|
errno = client.append_file1(appender_file_id, f.length(), new UploadLocalFileSender(local_filename));
|
||||||
errno = client.append_file1(appender_file_id, local_filename);
|
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
if (errno == 0) {
|
||||||
if (errno == 0)
|
System.err.println(client.get_file_info1(appender_file_id));
|
||||||
{
|
} else {
|
||||||
System.err.println(client.get_file_info1(appender_file_id));
|
System.err.println("append file fail, error no: " + errno);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
startTime = System.currentTimeMillis();
|
||||||
System.err.println("append file fail, error no: " + errno);
|
errno = client.modify_file1(appender_file_id, 0, f.length(), new UploadLocalFileSender(local_filename));
|
||||||
}
|
System.out.println("modify_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
}
|
if (errno == 0) {
|
||||||
|
System.err.println(client.get_file_info1(appender_file_id));
|
||||||
File f;
|
} else {
|
||||||
f = new File(local_filename);
|
System.err.println("modify file fail, error no: " + errno);
|
||||||
int nPos = local_filename.lastIndexOf('.');
|
}
|
||||||
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1)
|
|
||||||
{
|
startTime = System.currentTimeMillis();
|
||||||
file_ext_name = local_filename.substring(nPos+1);
|
errno = client.truncate_file1(appender_file_id, 0);
|
||||||
}
|
System.out.println("truncate_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
else
|
if (errno == 0) {
|
||||||
{
|
System.err.println(client.get_file_info1(appender_file_id));
|
||||||
file_ext_name = null;
|
} else {
|
||||||
}
|
System.err.println("truncate file fail, error no: " + errno);
|
||||||
|
}
|
||||||
appender_file_id = client.upload_appender_file1(null, f.length(),
|
} else {
|
||||||
new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
System.err.println("Upload file fail, error no: " + errno);
|
||||||
if (appender_file_id != null)
|
}
|
||||||
{
|
|
||||||
System.out.println(client.get_file_info1(appender_file_id));
|
storageServer = tracker.getFetchStorage1(trackerServer, appender_file_id);
|
||||||
|
if (storageServer == null) {
|
||||||
startTime = System.currentTimeMillis();
|
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
|
||||||
errno = client.append_file1(appender_file_id, f.length(), new UploadLocalFileSender(local_filename));
|
return;
|
||||||
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
}
|
||||||
if (errno == 0)
|
|
||||||
{
|
|
||||||
System.err.println(client.get_file_info1(appender_file_id));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("append file fail, error no: " + errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
errno = client.modify_file1(appender_file_id, 0, f.length(), new UploadLocalFileSender(local_filename));
|
|
||||||
System.out.println("modify_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
if (errno == 0)
|
|
||||||
{
|
|
||||||
System.err.println(client.get_file_info1(appender_file_id));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("modify file fail, error no: " + errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
errno = client.truncate_file1(appender_file_id, 0);
|
|
||||||
System.out.println("truncate_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
if (errno == 0)
|
|
||||||
{
|
|
||||||
System.err.println(client.get_file_info1(appender_file_id));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("truncate file fail, error no: " + errno);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("Upload file fail, error no: " + errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
storageServer = tracker.getFetchStorage1(trackerServer, appender_file_id);
|
|
||||||
if (storageServer == null)
|
|
||||||
{
|
|
||||||
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/* for test only */
|
/* for test only */
|
||||||
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
|
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
|
||||||
|
|
||||||
storageServer.close();
|
storageServer.close();
|
||||||
|
|
||||||
/* for test only */
|
/* for test only */
|
||||||
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
|
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
|
||||||
|
|
||||||
trackerServer.close();
|
trackerServer.close();
|
||||||
}
|
} catch (Exception ex) {
|
||||||
catch(Exception ex)
|
ex.printStackTrace();
|
||||||
{
|
}
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,67 +1,64 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
package org.csource.fastdfs.test;
|
package org.csource.fastdfs.test;
|
||||||
|
|
||||||
import java.io.*;
|
import org.csource.common.NameValuePair;
|
||||||
import java.net.*;
|
|
||||||
import java.util.*;
|
|
||||||
import org.csource.common.*;
|
|
||||||
import org.csource.fastdfs.*;
|
import org.csource.fastdfs.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* client test
|
* client test
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.18
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.18
|
||||||
public class TestClient
|
*/
|
||||||
{
|
public class TestClient {
|
||||||
private TestClient()
|
private TestClient() {
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* entry point
|
||||||
* entry point
|
*
|
||||||
* @param args comand arguments
|
* @param args comand arguments
|
||||||
* <ul><li>args[0]: config filename</li></ul>
|
* <ul><li>args[0]: config filename</li></ul>
|
||||||
* <ul><li>args[1]: local filename to upload</li></ul>
|
* <ul><li>args[1]: local filename to upload</li></ul>
|
||||||
*/
|
*/
|
||||||
public static void main(String args[])
|
public static void main(String args[]) {
|
||||||
{
|
if (args.length < 2) {
|
||||||
if (args.length < 2)
|
System.out.println("Error: Must have 2 parameters, one is config filename, "
|
||||||
{
|
+ "the other is the local filename to upload");
|
||||||
System.out.println("Error: Must have 2 parameters, one is config filename, "
|
return;
|
||||||
+ "the other is the local filename to upload");
|
}
|
||||||
return;
|
|
||||||
}
|
System.out.println("java.version=" + System.getProperty("java.version"));
|
||||||
|
|
||||||
System.out.println("java.version=" + System.getProperty("java.version"));
|
String conf_filename = args[0];
|
||||||
|
String local_filename = args[1];
|
||||||
String conf_filename = args[0];
|
|
||||||
String local_filename = args[1];
|
try {
|
||||||
|
ClientGlobal.init(conf_filename);
|
||||||
try
|
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
||||||
{
|
System.out.println("charset=" + ClientGlobal.g_charset);
|
||||||
ClientGlobal.init(conf_filename);
|
|
||||||
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
long startTime;
|
||||||
System.out.println("charset=" + ClientGlobal.g_charset);
|
String group_name;
|
||||||
|
String remote_filename;
|
||||||
long startTime;
|
ServerInfo[] servers;
|
||||||
String group_name;
|
TrackerClient tracker = new TrackerClient();
|
||||||
String remote_filename;
|
TrackerServer trackerServer = tracker.getConnection();
|
||||||
ServerInfo[] servers;
|
|
||||||
TrackerClient tracker = new TrackerClient();
|
StorageServer storageServer = null;
|
||||||
TrackerServer trackerServer = tracker.getConnection();
|
|
||||||
|
|
||||||
StorageServer storageServer = null;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
storageServer = tracker.getStoreStorage(trackerServer);
|
storageServer = tracker.getStoreStorage(trackerServer);
|
||||||
if (storageServer == null)
|
if (storageServer == null)
|
||||||
{
|
{
|
||||||
System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode());
|
System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode());
|
||||||
@ -69,283 +66,239 @@ public class TestClient
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
StorageClient client = new StorageClient(trackerServer, storageServer);
|
StorageClient client = new StorageClient(trackerServer, storageServer);
|
||||||
byte[] file_buff;
|
byte[] file_buff;
|
||||||
NameValuePair[] meta_list;
|
NameValuePair[] meta_list;
|
||||||
String[] results;
|
String[] results;
|
||||||
String master_filename;
|
String master_filename;
|
||||||
String prefix_name;
|
String prefix_name;
|
||||||
String file_ext_name;
|
String file_ext_name;
|
||||||
String generated_slave_filename;
|
String generated_slave_filename;
|
||||||
int errno;
|
int errno;
|
||||||
|
|
||||||
meta_list = new NameValuePair[4];
|
meta_list = new NameValuePair[4];
|
||||||
meta_list[0] = new NameValuePair("width", "800");
|
meta_list[0] = new NameValuePair("width", "800");
|
||||||
meta_list[1] = new NameValuePair("heigth", "600");
|
meta_list[1] = new NameValuePair("heigth", "600");
|
||||||
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
|
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
|
||||||
meta_list[3] = new NameValuePair("author", "Mike");
|
meta_list[3] = new NameValuePair("author", "Mike");
|
||||||
|
|
||||||
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
|
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
|
||||||
System.out.println("file length: " + file_buff.length);
|
System.out.println("file length: " + file_buff.length);
|
||||||
|
|
||||||
|
group_name = null;
|
||||||
|
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
|
||||||
|
if (storageServers == null) {
|
||||||
|
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
|
||||||
|
} else {
|
||||||
|
System.err.println("store storage servers count: " + storageServers.length);
|
||||||
|
for (int k = 0; k < storageServers.length; k++) {
|
||||||
|
System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
|
||||||
|
}
|
||||||
|
System.err.println("");
|
||||||
|
}
|
||||||
|
|
||||||
|
startTime = System.currentTimeMillis();
|
||||||
|
results = client.upload_file(file_buff, "txt", meta_list);
|
||||||
|
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
|
|
||||||
group_name = null;
|
|
||||||
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
|
|
||||||
if (storageServers == null)
|
|
||||||
{
|
|
||||||
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("store storage servers count: " + storageServers.length);
|
|
||||||
for (int k=0; k<storageServers.length; k++)
|
|
||||||
{
|
|
||||||
System.err.println((k+1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
|
|
||||||
}
|
|
||||||
System.err.println("");
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
results = client.upload_file(file_buff, "txt", meta_list);
|
|
||||||
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
group_name = "";
|
group_name = "";
|
||||||
results = client.upload_file(group_name, file_buff, "txt", meta_list);
|
results = client.upload_file(group_name, file_buff, "txt", meta_list);
|
||||||
*/
|
*/
|
||||||
if (results == null)
|
if (results == null) {
|
||||||
{
|
System.err.println("upload file fail, error code: " + client.getErrorCode());
|
||||||
System.err.println("upload file fail, error code: " + client.getErrorCode());
|
return;
|
||||||
return;
|
} else {
|
||||||
}
|
group_name = results[0];
|
||||||
else
|
remote_filename = results[1];
|
||||||
{
|
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
|
||||||
group_name = results[0];
|
System.err.println(client.get_file_info(group_name, remote_filename));
|
||||||
remote_filename = results[1];
|
|
||||||
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
|
|
||||||
System.err.println(client.get_file_info(group_name, remote_filename));
|
|
||||||
|
|
||||||
servers = tracker.getFetchStorages(trackerServer, group_name, remote_filename);
|
servers = tracker.getFetchStorages(trackerServer, group_name, remote_filename);
|
||||||
if (servers == null)
|
if (servers == null) {
|
||||||
{
|
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
|
||||||
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
|
} else {
|
||||||
}
|
System.err.println("storage servers count: " + servers.length);
|
||||||
else
|
for (int k = 0; k < servers.length; k++) {
|
||||||
{
|
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
|
||||||
System.err.println("storage servers count: " + servers.length);
|
}
|
||||||
for (int k=0; k<servers.length; k++)
|
System.err.println("");
|
||||||
{
|
}
|
||||||
System.err.println((k+1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
|
|
||||||
}
|
|
||||||
System.err.println("");
|
|
||||||
}
|
|
||||||
|
|
||||||
meta_list = new NameValuePair[4];
|
|
||||||
meta_list[0] = new NameValuePair("width", "1024");
|
|
||||||
meta_list[1] = new NameValuePair("heigth", "768");
|
|
||||||
meta_list[2] = new NameValuePair("bgcolor", "#000000");
|
|
||||||
meta_list[3] = new NameValuePair("title", "Untitle");
|
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
errno=client.set_metadata(group_name, remote_filename, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
|
|
||||||
System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
if (errno == 0)
|
|
||||||
{
|
|
||||||
System.err.println("set_metadata success");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("set_metadata fail, error no: " + errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
meta_list = client.get_metadata(group_name, remote_filename);
|
|
||||||
if (meta_list != null)
|
|
||||||
{
|
|
||||||
for (int i=0; i<meta_list.length; i++)
|
|
||||||
{
|
|
||||||
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Thread.sleep(30000);
|
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
file_buff = client.download_file(group_name, remote_filename);
|
|
||||||
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
|
|
||||||
if (file_buff != null)
|
|
||||||
{
|
|
||||||
System.out.println("file length:" + file_buff.length);
|
|
||||||
System.out.println((new String(file_buff)));
|
|
||||||
}
|
|
||||||
|
|
||||||
file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset);
|
|
||||||
master_filename = remote_filename;
|
|
||||||
prefix_name = "-part1";
|
|
||||||
file_ext_name = "txt";
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
results = client.upload_file(group_name, master_filename, prefix_name, file_buff, file_ext_name, meta_list);
|
|
||||||
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
|
||||||
if (results != null)
|
|
||||||
{
|
|
||||||
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
|
|
||||||
|
|
||||||
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
|
|
||||||
if (!generated_slave_filename.equals(results[1]))
|
|
||||||
{
|
|
||||||
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.err.println(client.get_file_info(results[0], results[1]));
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime = System.currentTimeMillis();
|
meta_list = new NameValuePair[4];
|
||||||
errno = client.delete_file(group_name, remote_filename);
|
meta_list[0] = new NameValuePair("width", "1024");
|
||||||
System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
meta_list[1] = new NameValuePair("heigth", "768");
|
||||||
if (errno == 0)
|
meta_list[2] = new NameValuePair("bgcolor", "#000000");
|
||||||
{
|
meta_list[3] = new NameValuePair("title", "Untitle");
|
||||||
System.err.println("Delete file success");
|
|
||||||
}
|
startTime = System.currentTimeMillis();
|
||||||
else
|
errno = client.set_metadata(group_name, remote_filename, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
|
||||||
{
|
System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
System.err.println("Delete file fail, error no: " + errno);
|
if (errno == 0) {
|
||||||
}
|
System.err.println("set_metadata success");
|
||||||
}
|
} else {
|
||||||
|
System.err.println("set_metadata fail, error no: " + errno);
|
||||||
results = client.upload_file(local_filename, null, meta_list);
|
}
|
||||||
if (results != null)
|
|
||||||
{
|
meta_list = client.get_metadata(group_name, remote_filename);
|
||||||
String file_id;
|
if (meta_list != null) {
|
||||||
int ts;
|
for (int i = 0; i < meta_list.length; i++) {
|
||||||
String token;
|
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
|
||||||
String file_url;
|
}
|
||||||
InetSocketAddress inetSockAddr;
|
}
|
||||||
|
|
||||||
group_name = results[0];
|
//Thread.sleep(30000);
|
||||||
remote_filename = results[1];
|
|
||||||
file_id = group_name + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remote_filename;
|
startTime = System.currentTimeMillis();
|
||||||
|
file_buff = client.download_file(group_name, remote_filename);
|
||||||
inetSockAddr = trackerServer.getInetSocketAddress();
|
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
|
|
||||||
if (ClientGlobal.g_tracker_http_port != 80)
|
if (file_buff != null) {
|
||||||
{
|
System.out.println("file length:" + file_buff.length);
|
||||||
file_url += ":" + ClientGlobal.g_tracker_http_port;
|
System.out.println((new String(file_buff)));
|
||||||
}
|
}
|
||||||
file_url += "/" + file_id;
|
|
||||||
if (ClientGlobal.g_anti_steal_token)
|
file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset);
|
||||||
{
|
master_filename = remote_filename;
|
||||||
ts = (int)(System.currentTimeMillis() / 1000);
|
prefix_name = "-part1";
|
||||||
token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
|
file_ext_name = "txt";
|
||||||
file_url += "?token=" + token + "&ts=" + ts;
|
startTime = System.currentTimeMillis();
|
||||||
}
|
results = client.upload_file(group_name, master_filename, prefix_name, file_buff, file_ext_name, meta_list);
|
||||||
|
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
|
if (results != null) {
|
||||||
System.err.println(client.get_file_info(group_name, remote_filename));
|
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
|
||||||
System.err.println("file url: " + file_url);
|
|
||||||
|
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
|
||||||
errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.replaceAll("/", "_"));
|
if (!generated_slave_filename.equals(results[1])) {
|
||||||
if (errno == 0)
|
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
|
||||||
{
|
}
|
||||||
System.err.println("Download file success");
|
|
||||||
}
|
System.err.println(client.get_file_info(results[0], results[1]));
|
||||||
else
|
}
|
||||||
{
|
|
||||||
System.err.println("Download file fail, error no: " + errno);
|
startTime = System.currentTimeMillis();
|
||||||
}
|
errno = client.delete_file(group_name, remote_filename);
|
||||||
|
System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
errno = client.download_file(group_name, remote_filename, 0, 0, new DownloadFileWriter("c:\\" + remote_filename.replaceAll("/", "-")));
|
if (errno == 0) {
|
||||||
if (errno == 0)
|
System.err.println("Delete file success");
|
||||||
{
|
} else {
|
||||||
System.err.println("Download file success");
|
System.err.println("Delete file fail, error no: " + errno);
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
|
||||||
System.err.println("Download file fail, error no: " + errno);
|
results = client.upload_file(local_filename, null, meta_list);
|
||||||
}
|
if (results != null) {
|
||||||
|
String file_id;
|
||||||
master_filename = remote_filename;
|
int ts;
|
||||||
prefix_name = "-part2";
|
String token;
|
||||||
file_ext_name = null;
|
String file_url;
|
||||||
startTime = System.currentTimeMillis();
|
InetSocketAddress inetSockAddr;
|
||||||
results = client.upload_file(group_name, master_filename, prefix_name, local_filename, null, meta_list);
|
|
||||||
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
group_name = results[0];
|
||||||
if (results != null)
|
remote_filename = results[1];
|
||||||
{
|
file_id = group_name + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remote_filename;
|
||||||
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
|
|
||||||
|
inetSockAddr = trackerServer.getInetSocketAddress();
|
||||||
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
|
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
|
||||||
if (!generated_slave_filename.equals(results[1]))
|
if (ClientGlobal.g_tracker_http_port != 80) {
|
||||||
{
|
file_url += ":" + ClientGlobal.g_tracker_http_port;
|
||||||
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
|
}
|
||||||
}
|
file_url += "/" + file_id;
|
||||||
|
if (ClientGlobal.g_anti_steal_token) {
|
||||||
System.err.println(client.get_file_info(results[0], results[1]));
|
ts = (int) (System.currentTimeMillis() / 1000);
|
||||||
}
|
token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
|
||||||
}
|
file_url += "?token=" + token + "&ts=" + ts;
|
||||||
|
}
|
||||||
File f;
|
|
||||||
f = new File(local_filename);
|
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
|
||||||
int nPos = local_filename.lastIndexOf('.');
|
System.err.println(client.get_file_info(group_name, remote_filename));
|
||||||
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1)
|
System.err.println("file url: " + file_url);
|
||||||
{
|
|
||||||
file_ext_name = local_filename.substring(nPos+1);
|
errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.replaceAll("/", "_"));
|
||||||
}
|
if (errno == 0) {
|
||||||
else
|
System.err.println("Download file success");
|
||||||
{
|
} else {
|
||||||
file_ext_name = null;
|
System.err.println("Download file fail, error no: " + errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
results = client.upload_file(null, f.length(),
|
errno = client.download_file(group_name, remote_filename, 0, 0, new DownloadFileWriter("c:\\" + remote_filename.replaceAll("/", "-")));
|
||||||
new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
if (errno == 0) {
|
||||||
if (results != null)
|
System.err.println("Download file success");
|
||||||
{
|
} else {
|
||||||
group_name = results[0];
|
System.err.println("Download file fail, error no: " + errno);
|
||||||
remote_filename = results[1];
|
}
|
||||||
|
|
||||||
System.out.println("group name: " + group_name + ", remote filename: " + remote_filename);
|
master_filename = remote_filename;
|
||||||
System.out.println(client.get_file_info(group_name, remote_filename));
|
prefix_name = "-part2";
|
||||||
|
file_ext_name = null;
|
||||||
master_filename = remote_filename;
|
startTime = System.currentTimeMillis();
|
||||||
prefix_name = "-part3";
|
results = client.upload_file(group_name, master_filename, prefix_name, local_filename, null, meta_list);
|
||||||
startTime = System.currentTimeMillis();
|
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
results = client.upload_file(group_name, master_filename, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
if (results != null) {
|
||||||
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
|
||||||
if (results != null)
|
|
||||||
{
|
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
|
||||||
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
|
if (!generated_slave_filename.equals(results[1])) {
|
||||||
|
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
|
||||||
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
|
}
|
||||||
if (!generated_slave_filename.equals(results[1]))
|
|
||||||
{
|
System.err.println(client.get_file_info(results[0], results[1]));
|
||||||
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.err.println(client.get_file_info(results[0], results[1]));
|
File f;
|
||||||
}
|
f = new File(local_filename);
|
||||||
}
|
int nPos = local_filename.lastIndexOf('.');
|
||||||
else
|
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
|
||||||
{
|
file_ext_name = local_filename.substring(nPos + 1);
|
||||||
System.err.println("Upload file fail, error no: " + errno);
|
} else {
|
||||||
}
|
file_ext_name = null;
|
||||||
|
}
|
||||||
storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename);
|
|
||||||
if (storageServer == null)
|
results = client.upload_file(null, f.length(),
|
||||||
{
|
new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
||||||
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
|
if (results != null) {
|
||||||
return;
|
group_name = results[0];
|
||||||
}
|
remote_filename = results[1];
|
||||||
|
|
||||||
|
System.out.println("group name: " + group_name + ", remote filename: " + remote_filename);
|
||||||
|
System.out.println(client.get_file_info(group_name, remote_filename));
|
||||||
|
|
||||||
|
master_filename = remote_filename;
|
||||||
|
prefix_name = "-part3";
|
||||||
|
startTime = System.currentTimeMillis();
|
||||||
|
results = client.upload_file(group_name, master_filename, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
||||||
|
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
|
||||||
|
if (results != null) {
|
||||||
|
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
|
||||||
|
|
||||||
|
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
|
||||||
|
if (!generated_slave_filename.equals(results[1])) {
|
||||||
|
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.err.println(client.get_file_info(results[0], results[1]));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.err.println("Upload file fail, error no: " + errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename);
|
||||||
|
if (storageServer == null) {
|
||||||
|
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
|
||||||
|
return;
|
||||||
|
}
|
||||||
/* for test only */
|
/* for test only */
|
||||||
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
|
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
|
||||||
|
|
||||||
storageServer.close();
|
storageServer.close();
|
||||||
|
|
||||||
/* for test only */
|
/* for test only */
|
||||||
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
|
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
|
||||||
|
|
||||||
trackerServer.close();
|
trackerServer.close();
|
||||||
}
|
} catch (Exception ex) {
|
||||||
catch(Exception ex)
|
ex.printStackTrace();
|
||||||
{
|
}
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,313 +1,266 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
package org.csource.fastdfs.test;
|
package org.csource.fastdfs.test;
|
||||||
|
|
||||||
import java.io.*;
|
import org.csource.common.NameValuePair;
|
||||||
import java.net.*;
|
|
||||||
import java.util.*;
|
|
||||||
import org.csource.common.*;
|
|
||||||
import org.csource.fastdfs.*;
|
import org.csource.fastdfs.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* client test
|
* client test
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.16
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.16
|
||||||
public class TestClient1
|
*/
|
||||||
{
|
public class TestClient1 {
|
||||||
private TestClient1()
|
private TestClient1() {
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* entry point
|
||||||
* entry point
|
*
|
||||||
* @param args comand arguments
|
* @param args comand arguments
|
||||||
* <ul><li>args[0]: config filename</li></ul>
|
* <ul><li>args[0]: config filename</li></ul>
|
||||||
* <ul><li>args[1]: local filename to upload</li></ul>
|
* <ul><li>args[1]: local filename to upload</li></ul>
|
||||||
*/
|
*/
|
||||||
public static void main(String args[])
|
public static void main(String args[]) {
|
||||||
{
|
if (args.length < 2) {
|
||||||
if (args.length < 2)
|
System.out.println("Error: Must have 2 parameters, one is config filename, "
|
||||||
{
|
+ "the other is the local filename to upload");
|
||||||
System.out.println("Error: Must have 2 parameters, one is config filename, "
|
return;
|
||||||
+ "the other is the local filename to upload");
|
}
|
||||||
return;
|
|
||||||
}
|
System.out.println("java.version=" + System.getProperty("java.version"));
|
||||||
|
|
||||||
System.out.println("java.version=" + System.getProperty("java.version"));
|
String conf_filename = args[0];
|
||||||
|
String local_filename = args[1];
|
||||||
String conf_filename = args[0];
|
String group_name;
|
||||||
String local_filename = args[1];
|
|
||||||
String group_name;
|
try {
|
||||||
|
ClientGlobal.init(conf_filename);
|
||||||
try
|
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
||||||
{
|
System.out.println("charset=" + ClientGlobal.g_charset);
|
||||||
ClientGlobal.init(conf_filename);
|
|
||||||
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
String file_id;
|
||||||
System.out.println("charset=" + ClientGlobal.g_charset);
|
|
||||||
|
TrackerClient tracker = new TrackerClient();
|
||||||
String file_id;
|
TrackerServer trackerServer = tracker.getConnection();
|
||||||
|
|
||||||
TrackerClient tracker = new TrackerClient();
|
StorageServer storageServer = null;
|
||||||
TrackerServer trackerServer = tracker.getConnection();
|
/*
|
||||||
|
storageServer = tracker.getStoreStorage(trackerServer);
|
||||||
StorageServer storageServer = null;
|
|
||||||
/*
|
|
||||||
storageServer = tracker.getStoreStorage(trackerServer);
|
|
||||||
if (storageServer == null)
|
if (storageServer == null)
|
||||||
{
|
{
|
||||||
System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode());
|
System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
||||||
byte[] file_buff;
|
byte[] file_buff;
|
||||||
NameValuePair[] meta_list;
|
NameValuePair[] meta_list;
|
||||||
String master_file_id;
|
String master_file_id;
|
||||||
String prefix_name;
|
String prefix_name;
|
||||||
String file_ext_name;
|
String file_ext_name;
|
||||||
String slave_file_id;
|
String slave_file_id;
|
||||||
String generated_slave_file_id;
|
String generated_slave_file_id;
|
||||||
int errno;
|
int errno;
|
||||||
|
|
||||||
group_name = "group1";
|
group_name = "group1";
|
||||||
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
|
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
|
||||||
if (storageServers == null)
|
if (storageServers == null) {
|
||||||
{
|
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
|
||||||
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
|
} else {
|
||||||
}
|
System.err.println("store storage servers count: " + storageServers.length);
|
||||||
else
|
for (int k = 0; k < storageServers.length; k++) {
|
||||||
{
|
System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
|
||||||
System.err.println("store storage servers count: " + storageServers.length);
|
}
|
||||||
for (int k=0; k<storageServers.length; k++)
|
System.err.println("");
|
||||||
{
|
}
|
||||||
System.err.println((k+1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
|
|
||||||
}
|
meta_list = new NameValuePair[4];
|
||||||
System.err.println("");
|
meta_list[0] = new NameValuePair("width", "800");
|
||||||
}
|
meta_list[1] = new NameValuePair("heigth", "600");
|
||||||
|
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
|
||||||
meta_list = new NameValuePair[4];
|
meta_list[3] = new NameValuePair("author", "Mike");
|
||||||
meta_list[0] = new NameValuePair("width", "800");
|
|
||||||
meta_list[1] = new NameValuePair("heigth", "600");
|
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
|
||||||
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
|
System.out.println("file length: " + file_buff.length);
|
||||||
meta_list[3] = new NameValuePair("author", "Mike");
|
|
||||||
|
file_id = client.upload_file1(file_buff, "txt", meta_list);
|
||||||
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
|
|
||||||
System.out.println("file length: " + file_buff.length);
|
|
||||||
|
|
||||||
file_id = client.upload_file1(file_buff, "txt", meta_list);
|
|
||||||
/*
|
/*
|
||||||
group_name = "group1";
|
group_name = "group1";
|
||||||
file_id = client.upload_file1(group_name, file_buff, "txt", meta_list);
|
file_id = client.upload_file1(group_name, file_buff, "txt", meta_list);
|
||||||
*/
|
*/
|
||||||
if (file_id == null)
|
if (file_id == null) {
|
||||||
{
|
System.err.println("upload file fail, error code: " + client.getErrorCode());
|
||||||
System.err.println("upload file fail, error code: " + client.getErrorCode());
|
return;
|
||||||
return;
|
} else {
|
||||||
}
|
System.err.println("file_id: " + file_id);
|
||||||
else
|
System.err.println(client.get_file_info1(file_id));
|
||||||
{
|
|
||||||
System.err.println("file_id: " + file_id);
|
ServerInfo[] servers = tracker.getFetchStorages1(trackerServer, file_id);
|
||||||
System.err.println(client.get_file_info1(file_id));
|
if (servers == null) {
|
||||||
|
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
|
||||||
ServerInfo[] servers = tracker.getFetchStorages1(trackerServer, file_id);
|
} else {
|
||||||
if (servers == null)
|
System.err.println("storage servers count: " + servers.length);
|
||||||
{
|
for (int k = 0; k < servers.length; k++) {
|
||||||
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
|
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
|
||||||
}
|
}
|
||||||
else
|
System.err.println("");
|
||||||
{
|
}
|
||||||
System.err.println("storage servers count: " + servers.length);
|
|
||||||
for (int k=0; k<servers.length; k++)
|
meta_list = new NameValuePair[4];
|
||||||
{
|
meta_list[0] = new NameValuePair("width", "1024");
|
||||||
System.err.println((k+1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
|
meta_list[1] = new NameValuePair("heigth", "768");
|
||||||
}
|
meta_list[2] = new NameValuePair("bgcolor", "#000000");
|
||||||
System.err.println("");
|
meta_list[3] = new NameValuePair("title", "Untitle");
|
||||||
}
|
|
||||||
|
if ((errno = client.set_metadata1(file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE)) == 0) {
|
||||||
meta_list = new NameValuePair[4];
|
System.err.println("set_metadata success");
|
||||||
meta_list[0] = new NameValuePair("width", "1024");
|
} else {
|
||||||
meta_list[1] = new NameValuePair("heigth", "768");
|
System.err.println("set_metadata fail, error no: " + errno);
|
||||||
meta_list[2] = new NameValuePair("bgcolor", "#000000");
|
}
|
||||||
meta_list[3] = new NameValuePair("title", "Untitle");
|
|
||||||
|
meta_list = client.get_metadata1(file_id);
|
||||||
if ((errno=client.set_metadata1(file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE)) == 0)
|
if (meta_list != null) {
|
||||||
{
|
for (int i = 0; i < meta_list.length; i++) {
|
||||||
System.err.println("set_metadata success");
|
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
|
||||||
System.err.println("set_metadata fail, error no: " + errno);
|
//Thread.sleep(30000);
|
||||||
}
|
|
||||||
|
file_buff = client.download_file1(file_id);
|
||||||
meta_list = client.get_metadata1(file_id);
|
if (file_buff != null) {
|
||||||
if (meta_list != null)
|
System.out.println("file length:" + file_buff.length);
|
||||||
{
|
System.out.println((new String(file_buff)));
|
||||||
for (int i=0; i<meta_list.length; i++)
|
}
|
||||||
{
|
|
||||||
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
|
master_file_id = file_id;
|
||||||
}
|
prefix_name = "-part1";
|
||||||
}
|
file_ext_name = "txt";
|
||||||
|
file_buff = "this is a slave buff.".getBytes(ClientGlobal.g_charset);
|
||||||
//Thread.sleep(30000);
|
slave_file_id = client.upload_file1(master_file_id, prefix_name, file_buff, file_ext_name, meta_list);
|
||||||
|
if (slave_file_id != null) {
|
||||||
file_buff = client.download_file1(file_id);
|
System.err.println("slave file_id: " + slave_file_id);
|
||||||
if (file_buff != null)
|
System.err.println(client.get_file_info1(slave_file_id));
|
||||||
{
|
|
||||||
System.out.println("file length:" + file_buff.length);
|
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
|
||||||
System.out.println((new String(file_buff)));
|
if (!generated_slave_file_id.equals(slave_file_id)) {
|
||||||
}
|
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
|
||||||
|
}
|
||||||
master_file_id = file_id;
|
}
|
||||||
prefix_name = "-part1";
|
|
||||||
file_ext_name = "txt";
|
//Thread.sleep(10000);
|
||||||
file_buff = "this is a slave buff.".getBytes(ClientGlobal.g_charset);
|
if ((errno = client.delete_file1(file_id)) == 0) {
|
||||||
slave_file_id = client.upload_file1(master_file_id, prefix_name, file_buff, file_ext_name, meta_list);
|
System.err.println("Delete file success");
|
||||||
if (slave_file_id != null)
|
} else {
|
||||||
{
|
System.err.println("Delete file fail, error no: " + errno);
|
||||||
System.err.println("slave file_id: " + slave_file_id);
|
}
|
||||||
System.err.println(client.get_file_info1(slave_file_id));
|
}
|
||||||
|
|
||||||
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
|
if ((file_id = client.upload_file1(local_filename, null, meta_list)) != null) {
|
||||||
if (!generated_slave_file_id.equals(slave_file_id))
|
int ts;
|
||||||
{
|
String token;
|
||||||
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
|
String file_url;
|
||||||
}
|
InetSocketAddress inetSockAddr;
|
||||||
}
|
|
||||||
|
System.err.println("file_id: " + file_id);
|
||||||
//Thread.sleep(10000);
|
System.err.println(client.get_file_info1(file_id));
|
||||||
if ((errno=client.delete_file1(file_id)) == 0)
|
|
||||||
{
|
inetSockAddr = trackerServer.getInetSocketAddress();
|
||||||
System.err.println("Delete file success");
|
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
|
||||||
}
|
if (ClientGlobal.g_tracker_http_port != 80) {
|
||||||
else
|
file_url += ":" + ClientGlobal.g_tracker_http_port;
|
||||||
{
|
}
|
||||||
System.err.println("Delete file fail, error no: " + errno);
|
file_url += "/" + file_id;
|
||||||
}
|
if (ClientGlobal.g_anti_steal_token) {
|
||||||
}
|
ts = (int) (System.currentTimeMillis() / 1000);
|
||||||
|
token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
|
||||||
if ((file_id=client.upload_file1(local_filename, null, meta_list)) != null)
|
file_url += "?token=" + token + "&ts=" + ts;
|
||||||
{
|
}
|
||||||
int ts;
|
System.err.println("file url: " + file_url);
|
||||||
String token;
|
|
||||||
String file_url;
|
errno = client.download_file1(file_id, 0, 100, "c:\\" + file_id.replaceAll("/", "_"));
|
||||||
InetSocketAddress inetSockAddr;
|
if (errno == 0) {
|
||||||
|
System.err.println("Download file success");
|
||||||
System.err.println("file_id: " + file_id);
|
} else {
|
||||||
System.err.println(client.get_file_info1(file_id));
|
System.err.println("Download file fail, error no: " + errno);
|
||||||
|
}
|
||||||
inetSockAddr = trackerServer.getInetSocketAddress();
|
|
||||||
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
|
errno = client.download_file1(file_id, new DownloadFileWriter("c:\\" + file_id.replaceAll("/", "-")));
|
||||||
if (ClientGlobal.g_tracker_http_port != 80)
|
if (errno == 0) {
|
||||||
{
|
System.err.println("Download file success");
|
||||||
file_url += ":" + ClientGlobal.g_tracker_http_port;
|
} else {
|
||||||
}
|
System.err.println("Download file fail, error no: " + errno);
|
||||||
file_url += "/" + file_id;
|
}
|
||||||
if (ClientGlobal.g_anti_steal_token)
|
|
||||||
{
|
master_file_id = file_id;
|
||||||
ts = (int)(System.currentTimeMillis() / 1000);
|
prefix_name = "-part2";
|
||||||
token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
|
file_ext_name = null;
|
||||||
file_url += "?token=" + token + "&ts=" + ts;
|
slave_file_id = client.upload_file1(master_file_id, prefix_name, local_filename, file_ext_name, meta_list);
|
||||||
}
|
if (slave_file_id != null) {
|
||||||
System.err.println("file url: " + file_url);
|
System.err.println("slave file_id: " + slave_file_id);
|
||||||
|
System.err.println(client.get_file_info1(slave_file_id));
|
||||||
errno = client.download_file1(file_id, 0, 100, "c:\\" + file_id.replaceAll("/", "_"));
|
|
||||||
if (errno == 0)
|
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
|
||||||
{
|
if (!generated_slave_file_id.equals(slave_file_id)) {
|
||||||
System.err.println("Download file success");
|
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
}
|
||||||
System.err.println("Download file fail, error no: " + errno);
|
|
||||||
}
|
File f;
|
||||||
|
f = new File(local_filename);
|
||||||
errno = client.download_file1(file_id, new DownloadFileWriter("c:\\" + file_id.replaceAll("/", "-")));
|
int nPos = local_filename.lastIndexOf('.');
|
||||||
if (errno == 0)
|
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
|
||||||
{
|
file_ext_name = local_filename.substring(nPos + 1);
|
||||||
System.err.println("Download file success");
|
} else {
|
||||||
}
|
file_ext_name = null;
|
||||||
else
|
}
|
||||||
{
|
|
||||||
System.err.println("Download file fail, error no: " + errno);
|
file_id = client.upload_file1(null, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
||||||
}
|
if (file_id != null) {
|
||||||
|
System.out.println("file id: " + file_id);
|
||||||
master_file_id = file_id;
|
System.out.println(client.get_file_info1(file_id));
|
||||||
prefix_name = "-part2";
|
master_file_id = file_id;
|
||||||
file_ext_name = null;
|
prefix_name = "-part3";
|
||||||
slave_file_id = client.upload_file1(master_file_id, prefix_name, local_filename, file_ext_name, meta_list);
|
slave_file_id = client.upload_file1(master_file_id, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
||||||
if (slave_file_id != null)
|
if (slave_file_id != null) {
|
||||||
{
|
System.err.println("slave file_id: " + slave_file_id);
|
||||||
System.err.println("slave file_id: " + slave_file_id);
|
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
|
||||||
System.err.println(client.get_file_info1(slave_file_id));
|
if (!generated_slave_file_id.equals(slave_file_id)) {
|
||||||
|
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
|
||||||
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
|
}
|
||||||
if (!generated_slave_file_id.equals(slave_file_id))
|
}
|
||||||
{
|
} else {
|
||||||
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
|
System.err.println("Upload file fail, error no: " + errno);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
storageServer = tracker.getFetchStorage1(trackerServer, file_id);
|
||||||
|
if (storageServer == null) {
|
||||||
|
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
File f;
|
|
||||||
f = new File(local_filename);
|
|
||||||
int nPos = local_filename.lastIndexOf('.');
|
|
||||||
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1)
|
|
||||||
{
|
|
||||||
file_ext_name = local_filename.substring(nPos+1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
file_ext_name = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
file_id = client.upload_file1(null, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
|
||||||
if (file_id != null)
|
|
||||||
{
|
|
||||||
System.out.println("file id: " + file_id);
|
|
||||||
System.out.println(client.get_file_info1(file_id));
|
|
||||||
master_file_id = file_id;
|
|
||||||
prefix_name = "-part3";
|
|
||||||
slave_file_id = client.upload_file1(master_file_id, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
|
|
||||||
if (slave_file_id != null)
|
|
||||||
{
|
|
||||||
System.err.println("slave file_id: " + slave_file_id);
|
|
||||||
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
|
|
||||||
if (!generated_slave_file_id.equals(slave_file_id))
|
|
||||||
{
|
|
||||||
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.err.println("Upload file fail, error no: " + errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
storageServer = tracker.getFetchStorage1(trackerServer, file_id);
|
|
||||||
if (storageServer == null)
|
|
||||||
{
|
|
||||||
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* for test only */
|
/* for test only */
|
||||||
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
|
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
|
||||||
storageServer.close();
|
storageServer.close();
|
||||||
|
|
||||||
/* for test only */
|
/* for test only */
|
||||||
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
|
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
|
||||||
|
|
||||||
trackerServer.close();
|
trackerServer.close();
|
||||||
}
|
} catch (Exception ex) {
|
||||||
catch(Exception ex)
|
ex.printStackTrace();
|
||||||
{
|
}
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,316 +1,263 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
package org.csource.fastdfs.test;
|
package org.csource.fastdfs.test;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.net.*;
|
|
||||||
import java.util.*;
|
|
||||||
import org.csource.common.*;
|
|
||||||
import org.csource.fastdfs.*;
|
import org.csource.fastdfs.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* load test class
|
* load test class
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.11
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.11
|
||||||
public class TestLoad
|
*/
|
||||||
{
|
public class TestLoad {
|
||||||
public static java.util.concurrent.ConcurrentLinkedQueue file_ids;
|
public static java.util.concurrent.ConcurrentLinkedQueue file_ids;
|
||||||
public static int total_download_count = 0;
|
public static int total_download_count = 0;
|
||||||
public static int success_download_count = 0;
|
public static int success_download_count = 0;
|
||||||
public static int fail_download_count = 0;
|
public static int fail_download_count = 0;
|
||||||
public static int total_upload_count = 0;
|
public static int total_upload_count = 0;
|
||||||
public static int success_upload_count = 0;
|
public static int success_upload_count = 0;
|
||||||
public static int upload_thread_count = 0;
|
public static int upload_thread_count = 0;
|
||||||
|
|
||||||
/**
|
|
||||||
* discard file content callback class when download file
|
|
||||||
* @author Happy Fish / YuQing
|
|
||||||
* @version Version 1.0
|
|
||||||
*/
|
|
||||||
public static class DownloadFileDiscard implements DownloadCallback
|
|
||||||
{
|
|
||||||
public DownloadFileDiscard()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public int recv(long file_size, byte[] data, int bytes)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private TestLoad() {
|
||||||
* file uploader
|
}
|
||||||
* @author Happy Fish / YuQing
|
|
||||||
* @version Version 1.0
|
|
||||||
*/
|
|
||||||
public static class Uploader
|
|
||||||
{
|
|
||||||
public TrackerClient tracker;
|
|
||||||
public TrackerServer trackerServer;
|
|
||||||
|
|
||||||
public Uploader() throws Exception
|
|
||||||
{
|
|
||||||
this.tracker = new TrackerClient();
|
|
||||||
this.trackerServer = tracker.getConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int uploadFile() throws Exception
|
|
||||||
{
|
|
||||||
StorageServer storageServer = null;
|
|
||||||
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
|
||||||
byte[] file_buff;
|
|
||||||
String file_id;
|
|
||||||
|
|
||||||
file_buff = new byte[2 * 1024];
|
|
||||||
java.util.Arrays.fill(file_buff, (byte)65);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
file_id = client.upload_file1(file_buff, "txt", null);
|
|
||||||
if (file_id == null)
|
|
||||||
{
|
|
||||||
System.out.println("upload file fail, error code: " + client.getErrorCode());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
TestLoad.file_ids.offer(file_id);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
System.out.println("upload file fail, error mesg: " + ex.getMessage());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* file downloader
|
* entry point
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.0
|
* @param args comand arguments
|
||||||
*/
|
* <ul><li>args[0]: config filename</li></ul>
|
||||||
public static class Downloader
|
*/
|
||||||
{
|
public static void main(String args[]) {
|
||||||
public TrackerClient tracker;
|
if (args.length < 1) {
|
||||||
public TrackerServer trackerServer;
|
System.out.println("Error: Must have 1 parameter: config filename");
|
||||||
public DownloadFileDiscard callback;
|
return;
|
||||||
|
}
|
||||||
public Downloader() throws Exception
|
|
||||||
{
|
System.out.println("java.version=" + System.getProperty("java.version"));
|
||||||
this.tracker = new TrackerClient();
|
|
||||||
this.trackerServer = tracker.getConnection();
|
try {
|
||||||
this.callback = new DownloadFileDiscard();
|
ClientGlobal.init(args[0]);
|
||||||
}
|
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
||||||
|
System.out.println("charset=" + ClientGlobal.g_charset);
|
||||||
public int downloadFile(String file_id) throws Exception
|
|
||||||
{
|
file_ids = new java.util.concurrent.ConcurrentLinkedQueue();
|
||||||
int errno;
|
|
||||||
StorageServer storageServer = null;
|
for (int i = 0; i < 10; i++) {
|
||||||
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
(new UploadThread(i)).start();
|
||||||
|
}
|
||||||
try
|
|
||||||
{
|
for (int i = 0; i < 20; i++) {
|
||||||
errno = client.download_file1(file_id, this.callback);
|
(new DownloadThread(i)).start();
|
||||||
if (errno != 0)
|
}
|
||||||
{
|
} catch (Exception ex) {
|
||||||
System.out.println("Download file fail, file_id: " + file_id + ", error no: " + errno);
|
ex.printStackTrace();
|
||||||
}
|
|
||||||
return errno;
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
System.out.println("Download file fail, error mesg: " + ex.getMessage());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* upload file thread
|
|
||||||
* @author Happy Fish / YuQing
|
|
||||||
* @version Version 1.0
|
|
||||||
*/
|
|
||||||
public static class UploadThread extends Thread
|
|
||||||
{
|
|
||||||
private int thread_index;
|
|
||||||
|
|
||||||
public UploadThread(int index)
|
|
||||||
{
|
|
||||||
this.thread_index = index;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
TestLoad.upload_thread_count++;
|
|
||||||
Uploader uploader = new Uploader();
|
|
||||||
|
|
||||||
System.out.println("upload thread " + this.thread_index + " start");
|
|
||||||
|
|
||||||
for (int i=0; i<50000; i++)
|
|
||||||
{
|
|
||||||
TestLoad.total_upload_count++;
|
|
||||||
if (uploader.uploadFile() == 0)
|
|
||||||
{
|
|
||||||
TestLoad.success_upload_count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
TestLoad.upload_thread_count--;
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("upload thread " + this.thread_index
|
|
||||||
+ " exit, total_upload_count: " + TestLoad.total_upload_count
|
|
||||||
+ ", success_upload_count: " + TestLoad.success_upload_count
|
|
||||||
+ ", total_download_count: " + TestLoad.total_download_count
|
|
||||||
+ ", success_download_count: " + TestLoad.success_download_count);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* download file thread
|
* discard file content callback class when download file
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.0
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.0
|
||||||
public static class DownloadThread extends Thread
|
*/
|
||||||
{
|
public static class DownloadFileDiscard implements DownloadCallback {
|
||||||
private int thread_index;
|
public DownloadFileDiscard() {
|
||||||
private static Integer counter_lock = new Integer(0);
|
}
|
||||||
|
|
||||||
public DownloadThread(int index)
|
public int recv(long file_size, byte[] data, int bytes) {
|
||||||
{
|
return 0;
|
||||||
this.thread_index = index;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
String file_id;
|
|
||||||
Downloader downloader = new Downloader();
|
|
||||||
|
|
||||||
System.out.println("download thread " + this.thread_index + " start");
|
|
||||||
|
|
||||||
file_id = "";
|
|
||||||
while (TestLoad.upload_thread_count != 0 || file_id != null)
|
|
||||||
{
|
|
||||||
file_id = (String)TestLoad.file_ids.poll();
|
|
||||||
if (file_id == null)
|
|
||||||
{
|
|
||||||
Thread.sleep(10);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized (this.counter_lock)
|
|
||||||
{
|
|
||||||
TestLoad.total_download_count++;
|
|
||||||
}
|
|
||||||
if (downloader.downloadFile(file_id) == 0)
|
|
||||||
{
|
|
||||||
synchronized (this.counter_lock)
|
|
||||||
{
|
|
||||||
TestLoad.success_download_count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
TestLoad.fail_download_count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i=0; i<3 && TestLoad.total_download_count < TestLoad.total_upload_count; i++)
|
|
||||||
{
|
|
||||||
file_id = (String)TestLoad.file_ids.poll();
|
|
||||||
if (file_id == null)
|
|
||||||
{
|
|
||||||
Thread.sleep(10);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized (this.counter_lock)
|
|
||||||
{
|
|
||||||
TestLoad.total_download_count++;
|
|
||||||
}
|
|
||||||
if (downloader.downloadFile(file_id) == 0)
|
|
||||||
{
|
|
||||||
synchronized (this.counter_lock)
|
|
||||||
{
|
|
||||||
TestLoad.success_download_count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
TestLoad.fail_download_count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("download thread " + this.thread_index
|
|
||||||
+ " exit, total_download_count: " + TestLoad.total_download_count
|
|
||||||
+ ", success_download_count: " + TestLoad.success_download_count
|
|
||||||
+ ", fail_download_count: " + TestLoad.fail_download_count);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private TestLoad()
|
/**
|
||||||
{
|
* file uploader
|
||||||
}
|
*
|
||||||
|
* @author Happy Fish / YuQing
|
||||||
/**
|
* @version Version 1.0
|
||||||
* entry point
|
*/
|
||||||
* @param args comand arguments
|
public static class Uploader {
|
||||||
* <ul><li>args[0]: config filename</li></ul>
|
public TrackerClient tracker;
|
||||||
*/
|
public TrackerServer trackerServer;
|
||||||
public static void main(String args[])
|
|
||||||
{
|
public Uploader() throws Exception {
|
||||||
if (args.length < 1)
|
this.tracker = new TrackerClient();
|
||||||
{
|
this.trackerServer = tracker.getConnection();
|
||||||
System.out.println("Error: Must have 1 parameter: config filename");
|
}
|
||||||
return;
|
|
||||||
}
|
public int uploadFile() throws Exception {
|
||||||
|
StorageServer storageServer = null;
|
||||||
System.out.println("java.version=" + System.getProperty("java.version"));
|
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
||||||
|
byte[] file_buff;
|
||||||
try
|
String file_id;
|
||||||
{
|
|
||||||
ClientGlobal.init(args[0]);
|
file_buff = new byte[2 * 1024];
|
||||||
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
|
java.util.Arrays.fill(file_buff, (byte) 65);
|
||||||
System.out.println("charset=" + ClientGlobal.g_charset);
|
|
||||||
|
try {
|
||||||
file_ids = new java.util.concurrent.ConcurrentLinkedQueue();
|
file_id = client.upload_file1(file_buff, "txt", null);
|
||||||
|
if (file_id == null) {
|
||||||
for (int i=0; i<10; i++)
|
System.out.println("upload file fail, error code: " + client.getErrorCode());
|
||||||
{
|
return -1;
|
||||||
(new UploadThread(i)).start();
|
}
|
||||||
}
|
|
||||||
|
TestLoad.file_ids.offer(file_id);
|
||||||
for (int i=0; i<20; i++)
|
return 0;
|
||||||
{
|
} catch (Exception ex) {
|
||||||
(new DownloadThread(i)).start();
|
System.out.println("upload file fail, error mesg: " + ex.getMessage());
|
||||||
}
|
return -1;
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
}
|
||||||
{
|
}
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
/**
|
||||||
|
* file downloader
|
||||||
|
*
|
||||||
|
* @author Happy Fish / YuQing
|
||||||
|
* @version Version 1.0
|
||||||
|
*/
|
||||||
|
public static class Downloader {
|
||||||
|
public TrackerClient tracker;
|
||||||
|
public TrackerServer trackerServer;
|
||||||
|
public DownloadFileDiscard callback;
|
||||||
|
|
||||||
|
public Downloader() throws Exception {
|
||||||
|
this.tracker = new TrackerClient();
|
||||||
|
this.trackerServer = tracker.getConnection();
|
||||||
|
this.callback = new DownloadFileDiscard();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int downloadFile(String file_id) throws Exception {
|
||||||
|
int errno;
|
||||||
|
StorageServer storageServer = null;
|
||||||
|
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
||||||
|
|
||||||
|
try {
|
||||||
|
errno = client.download_file1(file_id, this.callback);
|
||||||
|
if (errno != 0) {
|
||||||
|
System.out.println("Download file fail, file_id: " + file_id + ", error no: " + errno);
|
||||||
|
}
|
||||||
|
return errno;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println("Download file fail, error mesg: " + ex.getMessage());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* upload file thread
|
||||||
|
*
|
||||||
|
* @author Happy Fish / YuQing
|
||||||
|
* @version Version 1.0
|
||||||
|
*/
|
||||||
|
public static class UploadThread extends Thread {
|
||||||
|
private int thread_index;
|
||||||
|
|
||||||
|
public UploadThread(int index) {
|
||||||
|
this.thread_index = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
TestLoad.upload_thread_count++;
|
||||||
|
Uploader uploader = new Uploader();
|
||||||
|
|
||||||
|
System.out.println("upload thread " + this.thread_index + " start");
|
||||||
|
|
||||||
|
for (int i = 0; i < 50000; i++) {
|
||||||
|
TestLoad.total_upload_count++;
|
||||||
|
if (uploader.uploadFile() == 0) {
|
||||||
|
TestLoad.success_upload_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
TestLoad.upload_thread_count--;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("upload thread " + this.thread_index
|
||||||
|
+ " exit, total_upload_count: " + TestLoad.total_upload_count
|
||||||
|
+ ", success_upload_count: " + TestLoad.success_upload_count
|
||||||
|
+ ", total_download_count: " + TestLoad.total_download_count
|
||||||
|
+ ", success_download_count: " + TestLoad.success_download_count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* download file thread
|
||||||
|
*
|
||||||
|
* @author Happy Fish / YuQing
|
||||||
|
* @version Version 1.0
|
||||||
|
*/
|
||||||
|
public static class DownloadThread extends Thread {
|
||||||
|
private static Integer counter_lock = new Integer(0);
|
||||||
|
private int thread_index;
|
||||||
|
|
||||||
|
public DownloadThread(int index) {
|
||||||
|
this.thread_index = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
String file_id;
|
||||||
|
Downloader downloader = new Downloader();
|
||||||
|
|
||||||
|
System.out.println("download thread " + this.thread_index + " start");
|
||||||
|
|
||||||
|
file_id = "";
|
||||||
|
while (TestLoad.upload_thread_count != 0 || file_id != null) {
|
||||||
|
file_id = (String) TestLoad.file_ids.poll();
|
||||||
|
if (file_id == null) {
|
||||||
|
Thread.sleep(10);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (this.counter_lock) {
|
||||||
|
TestLoad.total_download_count++;
|
||||||
|
}
|
||||||
|
if (downloader.downloadFile(file_id) == 0) {
|
||||||
|
synchronized (this.counter_lock) {
|
||||||
|
TestLoad.success_download_count++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TestLoad.fail_download_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 3 && TestLoad.total_download_count < TestLoad.total_upload_count; i++) {
|
||||||
|
file_id = (String) TestLoad.file_ids.poll();
|
||||||
|
if (file_id == null) {
|
||||||
|
Thread.sleep(10);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (this.counter_lock) {
|
||||||
|
TestLoad.total_download_count++;
|
||||||
|
}
|
||||||
|
if (downloader.downloadFile(file_id) == 0) {
|
||||||
|
synchronized (this.counter_lock) {
|
||||||
|
TestLoad.success_download_count++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TestLoad.fail_download_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("download thread " + this.thread_index
|
||||||
|
+ " exit, total_download_count: " + TestLoad.total_download_count
|
||||||
|
+ ", success_download_count: " + TestLoad.success_download_count
|
||||||
|
+ ", fail_download_count: " + TestLoad.fail_download_count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,61 +1,56 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (C) 2008 Happy Fish / YuQing
|
* Copyright (C) 2008 Happy Fish / YuQing
|
||||||
*
|
* <p>
|
||||||
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
|
||||||
* General Public License (LGPL).
|
* General Public License (LGPL).
|
||||||
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.csource.fastdfs.test;
|
package org.csource.fastdfs.test;
|
||||||
|
|
||||||
import java.io.*;
|
import org.csource.fastdfs.UploadCallback;
|
||||||
import java.util.*;
|
|
||||||
import java.net.*;
|
import java.io.FileInputStream;
|
||||||
import org.csource.fastdfs.*;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* upload file callback class, local file sender
|
* upload file callback class, local file sender
|
||||||
* @author Happy Fish / YuQing
|
*
|
||||||
* @version Version 1.0
|
* @author Happy Fish / YuQing
|
||||||
*/
|
* @version Version 1.0
|
||||||
public class UploadLocalFileSender implements UploadCallback
|
*/
|
||||||
{
|
public class UploadLocalFileSender implements UploadCallback {
|
||||||
private String local_filename;
|
private String local_filename;
|
||||||
|
|
||||||
public UploadLocalFileSender(String szLocalFilename)
|
public UploadLocalFileSender(String szLocalFilename) {
|
||||||
{
|
this.local_filename = szLocalFilename;
|
||||||
this.local_filename = szLocalFilename;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* send file content callback function, be called only once when the file uploaded
|
||||||
* send file content callback function, be called only once when the file uploaded
|
*
|
||||||
* @param out output stream for writing file content
|
* @param out output stream for writing file content
|
||||||
* @return 0 success, return none zero(errno) if fail
|
* @return 0 success, return none zero(errno) if fail
|
||||||
*/
|
*/
|
||||||
public int send(OutputStream out) throws IOException
|
public int send(OutputStream out) throws IOException {
|
||||||
{
|
FileInputStream fis;
|
||||||
FileInputStream fis;
|
int readBytes;
|
||||||
int readBytes;
|
byte[] buff = new byte[256 * 1024];
|
||||||
byte[] buff = new byte[256 * 1024];
|
|
||||||
|
fis = new FileInputStream(this.local_filename);
|
||||||
fis = new FileInputStream(this.local_filename);
|
try {
|
||||||
try
|
while ((readBytes = fis.read(buff)) >= 0) {
|
||||||
{
|
if (readBytes == 0) {
|
||||||
while ((readBytes=fis.read(buff)) >= 0)
|
continue;
|
||||||
{
|
}
|
||||||
if (readBytes == 0)
|
|
||||||
{
|
out.write(buff, 0, readBytes);
|
||||||
continue;
|
}
|
||||||
}
|
} finally {
|
||||||
|
fis.close();
|
||||||
out.write(buff, 0, readBytes);
|
}
|
||||||
}
|
|
||||||
}
|
return 0;
|
||||||
finally
|
}
|
||||||
{
|
|
||||||
fis.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
13
src/main/resources/fastdfs-client.properties.sample
Normal file
13
src/main/resources/fastdfs-client.properties.sample
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
## fastdfs-client.properties
|
||||||
|
|
||||||
|
fastdfs.connect_timeout_in_seconds = 5
|
||||||
|
fastdfs.network_timeout_in_seconds = 30
|
||||||
|
|
||||||
|
fastdfs.charset = UTF-8
|
||||||
|
|
||||||
|
fastdfs.http_anti_steal_token = false
|
||||||
|
fastdfs.http_secret_key = FastDFS1234567890
|
||||||
|
fastdfs.http_tracker_http_port = 80
|
||||||
|
|
||||||
|
fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
|
||||||
|
|
26
src/test/java/org/csource/fastdfs/ClientGlobalTests.java
Normal file
26
src/test/java/org/csource/fastdfs/ClientGlobalTests.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package org.csource.fastdfs;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by James on 2017/5/19.
|
||||||
|
*/
|
||||||
|
public class ClientGlobalTests {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
String trackerServers = "10.0.11.101:22122,10.0.11.102:22122";
|
||||||
|
ClientGlobal.initByTrackers(trackerServers);
|
||||||
|
System.out.println("ClientGlobal.configInfo() : " + ClientGlobal.configInfo());
|
||||||
|
|
||||||
|
String propFilePath = "fastdfs-client.properties";
|
||||||
|
ClientGlobal.initByProperties(propFilePath);
|
||||||
|
System.out.println("ClientGlobal.configInfo() : " + ClientGlobal.configInfo());
|
||||||
|
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, "10.0.11.101:22122,10.0.11.102:22122");
|
||||||
|
ClientGlobal.initByProperties(props);
|
||||||
|
System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
13
src/test/resources/fastdfs-client.properties
Normal file
13
src/test/resources/fastdfs-client.properties
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
## fastdfs-client.properties
|
||||||
|
|
||||||
|
fastdfs.connect_timeout_in_seconds = 5
|
||||||
|
fastdfs.network_timeout_in_seconds = 30
|
||||||
|
|
||||||
|
fastdfs.charset = UTF-8
|
||||||
|
|
||||||
|
fastdfs.http_anti_steal_token = false
|
||||||
|
fastdfs.http_secret_key = FastDFS1234567890
|
||||||
|
fastdfs.http_tracker_http_port = 80
|
||||||
|
|
||||||
|
fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user