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.couchbase.client.core.endpoint.config.ConfigHandler.java

License:Apache License

/**
 * Decodes a {@link BucketStreamingResponse}.
 *
 * @param ctx the handler context.//  w  w  w. j  ava 2  s  .  com
 * @param header the received header.
 * @return a initialized {@link CouchbaseResponse}.
 */
private CouchbaseResponse handleBucketStreamingResponse(final ChannelHandlerContext ctx,
        final HttpResponse header) {
    SocketAddress addr = ctx.channel().remoteAddress();
    String host = addr instanceof InetSocketAddress ? ((InetSocketAddress) addr).getHostName()
            : addr.toString();
    ResponseStatus status = ResponseStatusConverter.fromHttp(header.getStatus().code());

    Observable<String> scheduledObservable = null;
    if (status.isSuccess()) {
        streamingConfigObservable = BehaviorSubject.create();
        scheduledObservable = streamingConfigObservable.onBackpressureBuffer().observeOn(env().scheduler());
    }
    return new BucketStreamingResponse(scheduledObservable, host, status, currentRequest());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueAuthHandler.java

License:Apache License

/**
 * Handles an incoming SASL list mechanisms response and dispatches the next SASL AUTH step.
 *
 * @param ctx the handler context.//w  w w. j  a  v a  2  s  .  c o m
 * @param msg the incoming message to investigate.
 * @throws Exception if something goes wrong during negotiation.
 */
private void handleListMechsResponse(ChannelHandlerContext ctx, FullBinaryMemcacheResponse msg)
        throws Exception {
    String remote = ctx.channel().remoteAddress().toString();
    String[] supportedMechanisms = msg.content().toString(CharsetUtil.UTF_8).split(" ");
    if (supportedMechanisms.length == 0) {
        throw new AuthenticationException("Received empty SASL mechanisms list from server: " + remote);
    }

    saslClient = Sasl.createSaslClient(supportedMechanisms, null, "couchbase", remote, null, this);
    selectedMechanism = saslClient.getMechanismName();
    int mechanismLength = selectedMechanism.length();
    byte[] bytePayload = saslClient.hasInitialResponse() ? saslClient.evaluateChallenge(new byte[] {}) : null;
    ByteBuf payload = bytePayload != null ? ctx.alloc().buffer().writeBytes(bytePayload)
            : Unpooled.EMPTY_BUFFER;

    FullBinaryMemcacheRequest initialRequest = new DefaultFullBinaryMemcacheRequest(
            selectedMechanism.getBytes(CharsetUtil.UTF_8), Unpooled.EMPTY_BUFFER, payload);
    initialRequest.setOpcode(SASL_AUTH_OPCODE).setKeyLength((short) mechanismLength)
            .setTotalBodyLength(mechanismLength + payload.readableBytes());

    ChannelFuture future = ctx.writeAndFlush(initialRequest);
    future.addListener(new GenericFutureListener<Future<Void>>() {
        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess()) {
                LOGGER.warn("Error during SASL Auth negotiation phase.", future);
            }
        }
    });
}

From source file:com.couchbase.client.core.io.endpoint.GenericEndpointHandler.java

License:Open Source License

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

    ctx.channel().eventLoop().scheduleAtFixedRate(new Runnable() {
        @Override/*from w  ww. jav a2  s.  c o m*/
        public void run() {
            ctx.flush();
        }
    }, 0, 75, TimeUnit.MICROSECONDS);
}

From source file:com.crystal.chat.SecureChatServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
    // Send the received message to all channels but the current one.
    System.out.println(">>>>>TCP>>>");
    processCommand(ctx.channel(), msg);
}

From source file:com.crystal.chat.SecureChatServerInitializer.java

License:Apache License

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    super.handlerAdded(ctx);
    ctx.channel().config().setOption(ChannelType.op, ChannelType.TYPE_TCP);
    SocketChannelBuffer.tcpchannels.add(ctx.channel());
}

From source file:com.demo.netty.echo.EchoServerHandler.java

License:Apache License

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    // Invoked when a Channel is registered to its EventLoop and able to handle IO
    super.channelRegistered(ctx);
    System.err.println("channel registered:" + ctx.channel().toString());
}

From source file:com.demo.netty.echo.EchoServerHandler.java

License:Apache License

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    super.channelUnregistered(ctx);
    System.err.println("channel unregistered:" + ctx.channel().toString());
}

From source file:com.demo.netty.echo.EchoServerHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    super.channelActive(ctx);
    // Invoked when a Channel is active, the Channel is connected/bound and ready
    System.err.println("channel active:" + ctx.channel().toString());
}

From source file:com.demo.netty.echo.EchoServerHandler.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    // Invoked when a Channel leaves active state is no longer connected to its remote peer
    super.channelInactive(ctx);
    System.err.println("channel inactive:" + ctx.channel().toString());
}

From source file:com.dempe.chat.connector.NettyUtils.java

License:Open Source License

public static Object getAttribute(ChannelHandlerContext ctx, AttributeKey<Object> key) {
    Attribute<Object> attr = ctx.channel().attr(key);
    return attr.get();
}