Search in sources :

Example 1 with BasicTCP

use of org.jgroups.protocols.BasicTCP in project JGroups by belaban.

the class ChannelTestBase method makeUnique.

protected void makeUnique(JChannel... channels) throws Exception {
    String mcast_addr = Util.getProperty(new String[] { Global.UDP_MCAST_ADDR, "jboss.partition.udpGroup" }, null, "mcast_addr", null);
    boolean is_udp = Stream.of(channels).anyMatch(ch -> ch.getProtocolStack().getTransport() instanceof UDP), is_tcp = Stream.of(channels).anyMatch(ch -> ch.getProtocolStack().getTransport() instanceof BasicTCP);
    if (is_udp) {
        InetAddress mcast = mcast_addr != null ? InetAddress.getByName(mcast_addr) : InetAddress.getByName(ResourceManager.getNextMulticastAddress());
        int mcast_port = ResourceManager.getNextMulticastPort(bind_addr);
        for (JChannel ch : channels) {
            UDP udp = (UDP) ch.getProtocolStack().getTransport();
            udp.setBindAddress(bind_addr);
            udp.setMulticastAddress(mcast).setMulticastPort(mcast_port);
        }
    } else if (is_tcp) {
        List<Integer> ports = ResourceManager.getNextTcpPorts(bind_addr, channels.length);
        Collection<InetSocketAddress> hosts = ports.stream().map(p -> new InetSocketAddress(bind_addr, p)).collect(Collectors.toList());
        for (int i = 0; i < channels.length; i++) {
            ProtocolStack stack = channels[i].getProtocolStack();
            TP tp = stack.getTransport();
            tp.setBindPort(ports.get(i));
            TCPPING ping = stack.findProtocol(TCPPING.class);
            if (ping == null)
                throw new IllegalStateException("TCP stack must consist of TCP:TCPPING - other configs are not supported");
            ping.setInitialHosts(hosts);
        }
    } else
        throw new IllegalStateException("Only UDP and TCP are supported as transport protocols");
}
Also used : UDP(org.jgroups.protocols.UDP) JChannel(org.jgroups.JChannel) InetSocketAddress(java.net.InetSocketAddress) BasicTCP(org.jgroups.protocols.BasicTCP) ProtocolStack(org.jgroups.stack.ProtocolStack) Collection(java.util.Collection) List(java.util.List) TP(org.jgroups.protocols.TP) InetAddress(java.net.InetAddress) TCPPING(org.jgroups.protocols.TCPPING)

Aggregations

InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Collection (java.util.Collection)1 List (java.util.List)1 JChannel (org.jgroups.JChannel)1 BasicTCP (org.jgroups.protocols.BasicTCP)1 TCPPING (org.jgroups.protocols.TCPPING)1 TP (org.jgroups.protocols.TP)1 UDP (org.jgroups.protocols.UDP)1 ProtocolStack (org.jgroups.stack.ProtocolStack)1