Search in sources :

Example 16 with JmDNS

use of javax.jmdns.JmDNS in project EngineDriver by JMRI.

the class JmmDNSImpl method registerService.

/*
     * (non-Javadoc)
     * @see javax.jmdns.JmmDNS#registerService(javax.jmdns.ServiceInfo)
     */
@Override
public void registerService(ServiceInfo info) throws IOException {
    // We need to get the list out of the synchronized block to prevent dead locks
    final JmDNS[] dnsArray = this.getDNS();
    // This is really complex. We need to clone the service info for each DNS but then we loose the ability to update it.
    synchronized (_services) {
        for (JmDNS mDNS : dnsArray) {
            mDNS.registerService(info.clone());
        }
        ((ServiceInfoImpl) info).setDelegate(this);
        _services.put(info.getQualifiedName(), info);
    }
}
Also used : JmDNS(javax.jmdns.JmDNS)

Example 17 with JmDNS

use of javax.jmdns.JmDNS in project EngineDriver by JMRI.

the class JmmDNSImpl method list.

/*
     * (non-Javadoc)
     * @see javax.jmdns.JmmDNS#list(java.lang.String, long)
     */
@Override
public ServiceInfo[] list(final String type, final long timeout) {
    final JmDNS[] dnsArray = this.getDNS();
    // We need to run this in parallel to respect the timeout.
    final Set<ServiceInfo> result = new HashSet<ServiceInfo>(dnsArray.length * 5);
    if (dnsArray.length > 0) {
        List<Callable<List<ServiceInfo>>> tasks = new ArrayList<Callable<List<ServiceInfo>>>(dnsArray.length);
        for (final JmDNS mDNS : dnsArray) {
            tasks.add(new Callable<List<ServiceInfo>>() {

                @Override
                public List<ServiceInfo> call() throws Exception {
                    return Arrays.asList(mDNS.list(type, timeout));
                }
            });
        }
        ExecutorService executor = Executors.newFixedThreadPool(tasks.size(), new NamedThreadFactory("JmmDNS.list"));
        try {
            List<Future<List<ServiceInfo>>> results = Collections.emptyList();
            try {
                results = executor.invokeAll(tasks, timeout + 100, TimeUnit.MILLISECONDS);
            } catch (InterruptedException exception) {
                logger.log(Level.FINE, "Interrupted ", exception);
                Thread.currentThread().interrupt();
            // Will terminate next loop early.
            }
            for (Future<List<ServiceInfo>> future : results) {
                if (future.isCancelled()) {
                    continue;
                }
                try {
                    result.addAll(future.get());
                } catch (InterruptedException exception) {
                    logger.log(Level.FINE, "Interrupted ", exception);
                    Thread.currentThread().interrupt();
                } catch (ExecutionException exception) {
                    logger.log(Level.WARNING, "Exception ", exception);
                }
            }
        } finally {
            executor.shutdown();
        }
    }
    return result.toArray(new ServiceInfo[result.size()]);
}
Also used : JmDNS(javax.jmdns.JmDNS) NamedThreadFactory(javax.jmdns.impl.util.NamedThreadFactory) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ServiceInfo(javax.jmdns.ServiceInfo) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 18 with JmDNS

use of javax.jmdns.JmDNS 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)

Example 19 with JmDNS

use of javax.jmdns.JmDNS in project EngineDriver by JMRI.

the class JmmDNSImpl method close.

/*
     * (non-Javadoc)
     * @see java.io.Closeable#close()
     */
@Override
public void close() throws IOException {
    if (_isClosing.compareAndSet(false, true)) {
        if (logger.isLoggable(Level.FINER)) {
            logger.finer("Cancelling JmmDNS: " + this);
        }
        _timer.cancel();
        _listenerExecutor.shutdown();
        _jmDNSExecutor.shutdown();
        // We need to cancel all the DNS
        ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("JmmDNS.close"));
        try {
            for (final JmDNS mDNS : this.getDNS()) {
                executor.submit(new Runnable() {

                    /**
                     * {@inheritDoc}
                     */
                    @Override
                    public void run() {
                        try {
                            mDNS.close();
                        } catch (IOException exception) {
                        // JmDNS never throws this is only because of the closeable interface
                        }
                    }
                });
            }
        } finally {
            executor.shutdown();
        }
        try {
            executor.awaitTermination(DNSConstants.CLOSE_TIMEOUT, TimeUnit.MILLISECONDS);
        } catch (InterruptedException exception) {
            logger.log(Level.WARNING, "Exception ", exception);
        }
        _knownMDNS.clear();
        _services.clear();
        _serviceListeners.clear();
        _typeListeners.clear();
        _serviceTypes.clear();
        _closed.set(true);
        JmmDNS.Factory.close();
    }
}
Also used : JmDNS(javax.jmdns.JmDNS) NamedThreadFactory(javax.jmdns.impl.util.NamedThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException)

Example 20 with JmDNS

use of javax.jmdns.JmDNS in project EngineDriver by JMRI.

the class JmmDNSImpl method getServiceInfos.

/*
     * (non-Javadoc)
     * @see javax.jmdns.JmmDNS#getServiceInfos(java.lang.String, java.lang.String, boolean, long)
     */
@Override
public ServiceInfo[] getServiceInfos(final String type, final String name, final boolean persistent, final long timeout) {
    // We need to run this in parallel to respect the timeout.
    final JmDNS[] dnsArray = this.getDNS();
    final Set<ServiceInfo> result = new HashSet<ServiceInfo>(dnsArray.length);
    if (dnsArray.length > 0) {
        List<Callable<ServiceInfo>> tasks = new ArrayList<Callable<ServiceInfo>>(dnsArray.length);
        for (final JmDNS mDNS : dnsArray) {
            tasks.add(new Callable<ServiceInfo>() {

                @Override
                public ServiceInfo call() throws Exception {
                    return mDNS.getServiceInfo(type, name, persistent, timeout);
                }
            });
        }
        ExecutorService executor = Executors.newFixedThreadPool(tasks.size(), new NamedThreadFactory("JmmDNS.getServiceInfos"));
        try {
            List<Future<ServiceInfo>> results = Collections.emptyList();
            try {
                results = executor.invokeAll(tasks, timeout + 100, TimeUnit.MILLISECONDS);
            } catch (InterruptedException exception) {
                logger.log(Level.FINE, "Interrupted ", exception);
                Thread.currentThread().interrupt();
            // Will terminate next loop early.
            }
            for (Future<ServiceInfo> future : results) {
                if (future.isCancelled()) {
                    continue;
                }
                try {
                    ServiceInfo info = future.get();
                    if (info != null) {
                        result.add(info);
                    }
                } catch (InterruptedException exception) {
                    logger.log(Level.FINE, "Interrupted ", exception);
                    Thread.currentThread().interrupt();
                } catch (ExecutionException exception) {
                    logger.log(Level.WARNING, "Exception ", exception);
                }
            }
        } finally {
            executor.shutdown();
        }
    }
    return result.toArray(new ServiceInfo[result.size()]);
}
Also used : JmDNS(javax.jmdns.JmDNS) NamedThreadFactory(javax.jmdns.impl.util.NamedThreadFactory) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ServiceInfo(javax.jmdns.ServiceInfo) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Aggregations

JmDNS (javax.jmdns.JmDNS)25 IOException (java.io.IOException)14 ServiceInfo (javax.jmdns.ServiceInfo)12 ExecutorService (java.util.concurrent.ExecutorService)6 InetAddress (java.net.InetAddress)4 ArrayList (java.util.ArrayList)4 ExecutionException (java.util.concurrent.ExecutionException)4 List (java.util.List)3 NetworkTopologyEvent (javax.jmdns.NetworkTopologyEvent)3 NetworkTopologyListener (javax.jmdns.NetworkTopologyListener)3 ServiceListener (javax.jmdns.ServiceListener)3 NamedThreadFactory (javax.jmdns.impl.util.NamedThreadFactory)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Callable (java.util.concurrent.Callable)2 Future (java.util.concurrent.Future)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 BigInteger (java.math.BigInteger)1 Inet4Address (java.net.Inet4Address)1