Search in sources :

Example 11 with BaseMessage

use of com.bonree.brfs.common.net.tcp.BaseMessage in project BRFS by zhangnianli.

the class TcpDiskNodeClient method writeData.

@Override
public WriteResult writeData(String path, byte[] bytes) throws IOException {
    try {
        WriteFileData data = new WriteFileData();
        data.setData(bytes);
        WriteFileMessage writeFileMessage = new WriteFileMessage();
        writeFileMessage.setFilePath(path);
        writeFileMessage.setDatas(new WriteFileData[] { data });
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_WRITE_FILE);
        message.setBody(ProtoStuffUtils.serialize(writeFileMessage));
        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) {
            WriteResultList resultList = ProtoStuffUtils.deserialize(response.getBody(), WriteResultList.class);
            WriteResult[] results = resultList.getWriteResults();
            if (results.length == 1) {
                return results[0];
            }
        }
    } catch (Exception e) {
        LOG.error("write file error", e);
    }
    return null;
}
Also used : IOException(java.io.IOException) BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) WriteFileData(com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileData) WriteFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage)

Example 12 with BaseMessage

use of com.bonree.brfs.common.net.tcp.BaseMessage in project BRFS by zhangnianli.

the class TcpDiskNodeClient method writeDatas.

@Override
public WriteResult[] writeDatas(String path, List<byte[]> dataList) throws IOException {
    try {
        WriteFileData[] datas = new WriteFileData[dataList.size()];
        for (int i = 0; i < datas.length; i++) {
            WriteFileData data = new WriteFileData();
            data.setData(dataList.get(i));
            datas[i] = data;
        }
        WriteFileMessage writeFileMessage = new WriteFileMessage();
        writeFileMessage.setFilePath(path);
        writeFileMessage.setDatas(datas);
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_WRITE_FILE);
        message.setBody(ProtoStuffUtils.serialize(writeFileMessage));
        CompletableFuture<BaseResponse> future = new CompletableFuture<BaseResponse>();
        LOG.debug("write [{}] datas to data node in file[{}]", dataList.size(), path);
        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) {
            WriteResultList resultList = ProtoStuffUtils.deserialize(response.getBody(), WriteResultList.class);
            return resultList.getWriteResults();
        }
    } catch (Exception e) {
        LOG.error("write file error", e);
    }
    return null;
}
Also used : IOException(java.io.IOException) BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) WriteFileData(com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileData) WriteFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage)

Example 13 with BaseMessage

use of com.bonree.brfs.common.net.tcp.BaseMessage in project BRFS by zhangnianli.

the class TcpDiskNodeClient method recover.

@Override
public boolean recover(String path, long fileLength, List<String> fullstates) {
    try {
        FileRecoveryMessage recoveryMessage = new FileRecoveryMessage();
        recoveryMessage.setFilePath(path);
        recoveryMessage.setOffset(fileLength);
        String[] states = new String[fullstates.size()];
        recoveryMessage.setSources(fullstates.toArray(states));
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_RECOVER_FILE);
        message.setBody(ProtoStuffUtils.serialize(recoveryMessage));
        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("get meta data from file [{}] error", path, e);
    }
    return false;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) FileRecoveryMessage(com.bonree.brfs.disknode.server.tcp.handler.data.FileRecoveryMessage) IOException(java.io.IOException)

Example 14 with BaseMessage

use of com.bonree.brfs.common.net.tcp.BaseMessage in project BRFS by zhangnianli.

the class TcpDiskNodeClient method openFile.

@Override
public long openFile(String path, long capacity) {
    try {
        OpenFileMessage openFileMessage = new OpenFileMessage();
        openFileMessage.setFilePath(path);
        openFileMessage.setCapacity(capacity);
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_OPEN_FILE);
        message.setBody(ProtoStuffUtils.serialize(openFileMessage));
        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 Longs.fromByteArray(response.getBody());
        }
    } catch (Exception e) {
        LOG.error("open file error", e);
    }
    return -1;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) OpenFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.OpenFileMessage) IOException(java.io.IOException)

Aggregations

BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)14 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)13 IOException (java.io.IOException)11 CompletableFuture (java.util.concurrent.CompletableFuture)10 TcpClientConfig (com.bonree.brfs.common.net.tcp.client.TcpClientConfig)2 WriteFileData (com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileData)2 WriteFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 TokenMessage (com.bonree.brfs.common.net.tcp.TokenMessage)1 ReadObject (com.bonree.brfs.common.net.tcp.file.ReadObject)1 AsyncFileReaderCreateConfig (com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig)1 FileContentPart (com.bonree.brfs.common.net.tcp.file.client.FileContentPart)1 Service (com.bonree.brfs.common.service.Service)1 TcpDiskNodeClient (com.bonree.brfs.disknode.client.TcpDiskNodeClient)1 DeleteFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.DeleteFileMessage)1 FileRecoveryMessage (com.bonree.brfs.disknode.server.tcp.handler.data.FileRecoveryMessage)1 ListFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage)1 OpenFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.OpenFileMessage)1 Bootstrap (io.netty.bootstrap.Bootstrap)1