Example usage for io.netty.channel ChannelHandlerContext fireChannelRead

List of usage examples for io.netty.channel ChannelHandlerContext fireChannelRead

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext fireChannelRead.

Prototype

@Override
    ChannelHandlerContext fireChannelRead(Object msg);

Source Link

Usage

From source file:com.baidu.rigel.biplatform.ma.file.client.monitor.FileServerMonitor.java

License:Open Source License

/**
 * {@inheritDoc}/*from   w  ww.ja va 2  s .c o m*/
 */
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    logger.info("receive message from server --> " + msg);
    if ("handshake".equals(msg)) {
        heartBeat = ctx.executor().scheduleAtFixedRate(new HeartBeatTask(ctx), 0, 1, TimeUnit.SECONDS);
    } else if ("pang".equals(msg)) {
        logger.info("Client receive server heart beat message : -->" + msg);
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.baidu.rigel.biplatform.ma.file.serv.HeartBeatRespHandler.java

License:Open Source License

/**
 * {@inheritDoc}/* w w  w.ja  va2  s .  c om*/
 */
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    logger.info("Receive message form client ---->" + msg);
    if ("handshake".equals(msg)) {
        ctx.writeAndFlush("handshake");
    } else if ("ping".equals(msg)) {
        ctx.writeAndFlush("pang");
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.baidu.rigel.biplatform.tesseract.netty.AbstractChannelInboundHandler.java

License:Open Source License

@Override
public final void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    AbstractMessage message = (AbstractMessage) msg;
    if (message.getMessageHeader() == null) {
        exceptionCaught(ctx, new MessageHeaderNullException());
        return;//  ww  w.  j a  va2s. co m
    } else if (!message.md5Check()) {
        exceptionCaught(ctx, new MessageDamagedException(message.toString()));
        return;
    }
    if (this.support(message.getMessageHeader().getAction())) {
        this.messageReceived(ctx, msg);

    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.barchart.netty.common.pipeline.MessageFlowHandler.java

License:BSD License

/**
 * Must be called by subclass when the flow completes. The simplest way to
 * do this is usually by using the OnComplete() state listener in your Flow.
 *
 * @see OnComplete//from  w w  w.j ava2 s  .co  m
 */
protected void complete(final ChannelHandlerContext ctx) throws Exception {

    log.debug("Flow complete, flushing inbound message queue");

    // Notify downstream that connection is active
    if (blockActivate) {
        super.channelActive(ctx);
    }

    Object msg;
    for (;;) {
        msg = inboundQueue.poll();
        if (msg == null) {
            break;
        }
        ctx.fireChannelRead(msg);
    }

    ctx.fireChannelReadComplete();

    ctx.pipeline().remove(this);

}

From source file:com.barchart.netty.common.pipeline.MessageFlowHandler.java

License:BSD License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {

    log.trace("Message received: " + msg);

    try {/*from  w w w.j  a  va 2s.  c om*/
        if (!messageReceived(msg)) {
            ctx.fireChannelRead(msg);
        }
    } catch (final Throwable t) {
        error(ctx, t);
    }

}

From source file:com.barchart.netty.common.pipeline.PingHandler.java

License:BSD License

@Override
public void channelRead0(final ChannelHandlerContext ctx, final Object msg) throws Exception {

    if (msg instanceof Ping) {

        ctx.write(new Pong() {

            @Override//from  w  w  w. jav a2s .c o m
            public long timestamp() {
                return System.currentTimeMillis();
            }

            @Override
            public long pinged() {
                return ((Ping) msg).timestamp();
            }

        });
        ctx.flush();

    } else if (msg instanceof Pong) {

        final Pong pong = (Pong) msg;

        // Compare the peer-received timestamp to current
        final long now = System.currentTimeMillis();
        latency = (now - pong.pinged()) / 2;
        clockSkew = pong.timestamp() - (now - latency);
        latencySampler.update(latency);

    }

    // Send upstream
    ctx.fireChannelRead(msg);

}

From source file:com.barchart.netty.common.pipeline.WebSocketConnectedNotifier.java

License:BSD License

@Override
public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception {

    if (evt == WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE
            || evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {

        ctx.fireChannelActive();/*w w  w.  jav a  2 s . c  om*/
        ctx.fireUserEventTriggered(evt);

        for (final Object msg : messages)
            ctx.fireChannelRead(msg);

        messages.clear();

        ctx.pipeline().remove(this);

    } else {

        ctx.fireUserEventTriggered(evt);

    }

}

From source file:com.barchart.netty.server.http.pipeline.HttpRequestChannelHandler.java

License:BSD License

private void handleWebSocket(final ChannelHandlerContext ctx, final FullHttpRequest msg) throws Exception {

    // Check if this path is a websocket
    final HandlerFactory<? extends ChannelHandler> factory = server.webSocketFactory(msg.getUri());

    if (factory == null) {

        sendServerError(ctx, new ServerException(HttpResponseStatus.NOT_IMPLEMENTED,
                "Websocket upgrade not available at this path"));

    } else {/* w w w .j  a v  a 2 s. c o  m*/

        // TODO log request
        // server.logger().access(request, 0);

        ctx.pipeline().addLast(
                // Handshaker
                new WebSocketServerProtocolHandler(msg.getUri()),
                // Fires channelActive() after handshake and removes self
                new WebSocketConnectedNotifier(),
                // BinaryWebSocketFrame <-> ByteBuf codec before user codecs
                new WebSocketBinaryCodec(),
                // Handlers should add any other codecs they need to the
                // pipeline using their handlerAdded() method.
                factory.newHandler());

        // Relay handshake to websocket handler
        ctx.fireChannelRead(msg);

        // Remove self from pipeline
        ctx.pipeline().remove(this);

    }

}

From source file:com.barchart.netty.server.pipeline.CapabilitiesHandler.java

License:BSD License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {

    if (msg instanceof Capabilities) {

        ctx.writeAndFlush(new Capabilities() {

            @Override/*from  w  w  w. j av a 2s. com*/
            public Set<String> capabilities() {
                return capabilities;
            }

            @Override
            public Version version() {
                return version;
            }

            @Override
            public Version minVersion() {
                return minVersion;
            }

        });

    } else if (msg instanceof VersionRequest) {

        final VersionRequest request = (VersionRequest) msg;

        final Version v = request.version();

        if (minVersion.lessThanOrEqual(v) && version.greaterThanOrEqual(v)) {

            activeVersion = v;

            ctx.writeAndFlush(new VersionResponse() {

                @Override
                public boolean success() {
                    return true;
                }

                @Override
                public Version version() {
                    return v;
                }

            });

        } else {

            ctx.writeAndFlush(new VersionResponse() {

                @Override
                public boolean success() {
                    return false;
                }

                @Override
                public Version version() {
                    return version;
                }

            });

        }

    } else {

        ctx.fireChannelRead(msg);

    }

}

From source file:com.barchart.netty.server.pipeline.NegotiationHandler.java

License:BSD License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {

    if (msg instanceof Capabilities) {

        ctx.writeAndFlush(new Capabilities() {

            @Override// w w  w . j av a2s  .co m
            public Set<String> capabilities() {
                return capabilities;
            }

            @Override
            public Version version() {
                return version;
            }

            @Override
            public Version minVersion() {
                return minVersion;
            }

        });

    } else if (msg instanceof VersionRequest) {

        final VersionRequest request = (VersionRequest) msg;

        final Version v = request.version();

        if (minVersion.lessThanOrEqual(v) && version.greaterThanOrEqual(v)) {

            activeVersion = v;

            ctx.writeAndFlush(new VersionResponse() {

                @Override
                public boolean success() {
                    return true;
                }

                @Override
                public Version version() {
                    return v;
                }

            });

        } else {

            ctx.writeAndFlush(new VersionResponse() {

                @Override
                public boolean success() {
                    return false;
                }

                @Override
                public Version version() {
                    return version;
                }

            });

        }

    } else if (msg instanceof StartTLS) {

        // TODO Use a specific SSL cert?
        final SSLEngine sslEngine = SSLContext.getDefault().createSSLEngine();
        sslEngine.setUseClientMode(false);

        final SslHandler handler = new SslHandler(sslEngine, true);

        handler.handshakeFuture().addListener(new GenericFutureListener<Future<Channel>>() {

            @Override
            public void operationComplete(final Future<Channel> future) throws Exception {

                if (future.isSuccess()) {

                    secure = true;

                } else {

                    secure = false;

                    // Failed, remove handler
                    ctx.pipeline().remove(SslHandler.class);

                }

            }

        });

        // Add SslHandler to pipeline
        ctx.pipeline().addFirst(handler);

        // Confirm start TLS, initiate handshake
        ctx.writeAndFlush(new StartTLS() {
        });

    } else {

        ctx.fireChannelRead(msg);

        // First non-negotiation message, we're done - clean up pipeline
        if (cleanup) {

            ctx.pipeline().remove(this);

            if (linked != null) {
                for (final ChannelHandler handler : linked) {
                    ctx.pipeline().remove(handler);
                }
            }

        }

    }

}