@SuppressWarnings({"checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity"}) protected Collection<Address> getPossibleAddresses() { final Collection<String> possibleMembers = getMembers(); final Set<Address> possibleAddresses = new HashSet<Address>(); final NetworkConfig networkConfig = config.getNetworkConfig(); 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) { EmptyStatement.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.isFinestEnabled()) { logger.finest("Error during resolving possible target!", e); } } } possibleAddresses.remove(node.getThisAddress()); return possibleAddresses; }
private Address getRequiredMemberAddress() { TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig(); String host = tcpIpConfig.getRequiredMember(); try { AddressHolder addressHolder = AddressUtil.getAddressHolder(host, config.getNetworkConfig().getPort()); if (AddressUtil.isIpAddress(addressHolder.getAddress())) { return new Address(addressHolder.getAddress(), addressHolder.getPort()); } InterfacesConfig interfaces = config.getNetworkConfig().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; }
protected NetworkConfig getLocalhostTcpIpNetworkConfig(int port) { NetworkConfig networkConfig = new NetworkConfig(); networkConfig.setPort(port); networkConfig.getJoin().getMulticastConfig().setEnabled(false); TcpIpConfig tcpIpConfig = networkConfig.getJoin().getTcpIpConfig(); tcpIpConfig.setEnabled(true); tcpIpConfig.addMember("127.0.0.1"); InterfacesConfig interfacesConfig = networkConfig.getInterfaces(); interfacesConfig.setEnabled(true); interfacesConfig.setInterfaces(Collections.singleton("127.0.0.*")); return networkConfig; }
@Test public void testNodeStartup() { TestHazelcastFactory factory = new TestHazelcastFactory(); Config config = new Config(); config.getNetworkConfig().setPort(50001); InterfacesConfig interfaces = config.getNetworkConfig().getInterfaces(); interfaces.clear(); interfaces.setEnabled(true); interfaces.addInterface("127.0.0.1"); final HazelcastInstance hazelcastInstance1 = factory.newHazelcastInstance(config); final HazelcastInstance hazelcastInstance2 = factory.newHazelcastInstance(config); final HazelcastInstance hazelcastInstance3 = factory.newHazelcastInstance(config); try { String xmlFileName = "hazelcast-client-discovery-spi-test.xml"; InputStream xmlResource = ClientDiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName); ClientConfig clientConfig = new XmlClientConfigBuilder(xmlResource).build(); final HazelcastInstance client = factory.newHazelcastClient(clientConfig); assertNotNull(hazelcastInstance1); assertNotNull(hazelcastInstance2); assertNotNull(hazelcastInstance3); assertNotNull(client); assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { assertEquals(3, hazelcastInstance1.getCluster().getMembers().size()); assertEquals(3, hazelcastInstance2.getCluster().getMembers().size()); assertEquals(3, hazelcastInstance3.getCluster().getMembers().size()); assertEquals(3, client.getCluster().getMembers().size()); } }); } finally { factory.shutdownAll(); } }