Search in sources :

Example 36 with ServiceInfo

use of javax.jmdns.ServiceInfo in project JAirPort by froks.

the class JmDNSImpl method makeServiceNameUnique.

/**
 * Generate a possibly unique name for a service using the information we have in the cache.
 *
 * @return returns true, if the name of the service info had to be changed.
 */
private boolean makeServiceNameUnique(ServiceInfoImpl info) {
    final String originalQualifiedName = info.getKey();
    final long now = System.currentTimeMillis();
    boolean collision;
    do {
        collision = false;
        // Check for collision in cache
        Collection<? extends DNSEntry> entryList = this.getCache().getDNSEntryList(info.getKey());
        if (entryList != null) {
            for (DNSEntry dnsEntry : entryList) {
                if (DNSRecordType.TYPE_SRV.equals(dnsEntry.getRecordType()) && !dnsEntry.isExpired(now)) {
                    final DNSRecord.Service s = (DNSRecord.Service) dnsEntry;
                    if (s.getPort() != info.getPort() || !s.getServer().equals(_localHost.getName())) {
                        if (logger.isLoggable(Level.FINER)) {
                            logger.finer("makeServiceNameUnique() JmDNS.makeServiceNameUnique srv collision:" + dnsEntry + " s.server=" + s.getServer() + " " + _localHost.getName() + " equals:" + (s.getServer().equals(_localHost.getName())));
                        }
                        info.setName(incrementName(info.getName()));
                        collision = true;
                        break;
                    }
                }
            }
        }
        // Check for collision with other service infos published by JmDNS
        final ServiceInfo selfService = _services.get(info.getKey());
        if (selfService != null && selfService != info) {
            info.setName(incrementName(info.getName()));
            collision = true;
        }
    } while (collision);
    return !(originalQualifiedName.equals(info.getKey()));
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) ExecutorService(java.util.concurrent.ExecutorService)

Example 37 with ServiceInfo

use of javax.jmdns.ServiceInfo in project JAirPort by froks.

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 38 with ServiceInfo

use of javax.jmdns.ServiceInfo in project JAirPort by froks.

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 Set<ServiceInfo> result = Collections.synchronizedSet(new HashSet<ServiceInfo>(_knownMDNS.size()));
    ExecutorService executor = Executors.newCachedThreadPool();
    for (final JmDNS mDNS : _knownMDNS.values()) {
        executor.submit(new Runnable() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void run() {
                result.add(mDNS.getServiceInfo(type, name, persistent, timeout));
            }
        });
    }
    executor.shutdown();
    try {
        executor.awaitTermination(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException exception) {
        logger.log(Level.WARNING, "Exception ", exception);
    }
    return result.toArray(new ServiceInfo[result.size()]);
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) JmDNS(javax.jmdns.JmDNS) ExecutorService(java.util.concurrent.ExecutorService)

Example 39 with ServiceInfo

use of javax.jmdns.ServiceInfo in project ecf by eclipse.

the class JMDNSDiscoveryContainer method createServiceInfoFromIServiceInfo.

private ServiceInfo createServiceInfoFromIServiceInfo(final IServiceInfo serviceInfo) {
    if (serviceInfo == null)
        return null;
    final IServiceID sID = serviceInfo.getServiceID();
    final Hashtable props = new Hashtable();
    final IServiceProperties svcProps = serviceInfo.getServiceProperties();
    if (svcProps != null) {
        for (final Enumeration e = svcProps.getPropertyNames(); e.hasMoreElements(); ) {
            final String key = (String) e.nextElement();
            final Object val = svcProps.getProperty(key);
            if (val instanceof String) {
                props.put(key, val);
            } else if (val instanceof Serializable) {
                final ByteArrayOutputStream bos = new ByteArrayOutputStream();
                try {
                    final ObjectOutputStream out = new ObjectOutputStream(bos);
                    out.writeObject(val);
                    out.close();
                } catch (final IOException e1) {
                    e1.printStackTrace();
                }
                final byte[] buf = bos.toByteArray();
                props.put(key, buf);
            // } else if (svcProps.getPropertyBytes(key) != null) {
            // byte[] bytes = svcProps.getPropertyBytes(key);
            // props.put(key, bytes);
            } else if (val != null) {
                props.put(key, val.toString());
            }
        }
    }
    // Add URI scheme to props
    final URI location = serviceInfo.getServiceID().getLocation();
    if (location != null) {
        props.put(SCHEME_PROPERTY, location.getScheme());
        props.put(URI_PATH_PROPERTY, location.getPath());
    }
    props.put(NAMING_AUTHORITY_PROPERTY, serviceInfo.getServiceID().getServiceTypeID().getNamingAuthority());
    final ServiceInfo si = ServiceInfo.create(sID.getServiceTypeID().getInternal(), serviceInfo.getServiceName(), location.getPort(), serviceInfo.getWeight(), serviceInfo.getPriority(), props);
    return si;
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) URI(java.net.URI)

Example 40 with ServiceInfo

use of javax.jmdns.ServiceInfo in project ecf by eclipse.

the class JMDNSDiscoveryContainer method getServiceInfo.

/**
 *********************** IDiscoveryContainerAdapter methods ********************
 */
/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#getServiceInfo(org.eclipse.ecf.discovery.identity.IServiceID)
	 */
public IServiceInfo getServiceInfo(final IServiceID service) {
    Assert.isNotNull(service);
    synchronized (lock) {
        try {
            // ECF discovery API defines identity to be the service type and the URI (location)
            // see https://bugs.eclipse.org/266723
            final ServiceInfo[] serviceInfos = jmdns.list(service.getServiceTypeID().getInternal());
            for (int i = 0; i < serviceInfos.length; i++) {
                ServiceInfo serviceInfo = serviceInfos[i];
                IServiceInfo iServiceInfo = createIServiceInfoFromServiceInfo(serviceInfo);
                Assert.isNotNull(iServiceInfo);
                Assert.isNotNull(iServiceInfo.getServiceID());
                if (iServiceInfo.getServiceID().equals(service)) {
                    return iServiceInfo;
                }
            }
            return null;
        } catch (final Exception e) {
            // $NON-NLS-1$
            Trace.catching(JMDNSPlugin.PLUGIN_ID, JMDNSDebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "getServiceInfo", e);
            return null;
        }
    }
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) ContainerConnectException(org.eclipse.ecf.core.ContainerConnectException)

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