/** * <p> * Create an instance of JmDNS. * </p> * <p> * <b>Note:</b> This is a convenience method. The preferred constructor is {@link #create(InetAddress, String)}.<br/> * Check that your platform correctly handle the default localhost IP address and the local hostname. In doubt use the explicit constructor.<br/> * This call is equivalent to <code>create(null, null)</code>. * </p> * * @see #create(InetAddress, String) * @return jmDNS instance * @exception IOException * if an exception occurs during the socket creation */ public static JmDNS create() throws IOException { return new JmDNSImpl(null, null); }
/** {@inheritDoc} */ @Override public void run() { try { _shutdown = null; close(); } catch (Throwable exception) { System.err.println("Error while shuting down. " + exception); } } }
/** * {@inheritDoc} */ @Override public void run() { __recover(); } };
ServiceInfoImpl resolveServiceInfo(String type, String name, String subtype, boolean persistent) { this.cleanCache(); String loType = type.toLowerCase(); this.registerServiceType(type); if (_serviceCollectors.putIfAbsent(loType, new ServiceCollector(type)) == null) { this.addServiceListener(loType, _serviceCollectors.get(loType), ListenerStatus.SYNCHONEOUS); } // Check if the answer is in the cache. final ServiceInfoImpl info = this.getServiceInfoFromCache(type, name, subtype, persistent); // We still run the resolver to do the dispatch but if the info is already there it will quit immediately this.startServiceInfoResolver(info); return info; }
if (this.isClosing()) { return; if (this.closeState()) { this.cancelTimer(); this.unregisterAllServices(); this.disposeServiceCollectors(); this.waitForCanceled(DNSConstants.CLOSE_TIMEOUT); this.cancelStateTimer(); this.closeMulticastSocket(); advanceState(null);
if (this.isClosing()) { return; if (this.cancelState()) { this.cancelTimer(); this.unregisterAllServices(); this.disposeServiceCollectors(); this.waitForCanceled(DNSConstants.CLOSE_TIMEOUT); this.cancelStateTimer(); this.closeMulticastSocket(); DNSTaskStarter.Factory.getInstance().disposeStarter(this.getDns()); advanceState(null);
/** * Recover jmdns when there is an error. */ public void recover() { logger.finer(this.getName() + "recover()"); // We have an IO error so lets try to recover if anything happens lets close it. // This should cover the case of the IP address changing under our feet if (this.isClosing() || this.isClosed() || this.isCanceling() || this.isCanceled()) { return; } // We need some definite lock here as we may have multiple timer running in the same thread that will not be stopped by the reentrant lock // in the state object. This is only a problem in this case as we are going to execute in seperate thread so that the timer can clear. synchronized (_recoverLock) { // Stop JmDNS // This protects against recursive calls if (this.cancelState()) { logger.finer(this.getName() + "recover() thread " + Thread.currentThread().getName()); Thread recover = new Thread(this.getName() + ".recover()") { /** * {@inheritDoc} */ @Override public void run() { __recover(); } }; recover.start(); } } }
/** * {@inheritDoc} */ @Override public ServiceInfo[] list(String type, long timeout) { this.cleanCache(); // Implementation note: The first time a list for a given type is // requested, a ServiceCollector is created which collects service // infos. This greatly speeds up the performance of subsequent calls // to this method. The caveats are, that 1) the first call to this // method for a given type is slow, and 2) we spawn a ServiceCollector // instance for each service type which increases network traffic a // little. String loType = type.toLowerCase(); boolean newCollectorCreated = false; if (this.isCanceling() || this.isCanceled()) { return new ServiceInfo[0]; } ServiceCollector collector = _serviceCollectors.get(loType); if (collector == null) { newCollectorCreated = _serviceCollectors.putIfAbsent(loType, new ServiceCollector(type)) == null; collector = _serviceCollectors.get(loType); if (newCollectorCreated) { this.addServiceListener(type, collector, ListenerStatus.SYNCHRONOUS); } } logger.debug("{}-collector: {}", this.getName(), collector); // At this stage the collector should never be null but it keeps findbugs happy. return (collector != null ? collector.list(timeout) : new ServiceInfo[0]); }
if (this.isClosing()) { return; if (this.closeState()) { this.cancelTimer(); this.disposeServiceCollectors(); this.cancelStateTimer(); this.closeMulticastSocket(); advanceState(null);
@Override public void run() { this.getDns().respondToQuery(_in); Set<DNSRecord> answers = new HashSet<DNSRecord>(); if (this.getDns().isAnnounced()) { try { if (!out.isEmpty()) this.getDns().send(out); this.getDns().close();
if (_serviceCollectors.putIfAbsent(loType, new ServiceCollector(type)) == null) { this.addServiceListener(loType, _serviceCollectors.get(loType), ListenerStatus.SYNCHONEOUS); Collection<DNSEntry> dnsEntryLits = this.getCache().allValues(); for (DNSEntry entry : dnsEntryLits) { final DNSRecord record = (DNSRecord) entry; serviceEvents.add(new ServiceEventImpl(this, record.getType(), toUnqualifiedName(record.getType(), record.getName()), record.getServiceInfo())); this.startServiceResolver(type);
@Override DNSOutgoing addAnswer(JmDNSImpl dns, DNSIncoming in, InetAddress addr, int port, DNSOutgoing out) throws IOException { ServiceInfoImpl info = (ServiceInfoImpl) dns.getServices().get(this.getKey()); if (info != null) { if (this._port == info.getPort() != _server.equals(dns.getLocalHost().getName())) { return dns.addAnswer(in, addr, port, out, new DNSRecord.Service(info.getQualifiedName(), DNSRecordClass.CLASS_IN, DNSRecordClass.UNIQUE, DNSConstants.DNS_TTL, info.getPriority(), info.getWeight(), info.getPort(), dns .getLocalHost().getName())); } } return out; }
/** * Associate the DNS host and the service infos with this task if not already associated and in the same state. * * @param state * target state */ protected void associate(DNSState state) { synchronized (this.getDns()) { this.getDns().associateWithTask(this, state); } for (ServiceInfo serviceInfo : this.getDns().getServices().values()) { ((ServiceInfoImpl) serviceInfo).associateWithTask(this, state); } }
final JmDNSImpl jmdns = new JmDNSImpl(null, null); final HostInfo hostInfo = HostInfo.newHostInfo(InetAddress.getByName("192.168.1.78"), jmdns, null); System.out.println("MDNS hostname (Bonjour): " + hostInfo.getName()); System.out.println("DNS hostname: " + hostInfo.getInetAddress().getHostName()); System.out.println("IP address: " + hostInfo.getInetAddress().getHostAddress()); jmdns.close();
/** * {@inheritDoc} */ @Override public void addServiceListener(String type, ServiceListener listener) { this.addServiceListener(type, listener, ListenerStatus.ASYNCHRONOUS); }
public ServiceInfoResolver(JmDNSImpl jmDNSImpl, ServiceInfoImpl info) { super(jmDNSImpl); this._info = info; info.setDns(this.getDns()); this.getDns().addListener(info, DNSQuestion.newQuestion(info.getQualifiedName(), DNSRecordType.TYPE_ANY, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE)); }
if (this.isClosing()) { return; if (this.closeState()) { this.cancelTimer(); this.unregisterAllServices(); this.disposeServiceCollectors(); this.waitForCanceled(DNSConstants.CLOSE_TIMEOUT); this.cancelStateTimer(); this.closeMulticastSocket(); advanceState(null);
logger.debug("{}.recover()", this.getName()); if (this.isClosing() || this.isClosed() || this.isCanceling() || this.isCanceled()) { return; if (this.cancelState()) { final String newThreadName = this.getName() + ".recover()"; logger.debug("{} thread {}", newThreadName, Thread.currentThread().getName()); Thread recover = new Thread(newThreadName) {
ServiceInfoImpl resolveServiceInfo(String type, String name, String subtype, boolean persistent) { this.cleanCache(); String loType = type.toLowerCase(); this.registerServiceType(type); if (_serviceCollectors.putIfAbsent(loType, new ServiceCollector(type)) == null) { this.addServiceListener(loType, _serviceCollectors.get(loType), ListenerStatus.SYNCHRONOUS); } // Check if the answer is in the cache. final ServiceInfoImpl info = this.getServiceInfoFromCache(type, name, subtype, persistent); // We still run the resolver to do the dispatch but if the info is already there it will quit immediately this.startServiceInfoResolver(info); return info; }
this.cleanCache(); if (this.isCanceling() || this.isCanceled()) { return new ServiceInfo[0]; collector = _serviceCollectors.get(loType); if (newCollectorCreated) { this.addServiceListener(type, collector, ListenerStatus.SYNCHONEOUS); logger.finer(this.getName() + ".collector: " + collector);