org.csource.fastdfs
Class ProtoCommon

java.lang.Object
  extended by org.csource.fastdfs.ProtoCommon

public class ProtoCommon
extends java.lang.Object

protocol common functions

Version:
Version 1.18
Author:
Happy Fish / YuQing

Nested Class Summary
static class ProtoCommon.RecvHeaderInfo
          receive header info
static class ProtoCommon.RecvPackageInfo
          receive package info
 
Field Summary
static byte ECONNREFUSED
           
static byte ERR_NO_EALREADY
           
static byte ERR_NO_EBUSY
           
static byte ERR_NO_EINVAL
           
static byte ERR_NO_EIO
           
static byte ERR_NO_ENOENT
           
static byte ERR_NO_ENOSPC
           
static int FDFS_DOMAIN_NAME_MAX_SIZE
           
static java.lang.String FDFS_FIELD_SEPERATOR
           
static byte FDFS_FILE_EXT_NAME_MAX_LEN
           
static byte FDFS_FILE_PATH_LEN
           
static byte FDFS_FILE_PREFIX_MAX_LEN
           
static byte FDFS_FILENAME_BASE64_LENGTH
           
static int FDFS_GROUP_NAME_MAX_LEN
           
static int FDFS_IPADDR_SIZE
           
static byte FDFS_PROTO_CMD_ACTIVE_TEST
           
static byte FDFS_PROTO_CMD_QUIT
           
static int FDFS_PROTO_CMD_SIZE
           
static int FDFS_PROTO_PKG_LEN_SIZE
           
static java.lang.String FDFS_RECORD_SEPERATOR
           
static byte FDFS_STORAGE_STATUS_ACTIVE
           
static byte FDFS_STORAGE_STATUS_DELETED
           
static byte FDFS_STORAGE_STATUS_INIT
           
static byte FDFS_STORAGE_STATUS_IP_CHANGED
           
static byte FDFS_STORAGE_STATUS_NONE
           
static byte FDFS_STORAGE_STATUS_OFFLINE
           
static byte FDFS_STORAGE_STATUS_ONLINE
           
static byte FDFS_STORAGE_STATUS_SYNCING
           
static byte FDFS_STORAGE_STATUS_WAIT_SYNC
           
static int FDFS_VERSION_SIZE
           
static long INFINITE_FILE_SIZE
           
protected static int PROTO_HEADER_CMD_INDEX
           
protected static int PROTO_HEADER_STATUS_INDEX
           
static byte STORAGE_PROTO_CMD_APPEND_FILE
           
static byte STORAGE_PROTO_CMD_DELETE_FILE
           
static byte STORAGE_PROTO_CMD_DOWNLOAD_FILE
           
static byte STORAGE_PROTO_CMD_GET_METADATA
           
static byte STORAGE_PROTO_CMD_MODIFY_FILE
           
static byte STORAGE_PROTO_CMD_QUERY_FILE_INFO
           
static byte STORAGE_PROTO_CMD_RESP
           
static byte STORAGE_PROTO_CMD_SET_METADATA
           
static byte STORAGE_PROTO_CMD_TRUNCATE_FILE
           
static byte STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE
           
static byte STORAGE_PROTO_CMD_UPLOAD_FILE
           
static byte STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE
           
static byte STORAGE_SET_METADATA_FLAG_MERGE
          for replace, insert when the meta item not exist, otherwise update it
static byte STORAGE_SET_METADATA_FLAG_OVERWRITE
          for overwrite all old metadata
static byte TRACKER_PROTO_CMD_RESP
           
static byte TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE
           
static byte TRACKER_PROTO_CMD_SERVER_LIST_GROUP
           
static byte TRACKER_PROTO_CMD_SERVER_LIST_STORAGE
           
static byte TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ALL
           
static byte TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE
           
static byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL
           
static byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE
           
static byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL
           
static byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE
           
static byte TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE
           
static int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN
           
static int TRACKER_QUERY_STORAGE_STORE_BODY_LEN
           
 
Method Summary
static boolean activeTest(java.net.Socket sock)
          send ACTIVE_TEST command to server, test if network is ok and the server is alive
static int buff2int(byte[] bs, int offset)
          buff convert to int
static long buff2long(byte[] bs, int offset)
          buff convert to long
static void closeSocket(java.net.Socket sock)
          send quit command to server and close socket
static java.lang.String genSlaveFilename(java.lang.String master_filename, java.lang.String prefix_name, java.lang.String ext_name)
          generate slave filename
static java.lang.String getIpAddress(byte[] bs, int offset)
          buff convert to ip address
static java.lang.String getStorageStatusCaption(byte status)
           
static java.lang.String getToken(java.lang.String file_id, int ts, java.lang.String secret_key)
          get token for file URL
static byte[] long2buff(long n)
          long convert to buff (big-endian)
static java.lang.String md5(byte[] source)
          md5 function
static java.lang.String pack_metadata(NameValuePair[] meta_list)
          pack metadata array to string
static byte[] packHeader(byte cmd, long pkg_len, byte errno)
          pack header by FastDFS transfer protocol
static ProtoCommon.RecvHeaderInfo recvHeader(java.io.InputStream in, byte expect_cmd, long expect_body_len)
          receive pack header
static ProtoCommon.RecvPackageInfo recvPackage(java.io.InputStream in, byte expect_cmd, long expect_body_len)
          receive whole pack
static NameValuePair[] split_metadata(java.lang.String meta_buff)
          split metadata to name value pair array
static NameValuePair[] split_metadata(java.lang.String meta_buff, java.lang.String recordSeperator, java.lang.String filedSeperator)
          split metadata to name value pair array
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FDFS_PROTO_CMD_QUIT

public static final byte FDFS_PROTO_CMD_QUIT
See Also:
Constant Field Values

TRACKER_PROTO_CMD_SERVER_LIST_GROUP

public static final byte TRACKER_PROTO_CMD_SERVER_LIST_GROUP
See Also:
Constant Field Values

TRACKER_PROTO_CMD_SERVER_LIST_STORAGE

public static final byte TRACKER_PROTO_CMD_SERVER_LIST_STORAGE
See Also:
Constant Field Values

TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE

public static final byte TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE
See Also:
Constant Field Values

TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE

public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE
See Also:
Constant Field Values

TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE

public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE
See Also:
Constant Field Values

TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE

public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE
See Also:
Constant Field Values

TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE

public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE
See Also:
Constant Field Values

TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ALL

public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ALL
See Also:
Constant Field Values

TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL

public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL
See Also:
Constant Field Values

TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL

public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL
See Also:
Constant Field Values

TRACKER_PROTO_CMD_RESP

public static final byte TRACKER_PROTO_CMD_RESP
See Also:
Constant Field Values

FDFS_PROTO_CMD_ACTIVE_TEST

public static final byte FDFS_PROTO_CMD_ACTIVE_TEST
See Also:
Constant Field Values

STORAGE_PROTO_CMD_UPLOAD_FILE

public static final byte STORAGE_PROTO_CMD_UPLOAD_FILE
See Also:
Constant Field Values

STORAGE_PROTO_CMD_DELETE_FILE

public static final byte STORAGE_PROTO_CMD_DELETE_FILE
See Also:
Constant Field Values

STORAGE_PROTO_CMD_SET_METADATA

public static final byte STORAGE_PROTO_CMD_SET_METADATA
See Also:
Constant Field Values

STORAGE_PROTO_CMD_DOWNLOAD_FILE

public static final byte STORAGE_PROTO_CMD_DOWNLOAD_FILE
See Also:
Constant Field Values

STORAGE_PROTO_CMD_GET_METADATA

public static final byte STORAGE_PROTO_CMD_GET_METADATA
See Also:
Constant Field Values

STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE

public static final byte STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE
See Also:
Constant Field Values

STORAGE_PROTO_CMD_QUERY_FILE_INFO

public static final byte STORAGE_PROTO_CMD_QUERY_FILE_INFO
See Also:
Constant Field Values

STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE

public static final byte STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE
See Also:
Constant Field Values

STORAGE_PROTO_CMD_APPEND_FILE

public static final byte STORAGE_PROTO_CMD_APPEND_FILE
See Also:
Constant Field Values

STORAGE_PROTO_CMD_MODIFY_FILE

public static final byte STORAGE_PROTO_CMD_MODIFY_FILE
See Also:
Constant Field Values

STORAGE_PROTO_CMD_TRUNCATE_FILE

public static final byte STORAGE_PROTO_CMD_TRUNCATE_FILE
See Also:
Constant Field Values

STORAGE_PROTO_CMD_RESP

public static final byte STORAGE_PROTO_CMD_RESP
See Also:
Constant Field Values

FDFS_STORAGE_STATUS_INIT

public static final byte FDFS_STORAGE_STATUS_INIT
See Also:
Constant Field Values

FDFS_STORAGE_STATUS_WAIT_SYNC

public static final byte FDFS_STORAGE_STATUS_WAIT_SYNC
See Also:
Constant Field Values

FDFS_STORAGE_STATUS_SYNCING

public static final byte FDFS_STORAGE_STATUS_SYNCING
See Also:
Constant Field Values

FDFS_STORAGE_STATUS_IP_CHANGED

public static final byte FDFS_STORAGE_STATUS_IP_CHANGED
See Also:
Constant Field Values

FDFS_STORAGE_STATUS_DELETED

public static final byte FDFS_STORAGE_STATUS_DELETED
See Also:
Constant Field Values

FDFS_STORAGE_STATUS_OFFLINE

public static final byte FDFS_STORAGE_STATUS_OFFLINE
See Also:
Constant Field Values

FDFS_STORAGE_STATUS_ONLINE

public static final byte FDFS_STORAGE_STATUS_ONLINE
See Also:
Constant Field Values

FDFS_STORAGE_STATUS_ACTIVE

public static final byte FDFS_STORAGE_STATUS_ACTIVE
See Also:
Constant Field Values

FDFS_STORAGE_STATUS_NONE

public static final byte FDFS_STORAGE_STATUS_NONE
See Also:
Constant Field Values

STORAGE_SET_METADATA_FLAG_OVERWRITE

public static final byte STORAGE_SET_METADATA_FLAG_OVERWRITE
for overwrite all old metadata

See Also:
Constant Field Values

STORAGE_SET_METADATA_FLAG_MERGE

public static final byte STORAGE_SET_METADATA_FLAG_MERGE
for replace, insert when the meta item not exist, otherwise update it

See Also:
Constant Field Values

FDFS_PROTO_PKG_LEN_SIZE

public static final int FDFS_PROTO_PKG_LEN_SIZE
See Also:
Constant Field Values

FDFS_PROTO_CMD_SIZE

public static final int FDFS_PROTO_CMD_SIZE
See Also:
Constant Field Values

FDFS_GROUP_NAME_MAX_LEN

public static final int FDFS_GROUP_NAME_MAX_LEN
See Also:
Constant Field Values

FDFS_IPADDR_SIZE

public static final int FDFS_IPADDR_SIZE
See Also:
Constant Field Values

FDFS_DOMAIN_NAME_MAX_SIZE

public static final int FDFS_DOMAIN_NAME_MAX_SIZE
See Also:
Constant Field Values

FDFS_VERSION_SIZE

public static final int FDFS_VERSION_SIZE
See Also:
Constant Field Values

FDFS_RECORD_SEPERATOR

public static final java.lang.String FDFS_RECORD_SEPERATOR
See Also:
Constant Field Values

FDFS_FIELD_SEPERATOR

public static final java.lang.String FDFS_FIELD_SEPERATOR
See Also:
Constant Field Values

TRACKER_QUERY_STORAGE_FETCH_BODY_LEN

public static final int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN
See Also:
Constant Field Values

TRACKER_QUERY_STORAGE_STORE_BODY_LEN

public static final int TRACKER_QUERY_STORAGE_STORE_BODY_LEN
See Also:
Constant Field Values

PROTO_HEADER_CMD_INDEX

protected static final int PROTO_HEADER_CMD_INDEX
See Also:
Constant Field Values

PROTO_HEADER_STATUS_INDEX

protected static final int PROTO_HEADER_STATUS_INDEX
See Also:
Constant Field Values

FDFS_FILE_EXT_NAME_MAX_LEN

public static final byte FDFS_FILE_EXT_NAME_MAX_LEN
See Also:
Constant Field Values

FDFS_FILE_PREFIX_MAX_LEN

public static final byte FDFS_FILE_PREFIX_MAX_LEN
See Also:
Constant Field Values

FDFS_FILE_PATH_LEN

public static final byte FDFS_FILE_PATH_LEN
See Also:
Constant Field Values

FDFS_FILENAME_BASE64_LENGTH

public static final byte FDFS_FILENAME_BASE64_LENGTH
See Also:
Constant Field Values

ERR_NO_ENOENT

public static final byte ERR_NO_ENOENT
See Also:
Constant Field Values

ERR_NO_EIO

public static final byte ERR_NO_EIO
See Also:
Constant Field Values

ERR_NO_EBUSY

public static final byte ERR_NO_EBUSY
See Also:
Constant Field Values

ERR_NO_EINVAL

public static final byte ERR_NO_EINVAL
See Also:
Constant Field Values

ERR_NO_ENOSPC

public static final byte ERR_NO_ENOSPC
See Also:
Constant Field Values

ECONNREFUSED

public static final byte ECONNREFUSED
See Also:
Constant Field Values

ERR_NO_EALREADY

public static final byte ERR_NO_EALREADY
See Also:
Constant Field Values

INFINITE_FILE_SIZE

public static final long INFINITE_FILE_SIZE
See Also:
Constant Field Values
Method Detail

getStorageStatusCaption

public static java.lang.String getStorageStatusCaption(byte status)

packHeader

public static byte[] packHeader(byte cmd,
                                long pkg_len,
                                byte errno)
                         throws java.io.UnsupportedEncodingException
pack header by FastDFS transfer protocol

Parameters:
cmd - which command to send
pkg_len - package body length
errno - status code, should be (byte)0
Returns:
packed byte buffer
Throws:
java.io.UnsupportedEncodingException

recvHeader

public static ProtoCommon.RecvHeaderInfo recvHeader(java.io.InputStream in,
                                                    byte expect_cmd,
                                                    long expect_body_len)
                                             throws java.io.IOException
receive pack header

Parameters:
in - input stream
expect_cmd - expect response command
expect_body_len - expect response package body length
Returns:
RecvHeaderInfo: errno and pkg body length
Throws:
java.io.IOException

recvPackage

public static ProtoCommon.RecvPackageInfo recvPackage(java.io.InputStream in,
                                                      byte expect_cmd,
                                                      long expect_body_len)
                                               throws java.io.IOException
receive whole pack

Parameters:
in - input stream
expect_cmd - expect response command
expect_body_len - expect response package body length
Returns:
RecvPackageInfo: errno and reponse body(byte buff)
Throws:
java.io.IOException

split_metadata

public static NameValuePair[] split_metadata(java.lang.String meta_buff)
split metadata to name value pair array

Parameters:
meta_buff - metadata
Returns:
name value pair array

split_metadata

public static NameValuePair[] split_metadata(java.lang.String meta_buff,
                                             java.lang.String recordSeperator,
                                             java.lang.String filedSeperator)
split metadata to name value pair array

Parameters:
meta_buff - metadata
recordSeperator - record/row seperator
filedSeperator - field/column seperator
Returns:
name value pair array

pack_metadata

public static java.lang.String pack_metadata(NameValuePair[] meta_list)
pack metadata array to string

Parameters:
meta_list - metadata array
Returns:
packed metadata

closeSocket

public static void closeSocket(java.net.Socket sock)
                        throws java.io.IOException
send quit command to server and close socket

Parameters:
sock - the Socket object
Throws:
java.io.IOException

activeTest

public static boolean activeTest(java.net.Socket sock)
                          throws java.io.IOException
send ACTIVE_TEST command to server, test if network is ok and the server is alive

Parameters:
sock - the Socket object
Throws:
java.io.IOException

long2buff

public static byte[] long2buff(long n)
long convert to buff (big-endian)

Parameters:
n - long number
Returns:
8 bytes buff

buff2long

public static long buff2long(byte[] bs,
                             int offset)
buff convert to long

Parameters:
bs - the buffer (big-endian)
offset - the start position based 0
Returns:
long number

buff2int

public static int buff2int(byte[] bs,
                           int offset)
buff convert to int

Parameters:
bs - the buffer (big-endian)
offset - the start position based 0
Returns:
int number

getIpAddress

public static java.lang.String getIpAddress(byte[] bs,
                                            int offset)
buff convert to ip address

Parameters:
bs - the buffer (big-endian)
offset - the start position based 0
Returns:
ip address

md5

public static java.lang.String md5(byte[] source)
                            throws java.security.NoSuchAlgorithmException
md5 function

Parameters:
source - the input buffer
Returns:
md5 string
Throws:
java.security.NoSuchAlgorithmException

getToken

public static java.lang.String getToken(java.lang.String file_id,
                                        int ts,
                                        java.lang.String secret_key)
                                 throws java.io.UnsupportedEncodingException,
                                        java.security.NoSuchAlgorithmException,
                                        MyException
get token for file URL

Parameters:
file_id - the file id return by FastDFS server
ts - unix timestamp, unit: second
secret_key - the secret key
Returns:
token string
Throws:
java.io.UnsupportedEncodingException
java.security.NoSuchAlgorithmException
MyException

genSlaveFilename

public static java.lang.String genSlaveFilename(java.lang.String master_filename,
                                                java.lang.String prefix_name,
                                                java.lang.String ext_name)
                                         throws MyException
generate slave filename

Parameters:
master_filename - the master filename to generate the slave filename
prefix_name - the prefix name to generate the slave filename
ext_name - the extension name of slave filename, null for same as the master extension name
Returns:
slave filename string
Throws:
MyException