Example usage for io.netty.channel.local LocalChannel id

List of usage examples for io.netty.channel.local LocalChannel id

Introduction

In this page you can find the example usage for io.netty.channel.local LocalChannel id.

Prototype

@Override
    public final ChannelId id() 

Source Link

Usage

From source file:org.waarp.openr66.protocol.localhandler.LocalTransaction.java

License:Open Source License

/**
 * Create a new Client//from w  w  w  .j a v  a 2 s .  c om
 * 
 * @param networkChannelReference
 * @param remoteId
 *            might be set to ChannelUtils.NOCHANNEL (real creation)
 * @param futureRequest
 *            might be null (from NetworkChannel Startup)
 * @return the LocalChannelReference
 * @throws OpenR66ProtocolSystemException
 * @throws OpenR66ProtocolRemoteShutdownException
 * @throws OpenR66ProtocolNoConnectionException
 */
public LocalChannelReference createNewClient(NetworkChannelReference networkChannelReference, Integer remoteId,
        R66Future futureRequest) throws OpenR66ProtocolSystemException, OpenR66ProtocolRemoteShutdownException,
        OpenR66ProtocolNoConnectionException {
    ChannelFuture channelFuture = null;
    logger.debug("Status LocalChannelServer: {} {}", serverChannel.getClass().getName(),
            serverChannel.config().getConnectTimeoutMillis() + " " + serverChannel.isOpen());
    for (int i = 0; i < Configuration.RETRYNB; i++) {
        if (R66ShutdownHook.isShutdownStarting()) {
            // Do not try since already locally in shutdown
            throw new OpenR66ProtocolNoConnectionException("Cannot connect to local handler: "
                    + socketLocalServerAddress + " " + serverChannel.isOpen() + " " + serverChannel
                    + " since the local server is in shutdown.");
        }
        channelFuture = clientBootstrap.connect(socketLocalServerAddress);
        try {
            channelFuture.await();
            //channelFuture.await(Configuration.configuration.TIMEOUTCON/3);
        } catch (InterruptedException e1) {
            logger.error("LocalChannelServer Interrupted: " + serverChannel.getClass().getName() + " "
                    + serverChannel.config().getConnectTimeoutMillis() + " " + serverChannel.isOpen());
            throw new OpenR66ProtocolSystemException("Interruption - Cannot connect to local handler: "
                    + socketLocalServerAddress + " " + serverChannel.isOpen() + " " + serverChannel, e1);
        }
        if (channelFuture.isSuccess()) {
            final LocalChannel channel = (LocalChannel) channelFuture.channel();
            localChannelGroup.add(channel);
            logger.debug(
                    "Will start localChannelReference and eventually generate a new Db Connection if not-thread-safe");
            final LocalChannelReference localChannelReference = new LocalChannelReference(channel,
                    networkChannelReference, remoteId, futureRequest);
            localChannelHashMap.put(channel.id().hashCode(), localChannelReference);
            logger.debug("Db connection done and Create LocalChannel entry: " + i + " {}",
                    localChannelReference);
            logger.info("Add one localChannel to a Network Channel: " + channel.id());
            // Now send first a Startup message
            StartupPacket startup = new StartupPacket(localChannelReference.getLocalId());
            channel.writeAndFlush(startup);
            return localChannelReference;
        } else {
            logger.error("Can't connect to local server " + i + " (Done: " + channelFuture.isDone() + ")");
        }
        try {
            Thread.sleep(Configuration.RETRYINMS * 10);
        } catch (InterruptedException e) {
            throw new OpenR66ProtocolSystemException("Cannot connect to local handler", e);
        }
    }
    logger.error("LocalChannelServer: " + serverChannel.getClass().getName() + " "
            + serverChannel.config().getConnectTimeoutMillis() + " " + serverChannel.isOpen());
    throw new OpenR66ProtocolSystemException("Cannot connect to local handler: " + socketLocalServerAddress
            + " " + serverChannel.isOpen() + " " + serverChannel, channelFuture.cause());
}