Example usage for io.netty.channel Channel isRegistered

List of usage examples for io.netty.channel Channel isRegistered

Introduction

In this page you can find the example usage for io.netty.channel Channel isRegistered.

Prototype

boolean isRegistered();

Source Link

Document

Returns true if the Channel is registered with an EventLoop .

Usage

From source file:com.test.AbstractBootstrap.java

License:Apache License

final ChannelFuture initAndRegister() {
    final Channel channel = channelFactory().newChannel();
    try {/*from  w w w  . j a va2 s  .co m*/
        init(channel);
    } catch (Throwable t) {
        channel.unsafe().closeForcibly();
        // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
        return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
    }

    ChannelFuture regFuture = group().register(channel);
    if (regFuture.cause() != null) {
        if (channel.isRegistered()) {
            channel.close();
        } else {
            channel.unsafe().closeForcibly();
        }
    }

    // If we are here and the promise is not failed, it's one of the following cases:
    // 1) If we attempted registration from the event loop, the registration has been completed at this point.
    //    i.e. It's safe to attempt bind() or connect() now because the channel has been registered.
    // 2) If we attempted registration from the other thread, the registration request has been successfully
    //    added to the event loop's task queue for later execution.
    //    i.e. It's safe to attempt bind() or connect() now:
    //         because bind() or connect() will be executed *after* the scheduled registration task is executed
    //         because register(), bind(), and connect() are all bound to the same thread.

    return regFuture;
}

From source file:io.reactivesocket.netty.tcp.server.ServerTcpDuplexConnection.java

License:Apache License

public String toString() {
    if (ctx == null || ctx.channel() == null) {
        return getClass().getName() + ":channel=null";
    }//  w w w.  j ava2 s .co  m

    Channel channel = ctx.channel();
    return getClass().getName() + ":channel=[" + "remoteAddress=" + channel.remoteAddress() + "," + "isActive="
            + channel.isActive() + "," + "isOpen=" + channel.isOpen() + "," + "isRegistered="
            + channel.isRegistered() + "," + "isWritable=" + channel.isWritable() + "," + "channelId="
            + channel.id().asLongText() + "]";

}

From source file:org.apache.giraph.comm.netty.NettyClient.java

License:Apache License

/**
 * Get the next available channel, reconnecting if necessary
 *
 * @param remoteServer Remote server to get a channel for
 * @return Available channel for this remote server
 *///www  . j ava 2s .  c o m
private Channel getNextChannel(InetSocketAddress remoteServer) {
    Channel channel = addressChannelMap.get(remoteServer).nextChannel();
    if (channel == null) {
        throw new IllegalStateException("getNextChannel: No channel exists for " + remoteServer);
    }

    // Return this channel if it is connected
    if (channel.isActive()) {
        return channel;
    }

    // Get rid of the failed channel
    if (addressChannelMap.get(remoteServer).removeChannel(channel)) {
        LOG.warn("getNextChannel: Unlikely event that the channel " + channel + " was already removed!");
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("getNextChannel: Fixing disconnected channel to " + remoteServer + ", open = "
                + channel.isOpen() + ", " + "bound = " + channel.isRegistered());
    }
    int reconnectFailures = 0;
    while (reconnectFailures < maxConnectionFailures) {
        ChannelFuture connectionFuture = bootstrap.connect(remoteServer);
        ProgressableUtils.awaitChannelFuture(connectionFuture, context);
        if (connectionFuture.isSuccess()) {
            if (LOG.isInfoEnabled()) {
                LOG.info("getNextChannel: Connected to " + remoteServer + "!");
            }
            addressChannelMap.get(remoteServer).addChannel(connectionFuture.channel());
            return connectionFuture.channel();
        }
        ++reconnectFailures;
        LOG.warn(
                "getNextChannel: Failed to reconnect to " + remoteServer + " on attempt " + reconnectFailures
                        + " out of " + maxConnectionFailures + " max attempts, sleeping for 5 secs",
                connectionFuture.cause());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            LOG.warn("getNextChannel: Unexpected interrupted exception", e);
        }
    }
    throw new IllegalStateException("getNextChannel: Failed to connect " + "to " + remoteServer + " in "
            + reconnectFailures + " connect attempts");
}

From source file:org.jboss.aerogear.simplepush.server.netty.UserAgentReaper.java

License:Apache License

private boolean isChannelInactive(final UserAgent<SockJsSessionContext> userAgent) {
    final Channel ch = userAgent.context().getContext().channel();
    return !ch.isActive() && !ch.isRegistered();
}

From source file:org.jboss.aerogear.simplepush.server.netty.UserAgentReaperTest.java

License:Apache License

private SockJsSessionContext newSessionContext(final boolean active) {
    final Channel channel = mock(Channel.class);
    when(channel.isActive()).thenReturn(active);
    when(channel.isRegistered()).thenReturn(active);
    final ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(channel);
    final SockJsSessionContext sessionContext = mock(SockJsSessionContext.class);
    when(sessionContext.getContext()).thenReturn(ctx);
    return sessionContext;
}

From source file:org.jupiter.transport.netty.handler.IdleStateChecker.java

License:Apache License

@SuppressWarnings("StatementWithEmptyBody")
@Override/* w  w  w .  j  a  v  a  2s .  c o  m*/
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    Channel ch = ctx.channel();

    if (ch.isActive() && ch.isRegistered()) {
        // channelActive() event has been fired already, which means this.channelActive() will
        // not be invoked. We have to initialize here instead.
        initialize(ctx);
    } else {
        // channelActive() event has not been fired yet.  this.channelActive() will be invoked
        // and initialization will occur there.
    }
}

From source file:org.pliers.netty.handler.IdleStateChecker.java

License:Apache License

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    Channel ch = ctx.channel();

    if (ch.isActive() && ch.isRegistered()) {
        // channelActive() event has been fired already, which means this.channelActive() will
        // not be invoked. We have to initialize here instead.
        initialize(ctx);/*  w w  w . j  a  va2  s  .  co m*/
    } else {
        // channelActive() event has not been fired yet.  this.channelActive() will be invoked
        // and initialization will occur there.
    }
}

From source file:org.restcomm.media.network.netty.NettyNetworkManagerTest.java

License:Open Source License

@Test
public void testOpenChannelSync() throws Exception {
    // given//from w ww  . j av a2s.com
    this.eventLoopGroup = new NioEventLoopGroup(1);
    final ChannelHandler channelHandler = mock(ChannelHandler.class);
    final Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioDatagramChannel.class)
            .handler(channelHandler);
    try (final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap)) {
        // when
        final Channel channel = networkManager.openChannel();

        // then
        assertTrue(channel.isOpen());
        assertTrue(channel.isRegistered());
        assertFalse(channel.isActive());
    }
}

From source file:sas.systems.imflux.participant.RtspParticipant.java

License:Apache License

/**
 * Creates a new {@link RtspParticipant} in the default state {@link State#INITIALIZING}.
 * /*from w  ww  . jav  a2  s  .  c  o m*/
 * @param channel
 * @return {@link RtpParticipant}
 */
public static RtspParticipant newInstance(Channel channel) {
    if (!channel.isActive()) {
        throw new IllegalArgumentException("Channel is not active!");
    }
    if (!channel.isRegistered()) {
        throw new IllegalArgumentException("Channel is not registered with an EventLoop!");
    }

    return new RtspParticipant(channel);
}

From source file:sas.systems.imflux.participant.RtspParticipant.java

License:Apache License

/**
 * Create a new {@link RtspParticipant} in the default state {@link State#INITIALIZING}.<br/>
 * Use the reference to the {@link RtpParticipant} to link to the RTP session.
 * //from  w w  w  .  j  a v  a  2  s  .c o m
 * @param channel
 * @param associatedRtpParticipant
 * @return {@link RtpParticipant}
 */
public static RtspParticipant newInstance(Channel channel, RtpParticipant associatedRtpParticipant) {
    if (!channel.isActive()) {
        throw new IllegalArgumentException("Channel is not active!");
    }
    if (!channel.isRegistered()) {
        throw new IllegalArgumentException("Channel is not registered with an EventLoop!");
    }

    return new RtspParticipant(channel, associatedRtpParticipant);
}