View Javadoc

1   // Copyright 2003-2005 Arthur van Hoff, Rick Blair
2   // Licensed under Apache License version 2.0
3   // Original license LGPL
4   
5   package javax.jmdns.impl.tasks.resolver;
6   
7   import java.io.IOException;
8   
9   import javax.jmdns.ServiceInfo;
10  import javax.jmdns.impl.DNSOutgoing;
11  import javax.jmdns.impl.DNSQuestion;
12  import javax.jmdns.impl.DNSRecord;
13  import javax.jmdns.impl.JmDNSImpl;
14  import javax.jmdns.impl.constants.DNSConstants;
15  import javax.jmdns.impl.constants.DNSRecordClass;
16  import javax.jmdns.impl.constants.DNSRecordType;
17  
18  /**
19   * The ServiceResolver queries three times consecutively for services of a given type, and then removes itself from the timer.
20   * <p/>
21   * 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.
22   */
23  public class ServiceResolver extends DNSResolverTask {
24  
25      private final String _type;
26  
27      public ServiceResolver(JmDNSImpl jmDNSImpl, String type) {
28          super(jmDNSImpl);
29          this._type = type;
30      }
31  
32      /*
33       * (non-Javadoc)
34       * @see javax.jmdns.impl.tasks.DNSTask#getName()
35       */
36      @Override
37      public String getName() {
38          return "ServiceResolver(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
39      }
40  
41      /*
42       * (non-Javadoc)
43       * @see javax.jmdns.impl.tasks.Resolver#addAnswers(javax.jmdns.impl.DNSOutgoing)
44       */
45      @Override
46      protected DNSOutgoing addAnswers(DNSOutgoing out) throws IOException {
47          DNSOutgoing newOut = out;
48          long now = System.currentTimeMillis();
49          for (ServiceInfo info : this.getDns().getServices().values()) {
50              newOut = this.addAnswer(newOut, new DNSRecord.Pointer(info.getType(), DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, info.getQualifiedName()), now);
51              // newOut = this.addAnswer(newOut, new DNSRecord.Service(info.getQualifiedName(), DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, info.getPriority(), info.getWeight(), info.getPort(),
52              // this.getDns().getLocalHost().getName()), now);
53          }
54          return newOut;
55      }
56  
57      /*
58       * (non-Javadoc)
59       * @see javax.jmdns.impl.tasks.Resolver#addQuestions(javax.jmdns.impl.DNSOutgoing)
60       */
61      @Override
62      protected DNSOutgoing addQuestions(DNSOutgoing out) throws IOException {
63          DNSOutgoing newOut = out;
64          newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_type, DNSRecordType.TYPE_PTR, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
65          // newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_type, DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
66          return newOut;
67      }
68  
69      /*
70       * (non-Javadoc)
71       * @see javax.jmdns.impl.tasks.Resolver#description()
72       */
73      @Override
74      protected String description() {
75          return "querying service";
76      }
77  }