Example usage for io.netty.channel ChannelHandlerContext pipeline

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

Introduction

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

Prototype

ChannelPipeline pipeline();

Source Link

Document

Return the assigned ChannelPipeline

Usage

From source file:SecureChatServerHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) {
    // Once session is secured, send a greeting and register the channel to the global channel
    // list so the channel received the messages from others.
    ctx.pipeline().get(SslHandler.class).handshakeFuture()
            .addListener(new GenericFutureListener<Future<Channel>>() {
                @Override/* www .  ja  va2  s.c  om*/
                public void operationComplete(Future<Channel> future) throws Exception {
                    ctx.writeAndFlush("Welcome to " + InetAddress.getLocalHost().getHostName()
                            + " secure chat service!\n");
                    ctx.writeAndFlush("Your session is protected by "
                            + ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite()
                            + " cipher suite.\n");

                    channels.add(ctx.channel());
                }
            });
}

From source file:at.yawk.dbus.protocol.auth.AuthClient.java

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    for (ChannelHandler handler : handlers) {
        ctx.pipeline().addBefore(ctx.executor(), ctx.name(), null, handler);
    }//  ww w. j  a v  a  2  s  . c  o m
    super.handlerAdded(ctx);
}

From source file:at.yawk.dbus.protocol.auth.AuthClient.java

@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    for (ChannelHandler handler : handlers) {
        ctx.pipeline().remove(handler);
    }//from ww  w . j ava  2s.c o m
    super.handlerRemoved(ctx);
}

From source file:at.yawk.dbus.protocol.codec.DbusMainProtocol.java

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    add(ctx, new ByteCollector());

    add(ctx, new MessageHeaderCodec());

    add(ctx, new BodyDecoder());
    add(ctx, new BodyEncoder());

    add(ctx, new IncomingMessageAdapter(consumer));

    ctx.pipeline().remove(this);
}

From source file:at.yawk.dbus.protocol.codec.DbusMainProtocol.java

private void add(ChannelHandlerContext ctx, ChannelHandler handler) {
    ctx.pipeline().addBefore(ctx.executor(), ctx.name(), null, handler);
}

From source file:books.netty.ssl.SecureChatServerHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) {
    // Once session is secured, send a greeting and register the channel to
    // the global channel
    // list so the channel received the messages from others.
    ctx.pipeline().get(SslHandler.class).handshakeFuture()
            .addListener(new GenericFutureListener<Future<Channel>>() {
                @Override/*from   w ww. j  a v a 2s.  c om*/
                public void operationComplete(Future<Channel> future) throws Exception {
                    ctx.writeAndFlush("Welcome to " + InetAddress.getLocalHost().getHostName()
                            + " secure chat service!\n");
                    ctx.writeAndFlush("Your session is protected by "
                            + ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite()
                            + " cipher suite.\n");

                    channels.add(ctx.channel());
                }
            });
}

From source file:ca.lambtoncollege.netty.chat.SecureChatServerHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) {
    // Once session is secured, send a greeting and register the channel to the global channel
    // list so the channel received the messages from others.
    ctx.pipeline().get(SslHandler.class).handshakeFuture()
            .addListener(new GenericFutureListener<Future<Channel>>() {
                @Override/*from ww w.ja  v a 2s  .com*/
                public void operationComplete(Future<Channel> future) throws Exception {
                    ctx.writeAndFlush("Welcome to " + InetAddress.getLocalHost().getHostName()
                            + " secure chat service!\n");
                    ctx.writeAndFlush("Your session is protected by "
                            + ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite()
                            + " cipher suite.\n");
                    channels.add(ctx.channel());
                }
            });
}

From source file:cc.agentx.client.net.nio.Socks5Handler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, SocksRequest request) throws Exception {
    switch (request.protocolVersion()) {
    case SOCKS4a:
        log.warn("\tBad Handshake! (protocol version not supported: 4)");
        ctx.write(new SocksInitResponse(SocksAuthScheme.UNKNOWN));
        if (ctx.channel().isActive()) {
            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
        }/*  w w w  .  j  a v a  2s .com*/
        break;
    case SOCKS5:
        switch (request.requestType()) {
        case INIT:
            ctx.pipeline().addFirst(new SocksCmdRequestDecoder());
            ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH));
            break;
        case AUTH:
            ctx.pipeline().addFirst(new SocksCmdRequestDecoder());
            ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS));
            break;
        case CMD:
            if (((SocksCmdRequest) request).cmdType() == SocksCmdType.CONNECT) {
                ctx.pipeline().addLast(new XConnectHandler());
                ctx.pipeline().remove(this);
                ctx.fireChannelRead(request);
            } else {
                ctx.close();
                log.warn("\tBad Handshake! (command not support: {})", ((SocksCmdRequest) request).cmdType());
            }
            break;
        case UNKNOWN:
            log.warn("\tBad Handshake! (unknown request type)");
        }
        break;
    case UNKNOWN:
        log.warn("\tBad Handshake! (protocol version not support: {}", request.protocolVersion());
        ctx.close();
        break;
    }
}

From source file:cc.agentx.client.net.nio.XConnectHandler.java

License:Apache License

@Override
public void channelRead0(final ChannelHandlerContext ctx, final SocksCmdRequest request) throws Exception {
    boolean proxyMode = isAgentXNeeded(request.host());
    log.info("\tClient -> Proxy           \tTarget {}:{} [{}]", request.host(), request.port(),
            proxyMode ? "AGENTX" : "DIRECT");
    Promise<Channel> promise = ctx.executor().newPromise();
    promise.addListener(new FutureListener<Channel>() {
        @Override// w  w w .  j a  v  a 2 s.c o m
        public void operationComplete(final Future<Channel> future) throws Exception {
            final Channel outboundChannel = future.getNow();
            if (future.isSuccess()) {
                ctx.channel().writeAndFlush(new SocksCmdResponse(SocksCmdStatus.SUCCESS, request.addressType()))
                        .addListener(channelFuture -> {
                            ByteBuf byteBuf = Unpooled.buffer();
                            request.encodeAsByteBuf(byteBuf);
                            if (byteBuf.hasArray()) {
                                byte[] xRequestBytes = new byte[byteBuf.readableBytes()];
                                byteBuf.getBytes(0, xRequestBytes);

                                if (proxyMode) {
                                    // handshaking to remote proxy
                                    xRequestBytes = requestWrapper.wrap(xRequestBytes);
                                    outboundChannel.writeAndFlush(Unpooled.wrappedBuffer(
                                            exposeRequest ? xRequestBytes : wrapper.wrap(xRequestBytes)));
                                }

                                // task handover
                                ReferenceCountUtil.retain(request); // auto-release? a trap?
                                ctx.pipeline().remove(XConnectHandler.this);
                                outboundChannel.pipeline().addLast(new XRelayHandler(ctx.channel(),
                                        proxyMode ? wrapper : rawWrapper, false));
                                ctx.pipeline().addLast(new XRelayHandler(outboundChannel,
                                        proxyMode ? wrapper : rawWrapper, true));
                            }
                        });
            } else {
                ctx.channel()
                        .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()));

                if (ctx.channel().isActive()) {
                    ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
                }
            }
        }
    });

    String host = request.host();
    int port = request.port();
    if (host.equals(config.getConsoleDomain())) {
        host = "localhost";
        port = config.getConsolePort();
    } else if (proxyMode) {
        host = config.getServerHost();
        port = config.getServerPort();
    }

    // ping target
    bootstrap.group(ctx.channel().eventLoop()).channel(NioSocketChannel.class)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true)
            .handler(new XPingHandler(promise, System.currentTimeMillis())).connect(host, port)
            .addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        ctx.channel().writeAndFlush(
                                new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()));
                        if (ctx.channel().isActive()) {
                            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
                        }
                    }
                }
            });
}

From source file:cc.agentx.client.net.nio.XPingHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    log.info("\t          Proxy <- Target \tPong ~{}ms", System.currentTimeMillis() - initializeTimeMillis);
    ctx.pipeline().remove(this);
    promise.setSuccess(ctx.channel());/*from  w  w  w  . j a  v a2s.c om*/
}