Search in sources :

Example 1 with ServiceTypeListener

use of javax.jmdns.ServiceTypeListener 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);
    }
}
Also used : JmDNS(javax.jmdns.JmDNS) ServiceListener(javax.jmdns.ServiceListener) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ServiceInfo(javax.jmdns.ServiceInfo) NetworkTopologyEvent(javax.jmdns.NetworkTopologyEvent) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) InetAddress(java.net.InetAddress) ServiceTypeListener(javax.jmdns.ServiceTypeListener) NetworkTopologyListener(javax.jmdns.NetworkTopologyListener)

Aggregations

IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 JmDNS (javax.jmdns.JmDNS)1 NetworkTopologyEvent (javax.jmdns.NetworkTopologyEvent)1 NetworkTopologyListener (javax.jmdns.NetworkTopologyListener)1 ServiceInfo (javax.jmdns.ServiceInfo)1 ServiceListener (javax.jmdns.ServiceListener)1 ServiceTypeListener (javax.jmdns.ServiceTypeListener)1