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.codebullets.external.party.simulator.connections.websocket.inbound.NettyWebSocketServerHandler.java

License:Apache License

@Override
public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception {
    LOG.warn("Exception on web socket server channel name={}, id={}.", connectionName, ctx.channel().id(),
            cause);//  ww w .ja  v a 2s  .  c  om
    ctx.close();
}

From source file:com.codebullets.external.party.simulator.connections.websocket.outbound.KeepAliveHandler.java

License:Apache License

@Override
public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
    isAlive = false;//from w  ww.  j  a  v  a  2s.com

    // start reconnect
    final EventLoop loop = ctx.channel().eventLoop();
    loop.schedule(new Runnable() {
        @Override
        public void run() {
            outboundWebSocketConnection.openConnection();
        }
    }, RECONNECT_DELAY_SEC, TimeUnit.SECONDS);
}

From source file:com.codebullets.external.party.simulator.connections.websocket.outbound.NettyWebSocketClientHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    handshaker.handshake(ctx.channel());
}

From source file:com.codebullets.external.party.simulator.connections.websocket.outbound.NettyWebSocketClientHandler.java

License:Apache License

@Override
public void messageReceived(final ChannelHandlerContext ctx, final Object msg) throws Exception {
    Channel ch = ctx.channel();
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        LOG.info("WebSocket client {} connected.", connectionName);
        connectionMonitor.connectionEstablished(getContext(ctx));
        handshakeFuture.setSuccess();/*from  w  ww .j a v  a2  s.  co  m*/
        return;
    }

    if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content="
                + response.content().toString(CharsetUtil.UTF_8) + ')');
    }

    WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof TextWebSocketFrame) {
        TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
        LOG.debug("WebSocket client {} received message: " + textFrame.text(), connectionName);
        connectionMonitor.messageReceived(MessageReceivedEvent.create(getContext(ctx), textFrame.text()));
    } else if (frame instanceof BinaryWebSocketFrame) {
        ByteBuf buffer = Unpooled.buffer(frame.content().capacity());
        buffer.writeBytes(frame.content());
        byte[] data = buffer.array();
        LOG.debug("WebSocket client {} received buffer with length " + data.length, connectionName);
        connectionMonitor.messageReceived(MessageReceivedEvent.create(getContext(ctx), buffer));
    } else if (frame instanceof PingWebSocketFrame) {
        LOG.trace("WebSocket client {} received ping.", connectionName);
        ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
    } else if (frame instanceof CloseWebSocketFrame) {
        LOG.debug("WebSocket client {} received closing frame.", connectionName);
        ch.close();
    }
}

From source file:com.codnos.dbgp.internal.handlers.DBGpServerToClientConnectionHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    LOGGER.fine("Got connection from client!");
    channelGroup.add(ctx.channel());
    super.channelActive(ctx);
}

From source file:com.codnos.dbgp.internal.handlers.DBGpServerToClientConnectionHandler.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    LOGGER.fine("Client disconnected!");
    channelGroup.remove(ctx.channel());
    super.channelInactive(ctx);
}

From source file:com.corundumstudio.socketio.handler.AuthorizeHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    SchedulerKey key = new SchedulerKey(Type.PING_TIMEOUT, ctx.channel());
    disconnectScheduler.schedule(key, new Runnable() {
        @Override/*w ww . j  a  v a2s.co  m*/
        public void run() {
            ctx.channel().close();
            log.debug("Client with ip {} opens channel but not sended any data! Channel closed!",
                    ctx.channel().remoteAddress());
        }
    }, configuration.getFirstDataTimeout(), TimeUnit.MILLISECONDS);
    super.channelActive(ctx);
}

From source file:com.corundumstudio.socketio.handler.AuthorizeHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    SchedulerKey key = new SchedulerKey(Type.PING_TIMEOUT, ctx.channel());
    disconnectScheduler.cancel(key);//  www . java2s  . co  m

    if (msg instanceof FullHttpRequest) {
        FullHttpRequest req = (FullHttpRequest) msg;
        Channel channel = ctx.channel();
        QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri());

        if (!configuration.isAllowCustomRequests() && !queryDecoder.path().startsWith(connectPath)) {
            HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
            channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
            req.release();
            log.warn("Blocked wrong request! url: {}, ip: {}", queryDecoder.path(), channel.remoteAddress());
            return;
        }

        List<String> sid = queryDecoder.parameters().get("sid");
        if (queryDecoder.path().equals(connectPath) && sid == null) {
            String origin = req.headers().get(HttpHeaders.Names.ORIGIN);
            if (!authorize(ctx, channel, origin, queryDecoder.parameters(), req)) {
                req.release();
                return;
            }
            // forward message to polling or websocket handler to bind channel
        }
    }
    ctx.fireChannelRead(msg);
}

From source file:com.corundumstudio.socketio.handler.EncoderHandler.java

License:Apache License

private void write(XHROptionsMessage msg, ChannelHandlerContext ctx) {
    HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);

    HttpHeaders.addHeader(res, "Set-Cookie", "io=" + msg.getSessionId());
    HttpHeaders.addHeader(res, CONNECTION, KEEP_ALIVE);
    HttpHeaders.addHeader(res, ACCESS_CONTROL_ALLOW_HEADERS, CONTENT_TYPE);
    addOriginHeaders(ctx.channel(), res);

    ByteBuf out = encoder.allocateBuffer(ctx.alloc());
    sendMessage(msg, ctx.channel(), out, res);
}

From source file:com.corundumstudio.socketio.handler.EncoderHandler.java

License:Apache License

private void write(XHRPostMessage msg, ChannelHandlerContext ctx) {
    ByteBuf out = encoder.allocateBuffer(ctx.alloc());
    out.writeBytes(OK);/*from   w w w . j a  v a  2s.com*/
    sendMessage(msg, ctx.channel(), out, "text/html");
}