Search in sources :

Example 1 with AddressMatcher

use of com.hazelcast.internal.util.AddressUtil.AddressMatcher in project hazelcast by hazelcast.

the class AddressUtilTest method testGetMatchingIpv4Addresses_whenDashForLastPart.

@Test
public void testGetMatchingIpv4Addresses_whenDashForLastPart() {
    AddressMatcher addressMatcher = AddressUtil.getAddressMatcher("192.168.1.1-42");
    Collection<String> actual = AddressUtil.getMatchingIpv4Addresses(addressMatcher);
    assertEquals(42, actual.size());
}
Also used : Ip4AddressMatcher(com.hazelcast.internal.util.AddressUtil.Ip4AddressMatcher) AddressMatcher(com.hazelcast.internal.util.AddressUtil.AddressMatcher) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with AddressMatcher

use of com.hazelcast.internal.util.AddressUtil.AddressMatcher in project hazelcast by hazelcast.

the class AddressUtilTest method testGetMatchingIpv4Addresses_whenIPv6AsMatcher.

@Test(expected = IllegalArgumentException.class)
public void testGetMatchingIpv4Addresses_whenIPv6AsMatcher() {
    AddressMatcher addressMatcher = AddressUtil.getAddressMatcher("2001:db8:85a3:0:0:8a2e:370:7334");
    AddressUtil.getMatchingIpv4Addresses(addressMatcher);
}
Also used : Ip4AddressMatcher(com.hazelcast.internal.util.AddressUtil.Ip4AddressMatcher) AddressMatcher(com.hazelcast.internal.util.AddressUtil.AddressMatcher) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with AddressMatcher

use of com.hazelcast.internal.util.AddressUtil.AddressMatcher in project hazelcast by hazelcast.

the class AddressUtilTest method testAddressMatcher.

@Test
public void testAddressMatcher() {
    AddressMatcher address;
    address = AddressUtil.getAddressMatcher("fe80::62c5:*:fe05:480a%en0");
    assertTrue(address.isIPv6());
    assertEquals("fe80:0:0:0:62c5:*:fe05:480a", address.getAddress());
    address = AddressUtil.getAddressMatcher("192.168.1.1");
    assertTrue(address instanceof Ip4AddressMatcher);
    assertEquals("192.168.1.1", address.getAddress());
    address = AddressUtil.getAddressMatcher("::ffff:192.0.2.128");
    assertTrue(address.isIPv4());
    assertEquals("192.0.2.128", address.getAddress());
}
Also used : Ip4AddressMatcher(com.hazelcast.internal.util.AddressUtil.Ip4AddressMatcher) Ip4AddressMatcher(com.hazelcast.internal.util.AddressUtil.Ip4AddressMatcher) AddressMatcher(com.hazelcast.internal.util.AddressUtil.AddressMatcher) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with AddressMatcher

use of com.hazelcast.internal.util.AddressUtil.AddressMatcher in project hazelcast by hazelcast.

the class TcpIpJoiner method getPossibleAddresses.

@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity" })
protected Collection<Address> getPossibleAddresses() {
    final Collection<String> possibleMembers = getMembers();
    final Set<Address> possibleAddresses = new HashSet<>();
    final NetworkConfig networkConfig = getActiveMemberNetworkConfig(config);
    for (String possibleMember : possibleMembers) {
        AddressHolder addressHolder = AddressUtil.getAddressHolder(possibleMember);
        try {
            boolean portIsDefined = addressHolder.getPort() != -1 || !networkConfig.isPortAutoIncrement();
            int count = portIsDefined ? 1 : maxPortTryCount;
            int port = addressHolder.getPort() != -1 ? addressHolder.getPort() : networkConfig.getPort();
            AddressMatcher addressMatcher = null;
            try {
                addressMatcher = AddressUtil.getAddressMatcher(addressHolder.getAddress());
            } catch (InvalidAddressException ignore) {
                ignore(ignore);
            }
            if (addressMatcher != null) {
                final Collection<String> matchedAddresses;
                if (addressMatcher.isIPv4()) {
                    matchedAddresses = AddressUtil.getMatchingIpv4Addresses(addressMatcher);
                } else {
                    // for IPv6 we are not doing wildcard matching
                    matchedAddresses = Collections.singleton(addressHolder.getAddress());
                }
                for (String matchedAddress : matchedAddresses) {
                    addPossibleAddresses(possibleAddresses, null, InetAddress.getByName(matchedAddress), port, count);
                }
            } else {
                final String host = addressHolder.getAddress();
                final InterfacesConfig interfaces = networkConfig.getInterfaces();
                if (interfaces.isEnabled()) {
                    final InetAddress[] inetAddresses = InetAddress.getAllByName(host);
                    for (InetAddress inetAddress : inetAddresses) {
                        if (AddressUtil.matchAnyInterface(inetAddress.getHostAddress(), interfaces.getInterfaces())) {
                            addPossibleAddresses(possibleAddresses, host, inetAddress, port, count);
                        }
                    }
                } else {
                    addPossibleAddresses(possibleAddresses, host, null, port, count);
                }
            }
        } catch (UnknownHostException e) {
            logger.warning("Cannot resolve hostname '" + addressHolder.getAddress() + "'. Please make sure host is valid and reachable.");
            if (logger.isFineEnabled()) {
                logger.fine("Error during resolving possible target!", e);
            }
        }
    }
    possibleAddresses.remove(node.getThisAddress());
    return possibleAddresses;
}
Also used : InterfacesConfig(com.hazelcast.config.InterfacesConfig) Address(com.hazelcast.cluster.Address) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) ConfigAccessor.getActiveMemberNetworkConfig(com.hazelcast.config.ConfigAccessor.getActiveMemberNetworkConfig) NetworkConfig(com.hazelcast.config.NetworkConfig) AddressMatcher(com.hazelcast.internal.util.AddressUtil.AddressMatcher) AddressHolder(com.hazelcast.internal.util.AddressUtil.AddressHolder) InetAddress(java.net.InetAddress) InvalidAddressException(com.hazelcast.internal.util.AddressUtil.InvalidAddressException) HashSet(java.util.HashSet)

Example 5 with AddressMatcher

use of com.hazelcast.internal.util.AddressUtil.AddressMatcher in project hazelcast by hazelcast.

the class AddressUtilTest method testGetMatchingIpv4Addresses_whenWildcardForLastPart.

@Test
public void testGetMatchingIpv4Addresses_whenWildcardForLastPart() {
    AddressMatcher addressMatcher = AddressUtil.getAddressMatcher("192.168.1.*");
    Collection<String> actual = AddressUtil.getMatchingIpv4Addresses(addressMatcher);
    assertEquals(256, actual.size());
}
Also used : Ip4AddressMatcher(com.hazelcast.internal.util.AddressUtil.Ip4AddressMatcher) AddressMatcher(com.hazelcast.internal.util.AddressUtil.AddressMatcher) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

AddressMatcher (com.hazelcast.internal.util.AddressUtil.AddressMatcher)5 Ip4AddressMatcher (com.hazelcast.internal.util.AddressUtil.Ip4AddressMatcher)4 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Test (org.junit.Test)4 Address (com.hazelcast.cluster.Address)1 ConfigAccessor.getActiveMemberNetworkConfig (com.hazelcast.config.ConfigAccessor.getActiveMemberNetworkConfig)1 InterfacesConfig (com.hazelcast.config.InterfacesConfig)1 NetworkConfig (com.hazelcast.config.NetworkConfig)1 AddressHolder (com.hazelcast.internal.util.AddressUtil.AddressHolder)1 InvalidAddressException (com.hazelcast.internal.util.AddressUtil.InvalidAddressException)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 HashSet (java.util.HashSet)1