InetAddress address = event.getInetAddress();
try {
synchronized (this) {
if (_knownMDNS.putIfAbsent(address, JmDNS.create(address)) == null) {
// We need to register the services and listeners with the new JmDNS
final JmDNS dns = _knownMDNS.get(address);
final Collection<String> types = _serviceTypes;
final Collection<ServiceInfo> infos = _services.values();
final Collection<ServiceTypeListener> typeListeners = _typeListeners;
final Map<String, List<ServiceListener>> serviceListeners = _serviceListeners;
_jmDNSExecutor.submit(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
// Register Types
for (String type : types) {
dns.registerServiceType(type);
}
// Register services
for (ServiceInfo info : infos) {
try {
dns.registerService(info.clone());
} catch (IOException exception) {
// logger.warning("Unexpected unhandled exception: " + exception);
}
}
// Add ServiceType Listeners
for (ServiceTypeListener listener : typeListeners) {
try {
dns.addServiceTypeListener(listener);
} catch (IOException exception) {
// logger.warning("Unexpected unhandled exception: " + exception);
}
}
// Add Service Listeners
for (String type : serviceListeners.keySet()) {
List<ServiceListener> listeners = serviceListeners.get(type);
synchronized (listeners) {
for (ServiceListener listener : listeners) {
dns.addServiceListener(type, listener);
}
}
}
}
});