Search in sources :

Example 1 with DeleteFileMessage

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

the class DeleteFileMessageHandler method handleMessage.

@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
    DeleteFileMessage message = ProtoStuffUtils.deserialize(baseMessage.getBody(), DeleteFileMessage.class);
    if (message == null) {
        writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
        return;
    }
    try {
        String filePath = diskContext.getConcreteFilePath(message.getFilePath());
        LOG.info("delete file[{}], force[{}], recursive[{}]", filePath, message.isForce(), message.isRecursive());
        File targetFile = new File(filePath);
        if (targetFile.isFile()) {
            try {
                closeFile(targetFile, message.isForce());
                writer.write(new BaseResponse(ResponseCode.OK));
            } catch (Exception e) {
                writer.write(new BaseResponse(ResponseCode.ERROR));
                return;
            }
        } else {
            try {
                closeDir(targetFile, message.isRecursive(), message.isForce());
                writer.write(new BaseResponse(ResponseCode.OK));
            } catch (Exception e) {
                writer.write(new BaseResponse(ResponseCode.ERROR));
                return;
            }
        }
    } catch (Exception e) {
        LOG.error("delete message error", e);
        writer.write(new BaseResponse(ResponseCode.ERROR));
    }
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) DeleteFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.DeleteFileMessage) File(java.io.File)

Example 2 with DeleteFileMessage

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

the class TcpDiskNodeClient method deleteDir.

@Override
public boolean deleteDir(String path, boolean force, boolean recursive) {
    try {
        DeleteFileMessage deleteFileMessage = new DeleteFileMessage();
        deleteFileMessage.setFilePath(path);
        deleteFileMessage.setForce(force);
        deleteFileMessage.setRecursive(recursive);
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_DELETE_FILE);
        message.setBody(ProtoStuffUtils.serialize(deleteFileMessage));
        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 true;
        }
    } catch (Exception e) {
        LOG.error("delete file [{}] error", path, e);
    }
    return false;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) DeleteFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.DeleteFileMessage) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) IOException(java.io.IOException)

Aggregations

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