Example usage for io.netty.channel ChannelHandlerContext fireChannelInactive

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

Introduction

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

Prototype

@Override
    ChannelHandlerContext fireChannelInactive();

Source Link

Usage

From source file:org.opendaylight.controller.netconf.nettyutil.handler.ssh.virtualsocket.ChannelInputStream.java

License:Open Source License

public void channelInactive(ChannelHandlerContext ctx) {
    ctx.fireChannelInactive();
}

From source file:org.opendaylight.controller.netconf.ssh.SshProxyClientHandler.java

License:Open Source License

@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    writeAdditionalHeader(ctx);//  w w w .  j  a  v  a  2  s.c  o m

    asyncSshHandlerWriter = new AsyncSshHandlerWriter(out);
    asyncSshHandlerReader = new AsyncSshHandlerReader(new AutoCloseable() {
        @Override
        public void close() throws Exception {
            // Close both sessions (delegate server and remote client)
            ctx.fireChannelInactive();
            ctx.disconnect();
            ctx.close();
            asyncSshHandlerReader.close();
            asyncSshHandlerWriter.close();
        }
    }, new AsyncSshHandlerReader.ReadMsgHandler() {
        @Override
        public void onMessageRead(final ByteBuf msg) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Forwarding message for client: {} on channel: {}, message: {}",
                        netconfHelloMessageAdditionalHeader.getAddress(), ctx.channel(),
                        AsyncSshHandlerWriter.byteBufToString(msg));
            }
            // Just forward to delegate
            ctx.writeAndFlush(msg);
        }
    }, "ssh" + netconfHelloMessageAdditionalHeader.getAddress(), in);

    super.channelActive(ctx);
}

From source file:org.opendaylight.controller.netconf.util.handler.ssh.virtualsocket.ChannelInputStream.java

License:Open Source License

public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    ctx.fireChannelInactive();
}

From source file:org.opendaylight.netconf.client.TcpClientChannelInitializer.java

License:Open Source License

@Override
public void initialize(final Channel ch, final Promise<NetconfClientSession> promise) {
    final Future<NetconfClientSession> negotiationFuture = promise;

    //We have to add this channel outbound handler to channel pipeline, in order
    //to get notifications from netconf negotiatior. Set connection promise to
    //success only after successful negotiation.
    ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
        ChannelPromise connectPromise;/*  ww  w.j av  a2s.  c  o  m*/
        GenericFutureListener<Future<NetconfClientSession>> negotiationFutureListener;

        @Override
        public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress,
                final SocketAddress localAddress, final ChannelPromise channelPromise) throws Exception {
            connectPromise = channelPromise;
            ChannelPromise tcpConnectFuture = new DefaultChannelPromise(ch);

            negotiationFutureListener = new GenericFutureListener<Future<NetconfClientSession>>() {
                @Override
                public void operationComplete(final Future<NetconfClientSession> future) throws Exception {
                    if (future.isSuccess()) {
                        connectPromise.setSuccess();
                    }
                }
            };

            tcpConnectFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
                @Override
                public void operationComplete(final Future<? super Void> future) throws Exception {
                    if (future.isSuccess()) {
                        //complete connection promise with netconf negotiation future
                        negotiationFuture.addListener(negotiationFutureListener);
                    } else {
                        connectPromise.setFailure(future.cause());
                    }
                }
            });
            ctx.connect(remoteAddress, localAddress, tcpConnectFuture);
        }

        @Override
        public void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
            // If we have already succeeded and the session was dropped after, we need to fire inactive to notify reconnect logic
            if (connectPromise.isSuccess()) {
                ctx.fireChannelInactive();
            }

            //If connection promise is not already set, it means negotiation failed
            //we must set connection promise to failure
            if (!connectPromise.isDone()) {
                connectPromise.setFailure(new IllegalStateException("Negotiation failed"));
            }

            //Remove listener from negotiation future, we don't want notifications
            //from negotiation anymore
            negotiationFuture.removeListener(negotiationFutureListener);

            super.disconnect(ctx, promise);
            promise.setSuccess();
        }
    });

    super.initialize(ch, promise);
}

From source file:org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandler.java

License:Open Source License

@Override
public synchronized void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) {
    LOG.trace("Closing SSH session on channel: {} with connect promise in state: {}", ctx.channel(),
            connectPromise);/*from   ww  w  .ja  va 2 s . c o  m*/

    // If we have already succeeded and the session was dropped after, we need to fire inactive to notify reconnect logic
    if (connectPromise.isSuccess()) {
        ctx.fireChannelInactive();
    }

    if (sshWriteAsyncHandler != null) {
        sshWriteAsyncHandler.close();
    }

    if (sshReadAsyncListener != null) {
        sshReadAsyncListener.close();
    }

    //If connection promise is not already set, it means negotiation failed
    //we must set connection promise to failure
    if (!connectPromise.isDone()) {
        connectPromise.setFailure(new IllegalStateException("Negotiation failed"));
    }

    //Remove listener from negotiation future, we don't want notifications
    //from negotiation anymore
    if (negotiationFuture != null) {
        negotiationFuture.removeListener(negotiationFutureListener);
    }

    if (session != null && !session.isClosed() && !session.isClosing()) {
        session.close(false).addListener(new SshFutureListener<CloseFuture>() {
            @Override
            public void operationComplete(final CloseFuture future) {
                if (future.isClosed() == false) {
                    session.close(true);
                }
                session = null;
            }
        });
    }

    // Super disconnect is necessary in this case since we are using NioSocketChannel and it needs to cleanup its resources
    // e.g. Socket that it tries to open in its constructor (https://bugs.opendaylight.org/show_bug.cgi?id=2430)
    // TODO better solution would be to implement custom ChannelFactory + Channel that will use mina SSH lib internally: port this to custom channel implementation
    try {
        // Disconnect has to be closed after inactive channel event was fired, because it interferes with it
        super.disconnect(ctx, ctx.newPromise());
    } catch (final Exception e) {
        LOG.warn("Unable to cleanup all resources for channel: {}. Ignoring.", ctx.channel(), e);
    }

    channel = null;
    promise.setSuccess();
    LOG.debug("SSH session closed on channel: {}", ctx.channel());
}

From source file:org.opendaylight.usc.plugin.UscRemoteDeviceHandler.java

License:Open Source License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    if (isRemote(ctx)) {
        broker.removeLocalSession(routeId);
    }/* w  w  w  .ja  v  a 2s  .  com*/
    ctx.fireChannelInactive();
}

From source file:org.opendaylight.usc.plugin.UscRemoteServerHandler.java

License:Open Source License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    LOG.trace("UscRemoteServerHandler channelInactive()");
    if (broker == null) {
        broker = UscServiceUtils.getService(UscRouteBrokerService.class);
    }//from  w w  w . ja v a  2 s. co m
    if (broker == null) {
        LOG.warn(
                "Broker service is null!Can't broadcast the channel close event to all other remote controller in cluster.");
    } else {
        UscRemoteChannelIdentifier remoteChannel = null;
        final UscChannelImpl connection = ctx.channel().attr(UscPlugin.CHANNEL).get();
        if (connection != null) {
            remoteChannel = new UscRemoteChannelIdentifier(connection.getDevice().getInetAddress(),
                    connection.getType());
        } else {
            // communicate directly with device
            UscRouteIdentifier localRouteId = ctx.channel().attr(UscPlugin.ROUTE_IDENTIFIER).get();
            if (localRouteId != null) {
                remoteChannel = localRouteId;
            }
        }
        if (remoteChannel != null) {
            UscRemoteChannelEventMessage message = new UscRemoteChannelEventMessage(remoteChannel,
                    UscRemoteChannelEventMessage.ChannelEventType.CLOSE);
            broker.broadcastMessage(message);
        }
    }
    ctx.fireChannelInactive();
}

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

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    RedisConnection connection = RedisConnection.getFrom(ctx.channel());
    if (connection != null) {
        connection.onDisconnect();/*from  ww w  .j a v  a 2s .  c  om*/
        if (!connection.isClosed()) {
            if (connection.isFastReconnect()) {
                tryReconnect(connection, 1);
                connection.clearFastReconnect();
            } else {
                reconnect(connection, 1);
            }
        }
    }
    ctx.fireChannelInactive();
}

From source file:org.rzo.netty.ahessian.application.jmx.remote.client.RPCClientSessionPipelineFactory.java

License:Apache License

public HandlerList getPipeline() throws Exception {
    HandlerList pipeline = new HandlerList();
    pipeline.addLast("logger", new OutLogger("1"));
    pipeline.addLast("reconnector", new ChannelInboundHandlerAdapter() {

        @Override//from  w w w . j  a  va  2s.co  m
        public void channelInactive(ChannelHandlerContext ctx) {
            ctx.fireChannelInactive();
            System.out.println("channel closed wait to reconnect ...");
            timer.schedule(new TimerTask() {
                public void run() {
                    System.out.println("reconnecting...");
                    ChannelFuture f = _bootstrap.connect();
                    try {
                        System.out.println("future wait");
                        f.awaitUninterruptibly();
                        System.out.println("future wait terminated");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if (f.isSuccess())
                        System.out.println("connected");
                    else {
                        System.out.println("not connected");
                        // f.getChannel().close();
                    }

                }
            }, RECONNECT_DELAY);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) {
            Throwable cause = e;
            if (cause instanceof ConnectException) {
                System.out.println("conection lost");
            }
            ctx.channel().close();
        }
    });
    pipeline.addLast("sessionFilter", _sessionFilter);

    return pipeline;
}

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

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    _token.disconnected();/*from  w  w  w  . j  a  va2 s  . c  o m*/
    ctx.fireChannelInactive();
}