Example usage for io.netty.channel ChannelHandlerContext channel

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

Introduction

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

Prototype

Channel channel();

Source Link

Document

Return the Channel which is bound to the ChannelHandlerContext .

Usage

From source file:com.caricah.iotracah.server.httpserver.netty.HttpServerHandler.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

    try {/*w  ww.j a  v a  2  s  .  c  o m*/
        log.info(" exceptionCaught : Unhandled exception: ", cause);

        JSONObject error = new JSONObject();
        error.put("message", cause.getMessage());
        error.put("status", "failure");

        ByteBuf buffer = Unpooled.copiedBuffer(error.toString(), CharsetUtil.UTF_8);

        // Build the response object.
        FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                HttpResponseStatus.INTERNAL_SERVER_ERROR, buffer);

        ctx.channel().writeAndFlush(httpResponse).addListener(ChannelFutureListener.CLOSE);
    } catch (Exception ex) {
        log.debug(" exceptionCaught : trying to close socket because we got an unhandled exception", ex);
    }

}

From source file:com.caricah.iotracah.server.mqttserver.netty.MqttServerHandler.java

License:Apache License

/**
 * Is called for each message of type {@link MqttMessage}.
 *
 * @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
 *            belongs to/* www . ja  va  2s  .co m*/
 * @param msg the message to handle
 * @throws Exception is thrown if an error occurred
 */
@Override
protected void messageReceived(ChannelHandlerContext ctx, MqttMessage msg) throws Exception {

    log.debug(" messageReceived : received the message {}", msg);

    String connectionId = ctx.channel().attr(ServerImpl.REQUEST_CONNECTION_ID).get();
    String sessionId = ctx.channel().attr(ServerImpl.REQUEST_SESSION_ID).get();

    getInternalServer().pushToWorker(connectionId, sessionId, msg);
}

From source file:com.caricah.iotracah.server.mqttserver.netty.MqttServerHandler.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

    try {//from   www.  j  a va  2 s.c  o m
        log.info(" exceptionCaught : Unhandled exception: ", cause);

        getServerImpl().closeClient(ctx.channel().id().asLongText());

    } catch (Exception ex) {
        log.debug(" exceptionCaught : trying to close socket because we got an unhandled exception", ex);
    }

}

From source file:com.caricah.iotracah.server.netty.ServerHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    Channel channel = ctx.channel();
    ChannelGroup channelGroup = getServerImpl().getChannelGroup();

    ctx.channel().attr(ServerImpl.REQUEST_CONNECTION_ID).set(channel.id().asLongText());

    channelGroup.add(channel);//w  ww  .j  av a2s  . c  o m
    super.channelActive(ctx);
}

From source file:com.caricah.iotracah.server.netty.ServerHandler.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    super.channelInactive(ctx);

    String sessionId = ctx.channel().attr(ServerImpl.REQUEST_SESSION_ID).get();

    if (null != sessionId) {

        String connectionId = ctx.channel().attr(ServerImpl.REQUEST_CONNECTION_ID).get();

        getInternalServer().dirtyDisconnect(connectionId, sessionId);

    }/*  ww w. ja  v  a2 s . c  o  m*/
}

From source file:com.carlos.netty.server.ObjectEchoServerHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) {
    channels.add(ctx.channel());
}

From source file:com.changxx.phei.netty.protocol.websocket.server.WebSocketServerHandler.java

License:Apache License

private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {

    // HTTP?HHTP//from ww  w .  j  av a 2  s  .c  om
    if (!req.getDecoderResult().isSuccess() || (!"websocket".equals(req.headers().get("Upgrade")))) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return;
    }

    // ??
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
            "ws://localhost:8080/websocket", null, false);
    handshaker = wsFactory.newHandshaker(req);
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel());
    } else {
        handshaker.handshake(ctx.channel(), req);
    }
}

From source file:com.chenyang.proxy.http.HttpIdleHandler.java

License:Apache License

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

    if (evt instanceof IdleStateEvent) {
        logger.warn(" time out and close");
        ctx.channel().close();
    }/*from w ww.j  ava  2s.c  o m*/
}

From source file:com.chenyang.proxy.http.HttpRelayHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {

    if (!ctx.channel().config().getOption(ChannelOption.AUTO_READ)) {
        ctx.read();//w w w.j  a  v  a  2s  .  c o  m
    }
}

From source file:com.chenyang.proxy.http.HttpRelayHandler.java

License:Apache License

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

    if (relayChannel.isActive()) {
        relayChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
            @Override//from  w w  w  .j  a  v a 2s.  com
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!ctx.channel().config().getOption(ChannelOption.AUTO_READ)) {
                    ctx.read();
                }
            }
        });
    } else {
        ReferenceCountUtil.release(msg);
    }

}