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

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

Introduction

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

Prototype

@Override
    public ChannelFuture writeAndFlush(Object msg) 

Source Link

Usage

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

License:Open Source License

/**
 * process response which getting from remote controller
 *
 * @param message/*from ww  w.j a va  2s  .  c o m*/
 *            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.manager.UscRouteBrokerService.java

License:Open Source License

/**
 * process remote exception message// w  w  w.  j av a 2  s . co m
 *
 * @param message
 */
public void processException(UscRemoteExceptionMessage message) {
    UscException ex = message.getException();
    LocalChannel serverChannel = getRequestSource(message.getRouteIdentifier());
    serverChannel.writeAndFlush(ex);
    UscRouteIdentifier routeId = message.getRouteIdentifier();
    if (ex instanceof UscSessionException) {
        UscSessionException tmp = (UscSessionException) ex;
        monitor.onEvent(new UscSessionErrorEvent(routeId.getIp(), routeId.getRemoteChannelType(),
                routeId.getSessionId() + "", tmp.getErrorCode().getCode(), UscErrorLevel.ERROR,
                tmp.getMessage()));
    } else {
        LOG.warn("Unmonitored error event: error is {}, remote identifier is {}.", ex,
                message.getRouteIdentifier());
    }
}

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

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, UscFrame frame) throws Exception {
    LOG.trace("UscDemultiplexer.channelRead: " + frame);

    if (frame instanceof UscControl) {
        UscControl controlMsg = (UscControl) frame;
        if (controlMsg.getControlCode() == UscControl.ControlCode.ECHO) {
            SocketAddress remoteAddress = ctx.channel().remoteAddress();
            promiseMap.get(remoteAddress).set(new Throwable("Success"));
            LOG.trace("channelRead0: promiseMap = " + promiseMap);
            return;
        }/*from   ww w. jav a2 s  .co m*/
    }

    final UscHeader header = frame.getHeader();
    final int sessionId = header.getSessionId();
    final UscChannelImpl connection = ctx.channel().attr(UscPlugin.CHANNEL).get();

    final UscSessionImpl session = connection.getSession(sessionId);
    final LocalChannel serverChannel = session.getServerChannel();
    if (frame instanceof UscError) {
        // propagate exception to the client channel
        UscSessionException ex = new UscSessionException(((UscError) frame).getErrorCode());

        serverChannel.writeAndFlush(ex);
        plugin.sendEvent(new UscSessionErrorEvent(session, ex));
    } else if (frame instanceof UscData) {

        if (serverChannel != null) {
            LOG.trace("write session " + sessionId + " to " + serverChannel + ": " + frame.getPayload());

            ByteBuf payload = frame.getPayload();

            plugin.sendEvent(new UscSessionTransactionEvent(session, payload.readableBytes(), 0));

            serverChannel.writeAndFlush(payload);
        } else {
            UscChannelException ex = new UscChannelException(
                    "write unknown session " + sessionId + "; discard");
            plugin.sendEvent(new UscChannelErrorEvent(session.getChannel(), ex));

            throw ex;
        }
    } else if (frame instanceof UscControl) {
        UscControl controlMsg = (UscControl) frame;
        Channel clientChannel = serverChannel.attr(UscPlugin.CLIENT_CHANNEL).get();
        if (controlMsg.getControlCode() == UscControl.ControlCode.TERMINATION_REQUEST) {
            LOG.trace("UscDemultiplexer received control message TERMINATION_REQUEST");
            clientChannel.close();

            // send back TERMINATION_RESPONSE
            UscControl data = new UscControl(session.getPort(), session.getSessionId(), 2);
            ctx.channel().writeAndFlush(data);
        } else if (controlMsg.getControlCode() == UscControl.ControlCode.TERMINATION_RESPONSE) {
            LOG.trace("UscDemultiplexer received control message TERMINATION_RESPONSE");
            if (clientChannel != null) {
                SettableFuture<Boolean> status = plugin.getCloseFuture().get(clientChannel);
                status.set(true);
                LOG.trace("UscDemultiplexer: termination status is " + status.get());
            }
        }
    } else {
        LOG.trace("UscDemultiplexer.channelRead: unexpected UscFrame object " + frame);
        UscChannelException ex = new UscChannelException("unexpected UscFrame object " + frame);
        plugin.sendEvent(new UscChannelErrorEvent(session.getChannel(), ex));

        throw ex;
    }
}

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

License:Open Source License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    SocketAddress remoteAddress = ctx.channel().remoteAddress();
    promiseMap.get(remoteAddress).set(cause);
    LOG.trace("exceptionCaught: promiseMap = " + promiseMap);

    if (cause instanceof UscChannelException) {
        LOG.trace("UscDemultiplexer exceptionCaught()");
        final UscChannelImpl connection = ctx.channel().attr(UscPlugin.CHANNEL).get();

        for (UscSessionImpl session : connection.getAllSessions()) {
            final LocalChannel serverChannel = session.getServerChannel();

            if (serverChannel != null) {
                LOG.trace("UscDemultiplexer exceptionCaught() and write to the server channel.");

                serverChannel.writeAndFlush(cause);
            }// w  w  w  .ja va 2  s .c  o  m
        }
    }
}

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

License:Open Source License

/**
 * Create a new Client//from w ww. j  a  va2 s .  co  m
 * 
 * @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());
}