1
2 package javax.jmdns.impl.tasks.resolver;
3
4 import java.io.IOException;
5 import java.util.Timer;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8
9 import javax.jmdns.impl.DNSOutgoing;
10 import javax.jmdns.impl.JmDNSImpl;
11 import javax.jmdns.impl.constants.DNSConstants;
12 import javax.jmdns.impl.tasks.DNSTask;
13
14
15
16
17
18
19 public abstract class DNSResolverTask extends DNSTask {
20 private static Logger logger = Logger.getLogger(DNSResolverTask.class.getName());
21
22
23
24
25 protected int _count = 0;
26
27
28
29
30 public DNSResolverTask(JmDNSImpl jmDNSImpl) {
31 super(jmDNSImpl);
32 }
33
34
35
36
37
38 @Override
39 public String toString() {
40 return super.toString() + " count: " + _count;
41 }
42
43
44
45
46
47 @Override
48 public void start(Timer timer) {
49 if (!this.getDns().isCanceling() && !this.getDns().isCanceled()) {
50 timer.schedule(this, DNSConstants.QUERY_WAIT_INTERVAL, DNSConstants.QUERY_WAIT_INTERVAL);
51 }
52 }
53
54
55
56
57
58 @Override
59 public void run() {
60 try {
61 if (this.getDns().isCanceling() || this.getDns().isCanceled()) {
62 this.cancel();
63 } else {
64 if (_count++ < 3) {
65 if (logger.isLoggable(Level.FINER)) {
66 logger.finer(this.getName() + ".run() JmDNS " + this.description());
67 }
68 DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_QUERY);
69 out = this.addQuestions(out);
70 if (this.getDns().isAnnounced()) {
71 out = this.addAnswers(out);
72 }
73 if (!out.isEmpty()) {
74 this.getDns().send(out);
75 }
76 } else {
77
78 this.cancel();
79 }
80 }
81 } catch (Throwable e) {
82 logger.log(Level.WARNING, this.getName() + ".run() exception ", e);
83 this.getDns().recover();
84 }
85 }
86
87
88
89
90
91
92
93
94
95
96 protected abstract DNSOutgoing addQuestions(DNSOutgoing out) throws IOException;
97
98
99
100
101
102
103
104
105
106
107 protected abstract DNSOutgoing addAnswers(DNSOutgoing out) throws IOException;
108
109
110
111
112
113
114 protected abstract String description();
115
116 }