Example usage for io.netty.channel.udt UdtChannel remoteAddress

List of usage examples for io.netty.channel.udt UdtChannel remoteAddress

Introduction

In this page you can find the example usage for io.netty.channel.udt UdtChannel remoteAddress.

Prototype

@Override
    InetSocketAddress remoteAddress();

Source Link

Usage

From source file:se.sics.kompics.network.netty.ChannelManager.java

License:Open Source License

void disambiguate(DisambiguateConnection msg, Channel c) {
    synchronized (this) {
        if (c.isActive()) { // might have been closed by the time we get the lock?
            component.setCustomMDC();//from w  w w  .  ja  v  a2  s .  c  o  m
            try {
                component.extLog.debug("Handling Disamb: {} on {}", msg, c);
                if (c instanceof SocketChannel) {
                    SocketChannel sc = (SocketChannel) c;
                    address4Remote.put(sc.remoteAddress(), msg.getSource().asSocket());
                    tcpChannels.put(msg.getSource().asSocket(), sc);
                    component.networkStatus(ConnectionStatus.established(msg.getSource(), Transport.TCP));

                    if (!tcpChannels.get(msg.getSource().asSocket()).isEmpty()) { // don't add if we don't have a TCP channel since host is most likely dead
                        udtBoundPorts.put(msg.getSource().asSocket(),
                                new InetSocketAddress(msg.getSource().getIp(), msg.udtPort));
                    }

                    component.trigger(new SendDelayed(msg.getSource(), Transport.TCP));
                    if (waitingForCreationUDT.remove(msg.getSource().asSocket())) {
                        component.extLog.debug("Requesting creation of outstanding UDT channel to {}",
                                msg.getSource());
                        createUDTChannel(msg.getSource(), component.bootstrapUDTClient);
                    }
                } else if (c instanceof UdtChannel) {
                    UdtChannel uc = (UdtChannel) c;
                    address4Remote.put(uc.remoteAddress(), msg.getSource().asSocket());
                    udtChannels.put(msg.getSource().asSocket(), uc);
                    component.networkStatus(ConnectionStatus.established(msg.getSource(), Transport.UDT));

                    if (!tcpChannels.get(msg.getSource().asSocket()).isEmpty()) { // don't add if we don't have a TCP channel since host is most likely dead
                        udtBoundPorts.put(msg.getSource().asSocket(),
                                new InetSocketAddress(msg.getSource().getIp(), msg.udtPort));
                    }

                    component.trigger(new SendDelayed(msg.getSource(), Transport.UDT));
                }
            } finally {
                MDC.clear();
            }
        }
    }
}

From source file:se.sics.kompics.network.netty.ChannelManager.java

License:Open Source License

void flushAndClose(CloseChannel msg, Channel c) {
    synchronized (this) {
        component.setCustomMDC();/* w  w  w.j a  v  a 2  s.c om*/
        try {
            if (c instanceof SocketChannel) {
                SocketChannel sc = (SocketChannel) c;
                SocketChannel activeC = tcpActiveChannels.get(msg.getSource().asSocket());
                tcpChannels.put(msg.getSource().asSocket(), sc); // just to make sure
                Set<SocketChannel> channels = tcpChannels.get(msg.getSource().asSocket());
                if (channels.size() < 2) {
                    component.extLog.warn(
                            "Can't close TCP channel between {} and {}: local {}, remote {} -- it's the only channel!",
                            new Object[] { msg.getSource(), msg.getDestination(), sc.localAddress(),
                                    sc.remoteAddress() });
                    tcpActiveChannels.put(msg.getSource().asSocket(), sc);
                    sc.writeAndFlush(new MessageNotify.Req(
                            new CheckChannelActive(component.self, msg.getSource(), Transport.TCP)));
                } else {
                    if (activeC.equals(sc)) { // pick any channel as active
                        for (SocketChannel channel : channels) {
                            if (!channel.equals(sc)) {
                                tcpActiveChannels.put(msg.getSource().asSocket(), channel);
                                activeC = channel;
                            }
                        }
                    }
                    ChannelFuture f = sc.writeAndFlush(new MessageNotify.Req(
                            new ChannelClosed(component.self, msg.getSource(), Transport.TCP)));
                    f.addListener(ChannelFutureListener.CLOSE);
                    component.extLog.info(
                            "Closing duplicate TCP channel between {} and {}: local {}, remote {}",
                            new Object[] { msg.getSource(), msg.getDestination(), sc.localAddress(),
                                    sc.remoteAddress() });

                }
            } else if (c instanceof UdtChannel) {
                UdtChannel uc = (UdtChannel) c;
                UdtChannel activeC = udtActiveChannels.get(msg.getSource().asSocket());
                udtChannels.put(msg.getSource().asSocket(), uc); // just to make sure
                Set<UdtChannel> channels = udtChannels.get(msg.getSource().asSocket());
                if (channels.size() < 2) {
                    component.extLog.warn(
                            "Can't close UDT channel between {} and {}: local {}, remote {} -- it's the only channel!",
                            new Object[] { msg.getSource(), msg.getDestination(), uc.localAddress(),
                                    uc.remoteAddress() });
                    udtActiveChannels.put(msg.getSource().asSocket(), uc);
                    uc.writeAndFlush(new MessageNotify.Req(
                            new CheckChannelActive(component.self, msg.getSource(), Transport.UDT)));
                } else {
                    if (activeC.equals(uc)) { // pick any channel as active
                        for (UdtChannel channel : channels) {
                            if (!channel.equals(uc)) {
                                udtActiveChannels.put(msg.getSource().asSocket(), channel);
                                activeC = channel;
                            }
                        }
                    }
                    ChannelFuture f = uc.writeAndFlush(new MessageNotify.Req(
                            new ChannelClosed(component.self, msg.getSource(), Transport.UDT)));
                    f.addListener(ChannelFutureListener.CLOSE);
                    component.extLog.info(
                            "Closing duplicate UDT channel between {} and {}: local {}, remote {}",
                            new Object[] { msg.getSource(), msg.getDestination(), uc.localAddress(),
                                    uc.remoteAddress() });

                }
            }
        } finally {
            MDC.clear();
        }
    }
}

From source file:se.sics.kompics.network.netty.ChannelManager.java

License:Open Source License

void checkUDTChannel(Msg msg, UdtChannel c) {
    // Ignore some messages
    if (msg instanceof CheckChannelActive) {
        return;//from   w w  w .  j  av a  2s .co m
    }
    if (msg instanceof CloseChannel) {
        return;
    }
    if (msg instanceof ChannelClosed) {
        return;
    }
    if (!c.equals(udtActiveChannels.get(msg.getSource().asSocket()))) {
        synchronized (this) {
            component.setCustomMDC();
            try {
                UdtChannel activeC = udtActiveChannels.get(msg.getSource().asSocket());

                udtActiveChannels.put(msg.getSource().asSocket(), c);
                udtChannels.put(msg.getSource().asSocket(), c);
                if (activeC != null && !activeC.equals(c)) {
                    component.extLog.warn("Duplicate TCP channel between {} and {}: local {}, remote {}",
                            new Object[] { msg.getSource(), msg.getDestination(), c.localAddress(),
                                    c.remoteAddress() });

                    UdtChannel minsc = minChannel(udtChannels.get(msg.getSource().asSocket()));

                    minsc.writeAndFlush(new MessageNotify.Req(
                            new CheckChannelActive(component.self, msg.getSource(), Transport.UDT)));

                }
            } finally {
                MDC.clear();
            }
        }
        component.trigger(new SendDelayed(msg.getSource(), Transport.UDT));
    }
}

From source file:se.sics.kompics.network.netty.ChannelManager.java

License:Open Source License

private int channel2Id(UdtChannel c) {
    return c.localAddress().getPort() + c.remoteAddress().getPort();
}

From source file:se.sics.kompics.network.netty.ChannelManager.java

License:Open Source License

UdtChannel createUDTChannel(final Address destination, final Bootstrap bootstrapUDTClient) {
    synchronized (this) {
        UdtChannel c = udtActiveChannels.get(destination.asSocket());
        if (c != null) {
            return c;
        }/*from w w  w .  j a v a 2s .com*/
        ChannelFuture f = udtIncompleteChannels.get(destination.asSocket());
        if (f != null) {
            component.extLog.trace("UDT channel to {} is already being created.", destination.asSocket());
            return null; // already establishing connection but not done, yet
        }
        InetSocketAddress isa = udtBoundPorts.get(destination.asSocket());
        if (isa == null) { // We have to ask for the UDT port first, since it's random
            component.extLog.trace("Need to find UDT port at {} before creating channel.",
                    destination.asSocket());
            DisambiguateConnection r = new DisambiguateConnection(component.self, new NettyAddress(destination),
                    Transport.TCP, component.boundUDTPort, true);
            SocketChannel tcpC = this.getTCPChannel(destination);
            if (tcpC == null) {
                tcpC = this.createTCPChannel(destination, component.bootstrapTCPClient);
            }
            if (tcpC == null) {
                component.extLog.debug("Putting disamb on hold until TCP channel is created: {}", r);
                waitingDisambs.put(destination.asSocket(), r);
                return null;
            }
            waitingForCreationUDT.add(destination.asSocket());
            tcpC.writeAndFlush(new MessageWrapper(r));
            return null;
        }
        component.extLog.trace("Creating new UDT channel to {}.", destination.asSocket());
        component.networkStatus(ConnectionStatus.requested(destination, Transport.UDT));
        f = bootstrapUDTClient.connect(isa);
        udtIncompleteChannels.put(destination.asSocket(), f);
        f.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                synchronized (ChannelManager.this) {
                    component.setCustomMDC();
                    try {
                        udtIncompleteChannels.remove(destination.asSocket());
                        if (future.isSuccess()) {
                            UdtChannel sc = (UdtChannel) future.channel();
                            udtActiveChannels.put(destination.asSocket(), sc);
                            udtChannels.put(destination.asSocket(), sc);
                            udtChannelsByRemote.put(sc.remoteAddress(), sc);
                            address4Remote.put(sc.remoteAddress(), destination.asSocket());
                            component.trigger(new SendDelayed(destination, Transport.UDT));
                            SocketUDT socket = NioUdtProvider.socketUDT(sc);
                            //                            if (component.udtBufferSizes > 0) {
                            //                                socket.setSendBufferSize(component.udtBufferSizes);
                            //                                socket.setReceiveBufferSize(component.udtBufferSizes);
                            //                            }
                            if (component.udtMSS > 0) {
                                socket.setOption(OptionUDT.Maximum_Transfer_Unit, component.udtMSS);
                            }
                            component.trigger(new SendDelayed(destination, Transport.UDT));
                            component.extLog.debug("New UDT channel to {} was created! Properties: \n {} \n {}",
                                    new Object[] { destination.asSocket(), socket.toStringOptions(),
                                            socket.toStringMonitor() });
                            component.networkStatus(ConnectionStatus.established(destination, Transport.UDT));
                        } else {
                            component.extLog.error("Error while trying to connect to {}! Error was {}",
                                    destination, future.cause());
                            component.networkStatus(ConnectionStatus.dropped(destination, Transport.UDT));
                            component.trigger(new DropDelayed(destination, Transport.UDT));
                        }
                    } finally {
                        MDC.clear();
                    }
                }
            }
        });

    }
    return null;
}

From source file:se.sics.kompics.network.netty.ChannelManager.java

License:Open Source License

void channelInactive(ChannelHandlerContext ctx, Transport protocol) {

    synchronized (this) {
        component.setCustomMDC();//from w  ww .ja va 2  s .  co  m
        try {
            SocketAddress addr = ctx.channel().remoteAddress();
            Channel c = ctx.channel();

            if (addr instanceof InetSocketAddress) {
                InetSocketAddress remoteAddress = (InetSocketAddress) addr;
                InetSocketAddress realAddress = address4Remote.remove(remoteAddress);
                switch (protocol) {
                case TCP:
                    if (realAddress != null) {
                        tcpChannels.remove(realAddress, c);
                        SocketChannel curChannel = tcpActiveChannels.get(realAddress);
                        if ((curChannel != null) && curChannel.equals(c)) {
                            SocketChannel newActive = minChannel(tcpChannels.get(realAddress));
                            if (newActive != null) {
                                tcpActiveChannels.put(realAddress, newActive);
                            } else {
                                tcpActiveChannels.remove(realAddress);
                            }
                        }
                        tcpChannelsByRemote.remove(remoteAddress);
                        component.extLog.debug("TCP Channel {} ({}) closed: {}",
                                new Object[] { realAddress, remoteAddress, c });
                        component.networkStatus(
                                ConnectionStatus.dropped(new NettyAddress(realAddress), Transport.TCP));
                        printStuff();
                        if (tcpChannels.get(realAddress).isEmpty()) {
                            component.extLog.info(
                                    "Last TCP Channel to {} dropped. " + "Also dropping all UDT channels under "
                                            + "the assumption that the host is dead.",
                                    realAddress);
                            UdtChannel uac = udtActiveChannels.remove(realAddress);
                            for (UdtChannel uc : udtChannels.get(realAddress)) {
                                InetSocketAddress udtRealAddr = address4Remote.remove(uc.remoteAddress());
                                udtChannelsByRemote.remove(uc.remoteAddress());
                                uc.close();
                                component.extLog.debug("   UDT Channel {} ({}) closed.", udtRealAddr,
                                        uc.remoteAddress());
                                component.networkStatus(
                                        ConnectionStatus.dropped(new NettyAddress(realAddress), Transport.UDT));
                            }
                            udtChannels.removeAll(realAddress);

                            udtBoundPorts.remove(realAddress);
                        } else {
                            component.extLog.trace("There are still {} TCP channel(s) remaining: [",
                                    tcpChannels.get(realAddress).size(), realAddress);
                            for (SocketChannel sc : tcpChannels.get(realAddress)) {
                                component.extLog.trace("TCP channel: {}", sc);
                            }
                            component.extLog.trace("]. Not closing UDT channels for {}",
                                    tcpChannels.get(realAddress).size(), realAddress);

                        }
                        printStuff();
                    } else {
                        tcpChannelsByRemote.remove(remoteAddress);
                        component.extLog.debug("TCP Channel {} was already closed.", remoteAddress);
                        printStuff();
                    }
                    return;
                case UDT:
                    if (realAddress != null) {
                        udtChannels.remove(realAddress, c);
                        UdtChannel curChannel = udtActiveChannels.get(realAddress);
                        if ((curChannel != null) && curChannel.equals(c)) {
                            UdtChannel newActive = minChannel(udtChannels.get(realAddress));
                            if (newActive != null) {
                                udtActiveChannels.put(realAddress, newActive);
                            } else {
                                udtActiveChannels.remove(realAddress);
                            }
                        }
                        udtChannelsByRemote.remove(remoteAddress);
                        component.extLog.debug("UDT Channel {} ({}) closed.", realAddress, remoteAddress);
                        component.networkStatus(
                                ConnectionStatus.dropped(new NettyAddress(realAddress), Transport.UDT));
                        printStuff();
                    } else {
                        udtChannelsByRemote.remove(remoteAddress);
                        component.extLog.debug("UDT Channel {} was already closed.\n", remoteAddress);
                        printStuff();
                    }
                    return;
                default:
                    component.extLog.error("Was supposed to close channel {}, but don't know transport {}",
                            remoteAddress, protocol);
                }
            }
        } finally {
            MDC.clear();
        }
    }

}

From source file:se.sics.kompics.network.netty.ChannelManager.java

License:Open Source License

void addLocalSocket(UdtChannel channel) {
    synchronized (this) {
        udtChannelsByRemote.put(channel.remoteAddress(), channel);
    }
}

From source file:se.sics.kompics.network.netty.UDTServerHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    super.channelActive(ctx);
    UdtChannel channel = (UdtChannel) ctx.channel();
    component.addLocalSocket(channel.remoteAddress(), channel);
    InetSocketAddress other = channel.remoteAddress();
    channel.writeAndFlush(new DisambiguateConnection.Req(component.self, component.socketToAddress(other),
            protocol, other.getPort(), component.boundUDTPort));
}