Search in sources :

Example 1 with BaseMessage

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

the class TcpDiskNodeClient method ping.

@Override
public boolean ping() {
    try {
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_PING_PONG);
        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("ping to server error", 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) IOException(java.io.IOException)

Example 2 with BaseMessage

use of com.bonree.brfs.common.net.tcp.BaseMessage 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 3 with BaseMessage

use of com.bonree.brfs.common.net.tcp.BaseMessage 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)

Example 4 with BaseMessage

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

the class AsyncTcpClientGroup method createClient.

@Override
public TcpClient<BaseMessage, BaseResponse> createClient(TcpClientConfig config, Executor executor) throws InterruptedException {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group);
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.connectTimeoutMillis());
    if (executor == null) {
        executor = new Executor() {

            @Override
            public void execute(Runnable command) {
                command.run();
            }
        };
    }
    AsyncTcpClient client = new AsyncTcpClient(executor);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new IdleStateHandler(0, DEFAULT_WRITE_IDLE_TIMEOUT_SECONDS, 0)).addLast(new BaseMessageEncoder()).addLast(new BaseResponseDecoder()).addLast(new SimpleChannelInboundHandler<TokenMessage<BaseResponse>>() {

                @Override
                protected void channelRead0(ChannelHandlerContext ctx, TokenMessage<BaseResponse> msg) throws Exception {
                    client.handle(msg.messageToken(), msg.message());
                }

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                    if (evt instanceof IdleStateEvent) {
                        IdleStateEvent e = (IdleStateEvent) evt;
                        if (e.state() == IdleState.WRITER_IDLE) {
                            ctx.writeAndFlush(new BaseMessage(-1));
                        }
                    }
                }
            });
        }
    });
    ChannelFuture future = bootstrap.connect(config.remoteAddress()).sync();
    if (!future.isSuccess()) {
        return null;
    }
    Channel channel = future.channel();
    channelList.add(channel);
    channel.closeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            channelList.remove(channel);
        }
    });
    LOG.info("create tcp client for {}", config.remoteAddress());
    client.attach(channel);
    return client;
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) TokenMessage(com.bonree.brfs.common.net.tcp.TokenMessage) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelFutureListener(io.netty.channel.ChannelFutureListener) IOException(java.io.IOException) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) Executor(java.util.concurrent.Executor) AdaptiveRecvByteBufAllocator(io.netty.channel.AdaptiveRecvByteBufAllocator) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 5 with BaseMessage

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

the class BaseMessageEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, TokenMessage<BaseMessage> msg, ByteBuf out) throws Exception {
    out.writeByte((byte) 0xBF);
    out.writeInt(msg.messageToken());
    BaseMessage baseMessage = msg.message();
    out.writeByte(baseMessage.getType());
    int length = baseMessage.getBody() == null ? 0 : baseMessage.getBody().length;
    out.writeInt(length);
    if (length > 0) {
        out.writeBytes(baseMessage.getBody());
    }
}
Also used : BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage)

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