Search in sources :

Example 16 with Connection

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

the class TrackerClient method listGroups.

/**
 * list groups
 *
 * @param trackerServer the tracker server
 * @return group stat array, return null if fail
 */
public StructGroupStat[] listGroups(TrackerServer trackerServer) throws IOException, ServiceException {
    byte[] header;
    String ip_addr;
    int port;
    byte cmd;
    int out_len;
    byte store_path;
    Connection connection;
    if (trackerServer == null) {
        trackerServer = getTrackerServer();
        if (trackerServer == null) {
            return null;
        }
    }
    connection = trackerServer.getConnection();
    OutputStream out = connection.getOutputStream();
    try {
        header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_GROUP, 0, (byte) 0);
        out.write(header);
        ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        ProtoStructDecoder<StructGroupStat> decoder = new ProtoStructDecoder<StructGroupStat>();
        return decoder.decode(pkgInfo.body, StructGroupStat.class, StructGroupStat.getFieldsTotalSize());
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        } finally {
            connection = null;
        }
        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 17 with Connection

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

the class TrackerClient method getStoreStorage.

/**
 * query storage server to upload file
 *
 * @param trackerServer the tracker server
 * @param groupName     the group name to upload file to, can be empty
 * @return storage server object, return null if fail
 */
public StorageServer getStoreStorage(TrackerServer trackerServer, String groupName) throws IOException, ServiceException {
    byte[] header;
    String ip_addr;
    int port;
    byte cmd;
    int out_len;
    byte store_path;
    Connection connection;
    if (trackerServer == null) {
        trackerServer = getTrackerServer();
    }
    connection = trackerServer.getConnection();
    OutputStream out = connection.getOutputStream();
    try {
        if (groupName == null || groupName.length() == 0) {
            cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE;
            out_len = 0;
        } else {
            cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE;
            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, ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        ip_addr = new String(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, ProtoCommon.FDFS_IPADDR_SIZE - 1).trim();
        port = (int) ProtoCommon.buff2long(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ProtoCommon.FDFS_IPADDR_SIZE - 1);
        store_path = pkgInfo.body[ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN - 1];
        return new StorageServer(ip_addr, port, store_path);
    } 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)

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