Search in sources :

Example 6 with Connection

use of com.zhouzifei.tool.common.fastdfs.pool.Connection in project simpleFS by shengdingbox.

the class StorageClient method delete_file.

/**
 * delete file from storage server
 *
 * @param group_name      the group name of storage server
 * @param remote_filename filename on storage server
 * @return 0 for success, none zero for fail (error code)
 */
public int delete_file(String group_name, String remote_filename) throws IOException, ServiceException {
    boolean bNewStorageServer = this.newUpdatableStorageConnection(group_name, remote_filename);
    Connection connection = this.storageServer.getConnection();
    try {
        this.send_package(ProtoCommon.STORAGE_PROTO_CMD_DELETE_FILE, group_name, remote_filename, connection);
        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);
    }
}
Also used : Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection)

Example 7 with Connection

use of com.zhouzifei.tool.common.fastdfs.pool.Connection in project simpleFS by shengdingbox.

the class StorageClient method set_metadata.

/**
 * set metadata items to storage server
 *
 * @param group_name      the group name of storage server
 * @param remote_filename filename on storage server
 * @param meta_list       meta item array
 * @param op_flag         flag, can be one of following values: <br>
 *                        <ul><li> ProtoCommon.STORAGE_SET_METADATA_FLAG_OVERWRITE: overwrite all old
 *                        metadata items</li></ul>
 *                        <ul><li> ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE: merge, insert when
 *                        the metadata item not exist, otherwise update it</li></ul>
 * @return 0 for success, !=0 fail (error code)
 */
public int set_metadata(String group_name, String remote_filename, NameValuePair[] meta_list, byte op_flag) throws IOException, ServiceException {
    boolean bNewStorageServer = this.newUpdatableStorageConnection(group_name, remote_filename);
    Connection connection = this.storageServer.getConnection();
    try {
        byte[] header;
        byte[] groupBytes;
        byte[] filenameBytes;
        byte[] meta_buff;
        byte[] bs;
        int groupLen;
        byte[] sizeBytes;
        ProtoCommon.RecvPackageInfo pkgInfo;
        if (meta_list == null) {
            meta_buff = new byte[0];
        } else {
            meta_buff = ProtoCommon.pack_metadata(meta_list).getBytes(ClientGlobal.g_charset);
        }
        filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset);
        sizeBytes = new byte[2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE];
        Arrays.fill(sizeBytes, (byte) 0);
        bs = ProtoCommon.long2buff(filenameBytes.length);
        System.arraycopy(bs, 0, sizeBytes, 0, bs.length);
        bs = ProtoCommon.long2buff(meta_buff.length);
        System.arraycopy(bs, 0, sizeBytes, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE, bs.length);
        groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
        bs = group_name.getBytes(ClientGlobal.g_charset);
        Arrays.fill(groupBytes, (byte) 0);
        if (bs.length <= groupBytes.length) {
            groupLen = bs.length;
        } else {
            groupLen = groupBytes.length;
        }
        System.arraycopy(bs, 0, groupBytes, 0, groupLen);
        header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_SET_METADATA, 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + 1 + groupBytes.length + filenameBytes.length + meta_buff.length, (byte) 0);
        OutputStream out = connection.getOutputStream();
        byte[] wholePkg = new byte[header.length + sizeBytes.length + 1 + groupBytes.length + filenameBytes.length];
        System.arraycopy(header, 0, wholePkg, 0, header.length);
        System.arraycopy(sizeBytes, 0, wholePkg, header.length, sizeBytes.length);
        wholePkg[header.length + sizeBytes.length] = op_flag;
        System.arraycopy(groupBytes, 0, wholePkg, header.length + sizeBytes.length + 1, groupBytes.length);
        System.arraycopy(filenameBytes, 0, wholePkg, header.length + sizeBytes.length + 1 + groupBytes.length, filenameBytes.length);
        out.write(wholePkg);
        if (meta_buff.length > 0) {
            out.write(meta_buff);
        }
        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);
    }
}
Also used : Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection)

Example 8 with Connection

use of com.zhouzifei.tool.common.fastdfs.pool.Connection in project simpleFS by shengdingbox.

the class StorageClient method download_file.

/**
 * download file from storage server
 *
 * @param group_name      the group name of storage server
 * @param remote_filename filename on storage server
 * @param file_offset     the start offset of the file
 * @param download_bytes  download bytes, 0 for remain bytes from offset
 * @param local_filename  filename on local
 * @return 0 success, return none zero errno if fail
 */
public int download_file(String group_name, String remote_filename, long file_offset, long download_bytes, String local_filename) throws IOException, ServiceException {
    boolean bNewStorageServer = this.newReadableStorageConnection(group_name, remote_filename);
    Connection connection = this.storageServer.getConnection();
    try {
        ProtoCommon.RecvHeaderInfo header;
        FileOutputStream out = new FileOutputStream(local_filename);
        try {
            this.errno = 0;
            this.send_download_package(group_name, remote_filename, file_offset, download_bytes, connection);
            InputStream in = connection.getInputStream();
            header = ProtoCommon.recvHeader(in, ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
            this.errno = header.errno;
            if (header.errno != 0) {
                return header.errno;
            }
            byte[] buff = new byte[256 * 1024];
            long remainBytes = header.body_len;
            int bytes;
            while (remainBytes > 0) {
                if ((bytes = in.read(buff, 0, remainBytes > buff.length ? buff.length : (int) remainBytes)) < 0) {
                    throw new IOException("recv package size " + (header.body_len - remainBytes) + " != " + header.body_len);
                }
                out.write(buff, 0, bytes);
                remainBytes -= bytes;
            // System.out.println("totalBytes=" + (header.body_len - remainBytes));
            }
            return 0;
        } catch (IOException ex) {
            if (this.errno == 0) {
                this.errno = ProtoCommon.ERR_NO_EIO;
            }
            throw ex;
        } finally {
            out.close();
            if (this.errno != 0) {
                (new File(local_filename)).delete();
            }
        }
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        } finally {
            connection = null;
        }
        throw ex;
    } finally {
        releaseConnection(connection, bNewStorageServer);
    }
}
Also used : Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection)

Example 9 with Connection

use of com.zhouzifei.tool.common.fastdfs.pool.Connection in project simpleFS by shengdingbox.

the class StorageClient method get_metadata.

/**
 * get all metadata items from storage server
 *
 * @param group_name      the group name of storage server
 * @param remote_filename filename on storage server
 * @return meta info array, return null if fail
 */
public NameValuePair[] get_metadata(String group_name, String remote_filename) throws IOException, ServiceException {
    boolean bNewStorageServer = this.newUpdatableStorageConnection(group_name, remote_filename);
    Connection connection = this.storageServer.getConnection();
    try {
        ProtoCommon.RecvPackageInfo pkgInfo;
        this.send_package(ProtoCommon.STORAGE_PROTO_CMD_GET_METADATA, group_name, remote_filename, connection);
        pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        return ProtoCommon.split_metadata(new String(pkgInfo.body, ClientGlobal.g_charset));
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        } finally {
            connection = null;
        }
        throw ex;
    } finally {
        releaseConnection(connection, bNewStorageServer);
    }
}
Also used : Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection)

Example 10 with Connection

use of com.zhouzifei.tool.common.fastdfs.pool.Connection in project simpleFS by shengdingbox.

the class StorageClient method do_upload_file.

/**
 * upload file to storage server
 *
 * @param cmd             the command code
 * @param group_name      the group name to upload file to, can be empty
 * @param master_filename the master file name to generate the slave file
 * @param prefix_name     the prefix name to generate the slave file
 * @param file_ext_name   file ext name, do not include dot(.)
 * @param file_size       the file size
 * @param callback        the write data callback object
 * @param meta_list       meta info array
 * @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
 */
protected String[] do_upload_file(byte cmd, String group_name, String master_filename, String prefix_name, String file_ext_name, long file_size, UploadCallback callback, NameValuePair[] meta_list) throws IOException, ServiceException {
    byte[] header;
    byte[] ext_name_bs;
    String new_group_name;
    String remote_filename;
    boolean bNewStorageServer;
    Connection connection = null;
    byte[] sizeBytes;
    byte[] hexLenBytes;
    byte[] masterFilenameBytes;
    boolean bUploadSlave;
    int offset;
    long body_len;
    bUploadSlave = ((group_name != null && group_name.length() > 0) && (master_filename != null && master_filename.length() > 0) && (prefix_name != null));
    if (bUploadSlave) {
        bNewStorageServer = this.newUpdatableStorageConnection(group_name, master_filename);
    } else {
        bNewStorageServer = this.newWritableStorageConnection(group_name);
    }
    try {
        connection = this.storageServer.getConnection();
        ext_name_bs = new byte[ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN];
        Arrays.fill(ext_name_bs, (byte) 0);
        if (file_ext_name != null && file_ext_name.length() > 0) {
            byte[] bs = file_ext_name.getBytes(ClientGlobal.g_charset);
            int ext_name_len = bs.length;
            if (ext_name_len > ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN) {
                ext_name_len = ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN;
            }
            System.arraycopy(bs, 0, ext_name_bs, 0, ext_name_len);
        }
        if (bUploadSlave) {
            masterFilenameBytes = master_filename.getBytes(ClientGlobal.g_charset);
            sizeBytes = new byte[2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE];
            body_len = sizeBytes.length + ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN + ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + masterFilenameBytes.length + file_size;
            hexLenBytes = ProtoCommon.long2buff(master_filename.length());
            System.arraycopy(hexLenBytes, 0, sizeBytes, 0, hexLenBytes.length);
            offset = hexLenBytes.length;
        } else {
            masterFilenameBytes = null;
            sizeBytes = new byte[1 + 1 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE];
            body_len = sizeBytes.length + ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + file_size;
            sizeBytes[0] = (byte) this.storageServer.getStorePathIndex();
            offset = 1;
        }
        hexLenBytes = ProtoCommon.long2buff(file_size);
        System.arraycopy(hexLenBytes, 0, sizeBytes, offset, hexLenBytes.length);
        OutputStream out = connection.getOutputStream();
        header = ProtoCommon.packHeader(cmd, body_len, (byte) 0);
        byte[] wholePkg = new byte[(int) (header.length + body_len - file_size)];
        System.arraycopy(header, 0, wholePkg, 0, header.length);
        System.arraycopy(sizeBytes, 0, wholePkg, header.length, sizeBytes.length);
        offset = header.length + sizeBytes.length;
        if (bUploadSlave) {
            byte[] prefix_name_bs = new byte[ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN];
            byte[] bs = prefix_name.getBytes(ClientGlobal.g_charset);
            int prefix_name_len = bs.length;
            Arrays.fill(prefix_name_bs, (byte) 0);
            if (prefix_name_len > ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN) {
                prefix_name_len = ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN;
            }
            if (prefix_name_len > 0) {
                System.arraycopy(bs, 0, prefix_name_bs, 0, prefix_name_len);
            }
            System.arraycopy(prefix_name_bs, 0, wholePkg, offset, prefix_name_bs.length);
            offset += prefix_name_bs.length;
        }
        System.arraycopy(ext_name_bs, 0, wholePkg, offset, ext_name_bs.length);
        offset += ext_name_bs.length;
        if (bUploadSlave) {
            System.arraycopy(masterFilenameBytes, 0, wholePkg, offset, masterFilenameBytes.length);
            offset += masterFilenameBytes.length;
        }
        out.write(wholePkg);
        if ((this.errno = (byte) callback.send(out)) != 0) {
            return null;
        }
        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);
        }
        new_group_name = new String(pkgInfo.body, 0, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN).trim();
        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;
        if (meta_list == null || meta_list.length == 0) {
            return results;
        }
        int result = 0;
        try {
            result = this.set_metadata(new_group_name, remote_filename, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_OVERWRITE);
        } catch (IOException ex) {
            result = 5;
            throw ex;
        } finally {
            if (result != 0) {
                this.errno = (byte) result;
                this.delete_file(new_group_name, remote_filename);
                return null;
            }
        }
        return results;
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        } finally {
            connection = null;
        }
        throw ex;
    } finally {
        releaseConnection(connection, bNewStorageServer);
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection)

Aggregations

Connection (com.zhouzifei.tool.common.fastdfs.pool.Connection)17 IOException (java.io.IOException)6 OutputStream (java.io.OutputStream)6 ServiceException (com.zhouzifei.tool.common.ServiceException)4