Example usage for io.netty.channel ChannelHandlerContext fireChannelActive

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

Introduction

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

Prototype

@Override
    ChannelHandlerContext fireChannelActive();

Source Link

Usage

From source file:org.redisson.client.handler.PingConnectionHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    RedisConnection connection = RedisConnection.getFrom(ctx.channel());
    connection.getConnectionPromise().onComplete((res, e) -> {
        if (e == null) {
            sendPing(ctx);//from  www.j av  a  2 s.c om
        }
    });
    ctx.fireChannelActive();
}

From source file:org.redisson.client.handler.RedisChannelInitializer.java

License:Apache License

private void initSsl(final RedisClientConfig config, Channel ch) throws KeyStoreException, IOException,
        NoSuchAlgorithmException, CertificateException, SSLException, UnrecoverableKeyException {
    if (!config.getAddress().isSsl()) {
        return;/*w  w w. j  ava  2  s  .c  o  m*/
    }

    io.netty.handler.ssl.SslProvider provided = io.netty.handler.ssl.SslProvider.JDK;
    if (config.getSslProvider() == SslProvider.OPENSSL) {
        provided = io.netty.handler.ssl.SslProvider.OPENSSL;
    }

    SslContextBuilder sslContextBuilder = SslContextBuilder.forClient().sslProvider(provided);
    if (config.getSslTruststore() != null) {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

        InputStream stream = config.getSslTruststore().openStream();
        try {
            char[] password = null;
            if (config.getSslTruststorePassword() != null) {
                password = config.getSslTruststorePassword().toCharArray();
            }
            keyStore.load(stream, password);
        } finally {
            stream.close();
        }

        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        sslContextBuilder.trustManager(trustManagerFactory);
    }

    if (config.getSslKeystore() != null) {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

        InputStream stream = config.getSslKeystore().openStream();
        char[] password = null;
        if (config.getSslKeystorePassword() != null) {
            password = config.getSslKeystorePassword().toCharArray();
        }
        try {
            keyStore.load(stream, password);
        } finally {
            stream.close();
        }

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, password);
        sslContextBuilder.keyManager(keyManagerFactory);
    }

    SSLParameters sslParams = new SSLParameters();
    if (config.isSslEnableEndpointIdentification()) {
        // TODO remove for JDK 1.7+
        try {
            Method method = sslParams.getClass().getDeclaredMethod("setEndpointIdentificationAlgorithm",
                    String.class);
            method.invoke(sslParams, "HTTPS");
        } catch (Exception e) {
            throw new SSLException(e);
        }
    } else {
        if (config.getSslTruststore() == null) {
            sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
        }
    }

    SslContext sslContext = sslContextBuilder.build();
    String hostname = config.getSslHostname();
    if (hostname == null || NetUtil.createByteArrayFromIpAddressString(hostname) != null) {
        hostname = config.getAddress().getHost();
    }

    SSLEngine sslEngine = sslContext.newEngine(ch.alloc(), hostname, config.getAddress().getPort());
    sslEngine.setSSLParameters(sslParams);

    SslHandler sslHandler = new SslHandler(sslEngine);
    ch.pipeline().addLast(sslHandler);
    ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

        volatile boolean sslInitDone;

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            if (sslInitDone) {
                super.channelActive(ctx);
            }
        }

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (!sslInitDone && (evt instanceof SslHandshakeCompletionEvent)) {
                SslHandshakeCompletionEvent e = (SslHandshakeCompletionEvent) evt;
                if (e.isSuccess()) {
                    sslInitDone = true;
                    ctx.fireChannelActive();
                } else {
                    RedisConnection connection = RedisConnection.getFrom(ctx.channel());
                    connection.getConnectionPromise().tryFailure(e.cause());
                }
            }

            super.userEventTriggered(ctx, evt);
        }

    });
}

From source file:org.rzo.netty.ahessian.auth.ClientAuthFilter.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    _token.sendPassword(ctx);//from w  w w. j ava  2 s .com
    ctx.fireChannelActive();
}

From source file:org.rzo.netty.ahessian.crypto.ClientCryptoFilterInbound.java

License:Apache License

private void sendSecretKey(ChannelHandlerContext ctx) {
    try {/*from  www . j a v a 2 s.  co  m*/
        // generate our secret key and iv and write it to a buffer
        byte[] symKeyEncoded = getSymKey();
        byte[] ivEncoded = getIv();
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        b.write(ivEncoded);
        b.write(symKeyEncoded);
        if (_data._password != null)
            b.write(_data._password);
        b.flush();

        System.out.println("generated iv+key: " + OutLogger.asString(b.toByteArray()));

        // encode it using the server's public key
        Cipher asymCipher = getAsymCipher();
        byte[] encryptedIvSymKey = asymCipher.doFinal(b.toByteArray());
        ByteBuf cb = Unpooled.buffer();
        cb.writeInt(encryptedIvSymKey.length);
        cb.writeBytes(encryptedIvSymKey);

        // send it to the server
        Channel channel = ctx.channel();
        // wait for the message transmission
        ctx.write(cb).sync();

        // we can now accept in/out messages encrypted with our key
        // first create symmetric ciphers
        _data._encodeCipher = StreamCipherFactory.createCipher(SYM_KEY_TYPE);
        _data._encodeCipher.engineInitEncrypt(symKeyEncoded, ivEncoded);

        _data._decodeCipher = StreamCipherFactory.createCipher(SYM_KEY_TYPE);
        _data._decodeCipher.engineInitDecrypt(symKeyEncoded, ivEncoded);

        // inform others in the pipeline that a secure connection has been
        // established
        ctx.fireChannelActive();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:org.rzo.netty.ahessian.crypto.ServerCryptoFilterInbound.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object e) throws Exception {
    // have we sent our secret key ?
    if (_decodeCipher != null) {
        ByteBuf m = Util.code(_decodeCipher, (ByteBuf) e, true);
        ctx.fireChannelRead(m);/*from   w  w  w . j a  v a2 s  .  co m*/
    } else {
        ByteBuf b = (ByteBuf) e;
        // is this our first message ?
        if (_cryptedIvKeyMessage == null) {
            int size = b.readInt();
            // consistency check, so we do not get an out of memory
            // exception
            if (size > 1024) {
                ctx.channel().close();
                return;
            }
            _cryptedIvKeyMessage = new byte[size];
        }
        // readin the client's secret key and iv
        int available = b.readableBytes();
        int toRead = Math.min(_cryptedIvKeyMessage.length - _bytesRead, available);
        b.readBytes(_cryptedIvKeyMessage, _bytesRead, toRead);
        _bytesRead += toRead;
        // we have completed receiption ?
        if (_bytesRead == _cryptedIvKeyMessage.length) {
            boolean ok = false;
            try {
                createCiphers();
                ok = true;
            } catch (Exception ex) {
                ex.printStackTrace();
                ctx.channel().close();
            }
            // inform pipline that we are ready for encrypted communication
            if (ok)
                ctx.fireChannelActive();
        }
    }
}

From source file:org.rzo.netty.ahessian.heartbeat.HeartbeatHandlerInbound.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ping();/* w  w w  .j  av a 2  s  . co m*/
    _ctx = ctx;
    _intervalTimer.setName(_name + ":" + _ctx.channel().hashCode());

    Constants.ahessianLogger
            .info("AbstractHeartBeatHandler scheduler started: " + _intervalTimer.getInterval());
    _intervalTimer.start();
    ctx.fireChannelActive();
}

From source file:org.rzo.netty.ahessian.io.OutputProducer.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    doChannelActive(ctx);
    ctx.fireChannelActive();
}

From source file:org.rzo.netty.ahessian.io.PullInputStreamConsumer.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    // if (_executor == null)
    {//from   w  w w .j  a  v a 2 s .c  o  m
        // _executor = Executors.newSingleThreadExecutor();

        _executor.execute(new Runnable() {
            public void run() {
                String tName = Thread.currentThread().getName();
                Thread.currentThread()
                        .setName("ahessian-PullInputStreamConsumer-#" + _threadCounter.incrementAndGet());
                _currentThread = Thread.currentThread();
                try {
                    waitForData();
                    while (!_stop) {
                        try {
                            _consumer.consume(_ctx, _inputStream);
                            // System.out.println("consumed "+System.currentTimeMillis());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        waitForData();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    Thread.currentThread().setName(tName);
                    // _threadCounter.decrementAndGet();
                }
            }
        });
    }

    if (_ctx != ctx)
        _ctx = ctx;
    InputStream in = InputStreamHandler.getInputStream(ctx);
    if (_inputStream != in) {
        _inputStream = in;
        ((InputStreamBuffer) _inputStream).setReadTimeout(-1);
    }
    _consumer.setContext(ctx);
    if (_waiting) {
        _lock.lock();
        try {
            _hasData.signal();
        } finally {
            _lock.unlock();
        }
    }
    ctx.fireChannelActive();
}

From source file:org.rzo.netty.ahessian.io.PushInputStreamConsumer.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    _lock.lock();//from  w  w w . j  a v a 2 s .c  o m
    try {
        _consumer.setContext(ctx);
    } finally {
        _lock.unlock();
    }
    ctx.fireChannelActive();
}

From source file:org.rzo.netty.ahessian.rpc.message.HessianRPCReplyEncoder.java

License:Apache License

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
        ChannelPromise promise) throws Exception {
    super.connect(ctx, remoteAddress, localAddress, promise);
    if (hOut == null) {
        OutputStream out = OutputStreamHandler.getOutputStream(ctx);
        hOut = new Hessian2Output(out);
        if (_serializerFactory != null)
            hOut.getSerializerFactory().addFactory(_serializerFactory);
    } else//from w  ww. j  av  a  2  s.com
        hOut.reset();
    ctx.fireChannelActive();
}