Search in sources :

Example 1 with TokenMessage

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

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

the class AbstractTcpClient method sendMessage.

@Override
public void sendMessage(S msg, ResponseHandler<R> handler) throws Exception {
    Preconditions.checkNotNull(msg);
    Preconditions.checkNotNull(handler);
    final int token = tokenMaker.getAndIncrement() & Integer.MAX_VALUE;
    handlers.put(token, handler);
    channel.writeAndFlush(new TokenMessage<S>() {

        @Override
        public int messageToken() {
            return token;
        }

        @Override
        public S message() {
            return msg;
        }
    }).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                handlers.remove(token);
                executor.execute(new Runnable() {

                    @Override
                    public void run() {
                        handler.error(new Exception("send message of token[" + token + "] error"));
                    }
                });
                channel.close();
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) TokenMessage(com.bonree.brfs.common.net.tcp.TokenMessage) ChannelFutureListener(io.netty.channel.ChannelFutureListener) IOException(java.io.IOException)

Aggregations

TokenMessage (com.bonree.brfs.common.net.tcp.TokenMessage)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 IOException (java.io.IOException)2 BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)1 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 AdaptiveRecvByteBufAllocator (io.netty.channel.AdaptiveRecvByteBufAllocator)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)1 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)1 Executor (java.util.concurrent.Executor)1