Search in sources :

Example 1 with Connection

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

the class TrackerClient method getStorages.

/**
 * query storage server to download file
 *
 * @param trackerServer the tracker server
 * @param cmd           command code, ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE or
 *                      ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE
 * @param groupName     the group name of storage server
 * @param filename      filename on storage server
 * @return storage server Socket object, return null if fail
 */
protected ServerInfo[] getStorages(TrackerServer trackerServer, byte cmd, String groupName, String filename) throws IOException, ServiceException {
    byte[] header;
    byte[] bFileName;
    byte[] bGroupName;
    byte[] bs;
    int len;
    String ip_addr;
    int port;
    Connection connection;
    if (trackerServer == null) {
        trackerServer = getTrackerServer();
        if (trackerServer == null) {
            return null;
        }
    }
    connection = trackerServer.getConnection();
    OutputStream out = connection.getOutputStream();
    try {
        bs = groupName.getBytes(ClientGlobal.g_charset);
        bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
        bFileName = filename.getBytes(ClientGlobal.g_charset);
        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);
        header = ProtoCommon.packHeader(cmd, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + bFileName.length, (byte) 0);
        byte[] wholePkg = new byte[header.length + bGroupName.length + bFileName.length];
        System.arraycopy(header, 0, wholePkg, 0, header.length);
        System.arraycopy(bGroupName, 0, wholePkg, header.length, bGroupName.length);
        System.arraycopy(bFileName, 0, wholePkg, header.length + bGroupName.length, bFileName.length);
        out.write(wholePkg);
        ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        if (pkgInfo.body.length < ProtoCommon.TRACKER_QUERY_STORAGE_FETCH_BODY_LEN) {
            throw new IOException("Invalid body length: " + pkgInfo.body.length);
        }
        if ((pkgInfo.body.length - ProtoCommon.TRACKER_QUERY_STORAGE_FETCH_BODY_LEN) % (ProtoCommon.FDFS_IPADDR_SIZE - 1) != 0) {
            throw new IOException("Invalid body length: " + pkgInfo.body.length);
        }
        int server_count = 1 + (pkgInfo.body.length - ProtoCommon.TRACKER_QUERY_STORAGE_FETCH_BODY_LEN) / (ProtoCommon.FDFS_IPADDR_SIZE - 1);
        ip_addr = new String(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, ProtoCommon.FDFS_IPADDR_SIZE - 1).trim();
        int offset = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ProtoCommon.FDFS_IPADDR_SIZE - 1;
        port = (int) ProtoCommon.buff2long(pkgInfo.body, offset);
        offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
        ServerInfo[] servers = new ServerInfo[server_count];
        servers[0] = new ServerInfo(ip_addr, port);
        for (int i = 1; i < server_count; i++) {
            servers[i] = new ServerInfo(new String(pkgInfo.body, offset, ProtoCommon.FDFS_IPADDR_SIZE - 1).trim(), port);
            offset += ProtoCommon.FDFS_IPADDR_SIZE - 1;
        }
        return servers;
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        } finally {
            connection = null;
        }
        throw ex;
    } finally {
        if (connection != null) {
            try {
                connection.release();
            } catch (IOException ex1) {
                ex1.printStackTrace();
            }
        }
    }
}
Also used : OutputStream(java.io.OutputStream) Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection) IOException(java.io.IOException)

Example 2 with Connection

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

the class TrackerClient method listStorages.

/**
 * query storage server stat info of the group
 *
 * @param trackerServer the tracker server
 * @param groupName     the group name of storage server
 * @param storageIpAddr the storage server ip address, can be null or empty
 * @return storage server stat array, return null if fail
 */
public StructStorageStat[] listStorages(TrackerServer trackerServer, String groupName, String storageIpAddr) throws IOException, ServiceException {
    byte[] header;
    byte[] bGroupName;
    byte[] bs;
    int len;
    Connection connection;
    if (trackerServer == null) {
        trackerServer = getTrackerServer();
        if (trackerServer == null) {
            return null;
        }
    }
    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;
        if (storageIpAddr != null && storageIpAddr.length() > 0) {
            bIpAddr = storageIpAddr.getBytes(ClientGlobal.g_charset);
            if (bIpAddr.length < ProtoCommon.FDFS_IPADDR_SIZE) {
                ipAddrLen = bIpAddr.length;
            } else {
                ipAddrLen = ProtoCommon.FDFS_IPADDR_SIZE - 1;
            }
        } else {
            bIpAddr = null;
            ipAddrLen = 0;
        }
        header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_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);
        if (ipAddrLen > 0) {
            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, -1);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        ProtoStructDecoder<StructStorageStat> decoder = new ProtoStructDecoder<StructStorageStat>();
        return decoder.decode(pkgInfo.body, StructStorageStat.class, StructStorageStat.getFieldsTotalSize());
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
        throw ex;
    } catch (Exception ex) {
        ex.printStackTrace();
        this.errno = ProtoCommon.ERR_NO_EINVAL;
        return null;
    } finally {
        if (connection != null) {
            try {
                connection.release();
            } catch (IOException ex1) {
                ex1.printStackTrace();
            }
        }
    }
}
Also used : OutputStream(java.io.OutputStream) Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection) IOException(java.io.IOException) ServiceException(com.zhouzifei.tool.common.ServiceException) IOException(java.io.IOException)

Example 3 with Connection

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

the class TrackerClient method getStoreStorages.

/**
 * query storage servers to upload file
 *
 * @param trackerServer the tracker server
 * @param groupName     the group name to upload file to, can be empty
 * @return storage servers, return null if fail
 */
public StorageServer[] getStoreStorages(TrackerServer trackerServer, String groupName) throws IOException, ServiceException {
    byte[] header;
    String ip_addr;
    int port;
    byte cmd;
    int out_len;
    Connection connection;
    if (trackerServer == null) {
        trackerServer = getTrackerServer();
        if (trackerServer == null) {
            return null;
        }
    }
    connection = trackerServer.getConnection();
    OutputStream out = connection.getOutputStream();
    try {
        if (groupName == null || groupName.length() == 0) {
            cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL;
            out_len = 0;
        } else {
            cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL;
            out_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
        }
        header = ProtoCommon.packHeader(cmd, out_len, (byte) 0);
        out.write(header);
        if (groupName != null && groupName.length() > 0) {
            byte[] bGroupName;
            byte[] bs;
            int group_len;
            bs = groupName.getBytes(ClientGlobal.g_charset);
            bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
            if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) {
                group_len = bs.length;
            } else {
                group_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
            }
            Arrays.fill(bGroupName, (byte) 0);
            System.arraycopy(bs, 0, bGroupName, 0, group_len);
            out.write(bGroupName);
        }
        ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        if (pkgInfo.body.length < ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN) {
            this.errno = ProtoCommon.ERR_NO_EINVAL;
            return null;
        }
        int ipPortLen = pkgInfo.body.length - (ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1);
        final int recordLength = ProtoCommon.FDFS_IPADDR_SIZE - 1 + ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
        if (ipPortLen % recordLength != 0) {
            this.errno = ProtoCommon.ERR_NO_EINVAL;
            return null;
        }
        int serverCount = ipPortLen / recordLength;
        if (serverCount > 16) {
            this.errno = ProtoCommon.ERR_NO_ENOSPC;
            return null;
        }
        StorageServer[] results = new StorageServer[serverCount];
        byte store_path = pkgInfo.body[pkgInfo.body.length - 1];
        int offset = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
        for (int i = 0; i < serverCount; i++) {
            ip_addr = new String(pkgInfo.body, offset, ProtoCommon.FDFS_IPADDR_SIZE - 1).trim();
            offset += ProtoCommon.FDFS_IPADDR_SIZE - 1;
            port = (int) ProtoCommon.buff2long(pkgInfo.body, offset);
            offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
            results[i] = new StorageServer(ip_addr, port, store_path);
        }
        return results;
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        } finally {
            connection = null;
        }
        throw ex;
    } finally {
        if (connection != null) {
            try {
                connection.release();
            } catch (IOException ex1) {
                ex1.printStackTrace();
            }
        }
    }
}
Also used : OutputStream(java.io.OutputStream) Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection) IOException(java.io.IOException)

Example 4 with Connection

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

the class StorageClient method query_file_info.

/**
 * get file info from storage server
 *
 * @param group_name      the group name of storage server
 * @param remote_filename filename on storage server
 * @return FileInfo object for success, return null for fail
 */
public FileInfo query_file_info(String group_name, String remote_filename) throws IOException, ServiceException {
    boolean bNewStorageServer = this.newUpdatableStorageConnection(group_name, remote_filename);
    Connection connection = this.storageServer.getConnection();
    try {
        byte[] header;
        byte[] groupBytes;
        byte[] filenameBytes;
        byte[] bs;
        int groupLen;
        ProtoCommon.RecvPackageInfo pkgInfo;
        filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset);
        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_QUERY_FILE_INFO, +groupBytes.length + filenameBytes.length, (byte) 0);
        OutputStream out = connection.getOutputStream();
        byte[] wholePkg = new byte[header.length + groupBytes.length + filenameBytes.length];
        System.arraycopy(header, 0, wholePkg, 0, header.length);
        System.arraycopy(groupBytes, 0, wholePkg, header.length, groupBytes.length);
        System.arraycopy(filenameBytes, 0, wholePkg, header.length + groupBytes.length, filenameBytes.length);
        out.write(wholePkg);
        pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + ProtoCommon.FDFS_IPADDR_SIZE);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        long file_size = ProtoCommon.buff2long(pkgInfo.body, 0);
        int create_timestamp = (int) ProtoCommon.buff2long(pkgInfo.body, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
        int crc32 = (int) ProtoCommon.buff2long(pkgInfo.body, 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
        String source_ip_addr = (new String(pkgInfo.body, 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE, ProtoCommon.FDFS_IPADDR_SIZE)).trim();
        return new FileInfo(true, FileInfo.FILE_TYPE_NORMAL, file_size, create_timestamp, crc32, source_ip_addr);
    } 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 5 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 callback        call callback.recv() when data arrive
 * @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, DownloadCallback callback) throws IOException, ServiceException {
    int result;
    boolean bNewStorageServer = this.newReadableStorageConnection(group_name, remote_filename);
    Connection connection = this.storageServer.getConnection();
    try {
        ProtoCommon.RecvHeaderInfo header;
        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[2 * 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);
            }
            if ((result = callback.recv(header.body_len, buff, bytes)) != 0) {
                this.errno = (byte) result;
                return result;
            }
            remainBytes -= bytes;
        // System.out.println("totalBytes=" + (header.body_len - remainBytes));
        }
        return 0;
    } 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)

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