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.yea.remote.netty.handle.AbstractHeartBeatServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Message message = (Message) msg;/* www. j a  va2 s  . com*/
    // ?
    if (message.getHeader() != null
            && message.getHeader().getType() == RemoteConstants.MessageType.HEARTBEAT_REQ.value()) {
        Message heartBeat = buildHeatBeatResp(message.getHeader().getSessionID(),
                (IdleState) message.getHeader().getAttachment().get(EVT_STATE));
        ctx.writeAndFlush(heartBeat);
    } else if (message.getHeader() != null
            && message.getHeader().getType() == RemoteConstants.MessageType.HEARTBEAT_RESP.value()) {
        if (((IdleState) message.getHeader().getAttachment().get(EVT_STATE)).equals(IdleState.READER_IDLE)) {
            mapHeartBeat.put(HEARTBEAT_READ_TIMEOUT, new Date().getTime());
            mapHeartBeat.put(HEARTBEAT_READ_RETRY, 0L);
        } else if (((IdleState) message.getHeader().getAttachment().get(EVT_STATE))
                .equals(IdleState.WRITER_IDLE)) {
            mapHeartBeat.put(HEARTBEAT_WRITE_TIMEOUT, new Date().getTime());
            mapHeartBeat.put(HEARTBEAT_WRITE_RETRY, 0L);
        }

    } else {
        //?
        mapHeartBeat.put(IDLE_TIME, new Date().getTime());
        ctx.fireChannelRead(msg);
    }

}

From source file:com.yea.remote.netty.handle.AbstractServiceHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    Message message = (Message) msg;//from  w  ww . j a  va  2  s .com
    if (message.getHeader() != null
            && (message.getHeader().getType() == RemoteConstants.MessageType.SERVICE_REQ.value()
                    || message.getHeader().getType() == RemoteConstants.MessageType.SERVICE_RESP.value())) {
        try {
            execute(ctx, message);
        } catch (Exception ex) {
            if (RemoteConstants.MessageType.SERVICE_RESP.value() == message.getHeader().getType()) {
                message.getHeader().setType(RemoteConstants.MessageType.SUSPEND_RESP.value());
                message.getHeader().setResult(RemoteConstants.MessageResult.FAILURE.value());
                message.setBody(ex);
            } else {
                message.getHeader().setType(RemoteConstants.MessageType.SUSPEND_REQ.value());
                message.getHeader().setResult(RemoteConstants.MessageResult.FAILURE.value());
                message.setBody(ex);
            }
            ctx.fireChannelRead(msg);
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.yea.remote.netty.handle.ExceptionHandler.java

License:Apache License

/**
 * Calls {@link ChannelHandlerContext#fireChannelRead(Object)} to forward to
 * the next {@link ChannelHandler} in the {@link ChannelPipeline}.
 * /*from w w w.  j  a va 2 s  .  co  m*/
 * Sub-classes may override this method to change behavior.
 */
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Message message = (Message) msg;
    if (message.getHeader() == null) {
        ctx.fireChannelRead(msg);
        return;
    }
    if (message.getHeader().getType() == RemoteConstants.MessageType.SUSPEND_REQ.value()) {
        LOGGER.info(ctx.channel().localAddress() + "" + ctx.channel().remoteAddress()
                + "???" + message.getBody());
        ctx.writeAndFlush(buildResp(message.getHeader().getResult(), message.getHeader().getSessionID(),
                (Date) message.getHeader().getAttachment()
                        .get(NettyConstants.MessageHeaderAttachment.HEADER_DATE.value()),
                new Date(), message.getBody(), (String) message.getHeader().getAttachment()
                        .get(NettyConstants.MessageHeaderAttachment.CALLBACK_ACT.value())));
    } else if (message.getHeader().getType() == RemoteConstants.MessageType.SUSPEND_RESP.value()) {
        LOGGER.info(ctx.channel().localAddress() + "" + ctx.channel().remoteAddress()
                + "????" + message.getBody());
        notifyObservers(message.getHeader().getSessionID(), message.getHeader(), message.getBody());
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.yea.remote.netty.handle.PingHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    Message message = (Message) msg;//from  w ww.  ja va 2  s  . c o m
    if (message.getHeader() != null
            && (message.getHeader().getType() == RemoteConstants.MessageType.PING_REQ.value()
                    || message.getHeader().getType() == RemoteConstants.MessageType.PING_RESP.value())) {
        try {
            execute(ctx, message);
        } catch (Exception ex) {
            if (RemoteConstants.MessageType.PING_RESP.value() == message.getHeader().getType()) {
                message.getHeader().setType(RemoteConstants.MessageType.SUSPEND_RESP.value());
                message.getHeader().setResult(RemoteConstants.MessageResult.FAILURE.value());
                message.setBody(ex);
            } else {
                message.getHeader().setType(RemoteConstants.MessageType.SUSPEND_REQ.value());
                message.getHeader().setResult(RemoteConstants.MessageResult.FAILURE.value());
                message.setBody(ex);
            }
            ctx.fireChannelRead(msg);
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.yea.remote.netty.handle.PingHandler.java

License:Apache License

protected void execute(ChannelHandlerContext ctx, Message message) throws Exception {
    // TODO Auto-generated method stub
    if (message.getHeader().getType() == RemoteConstants.MessageType.PING_REQ.value()) {
        Message respMessage = new Message();
        Header header = new Header();
        header.setType(RemoteConstants.MessageType.PING_RESP.value());
        header.setSessionID(message.getHeader().getSessionID());
        header.setResult(RemoteConstants.MessageResult.SUCCESS.value());
        respMessage.setHeader(header);/*from   ww w  . j av  a2s  .c om*/
        ctx.writeAndFlush(respMessage);
    } else if (message.getHeader().getType() == RemoteConstants.MessageType.PING_RESP.value()) {
        notifyObservers(message.getHeader().getSessionID(), message.getHeader(), Boolean.valueOf(true));
    } else {
        ctx.fireChannelRead(message);
    }
}

From source file:com.yea.remote.netty.server.handle.LoginAuthServerHandler.java

License:Apache License

/**
 * Calls {@link ChannelHandlerContext#fireChannelRead(Object)} to forward to
 * the next {@link ChannelHandler} in the {@link ChannelPipeline}.
 * //ww  w .  j  a  va2s. co m
 * Sub-classes may override this method to change behavior.
 */
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Message message = (Message) msg;

    // ?????
    if (message.getHeader() != null
            && message.getHeader().getType() == RemoteConstants.MessageType.LOGIN_REQ.value()) {
        String nodeIndex = ctx.channel().remoteAddress().toString();
        Message loginResp = null;
        // ???
        if (nodeCheck.containsKey(nodeIndex)) {
            loginResp = buildLoginResp(RemoteConstants.MessageResult.FAILURE.value());
        } else {
            InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
            String ip = address.getAddress().getHostAddress();
            boolean isOK = false;
            for (String WIP : ipList) {
                if (WIP.equals(ip)) {
                    isOK = true;
                    break;
                }
            }
            if (isOK) {
                if (!systemUser.containsKey(
                        message.getHeader().getAttachment().get(NettyConstants.LoginAuth.USERNAME.value()))) {
                    isOK = false;
                }
                if (!systemUser.containsValue(
                        message.getHeader().getAttachment().get(NettyConstants.LoginAuth.PASSWORD.value()))) {
                    isOK = false;
                }
            }
            loginResp = isOK ? buildLoginResp(RemoteConstants.MessageResult.SUCCESS.value())
                    : buildLoginResp(RemoteConstants.MessageResult.FAILURE.value());
            if (isOK)
                nodeCheck.put(nodeIndex, true);
        }
        ctx.writeAndFlush(loginResp);
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.yea.remote.netty.server.handle.ServiceServerHandler.java

License:Apache License

@Override
protected void execute(ChannelHandlerContext ctx, Message message) throws Exception {
    // TODO Auto-generated method stub
    if (message.getHeader().getType() == RemoteConstants.MessageType.SERVICE_REQ.value()) {
        long times = new Date().getTime() - ((Date) message.getHeader().getAttachment()
                .get(NettyConstants.MessageHeaderAttachment.HEADER_DATE.value())).getTime();
        if (times > 1500) {
            LOGGER.warn(/*from w  w  w.ja va 2s  .c  om*/
                    "{}{}[:{}]"
                            + ",:{}(:{})(),?:{}",
                    ctx.channel().localAddress(), ctx.channel().remoteAddress(),
                    UUIDGenerator.restore(message.getHeader().getSessionID()), times,
                    new Date().getTime() - ((Date) message.getHeader().getAttachment()
                            .get(NettyConstants.MessageHeaderAttachment.SEND_DATE.value())).getTime(),
                    message.getHeader().getLength());
        } else {
            LOGGER.info("{}{}[:{}]" + ",:{}(:{}),?:{}",
                    ctx.channel().localAddress(), ctx.channel().remoteAddress(),
                    UUIDGenerator.restore(message.getHeader().getSessionID()), times,
                    new Date().getTime() - ((Date) message.getHeader().getAttachment()
                            .get(NettyConstants.MessageHeaderAttachment.SEND_DATE.value())).getTime(),
                    message.getHeader().getLength());
        }

        pool.execute(new InnerTask(this.getApplicationContext(), ctx, message));
    } else {
        ctx.fireChannelRead(message);
    }
}

From source file:com.yeetor.server.handler.WSHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof FullHttpRequest) {
        FullHttpRequest req = (FullHttpRequest) msg;

        if (!req.getDecoderResult().isSuccess() || (!"websocket".equals(req.headers().get("Upgrade")))) {
            ctx.fireChannelRead(req);
            return;
        }//from w  w w  .  j av  a2 s  . com
        handleWSConnect(ctx, req);
    } else if (msg instanceof WebSocketFrame) {
        WebSocketFrame frame = (WebSocketFrame) msg;
        handlerWebSocketFrame(ctx, frame);
    }
    if (!autorelease)
        ReferenceCountUtil.release(msg);
}

From source file:com.zank.websocket.server.ServerHandler.java

License:Apache License

private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
    // Handle a bad request.
    if (!req.decoderResult().isSuccess()) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return;/*from   w w  w  .j  a  v  a  2 s .  c o  m*/
    }
    // Send the demo page and favicon.ico
    if ("/".equals(req.uri())) {
        ByteBuf content = WebSocketServerIndexPage.getContent(getWebSocketLocation(req));
        FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content);

        res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
        HttpHeaderUtil.setContentLength(res, content.readableBytes());

        sendHttpResponse(ctx, req, res);
        return;
    }
    if ("/favicon.ico".equals(req.uri())) {
        FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
        sendHttpResponse(ctx, req, res);
        return;
    }

    ctx.fireChannelRead(req.copy());
}

From source file:com.zank.websocket.server.ServerHandler.java

License:Apache License

private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
    ctx.fireChannelRead(frame.copy());
}