use of com.zhouzifei.tool.common.fastdfs.pool.Connection in project simpleFS by shengdingbox.
the class StorageClient method truncate_file.
/**
* truncate appender file from storage server
*
* @param group_name the group name of storage server
* @param appender_filename the appender filename
* @param truncated_file_size truncated file size
* @return 0 for success, none zero for fail (error code)
*/
public int truncate_file(String group_name, String appender_filename, long truncated_file_size) throws IOException, ServiceException {
byte[] header;
boolean bNewStorageServer;
Connection connection = null;
byte[] hexLenBytes;
byte[] appenderFilenameBytes;
int offset;
int body_len;
if ((group_name == null || group_name.length() == 0) || (appender_filename == null || appender_filename.length() == 0)) {
this.errno = ProtoCommon.ERR_NO_EINVAL;
return this.errno;
}
bNewStorageServer = this.newUpdatableStorageConnection(group_name, appender_filename);
try {
connection = this.storageServer.getConnection();
appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset);
body_len = 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length;
header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_TRUNCATE_FILE, body_len, (byte) 0);
byte[] wholePkg = new byte[header.length + body_len];
System.arraycopy(header, 0, wholePkg, 0, header.length);
offset = header.length;
hexLenBytes = ProtoCommon.long2buff(appender_filename.length());
System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length);
offset += hexLenBytes.length;
hexLenBytes = ProtoCommon.long2buff(truncated_file_size);
System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length);
offset += hexLenBytes.length;
OutputStream out = connection.getOutputStream();
System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length);
offset += appenderFilenameBytes.length;
out.write(wholePkg);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
this.errno = pkgInfo.errno;
return pkgInfo.errno;
} catch (IOException ex) {
try {
connection.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
connection = null;
}
throw ex;
} finally {
releaseConnection(connection, bNewStorageServer);
}
}
use of com.zhouzifei.tool.common.fastdfs.pool.Connection in project simpleFS by shengdingbox.
the class StorageClient method regenerate_appender_filename.
/**
* regenerate filename for appender file
*
* @param group_name the group name of appender file
* @param appender_filename the appender filename
* @return 2 elements string array if success:<br>
* <ul><li> results[0]: the group name to store the file</li></ul>
* <ul><li> results[1]: the new created filename</li></ul>
* return null if fail
*/
public String[] regenerate_appender_filename(String group_name, String appender_filename) throws IOException, ServiceException {
byte[] header;
boolean bNewStorageServer;
Connection connection = null;
byte[] hexLenBytes;
byte[] appenderFilenameBytes;
int offset;
long body_len;
if ((group_name == null || group_name.length() == 0) || (appender_filename == null || appender_filename.length() == 0)) {
this.errno = ProtoCommon.ERR_NO_EINVAL;
return null;
}
bNewStorageServer = this.newUpdatableStorageConnection(group_name, appender_filename);
try {
connection = this.storageServer.getConnection();
appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset);
body_len = appenderFilenameBytes.length;
header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_REGENERATE_APPENDER_FILENAME, body_len, (byte) 0);
byte[] wholePkg = new byte[(int) (header.length + body_len)];
System.arraycopy(header, 0, wholePkg, 0, header.length);
offset = header.length;
System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length);
offset += appenderFilenameBytes.length;
OutputStream out = connection.getOutputStream();
out.write(wholePkg);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
return null;
}
if (pkgInfo.body.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) {
throw new ServiceException("body length: " + pkgInfo.body.length + " <= " + ProtoCommon.FDFS_GROUP_NAME_MAX_LEN);
}
String new_group_name = new String(pkgInfo.body, 0, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN).trim();
String remote_filename = new String(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, pkgInfo.body.length - ProtoCommon.FDFS_GROUP_NAME_MAX_LEN);
String[] results = new String[2];
results[0] = new_group_name;
results[1] = remote_filename;
return results;
} catch (IOException ex) {
try {
connection.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
connection = null;
}
throw ex;
} finally {
releaseConnection(connection, bNewStorageServer);
}
}
use of com.zhouzifei.tool.common.fastdfs.pool.Connection in project simpleFS by shengdingbox.
the class StorageClient method do_append_file.
/**
* append file to storage server
*
* @param group_name the group name of appender file
* @param appender_filename the appender filename
* @param file_size the file size
* @param callback the write data callback object
* @return return true for success, false for fail
*/
protected int do_append_file(String group_name, String appender_filename, long file_size, UploadCallback callback) throws IOException, ServiceException {
byte[] header;
boolean bNewStorageServer;
Connection connection = null;
byte[] hexLenBytes;
byte[] appenderFilenameBytes;
int offset;
long body_len;
if ((group_name == null || group_name.length() == 0) || (appender_filename == null || appender_filename.length() == 0)) {
this.errno = ProtoCommon.ERR_NO_EINVAL;
return this.errno;
}
bNewStorageServer = this.newUpdatableStorageConnection(group_name, appender_filename);
try {
connection = this.storageServer.getConnection();
appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset);
body_len = 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length + file_size;
header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_APPEND_FILE, body_len, (byte) 0);
byte[] wholePkg = new byte[(int) (header.length + body_len - file_size)];
System.arraycopy(header, 0, wholePkg, 0, header.length);
offset = header.length;
hexLenBytes = ProtoCommon.long2buff(appender_filename.length());
System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length);
offset += hexLenBytes.length;
hexLenBytes = ProtoCommon.long2buff(file_size);
System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length);
offset += hexLenBytes.length;
OutputStream out = connection.getOutputStream();
System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length);
offset += appenderFilenameBytes.length;
out.write(wholePkg);
if ((this.errno = (byte) callback.send(out)) != 0) {
return this.errno;
}
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
return this.errno;
}
return 0;
} catch (IOException ex) {
try {
connection.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
connection = null;
}
throw ex;
} finally {
releaseConnection(connection, bNewStorageServer);
}
}
use of com.zhouzifei.tool.common.fastdfs.pool.Connection in project simpleFS by shengdingbox.
the class StorageClient method do_modify_file.
/**
* modify appender file to storage server
*
* @param group_name the group name of appender file
* @param appender_filename the appender filename
* @param file_offset the offset of appender file
* @param modify_size the modify size
* @param callback the write data callback object
* @return return true for success, false for fail
*/
protected int do_modify_file(String group_name, String appender_filename, long file_offset, long modify_size, com.zhouzifei.tool.common.fastdfs.UploadCallback callback) throws IOException, ServiceException {
byte[] header;
boolean bNewStorageServer;
Connection connection = null;
byte[] hexLenBytes;
byte[] appenderFilenameBytes;
int offset;
long body_len;
if ((group_name == null || group_name.length() == 0) || (appender_filename == null || appender_filename.length() == 0)) {
this.errno = ProtoCommon.ERR_NO_EINVAL;
return this.errno;
}
bNewStorageServer = this.newUpdatableStorageConnection(group_name, appender_filename);
try {
connection = this.storageServer.getConnection();
appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset);
body_len = 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length + modify_size;
header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_MODIFY_FILE, body_len, (byte) 0);
byte[] wholePkg = new byte[(int) (header.length + body_len - modify_size)];
System.arraycopy(header, 0, wholePkg, 0, header.length);
offset = header.length;
hexLenBytes = ProtoCommon.long2buff(appender_filename.length());
System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length);
offset += hexLenBytes.length;
hexLenBytes = ProtoCommon.long2buff(file_offset);
System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length);
offset += hexLenBytes.length;
hexLenBytes = ProtoCommon.long2buff(modify_size);
System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length);
offset += hexLenBytes.length;
OutputStream out = connection.getOutputStream();
System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length);
offset += appenderFilenameBytes.length;
out.write(wholePkg);
if ((this.errno = (byte) callback.send(out)) != 0) {
return this.errno;
}
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
return this.errno;
}
return 0;
} catch (IOException ex) {
try {
connection.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
connection = null;
}
throw ex;
} finally {
releaseConnection(connection, bNewStorageServer);
}
}
use of com.zhouzifei.tool.common.fastdfs.pool.Connection in project simpleFS by shengdingbox.
the class TrackerClient method deleteStorage.
/**
* delete a storage server from the tracker server
*
* @param trackerServer the connected tracker server
* @param groupName the group name of storage server
* @param storageIpAddr the storage server ip address
* @return true for success, false for fail
*/
private boolean deleteStorage(TrackerServer trackerServer, String groupName, String storageIpAddr) throws IOException, ServiceException {
byte[] header;
byte[] bGroupName;
byte[] bs;
int len;
Connection connection;
connection = trackerServer.getConnection();
OutputStream out = connection.getOutputStream();
try {
bs = groupName.getBytes(ClientGlobal.g_charset);
bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) {
len = bs.length;
} else {
len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
}
Arrays.fill(bGroupName, (byte) 0);
System.arraycopy(bs, 0, bGroupName, 0, len);
int ipAddrLen;
byte[] bIpAddr = storageIpAddr.getBytes(ClientGlobal.g_charset);
if (bIpAddr.length < ProtoCommon.FDFS_IPADDR_SIZE) {
ipAddrLen = bIpAddr.length;
} else {
ipAddrLen = ProtoCommon.FDFS_IPADDR_SIZE - 1;
}
header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ipAddrLen, (byte) 0);
byte[] wholePkg = new byte[header.length + bGroupName.length + ipAddrLen];
System.arraycopy(header, 0, wholePkg, 0, header.length);
System.arraycopy(bGroupName, 0, wholePkg, header.length, bGroupName.length);
System.arraycopy(bIpAddr, 0, wholePkg, header.length + bGroupName.length, ipAddrLen);
out.write(wholePkg);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.TRACKER_PROTO_CMD_RESP, 0);
this.errno = pkgInfo.errno;
return pkgInfo.errno == 0;
} catch (IOException e) {
try {
connection.close();
} finally {
connection = null;
}
throw e;
} finally {
if (connection != null) {
connection.release();
}
}
}
Aggregations