Search in sources :

Example 1 with AddressHolder

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

the class TcpIpJoiner method getRequiredMemberAddress.

private Address getRequiredMemberAddress() {
    TcpIpConfig tcpIpConfig = joinConfig.getTcpIpConfig();
    String host = tcpIpConfig.getRequiredMember();
    try {
        AddressHolder addressHolder = AddressUtil.getAddressHolder(host, getActiveMemberNetworkConfig(config).getPort());
        if (AddressUtil.isIpAddress(addressHolder.getAddress())) {
            return new Address(addressHolder.getAddress(), addressHolder.getPort());
        }
        InterfacesConfig interfaces = getActiveMemberNetworkConfig(config).getInterfaces();
        if (interfaces.isEnabled()) {
            InetAddress[] inetAddresses = InetAddress.getAllByName(addressHolder.getAddress());
            if (inetAddresses.length > 1) {
                for (InetAddress inetAddress : inetAddresses) {
                    if (AddressUtil.matchAnyInterface(inetAddress.getHostAddress(), interfaces.getInterfaces())) {
                        return new Address(inetAddress, addressHolder.getPort());
                    }
                }
            } else if (AddressUtil.matchAnyInterface(inetAddresses[0].getHostAddress(), interfaces.getInterfaces())) {
                return new Address(addressHolder.getAddress(), addressHolder.getPort());
            }
        } else {
            return new Address(addressHolder.getAddress(), addressHolder.getPort());
        }
    } catch (final Exception e) {
        logger.warning(e);
    }
    return null;
}
Also used : AddressHolder(com.hazelcast.internal.util.AddressUtil.AddressHolder) InterfacesConfig(com.hazelcast.config.InterfacesConfig) Address(com.hazelcast.cluster.Address) InetAddress(java.net.InetAddress) TcpIpConfig(com.hazelcast.config.TcpIpConfig) InetAddress(java.net.InetAddress) InvalidAddressException(com.hazelcast.internal.util.AddressUtil.InvalidAddressException) UnknownHostException(java.net.UnknownHostException)

Example 2 with AddressHolder

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

the class AddressUtilTest method testParsingHostAndPort.

@Test
public void testParsingHostAndPort() {
    AddressHolder addressHolder = AddressUtil.getAddressHolder("[fe80::62c5:*:fe05:480a%en0]:8080");
    assertEquals("fe80::62c5:*:fe05:480a", addressHolder.getAddress());
    assertEquals(8080, addressHolder.getPort());
    assertEquals("en0", addressHolder.getScopeId());
    addressHolder = AddressUtil.getAddressHolder("[::ffff:192.0.2.128]:5700");
    assertEquals("::ffff:192.0.2.128", addressHolder.getAddress());
    assertEquals(5700, addressHolder.getPort());
    addressHolder = AddressUtil.getAddressHolder("192.168.1.1:5700");
    assertEquals("192.168.1.1", addressHolder.getAddress());
    assertEquals(5700, addressHolder.getPort());
    addressHolder = AddressUtil.getAddressHolder("hazelcast.com:80");
    assertEquals("hazelcast.com", addressHolder.getAddress());
    assertEquals(80, addressHolder.getPort());
}
Also used : AddressHolder(com.hazelcast.internal.util.AddressUtil.AddressHolder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with AddressHolder

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

the class AddressHelper method getSocketAddresses.

public static Addresses getSocketAddresses(String address) {
    AddressHolder addressHolder = AddressUtil.getAddressHolder(address, -1);
    String scopedAddress = getScopedHostName(addressHolder);
    int port = addressHolder.getPort();
    int maxPortTryCount = 1;
    if (port == -1) {
        maxPortTryCount = MAX_PORT_TRIES;
    }
    return getPossibleSocketAddresses(port, scopedAddress, maxPortTryCount);
}
Also used : AddressHolder(com.hazelcast.internal.util.AddressUtil.AddressHolder)

Example 4 with AddressHolder

use of com.hazelcast.internal.util.AddressUtil.AddressHolder 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)

Aggregations

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