use of java.net.Inet4Address in project hazelcast by hazelcast.
the class MulticastLoopbackModeTest method hasConfiguredNetworkInterface.
/**
* Replies if a network interface was properly configured.
*
* @return <code>true</code> if there is at least one configured interface;
* <code>false</code> otherwise.
*/
protected static boolean hasConfiguredNetworkInterface() {
try {
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface i = e.nextElement();
Enumeration<InetAddress> as = i.getInetAddresses();
while (as.hasMoreElements()) {
InetAddress a = as.nextElement();
if (a instanceof Inet4Address && !a.isLoopbackAddress() && !a.isMulticastAddress()) {
return true;
}
}
}
} catch (Exception ignored) {
// silently cast the exceptions
}
return false;
}
use of java.net.Inet4Address in project JAirPort by froks.
the class JmDNSImpl method getServiceInfoFromCache.
ServiceInfoImpl getServiceInfoFromCache(String type, String name, String subtype, boolean persistent) {
// Check if the answer is in the cache.
ServiceInfoImpl info = new ServiceInfoImpl(type, name, subtype, 0, 0, 0, persistent, (byte[]) null);
DNSEntry pointerEntry = this.getCache().getDNSEntry(new DNSRecord.Pointer(type, DNSRecordClass.CLASS_ANY, false, 0, info.getQualifiedName()));
if (pointerEntry instanceof DNSRecord) {
ServiceInfoImpl cachedInfo = (ServiceInfoImpl) ((DNSRecord) pointerEntry).getServiceInfo(persistent);
if (cachedInfo != null) {
// To get a complete info record we need to retrieve the service, address and the text bytes.
Map<Fields, String> map = cachedInfo.getQualifiedNameMap();
byte[] srvBytes = null;
String server = "";
DNSEntry serviceEntry = this.getCache().getDNSEntry(info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_ANY);
if (serviceEntry instanceof DNSRecord) {
ServiceInfo cachedServiceEntryInfo = ((DNSRecord) serviceEntry).getServiceInfo(persistent);
if (cachedServiceEntryInfo != null) {
cachedInfo = new ServiceInfoImpl(map, cachedServiceEntryInfo.getPort(), cachedServiceEntryInfo.getWeight(), cachedServiceEntryInfo.getPriority(), persistent, (byte[]) null);
srvBytes = cachedServiceEntryInfo.getTextBytes();
server = cachedServiceEntryInfo.getServer();
}
}
DNSEntry addressEntry = this.getCache().getDNSEntry(server, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY);
if (addressEntry instanceof DNSRecord) {
ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(persistent);
if (cachedAddressInfo != null) {
for (Inet4Address address : cachedAddressInfo.getInet4Addresses()) {
cachedInfo.addAddress(address);
}
cachedInfo._setText(cachedAddressInfo.getTextBytes());
}
}
addressEntry = this.getCache().getDNSEntry(server, DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_ANY);
if (addressEntry instanceof DNSRecord) {
ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(persistent);
if (cachedAddressInfo != null) {
for (Inet6Address address : cachedAddressInfo.getInet6Addresses()) {
cachedInfo.addAddress(address);
}
cachedInfo._setText(cachedAddressInfo.getTextBytes());
}
}
DNSEntry textEntry = this.getCache().getDNSEntry(cachedInfo.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_ANY);
if (textEntry instanceof DNSRecord) {
ServiceInfo cachedTextInfo = ((DNSRecord) textEntry).getServiceInfo(persistent);
if (cachedTextInfo != null) {
cachedInfo._setText(cachedTextInfo.getTextBytes());
}
}
if (cachedInfo.getTextBytes().length == 0) {
cachedInfo._setText(srvBytes);
}
if (cachedInfo.hasData()) {
info = cachedInfo;
}
}
}
return info;
}
use of java.net.Inet4Address in project JAirPort by froks.
the class ServiceInfoImpl method clone.
/*
* (non-Javadoc)
* @see javax.jmdns.ServiceInfo#clone()
*/
@Override
public ServiceInfoImpl clone() {
ServiceInfoImpl serviceInfo = new ServiceInfoImpl(this.getQualifiedNameMap(), _port, _weight, _priority, _persistent, _text);
Inet6Address[] ipv6Addresses = this.getInet6Addresses();
for (Inet6Address address : ipv6Addresses) {
serviceInfo._ipv6Addresses.add(address);
}
Inet4Address[] ipv4Addresses = this.getInet4Addresses();
for (Inet4Address address : ipv4Addresses) {
serviceInfo._ipv4Addresses.add(address);
}
return serviceInfo;
}
use of java.net.Inet4Address in project JAirPort by froks.
the class ServiceInfoImpl method updateRecord.
/**
* JmDNS callback to update a DNS record.
*
* @param dnsCache
* @param now
* @param rec
*/
@Override
public void updateRecord(DNSCache dnsCache, long now, DNSEntry rec) {
if ((rec instanceof DNSRecord) && !rec.isExpired(now)) {
boolean serviceUpdated = false;
switch(rec.getRecordType()) {
case // IPv4
TYPE_A:
if (rec.getName().equalsIgnoreCase(this.getServer())) {
_ipv4Addresses.add((Inet4Address) ((DNSRecord.Address) rec).getAddress());
serviceUpdated = true;
}
break;
case // IPv6
TYPE_AAAA:
if (rec.getName().equalsIgnoreCase(this.getServer())) {
_ipv6Addresses.add((Inet6Address) ((DNSRecord.Address) rec).getAddress());
serviceUpdated = true;
}
break;
case TYPE_SRV:
if (rec.getName().equalsIgnoreCase(this.getQualifiedName())) {
DNSRecord.Service srv = (DNSRecord.Service) rec;
boolean serverChanged = (_server == null) || !_server.equalsIgnoreCase(srv.getServer());
_server = srv.getServer();
_port = srv.getPort();
_weight = srv.getWeight();
_priority = srv.getPriority();
if (serverChanged) {
_ipv4Addresses.clear();
_ipv6Addresses.clear();
for (DNSEntry entry : dnsCache.getDNSEntryList(_server, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_IN)) {
this.updateRecord(dnsCache, now, entry);
}
for (DNSEntry entry : dnsCache.getDNSEntryList(_server, DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_IN)) {
this.updateRecord(dnsCache, now, entry);
}
// We do not want to trigger the listener in this case as it will be triggered if the address resolves.
} else {
serviceUpdated = true;
}
}
break;
case TYPE_TXT:
if (rec.getName().equalsIgnoreCase(this.getQualifiedName())) {
DNSRecord.Text txt = (DNSRecord.Text) rec;
_text = txt.getText();
serviceUpdated = true;
}
break;
case TYPE_PTR:
if ((this.getSubtype().length() == 0) && (rec.getSubtype().length() != 0)) {
_subtype = rec.getSubtype();
serviceUpdated = true;
}
break;
default:
break;
}
if (serviceUpdated && this.hasData()) {
JmDNSImpl dns = this.getDns();
if (dns != null) {
ServiceEvent event = ((DNSRecord) rec).getServiceEvent(dns);
event = new ServiceEventImpl(dns, event.getType(), event.getName(), this);
dns.handleServiceResolved(event);
}
}
// This is done, to notify the wait loop in method JmDNS.waitForInfoData(ServiceInfo info, int timeout);
synchronized (this) {
this.notifyAll();
}
}
}
use of java.net.Inet4Address in project j2objc by google.
the class NetworkInterfaceTest method testLoopback.
public void testLoopback() throws Exception {
// We know lo shouldn't have a hardware address.
NetworkInterface lo = NetworkInterface.getByName("lo0");
assertNull(lo.getHardwareAddress());
// But eth0, if it exists, should...
NetworkInterface eth0 = NetworkInterface.getByName("eth0");
if (eth0 != null) {
assertEquals(6, eth0.getHardwareAddress().length);
for (InterfaceAddress ia : eth0.getInterfaceAddresses()) {
if (ia.getAddress() instanceof Inet4Address) {
assertNotNull(ia.getBroadcast());
}
}
}
}
Aggregations