Search in sources :

Example 1 with ServiceListenerStatus

use of javax.jmdns.impl.ListenerStatus.ServiceListenerStatus in project JAirPort by froks.

the class JmDNSImpl method handleServiceResolved.

void handleServiceResolved(ServiceEvent event) {
    List<ServiceListenerStatus> list = _serviceListeners.get(event.getType().toLowerCase());
    final List<ServiceListenerStatus> listCopy;
    if ((list != null) && (!list.isEmpty())) {
        if ((event.getInfo() != null) && event.getInfo().hasData()) {
            final ServiceEvent localEvent = event;
            synchronized (list) {
                listCopy = new ArrayList<ServiceListenerStatus>(list);
            }
            for (final ServiceListenerStatus listener : listCopy) {
                _executor.submit(new Runnable() {

                    /** {@inheritDoc} */
                    @Override
                    public void run() {
                        listener.serviceResolved(localEvent);
                    }
                });
            }
        }
    }
}
Also used : ServiceListenerStatus(javax.jmdns.impl.ListenerStatus.ServiceListenerStatus) ServiceEvent(javax.jmdns.ServiceEvent)

Example 2 with ServiceListenerStatus

use of javax.jmdns.impl.ListenerStatus.ServiceListenerStatus in project JAirPort by froks.

the class JmDNSImpl method removeServiceListener.

/**
     * {@inheritDoc}
     */
@Override
public void removeServiceListener(String type, ServiceListener listener) {
    String loType = type.toLowerCase();
    List<ServiceListenerStatus> list = _serviceListeners.get(loType);
    if (list != null) {
        synchronized (list) {
            ServiceListenerStatus status = new ServiceListenerStatus(listener, ListenerStatus.ASYNCHONEOUS);
            list.remove(status);
            if (list.isEmpty()) {
                _serviceListeners.remove(loType, list);
            }
        }
    }
}
Also used : ServiceListenerStatus(javax.jmdns.impl.ListenerStatus.ServiceListenerStatus)

Example 3 with ServiceListenerStatus

use of javax.jmdns.impl.ListenerStatus.ServiceListenerStatus in project JAirPort by froks.

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 4 with ServiceListenerStatus

use of javax.jmdns.impl.ListenerStatus.ServiceListenerStatus in project JAirPort by froks.

the class JmDNSImpl method addServiceListener.

private void addServiceListener(String type, ServiceListener listener, boolean synch) {
    ServiceListenerStatus status = new ServiceListenerStatus(listener, synch);
    final String loType = type.toLowerCase();
    List<ServiceListenerStatus> list = _serviceListeners.get(loType);
    if (list == null) {
        if (_serviceListeners.putIfAbsent(loType, new LinkedList<ServiceListenerStatus>()) == null) {
            if (_serviceCollectors.putIfAbsent(loType, new ServiceCollector(type)) == null) {
                // We have a problem here. The service collectors must be called synchronously so that their cache get cleaned up immediately or we will report .
                this.addServiceListener(loType, _serviceCollectors.get(loType), ListenerStatus.SYNCHONEOUS);
            }
        }
        list = _serviceListeners.get(loType);
    }
    if (list != null) {
        synchronized (list) {
            if (!list.contains(listener)) {
                list.add(status);
            }
        }
    }
    // report cached service types
    final List<ServiceEvent> serviceEvents = new ArrayList<ServiceEvent>();
    Collection<DNSEntry> dnsEntryLits = this.getCache().allValues();
    for (DNSEntry entry : dnsEntryLits) {
        final DNSRecord record = (DNSRecord) entry;
        if (record.getRecordType() == DNSRecordType.TYPE_SRV) {
            if (record.getKey().endsWith(loType)) {
                // Do not used the record embedded method for generating event this will not work.
                // serviceEvents.add(record.getServiceEvent(this));
                serviceEvents.add(new ServiceEventImpl(this, record.getType(), toUnqualifiedName(record.getType(), record.getName()), record.getServiceInfo()));
            }
        }
    }
    // Actually call listener with all service events added above
    for (ServiceEvent serviceEvent : serviceEvents) {
        status.serviceAdded(serviceEvent);
    }
    // Create/start ServiceResolver
    this.startServiceResolver(type);
}
Also used : ServiceListenerStatus(javax.jmdns.impl.ListenerStatus.ServiceListenerStatus) ServiceEvent(javax.jmdns.ServiceEvent) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Aggregations

ServiceListenerStatus (javax.jmdns.impl.ListenerStatus.ServiceListenerStatus)4 ServiceEvent (javax.jmdns.ServiceEvent)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)1 ServiceInfo (javax.jmdns.ServiceInfo)1