summaryrefslogtreecommitdiff
path: root/src/javax/jmdns/impl/tasks/resolver
diff options
context:
space:
mode:
Diffstat (limited to 'src/javax/jmdns/impl/tasks/resolver')
-rw-r--r--src/javax/jmdns/impl/tasks/resolver/DNSResolverTask.java116
-rw-r--r--src/javax/jmdns/impl/tasks/resolver/ServiceInfoResolver.java102
-rw-r--r--src/javax/jmdns/impl/tasks/resolver/ServiceResolver.java77
-rw-r--r--src/javax/jmdns/impl/tasks/resolver/TypeResolver.java75
-rw-r--r--src/javax/jmdns/impl/tasks/resolver/package-info.java2
5 files changed, 372 insertions, 0 deletions
diff --git a/src/javax/jmdns/impl/tasks/resolver/DNSResolverTask.java b/src/javax/jmdns/impl/tasks/resolver/DNSResolverTask.java
new file mode 100644
index 0000000..7fa7abb
--- /dev/null
+++ b/src/javax/jmdns/impl/tasks/resolver/DNSResolverTask.java
@@ -0,0 +1,116 @@
+// Licensed under Apache License version 2.0
+package javax.jmdns.impl.tasks.resolver;
+
+import java.io.IOException;
+import java.util.Timer;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.jmdns.impl.DNSOutgoing;
+import javax.jmdns.impl.JmDNSImpl;
+import javax.jmdns.impl.constants.DNSConstants;
+import javax.jmdns.impl.tasks.DNSTask;
+
+/**
+ * This is the root class for all resolver tasks.
+ *
+ * @author Pierre Frisch
+ */
+public abstract class DNSResolverTask extends DNSTask {
+ private static Logger logger = Logger.getLogger(DNSResolverTask.class.getName());
+
+ /**
+ * Counts the number of queries being sent.
+ */
+ protected int _count = 0;
+
+ /**
+ * @param jmDNSImpl
+ */
+ public DNSResolverTask(JmDNSImpl jmDNSImpl) {
+ super(jmDNSImpl);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return super.toString() + " count: " + _count;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.DNSTask#start(java.util.Timer)
+ */
+ @Override
+ public void start(Timer timer) {
+ if (!this.getDns().isCanceling() && !this.getDns().isCanceled()) {
+ timer.schedule(this, DNSConstants.QUERY_WAIT_INTERVAL, DNSConstants.QUERY_WAIT_INTERVAL);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see java.util.TimerTask#run()
+ */
+ @Override
+ public void run() {
+ try {
+ if (this.getDns().isCanceling() || this.getDns().isCanceled()) {
+ this.cancel();
+ } else {
+ if (_count++ < 3) {
+ if (logger.isLoggable(Level.FINER)) {
+ logger.finer(this.getName() + ".run() JmDNS " + this.description());
+ }
+ DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_QUERY);
+ out = this.addQuestions(out);
+ if (this.getDns().isAnnounced()) {
+ out = this.addAnswers(out);
+ }
+ if (!out.isEmpty()) {
+ this.getDns().send(out);
+ }
+ } else {
+ // After three queries, we can quit.
+ this.cancel();
+ }
+ }
+ } catch (Throwable e) {
+ logger.log(Level.WARNING, this.getName() + ".run() exception ", e);
+ this.getDns().recover();
+ }
+ }
+
+ /**
+ * Overridden by subclasses to add questions to the message.<br/>
+ * <b>Note:</b> Because of message size limitation the returned message may be different than the message parameter.
+ *
+ * @param out
+ * outgoing message
+ * @return the outgoing message.
+ * @exception IOException
+ */
+ protected abstract DNSOutgoing addQuestions(DNSOutgoing out) throws IOException;
+
+ /**
+ * Overridden by subclasses to add questions to the message.<br/>
+ * <b>Note:</b> Because of message size limitation the returned message may be different than the message parameter.
+ *
+ * @param out
+ * outgoing message
+ * @return the outgoing message.
+ * @exception IOException
+ */
+ protected abstract DNSOutgoing addAnswers(DNSOutgoing out) throws IOException;
+
+ /**
+ * Returns a description of the resolver for debugging
+ *
+ * @return resolver description
+ */
+ protected abstract String description();
+
+}
diff --git a/src/javax/jmdns/impl/tasks/resolver/ServiceInfoResolver.java b/src/javax/jmdns/impl/tasks/resolver/ServiceInfoResolver.java
new file mode 100644
index 0000000..3147efd
--- /dev/null
+++ b/src/javax/jmdns/impl/tasks/resolver/ServiceInfoResolver.java
@@ -0,0 +1,102 @@
+// Copyright 2003-2005 Arthur van Hoff, Rick Blair
+// Licensed under Apache License version 2.0
+// Original license LGPL
+
+package javax.jmdns.impl.tasks.resolver;
+
+import java.io.IOException;
+
+import javax.jmdns.impl.DNSOutgoing;
+import javax.jmdns.impl.DNSQuestion;
+import javax.jmdns.impl.DNSRecord;
+import javax.jmdns.impl.JmDNSImpl;
+import javax.jmdns.impl.ServiceInfoImpl;
+import javax.jmdns.impl.constants.DNSRecordClass;
+import javax.jmdns.impl.constants.DNSRecordType;
+
+/**
+ * The ServiceInfoResolver queries up to three times consecutively for a service info, and then removes itself from the timer.
+ * <p/>
+ * The ServiceInfoResolver will run only if JmDNS is in state ANNOUNCED. REMIND: Prevent having multiple service resolvers for the same info in the timer queue.
+ */
+public class ServiceInfoResolver extends DNSResolverTask {
+
+ private final ServiceInfoImpl _info;
+
+ 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));
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.DNSTask#getName()
+ */
+ @Override
+ public String getName() {
+ return "ServiceInfoResolver(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see java.util.TimerTask#cancel()
+ */
+ @Override
+ public boolean cancel() {
+ // We should not forget to remove the listener
+ boolean result = super.cancel();
+ if (!_info.isPersistent()) {
+ this.getDns().removeListener(_info);
+ }
+ return result;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.Resolver#addAnswers(javax.jmdns.impl.DNSOutgoing)
+ */
+ @Override
+ protected DNSOutgoing addAnswers(DNSOutgoing out) throws IOException {
+ DNSOutgoing newOut = out;
+ if (!_info.hasData()) {
+ long now = System.currentTimeMillis();
+ newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_IN), now);
+ newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_IN), now);
+ if (_info.getServer().length() > 0) {
+ newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getServer(), DNSRecordType.TYPE_A, DNSRecordClass.CLASS_IN), now);
+ newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getServer(), DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_IN), now);
+ }
+ }
+ return newOut;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.Resolver#addQuestions(javax.jmdns.impl.DNSOutgoing)
+ */
+ @Override
+ protected DNSOutgoing addQuestions(DNSOutgoing out) throws IOException {
+ DNSOutgoing newOut = out;
+ if (!_info.hasData()) {
+ newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
+ newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_info.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
+ if (_info.getServer().length() > 0) {
+ newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_info.getServer(), DNSRecordType.TYPE_A, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
+ newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_info.getServer(), DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
+ }
+ }
+ return newOut;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.Resolver#description()
+ */
+ @Override
+ protected String description() {
+ return "querying service info: " + (_info != null ? _info.getQualifiedName() : "null");
+ }
+
+} \ No newline at end of file
diff --git a/src/javax/jmdns/impl/tasks/resolver/ServiceResolver.java b/src/javax/jmdns/impl/tasks/resolver/ServiceResolver.java
new file mode 100644
index 0000000..ae7dee5
--- /dev/null
+++ b/src/javax/jmdns/impl/tasks/resolver/ServiceResolver.java
@@ -0,0 +1,77 @@
+// Copyright 2003-2005 Arthur van Hoff, Rick Blair
+// Licensed under Apache License version 2.0
+// Original license LGPL
+
+package javax.jmdns.impl.tasks.resolver;
+
+import java.io.IOException;
+
+import javax.jmdns.ServiceInfo;
+import javax.jmdns.impl.DNSOutgoing;
+import javax.jmdns.impl.DNSQuestion;
+import javax.jmdns.impl.DNSRecord;
+import javax.jmdns.impl.JmDNSImpl;
+import javax.jmdns.impl.constants.DNSConstants;
+import javax.jmdns.impl.constants.DNSRecordClass;
+import javax.jmdns.impl.constants.DNSRecordType;
+
+/**
+ * The ServiceResolver queries three times consecutively for services of a given type, and then removes itself from the timer.
+ * <p/>
+ * The ServiceResolver will run only if JmDNS is in state ANNOUNCED. REMIND: Prevent having multiple service resolvers for the same type in the timer queue.
+ */
+public class ServiceResolver extends DNSResolverTask {
+
+ private final String _type;
+
+ public ServiceResolver(JmDNSImpl jmDNSImpl, String type) {
+ super(jmDNSImpl);
+ this._type = type;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.DNSTask#getName()
+ */
+ @Override
+ public String getName() {
+ return "ServiceResolver(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.Resolver#addAnswers(javax.jmdns.impl.DNSOutgoing)
+ */
+ @Override
+ protected DNSOutgoing addAnswers(DNSOutgoing out) throws IOException {
+ DNSOutgoing newOut = out;
+ long now = System.currentTimeMillis();
+ for (ServiceInfo info : this.getDns().getServices().values()) {
+ newOut = this.addAnswer(newOut, new DNSRecord.Pointer(info.getType(), DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, info.getQualifiedName()), now);
+ // newOut = this.addAnswer(newOut, new DNSRecord.Service(info.getQualifiedName(), DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, info.getPriority(), info.getWeight(), info.getPort(),
+ // this.getDns().getLocalHost().getName()), now);
+ }
+ return newOut;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.Resolver#addQuestions(javax.jmdns.impl.DNSOutgoing)
+ */
+ @Override
+ protected DNSOutgoing addQuestions(DNSOutgoing out) throws IOException {
+ DNSOutgoing newOut = out;
+ newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_type, DNSRecordType.TYPE_PTR, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
+ // newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_type, DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
+ return newOut;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.Resolver#description()
+ */
+ @Override
+ protected String description() {
+ return "querying service";
+ }
+} \ No newline at end of file
diff --git a/src/javax/jmdns/impl/tasks/resolver/TypeResolver.java b/src/javax/jmdns/impl/tasks/resolver/TypeResolver.java
new file mode 100644
index 0000000..8382e54
--- /dev/null
+++ b/src/javax/jmdns/impl/tasks/resolver/TypeResolver.java
@@ -0,0 +1,75 @@
+// Copyright 2003-2005 Arthur van Hoff, Rick Blair
+// Licensed under Apache License version 2.0
+// Original license LGPL
+
+package javax.jmdns.impl.tasks.resolver;
+
+import java.io.IOException;
+
+import javax.jmdns.impl.DNSOutgoing;
+import javax.jmdns.impl.DNSQuestion;
+import javax.jmdns.impl.DNSRecord;
+import javax.jmdns.impl.JmDNSImpl;
+import javax.jmdns.impl.JmDNSImpl.ServiceTypeEntry;
+import javax.jmdns.impl.constants.DNSConstants;
+import javax.jmdns.impl.constants.DNSRecordClass;
+import javax.jmdns.impl.constants.DNSRecordType;
+
+/**
+ * Helper class to resolve service types.
+ * <p/>
+ * The TypeResolver queries three times consecutively for service types, and then removes itself from the timer.
+ * <p/>
+ * The TypeResolver will run only if JmDNS is in state ANNOUNCED.
+ */
+public class TypeResolver extends DNSResolverTask {
+
+ /**
+ * @param jmDNSImpl
+ */
+ public TypeResolver(JmDNSImpl jmDNSImpl) {
+ super(jmDNSImpl);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.DNSTask#getName()
+ */
+ @Override
+ public String getName() {
+ return "TypeResolver(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.Resolver#addAnswers(javax.jmdns.impl.DNSOutgoing)
+ */
+ @Override
+ protected DNSOutgoing addAnswers(DNSOutgoing out) throws IOException {
+ DNSOutgoing newOut = out;
+ long now = System.currentTimeMillis();
+ for (String type : this.getDns().getServiceTypes().keySet()) {
+ ServiceTypeEntry typeEntry = this.getDns().getServiceTypes().get(type);
+ newOut = this.addAnswer(newOut, new DNSRecord.Pointer("_services._dns-sd._udp.local.", DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, typeEntry.getType()), now);
+ }
+ return newOut;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.Resolver#addQuestions(javax.jmdns.impl.DNSOutgoing)
+ */
+ @Override
+ protected DNSOutgoing addQuestions(DNSOutgoing out) throws IOException {
+ return this.addQuestion(out, DNSQuestion.newQuestion("_services._dns-sd._udp.local.", DNSRecordType.TYPE_PTR, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see javax.jmdns.impl.tasks.Resolver#description()
+ */
+ @Override
+ protected String description() {
+ return "querying type";
+ }
+} \ No newline at end of file
diff --git a/src/javax/jmdns/impl/tasks/resolver/package-info.java b/src/javax/jmdns/impl/tasks/resolver/package-info.java
new file mode 100644
index 0000000..9fada6a
--- /dev/null
+++ b/src/javax/jmdns/impl/tasks/resolver/package-info.java
@@ -0,0 +1,2 @@
+package javax.jmdns.impl.tasks.resolver;
+