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.impl.DNSOutgoing;
10  import javax.jmdns.impl.DNSQuestion;
11  import javax.jmdns.impl.DNSRecord;
12  import javax.jmdns.impl.JmDNSImpl;
13  import javax.jmdns.impl.ServiceInfoImpl;
14  import javax.jmdns.impl.constants.DNSRecordClass;
15  import javax.jmdns.impl.constants.DNSRecordType;
16  
17  /**
18   * The ServiceInfoResolver queries up to three times consecutively for a service info, and then removes itself from the timer.
19   * <p/>
20   * 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.
21   */
22  public class ServiceInfoResolver extends DNSResolverTask {
23  
24      private final ServiceInfoImpl _info;
25  
26      public ServiceInfoResolver(JmDNSImpl jmDNSImpl, ServiceInfoImpl info) {
27          super(jmDNSImpl);
28          this._info = info;
29          info.setDns(this.getDns());
30          this.getDns().addListener(info, DNSQuestion.newQuestion(info.getQualifiedName(), DNSRecordType.TYPE_ANY, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
31      }
32  
33      /*
34       * (non-Javadoc)
35       * @see javax.jmdns.impl.tasks.DNSTask#getName()
36       */
37      @Override
38      public String getName() {
39          return "ServiceInfoResolver(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
40      }
41  
42      /*
43       * (non-Javadoc)
44       * @see java.util.TimerTask#cancel()
45       */
46      @Override
47      public boolean cancel() {
48          // We should not forget to remove the listener
49          boolean result = super.cancel();
50          if (!_info.isPersistent()) {
51              this.getDns().removeListener(_info);
52          }
53          return result;
54      }
55  
56      /*
57       * (non-Javadoc)
58       * @see javax.jmdns.impl.tasks.Resolver#addAnswers(javax.jmdns.impl.DNSOutgoing)
59       */
60      @Override
61      protected DNSOutgoing addAnswers(DNSOutgoing out) throws IOException {
62          DNSOutgoing newOut = out;
63          if (!_info.hasData()) {
64              long now = System.currentTimeMillis();
65              newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_IN), now);
66              newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_IN), now);
67              if (_info.getServer().length() > 0) {
68                  newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getServer(), DNSRecordType.TYPE_A, DNSRecordClass.CLASS_IN), now);
69                  newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getServer(), DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_IN), now);
70              }
71          }
72          return newOut;
73      }
74  
75      /*
76       * (non-Javadoc)
77       * @see javax.jmdns.impl.tasks.Resolver#addQuestions(javax.jmdns.impl.DNSOutgoing)
78       */
79      @Override
80      protected DNSOutgoing addQuestions(DNSOutgoing out) throws IOException {
81          DNSOutgoing newOut = out;
82          if (!_info.hasData()) {
83              newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
84              newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_info.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
85              if (_info.getServer().length() > 0) {
86                  newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_info.getServer(), DNSRecordType.TYPE_A, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
87                  newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_info.getServer(), DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
88              }
89          }
90          return newOut;
91      }
92  
93      /*
94       * (non-Javadoc)
95       * @see javax.jmdns.impl.tasks.Resolver#description()
96       */
97      @Override
98      protected String description() {
99          return "querying service info: " + (_info != null ? _info.getQualifiedName() : "null");
100     }
101 
102 }