use of javax.jmdns.ServiceListener in project EngineDriver by JMRI.
the class JmmDNSImpl method addServiceListener.
/*
* (non-Javadoc)
* @see javax.jmdns.JmmDNS#addServiceListener(java.lang.String, javax.jmdns.ServiceListener)
*/
@Override
public void addServiceListener(String type, ServiceListener listener) {
final String loType = type.toLowerCase();
List<ServiceListener> list = _serviceListeners.get(loType);
if (list == null) {
_serviceListeners.putIfAbsent(loType, new LinkedList<ServiceListener>());
list = _serviceListeners.get(loType);
}
if (list != null) {
synchronized (list) {
if (!list.contains(listener)) {
list.add(listener);
}
}
}
for (JmDNS mDNS : this.getDNS()) {
mDNS.addServiceListener(type, listener);
}
}
use of javax.jmdns.ServiceListener in project EngineDriver by JMRI.
the class JmmDNSImpl method inetAddressAdded.
/*
* (non-Javadoc)
* @see javax.jmdns.NetworkTopologyListener#inetAddressAdded(javax.jmdns.NetworkTopologyEvent)
*/
@Override
public void inetAddressAdded(NetworkTopologyEvent event) {
InetAddress address = event.getInetAddress();
try {
if (!_knownMDNS.containsKey(address)) {
synchronized (_knownMDNS) {
if (!_knownMDNS.containsKey(address)) {
final JmDNS dns = JmDNS.create(address);
if (_knownMDNS.putIfAbsent(address, dns) == null) {
// We need to register the services and listeners with the new JmDNS
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);
}
}
}
}
});
final NetworkTopologyEvent jmdnsEvent = new NetworkTopologyEventImpl(dns, address);
for (final NetworkTopologyListener listener : this.networkListeners()) {
_listenerExecutor.submit(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
listener.inetAddressAdded(jmdnsEvent);
}
});
}
} else {
dns.close();
}
}
}
}
} catch (Exception e) {
logger.warning("Unexpected unhandled exception: " + e);
}
}
use of javax.jmdns.ServiceListener in project EngineDriver by JMRI.
the class JmmDNSImpl method removeServiceListener.
/*
* (non-Javadoc)
* @see javax.jmdns.JmmDNS#removeServiceListener(java.lang.String, javax.jmdns.ServiceListener)
*/
@Override
public void removeServiceListener(String type, ServiceListener listener) {
String loType = type.toLowerCase();
List<ServiceListener> list = _serviceListeners.get(loType);
if (list != null) {
synchronized (list) {
list.remove(listener);
if (list.isEmpty()) {
_serviceListeners.remove(loType, list);
}
}
}
for (JmDNS mDNS : this.getDNS()) {
mDNS.removeServiceListener(type, listener);
}
}
Aggregations