Search in sources :

Example 1 with ListFileMessage

use of com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage in project BRFS by zhangnianli.

the class TcpDiskNodeClient method listFiles.

@Override
public List<FileInfo> listFiles(String path, int level) {
    try {
        ListFileMessage listFileMessage = new ListFileMessage();
        listFileMessage.setPath(path);
        listFileMessage.setLevel(level);
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_LIST_FILE);
        message.setBody(ProtoStuffUtils.serialize(listFileMessage));
        CompletableFuture<BaseResponse> future = new CompletableFuture<BaseResponse>();
        client.sendMessage(message, new ResponseHandler<BaseResponse>() {

            @Override
            public void handle(BaseResponse response) {
                future.complete(response);
            }

            @Override
            public void error(Throwable e) {
                future.completeExceptionally(e);
            }
        });
        BaseResponse response = future.get();
        if (response != null && response.getCode() == ResponseCode.OK) {
            return JsonUtils.toObject(response.getBody(), new TypeReference<List<FileInfo>>() {
            });
        }
    } catch (Exception e) {
        LOG.error("list files of dir[{}] with level[{}] error", path, level, e);
    }
    return null;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) ListFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage) List(java.util.List) IOException(java.io.IOException)

Example 2 with ListFileMessage

use of com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage in project BRFS by zhangnianli.

the class ListFileMessageHandler method handleMessage.

@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
    ListFileMessage message = ProtoStuffUtils.deserialize(baseMessage.getBody(), ListFileMessage.class);
    if (message == null) {
        writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
        return;
    }
    String dirPath = null;
    try {
        dirPath = context.getConcreteFilePath(message.getPath());
        File dir = new File(dirPath);
        if (!dir.exists()) {
            writer.write(new BaseResponse(ResponseCode.ERROR));
            return;
        }
        if (!dir.isDirectory()) {
            writer.write(new BaseResponse(ResponseCode.ERROR));
            return;
        }
        FileInfo dirInfo = new FileInfo();
        dirInfo.setLevel(0);
        dirInfo.setType(FileInfo.TYPE_DIR);
        dirInfo.setPath(dirPath);
        fileList.addLast(dirInfo);
        ArrayList<FileInfo> fileInfoList = new ArrayList<FileInfo>();
        traverse(message.getLevel(), fileInfoList);
        BaseResponse response = new BaseResponse(ResponseCode.OK);
        response.setBody(JsonUtils.toJsonBytes(fileInfoList));
        writer.write(response);
    } catch (Exception e) {
        LOG.error("list dir[{}] error", dirPath, e);
        writer.write(new BaseResponse(ResponseCode.ERROR));
    }
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) FileInfo(com.bonree.brfs.disknode.server.handler.data.FileInfo) ArrayList(java.util.ArrayList) ListFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage) File(java.io.File)

Aggregations

BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)2 ListFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage)2 BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)1 FileInfo (com.bonree.brfs.disknode.server.handler.data.FileInfo)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1