Search in sources :

Example 21 with ServiceInfo

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

the class JmDNSImpl method __recover.

void __recover() {
    // 
    if (logger.isLoggable(Level.FINER)) {
        logger.finer(this.getName() + "recover() Cleanning up");
    }
    logger.warning("RECOVERING");
    // Purge the timer
    this.purgeTimer();
    // We need to keep a copy for reregistration
    final Collection<ServiceInfo> oldServiceInfos = new ArrayList<ServiceInfo>(getServices().values());
    // Cancel all services
    this.unregisterAllServices();
    this.disposeServiceCollectors();
    this.waitForCanceled(DNSConstants.CLOSE_TIMEOUT);
    // Purge the canceler timer
    this.purgeStateTimer();
    // 
    // close multicast socket
    this.closeMulticastSocket();
    // 
    this.getCache().clear();
    if (logger.isLoggable(Level.FINER)) {
        logger.finer(this.getName() + "recover() All is clean");
    }
    if (this.isCanceled()) {
        // 
        for (ServiceInfo info : oldServiceInfos) {
            ((ServiceInfoImpl) info).recoverState();
        }
        this.recoverState();
        try {
            this.openMulticastSocket(this.getLocalHost());
            this.start(oldServiceInfos);
        } catch (final Exception exception) {
            logger.log(Level.WARNING, this.getName() + "recover() Start services exception ", exception);
        }
        logger.log(Level.WARNING, this.getName() + "recover() We are back!");
    } else {
        // We have a problem. We could not clear the state.
        logger.log(Level.WARNING, this.getName() + "recover() Could not recover we are Down!");
        if (this.getDelegate() != null) {
            this.getDelegate().cannotRecoverFromIOError(this.getDns(), oldServiceInfos);
        }
    }
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) IOException(java.io.IOException)

Example 22 with ServiceInfo

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

the class JmDNSImpl method listBySubtype.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, ServiceInfo[]> listBySubtype(String type, long timeout) {
    Map<String, List<ServiceInfo>> map = new HashMap<String, List<ServiceInfo>>(5);
    for (ServiceInfo info : this.list(type, timeout)) {
        String subtype = info.getSubtype().toLowerCase();
        if (!map.containsKey(subtype)) {
            map.put(subtype, new ArrayList<ServiceInfo>(10));
        }
        map.get(subtype).add(info);
    }
    Map<String, ServiceInfo[]> result = new HashMap<String, ServiceInfo[]>(map.size());
    for (String subtype : map.keySet()) {
        List<ServiceInfo> infoForSubType = map.get(subtype);
        result.put(subtype, infoForSubType.toArray(new ServiceInfo[infoForSubType.size()]));
    }
    return result;
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 23 with ServiceInfo

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

the class JmDNSImpl method start.

private void start(Collection<? extends ServiceInfo> serviceInfos) {
    if (_incomingListener == null) {
        _incomingListener = new SocketListener(this);
        _incomingListener.start();
    }
    this.startProber();
    for (ServiceInfo info : serviceInfos) {
        try {
            this.registerService(new ServiceInfoImpl(info));
        } catch (final Exception exception) {
            logger.log(Level.WARNING, "start() Registration exception ", exception);
        }
    }
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) SocketException(java.net.SocketException) IOException(java.io.IOException)

Example 24 with ServiceInfo

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

the class JmDNSImpl method updateRecord.

// Remind: Method updateRecord should receive a better name.
/**
 * Notify all listeners that a record was updated.
 *
 * @param now
 *            update date
 * @param rec
 *            DNS record
 * @param operation
 *            DNS cache operation
 */
public void updateRecord(long now, DNSRecord rec, Operation operation) {
    // We do not want to block the entire DNS while we are updating the record for each listener (service info)
    {
        List<DNSListener> listenerList = null;
        synchronized (_listeners) {
            listenerList = new ArrayList<DNSListener>(_listeners);
        }
        for (DNSListener listener : listenerList) {
            listener.updateRecord(this.getCache(), now, rec);
        }
    }
    if (DNSRecordType.TYPE_PTR.equals(rec.getRecordType())) // if (DNSRecordType.TYPE_PTR.equals(rec.getRecordType()) || DNSRecordType.TYPE_SRV.equals(rec.getRecordType()))
    {
        ServiceEvent event = rec.getServiceEvent(this);
        if ((event.getInfo() == null) || !event.getInfo().hasData()) {
            // We do not care about the subtype because the info is only used if complete and the subtype will then be included.
            ServiceInfo info = this.getServiceInfoFromCache(event.getType(), event.getName(), "", false);
            if (info.hasData()) {
                event = new ServiceEventImpl(this, event.getType(), event.getName(), info);
            }
        }
        List<ServiceListenerStatus> list = _serviceListeners.get(event.getType().toLowerCase());
        final List<ServiceListenerStatus> serviceListenerList;
        if (list != null) {
            synchronized (list) {
                serviceListenerList = new ArrayList<ServiceListenerStatus>(list);
            }
        } else {
            serviceListenerList = Collections.emptyList();
        }
        if (logger.isLoggable(Level.FINEST)) {
            logger.finest(this.getName() + ".updating record for event: " + event + " list " + serviceListenerList + " operation: " + operation);
        }
        if (!serviceListenerList.isEmpty()) {
            final ServiceEvent localEvent = event;
            switch(operation) {
                case Add:
                    for (final ServiceListenerStatus listener : serviceListenerList) {
                        if (listener.isSynchronous()) {
                            listener.serviceAdded(localEvent);
                        } else {
                            _executor.submit(new Runnable() {

                                /**
                                 * {@inheritDoc}
                                 */
                                @Override
                                public void run() {
                                    listener.serviceAdded(localEvent);
                                }
                            });
                        }
                    }
                    break;
                case Remove:
                    for (final ServiceListenerStatus listener : serviceListenerList) {
                        if (listener.isSynchronous()) {
                            listener.serviceRemoved(localEvent);
                        } else {
                            _executor.submit(new Runnable() {

                                /**
                                 * {@inheritDoc}
                                 */
                                @Override
                                public void run() {
                                    listener.serviceRemoved(localEvent);
                                }
                            });
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) ServiceListenerStatus(javax.jmdns.impl.ListenerStatus.ServiceListenerStatus) ServiceEvent(javax.jmdns.ServiceEvent) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 25 with ServiceInfo

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

the class JmDNSImpl method getServiceInfoFromCache.

ServiceInfoImpl getServiceInfoFromCache(String type, String name, String subtype, boolean persistent) {
    // Check if the answer is in the cache.
    ServiceInfoImpl info = new ServiceInfoImpl(type, name, subtype, 0, 0, 0, persistent, (byte[]) null);
    DNSEntry pointerEntry = this.getCache().getDNSEntry(new DNSRecord.Pointer(type, DNSRecordClass.CLASS_ANY, false, 0, info.getQualifiedName()));
    if (pointerEntry instanceof DNSRecord) {
        ServiceInfoImpl cachedInfo = (ServiceInfoImpl) ((DNSRecord) pointerEntry).getServiceInfo(persistent);
        if (cachedInfo != null) {
            // To get a complete info record we need to retrieve the service, address and the text bytes.
            Map<Fields, String> map = cachedInfo.getQualifiedNameMap();
            byte[] srvBytes = null;
            String server = "";
            DNSEntry serviceEntry = this.getCache().getDNSEntry(info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_ANY);
            if (serviceEntry instanceof DNSRecord) {
                ServiceInfo cachedServiceEntryInfo = ((DNSRecord) serviceEntry).getServiceInfo(persistent);
                if (cachedServiceEntryInfo != null) {
                    cachedInfo = new ServiceInfoImpl(map, cachedServiceEntryInfo.getPort(), cachedServiceEntryInfo.getWeight(), cachedServiceEntryInfo.getPriority(), persistent, (byte[]) null);
                    srvBytes = cachedServiceEntryInfo.getTextBytes();
                    server = cachedServiceEntryInfo.getServer();
                }
            }
            for (DNSEntry addressEntry : this.getCache().getDNSEntryList(server, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY)) {
                if (addressEntry instanceof DNSRecord) {
                    ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(persistent);
                    if (cachedAddressInfo != null) {
                        for (Inet4Address address : cachedAddressInfo.getInet4Addresses()) {
                            cachedInfo.addAddress(address);
                        }
                        cachedInfo._setText(cachedAddressInfo.getTextBytes());
                    }
                }
            }
            for (DNSEntry addressEntry : this.getCache().getDNSEntryList(server, DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_ANY)) {
                if (addressEntry instanceof DNSRecord) {
                    ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(persistent);
                    if (cachedAddressInfo != null) {
                        for (Inet6Address address : cachedAddressInfo.getInet6Addresses()) {
                            cachedInfo.addAddress(address);
                        }
                        cachedInfo._setText(cachedAddressInfo.getTextBytes());
                    }
                }
            }
            DNSEntry textEntry = this.getCache().getDNSEntry(cachedInfo.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_ANY);
            if (textEntry instanceof DNSRecord) {
                ServiceInfo cachedTextInfo = ((DNSRecord) textEntry).getServiceInfo(persistent);
                if (cachedTextInfo != null) {
                    cachedInfo._setText(cachedTextInfo.getTextBytes());
                }
            }
            if (cachedInfo.getTextBytes().length == 0) {
                cachedInfo._setText(srvBytes);
            }
            if (cachedInfo.hasData()) {
                info = cachedInfo;
            }
        }
    }
    return info;
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) Inet4Address(java.net.Inet4Address) Fields(javax.jmdns.ServiceInfo.Fields) Inet6Address(java.net.Inet6Address)

Aggregations

ServiceInfo (javax.jmdns.ServiceInfo)43 ArrayList (java.util.ArrayList)15 IOException (java.io.IOException)14 JmDNS (javax.jmdns.JmDNS)11 List (java.util.List)10 LinkedList (java.util.LinkedList)7 HashMap (java.util.HashMap)6 ExecutorService (java.util.concurrent.ExecutorService)6 SocketException (java.net.SocketException)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 DNSOutgoing (javax.jmdns.impl.DNSOutgoing)4 Inet4Address (java.net.Inet4Address)3 Inet6Address (java.net.Inet6Address)3 InetAddress (java.net.InetAddress)3 URI (java.net.URI)3 ExecutionException (java.util.concurrent.ExecutionException)3 HashSet (java.util.HashSet)2 Callable (java.util.concurrent.Callable)2 Future (java.util.concurrent.Future)2 ServiceEvent (javax.jmdns.ServiceEvent)2