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

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

Introduction

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

Prototype

@Override
public final int hashCode() 

Source Link

Document

Returns the ID of this channel.

Usage

From source file:org.opendaylight.usc.manager.UscRouteBrokerService.java

License:Open Source License

/**
 * process response which getting from remote controller
 *
 * @param message/* w  w w .j  a  va  2  s.com*/
 *            response content
 */
public void processResponse(UscRemoteMessage message) {
    if (message instanceof UscRemoteDataMessage) {
        LOG.info("get response from remote channel, for " + message.getRouteIdentifier());
        UscRemoteDataMessage temp = (UscRemoteDataMessage) message;
        UscRouteIdentifier localRouteId = message.getRouteIdentifier();
        LocalChannel serverChannel = getRequestSource(localRouteId);
        if (serverChannel != null) {
            LOG.trace("Write response to serverChannel(" + serverChannel.hashCode() + "), content "
                    + new String(temp.getPayload()));
            serverChannel.writeAndFlush(Unpooled.copiedBuffer(temp.getPayload()));
            monitor.onEvent(
                    new UscSessionTransactionEvent(localRouteId.getIp(), localRouteId.getRemoteChannelType(),
                            localRouteId.getSessionId() + "", temp.getPayload().length, 0));
        } else {
            LOG.error("Failed to find the server channel for routeIdentifier({}), can't process response({})!",
                    temp.getRouteIdentifier(), message);
        }
    } else {
        LOG.warn("The message type is different, it can't be processed.message type is {}", message.getClass());
    }
}

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

License:Open Source License

public ChannelFuture connect(Bootstrap clientBootstrap, final InetSocketAddress address, boolean remote)
        throws InterruptedException, ExecutionException, Exception {

    LOG.trace("Attempt to connect to " + address + ",remote is " + remote);
    boolean remoteDevice = false;

    // Connect to USC Agent to the device if one's not already created
    UscChannelImpl connection = null;/*from   w  w w.  ja  v  a2  s  . c  o  m*/
    Channel directChannel = null;
    UscDevice device = new UscDevice(address.getAddress(), address.getPort());
    UscRouteBrokerService routeBroker = UscServiceUtils.getService(UscRouteBrokerService.class);
    Exception connectException = null;

    if (remote) {
        if (routeBroker != null) {
            if (routeBroker.existRemoteChannel(
                    new UscRemoteChannelIdentifier(device.getInetAddress(), getChannelType()))) {
                remoteDevice = true;
                LOG.trace("Find remote channel for device " + device);
            } else {
                remote = false;
                LOG.warn("remote channel is not found for device " + device + ", try to connect from local.");
            }
        } else {
            LOG.error("Broker service is null, try to connect from local.");
            remote = false;
        }
    }
    if (!remote) {
        if (getChannelType() == ChannelType.DTLS || getChannelType() == ChannelType.UDP) {
            try {
                connection = connectionManager.getConnection(
                        new UscDevice(address.getAddress(), address.getPort()), getChannelType());
            } catch (Exception e) {
                LOG.error("Failed to get udp agent connection, try to directly connect.error is "
                        + e.getMessage());
                connectException = e;
            }

            LOG.trace("Returned connection is " + connection);
            Channel channel = connection.getChannel();
            UscDemultiplexer handler = (UscDemultiplexer) channel.pipeline().get("UscDemultiplexer");
            SocketAddress remoteAddress = channel.remoteAddress();
            if (!handler.promiseMap.containsKey(remoteAddress)) {
                handler.promiseMap.putIfAbsent(remoteAddress, SettableFuture.<Throwable>create());

                UscControl echoControl = new UscControl(address.getPort(), 1,
                        UscControl.ControlCode.ECHO.getCode());
                channel.writeAndFlush(echoControl);
                LOG.trace("Send a ECHO message to see if the usc agent port is reachable.");
            }

            Throwable e = null;
            e = handler.promiseMap.get(remoteAddress).get(5000, TimeUnit.MILLISECONDS);

            if (e != null) {
                LOG.trace("connect: handler.promise is " + e);
                if (e != null && e instanceof PortUnreachableException) {
                    LOG.trace("connect: caught exception PortUnreachableException");
                    channel.close();
                    connectionManager.removeConnection(connection);
                    connection = null;
                    LOG.trace("connect: start connecting to " + address.getAddress() + (":") + address.getPort()
                            + " directly.");
                    try {
                        directChannel = connectToDeviceDirectly(
                                new UscDevice(address.getAddress(), address.getPort()));
                    } catch (Exception ex) {
                        LOG.error("Failed to get direct connection, try to remote connect.error is "
                                + e.getMessage());
                        connectException = ex;
                    }
                }
            }
        } else {
            try {
                connection = connectionManager.getConnection(
                        new UscDevice(address.getAddress(), address.getPort()), getChannelType());
            } catch (Exception e) {
                LOG.error("Failed to get agent connection, try to directly connect.error is " + e.getMessage());
                try {
                    directChannel = connectToDeviceDirectly(
                            new UscDevice(address.getAddress(), address.getPort()));
                } catch (Exception ex) {
                    LOG.error("Failed to get direct connection, try to remote connect.error is "
                            + e.getMessage());
                    connectException = ex;
                }
            }
        }
    }
    if (connectException != null) {
        if (routeBroker != null) {
            if (routeBroker.existRemoteChannel(
                    new UscRemoteChannelIdentifier(device.getInetAddress(), getChannelType()))) {
                remoteDevice = true;
                LOG.trace("Found remote channel for device " + device);
            } else {
                LOG.warn("Failed to find remote channel in device table!");
                throw connectException;
            }
        } else {
            LOG.warn("Broker service is null, can't find exist remote channel, throw exception dirctly.");
            throw connectException;
        }
    }

    final ChannelFuture clientChannelFuture = clientBootstrap.connect(localServerAddr);
    clientChannelFuture.channel().pipeline().addLast(uscExceptionHandler);

    // sync to ensure that localAddress is not null
    final Channel clientChannel = clientChannelFuture.sync().channel();
    SocketAddress localAddress = clientChannel.localAddress();
    serverChannels.putIfAbsent(localAddress, SettableFuture.<LocalChannel>create());

    // wait for the peer to populate
    LocalChannel serverChannel = serverChannels.get(localAddress).get();

    LOG.trace("connect: serverChannel = " + serverChannel);

    assert serverChannel != null;
    // remove the entry from the map as its purpose is complete
    serverChannels.remove(localAddress);

    if (connection != null) {
        UscSessionImpl session = connection.addSession(address.getPort(), serverChannel);

        LOG.trace("clientChannel set session " + session);
        // these attributes are used by unit test cases
        clientChannel.attr(SESSION).setIfAbsent(SettableFuture.<UscSessionImpl>create());
        clientChannel.attr(SESSION).get().set(session);

        // these attributes are used by UscMultiplexer
        serverChannel.attr(SESSION).get().set(session);

        // this attribute is used by UscDemultiplexer
        serverChannel.attr(CLIENT_CHANNEL).set(clientChannel);
        LOG.info("Connected with channel for " + session);
    } else if (directChannel != null) {
        clientChannel.attr(LOCAL_SERVER_CHANNEL).set(serverChannel);
        serverChannel.attr(DIRECT_CHANNEL).set(directChannel);
        serverChannel.attr(CLIENT_CHANNEL).set(clientChannel);
        directChannel.attr(LOCAL_SERVER_CHANNEL).set(serverChannel);
        LOG.info("Connected channel using direct way for " + device);
    }

    if (remoteDevice) {
        UscRemoteChannelIdentifier remoteChannel = new UscRemoteChannelIdentifier(device.getInetAddress(),
                getChannelType());
        UscRouteIdentifier routeId = new UscRouteIdentifier(remoteChannel, serverChannel.hashCode(),
                address.getPort());
        clientChannel.attr(ROUTE_IDENTIFIER).setIfAbsent(routeId);
        serverChannel.attr(ROUTE_IDENTIFIER).setIfAbsent(routeId);
        sendEvent(new UscChannelCreateEvent(remoteChannel.getIp(), true, remoteChannel.getRemoteChannelType()));
        // register local session for routing to remote device
        routeBroker.addLocalSession(routeId, serverChannel);
        if (directChannel != null) {
            // direct connection only has one session
            directChannel.attr(ROUTE_IDENTIFIER).set(routeId);
        }
        LOG.info("Initialized local remote channel for " + routeId);
    }
    return clientChannelFuture;
}