1
2
3
4
5 package javax.jmdns.impl.tasks.state;
6
7 import java.io.IOException;
8 import java.util.Timer;
9 import java.util.logging.Logger;
10
11 import javax.jmdns.impl.DNSOutgoing;
12 import javax.jmdns.impl.DNSRecord;
13 import javax.jmdns.impl.JmDNSImpl;
14 import javax.jmdns.impl.ServiceInfoImpl;
15 import javax.jmdns.impl.constants.DNSConstants;
16 import javax.jmdns.impl.constants.DNSRecordClass;
17 import javax.jmdns.impl.constants.DNSState;
18
19
20
21
22 public class Canceler extends DNSStateTask {
23 static Logger logger = Logger.getLogger(Canceler.class.getName());
24
25 public Canceler(JmDNSImpl jmDNSImpl) {
26 super(jmDNSImpl, 0);
27
28 this.setTaskState(DNSState.CANCELING_1);
29 this.associate(DNSState.CANCELING_1);
30 }
31
32
33
34
35
36 @Override
37 public String getName() {
38 return "Canceler(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
39 }
40
41
42
43
44
45 @Override
46 public String toString() {
47 return super.toString() + " state: " + this.getTaskState();
48 }
49
50
51
52
53
54 @Override
55 public void start(Timer timer) {
56 timer.schedule(this, 0, DNSConstants.ANNOUNCE_WAIT_INTERVAL);
57 }
58
59
60
61
62
63 @Override
64 public boolean cancel() {
65 this.removeAssociation();
66
67 return super.cancel();
68 }
69
70
71
72
73
74 @Override
75 public String getTaskDescription() {
76 return "canceling";
77 }
78
79
80
81
82
83 @Override
84 protected boolean checkRunCondition() {
85 return true;
86 }
87
88
89
90
91
92 @Override
93 protected DNSOutgoing createOugoing() {
94 return new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA);
95 }
96
97
98
99
100
101 @Override
102 protected DNSOutgoing buildOutgoingForDNS(DNSOutgoing out) throws IOException {
103 DNSOutgoing newOut = out;
104 for (DNSRecord answer : this.getDns().getLocalHost().answers(DNSRecordClass.UNIQUE, this.getTTL())) {
105 newOut = this.addAnswer(newOut, null, answer);
106 }
107 return newOut;
108 }
109
110
111
112
113
114 @Override
115 protected DNSOutgoing buildOutgoingForInfo(ServiceInfoImpl info, DNSOutgoing out) throws IOException {
116 DNSOutgoing newOut = out;
117 for (DNSRecord answer : info.answers(DNSRecordClass.UNIQUE, this.getTTL(), this.getDns().getLocalHost())) {
118 newOut = this.addAnswer(newOut, null, answer);
119 }
120 return newOut;
121 }
122
123
124
125
126
127 @Override
128 protected void recoverTask(Throwable e) {
129 this.getDns().recover();
130 }
131
132
133
134
135
136 @Override
137 protected void advanceTask() {
138 this.setTaskState(this.getTaskState().advance());
139 if (!this.getTaskState().isCanceling()) {
140 cancel();
141 }
142 }
143 }