use of java.net.InetAddress in project zookeeper by apache.
the class QuorumPeerTest method testQuorumPeerListendOnSpecifiedClientIP.
/**
* Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2301
*/
@Test
public void testQuorumPeerListendOnSpecifiedClientIP() throws IOException {
long myId = 1;
File dataDir = ClientBase.createTmpDir();
int clientPort = PortAssignment.unique();
Map<Long, QuorumServer> peersView = new HashMap<Long, QuorumServer>();
InetAddress clientIP = InetAddress.getLoopbackAddress();
peersView.put(Long.valueOf(myId), new QuorumServer(myId, new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, clientPort), LearnerType.PARTICIPANT));
/**
* QuorumPeer constructor without QuorumVerifier
*/
QuorumPeer peer1 = new QuorumPeer(peersView, dataDir, dataDir, clientPort, electionAlg, myId, tickTime, initLimit, syncLimit);
String hostString1 = peer1.cnxnFactory.getLocalAddress().getHostString();
assertEquals(clientIP.getHostAddress(), hostString1);
// cleanup
peer1.shutdown();
/**
* QuorumPeer constructor with QuorumVerifier
*/
peersView.clear();
clientPort = PortAssignment.unique();
peersView.put(Long.valueOf(myId), new QuorumServer(myId, new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, clientPort), LearnerType.PARTICIPANT));
QuorumPeer peer2 = new QuorumPeer(peersView, dataDir, dataDir, clientPort, electionAlg, myId, tickTime, initLimit, syncLimit);
String hostString2 = peer2.cnxnFactory.getLocalAddress().getHostString();
assertEquals(clientIP.getHostAddress(), hostString2);
// cleanup
peer2.shutdown();
}
use of java.net.InetAddress in project crate by crate.
the class AzureUnicastHostsProvider method listIPAddresses.
public static List<String> listIPAddresses(NetworkResourceProviderClient networkResourceProviderClient, String rgName, String vnetName, String subnetName, String discoveryMethod, HostType hostType, ESLogger logger) {
List<String> ipList = new ArrayList<>();
List<ResourceId> ipConfigurations = new ArrayList<>();
try {
List<Subnet> subnets = networkResourceProviderClient.getVirtualNetworksOperations().get(rgName, vnetName).getVirtualNetwork().getSubnets();
if (discoveryMethod.equalsIgnoreCase(AzureDiscovery.VNET)) {
for (Subnet subnet : subnets) {
ipConfigurations.addAll(subnet.getIpConfigurations());
}
} else {
for (Subnet subnet : subnets) {
if (subnet.getName().equalsIgnoreCase(subnetName)) {
ipConfigurations.addAll(subnet.getIpConfigurations());
}
}
}
for (ResourceId resourceId : ipConfigurations) {
String[] nicURI = resourceId.getId().split("/");
NetworkInterface nic = networkResourceProviderClient.getNetworkInterfacesOperations().get(rgName, nicURI[nicURI.length - 3]).getNetworkInterface();
ArrayList<NetworkInterfaceIpConfiguration> ips = nic.getIpConfigurations();
// find public ip address
for (NetworkInterfaceIpConfiguration ipConfiguration : ips) {
String networkAddress = null;
// Let's detect if we want to use public or private IP
switch(hostType) {
case PRIVATE_IP:
InetAddress privateIp = InetAddress.getByName(ipConfiguration.getPrivateIpAddress());
if (privateIp != null) {
networkAddress = NetworkAddress.format(privateIp);
} else {
logger.trace("no private ip provided. ignoring [{}]...", nic.getName());
}
break;
case PUBLIC_IP:
if (ipConfiguration.getPublicIpAddress() != null) {
String[] pipID = ipConfiguration.getPublicIpAddress().getId().split("/");
PublicIpAddress pip = networkResourceProviderClient.getPublicIpAddressesOperations().get(rgName, pipID[pipID.length - 1]).getPublicIpAddress();
networkAddress = NetworkAddress.format(InetAddress.getByName(pip.getIpAddress()));
}
if (networkAddress == null) {
logger.trace("no public ip provided. ignoring [{}]...", nic.getName());
}
break;
}
if (networkAddress == null) {
// We have a bad parameter here or not enough information from azure
logger.warn("no network address found. ignoring [{}]...", nic.getName());
continue;
} else {
ipList.add(networkAddress);
}
}
}
} catch (Exception e) {
logger.error("Could not retrieve IP addresses for unicast host list", e);
}
return ipList;
}
use of java.net.InetAddress in project jcollectd by collectd.
the class UdpReceiver method getSocket.
public DatagramSocket getSocket() throws IOException {
if (_socket == null) {
if (_bindAddress == null) {
_socket = new DatagramSocket(_port);
} else {
InetAddress addr = InetAddress.getByName(_bindAddress);
if (addr.isMulticastAddress()) {
MulticastSocket mcast = new MulticastSocket(_port);
if (_ifAddress != null) {
mcast.setInterface(InetAddress.getByName(_ifAddress));
}
mcast.joinGroup(addr);
_socket = mcast;
} else {
_socket = new DatagramSocket(_port, addr);
}
}
}
return _socket;
}
use of java.net.InetAddress in project cw-omnibus by commonsguy.
the class WebServerService method raiseReadyEvent.
private void raiseReadyEvent() {
ServerStartedEvent event = new ServerStartedEvent();
try {
for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
NetworkInterface ni = enInterfaces.nextElement();
for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
InetAddress addr = enAddresses.nextElement();
if (addr instanceof Inet4Address) {
event.addUrl("http://" + addr.getHostAddress() + ":4999" + rootPath + "/");
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
EventBus.getDefault().removeAllStickyEvents();
EventBus.getDefault().postSticky(event);
}
use of java.net.InetAddress in project cw-omnibus by commonsguy.
the class WebServerService method raiseReadyEvent.
private void raiseReadyEvent() {
ServerStartedEvent event = new ServerStartedEvent();
try {
for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
NetworkInterface ni = enInterfaces.nextElement();
for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
InetAddress addr = enAddresses.nextElement();
if (addr instanceof Inet4Address) {
event.addUrl("http://" + addr.getHostAddress() + ":4999" + rootPath + "/");
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
EventBus.getDefault().removeAllStickyEvents();
EventBus.getDefault().postSticky(event);
}
Aggregations