1
2
3
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
19
20
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
35
36
37 @Override
38 public String getName() {
39 return "ServiceInfoResolver(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
40 }
41
42
43
44
45
46 @Override
47 public boolean cancel() {
48
49 boolean result = super.cancel();
50 if (!_info.isPersistent()) {
51 this.getDns().removeListener(_info);
52 }
53 return result;
54 }
55
56
57
58
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
77
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
95
96
97 @Override
98 protected String description() {
99 return "querying service info: " + (_info != null ? _info.getQualifiedName() : "null");
100 }
101
102 }