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()));
}
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);
}
}
}
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()]);
}
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;
}
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;
}
}
}
Aggregations