Search in sources :

Example 1 with ServiceListener

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);
    }
}
Also used : ServiceListener(javax.jmdns.ServiceListener) JmDNS(javax.jmdns.JmDNS)

Example 2 with ServiceListener

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);
    }
}
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)

Example 3 with ServiceListener

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);
    }
}
Also used : ServiceListener(javax.jmdns.ServiceListener) JmDNS(javax.jmdns.JmDNS)

Aggregations

JmDNS (javax.jmdns.JmDNS)3 ServiceListener (javax.jmdns.ServiceListener)3 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 NetworkTopologyEvent (javax.jmdns.NetworkTopologyEvent)1 NetworkTopologyListener (javax.jmdns.NetworkTopologyListener)1 ServiceInfo (javax.jmdns.ServiceInfo)1 ServiceTypeListener (javax.jmdns.ServiceTypeListener)1