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 Renewer extends DNSStateTask {
23 static Logger logger = Logger.getLogger(Renewer.class.getName());
24
25 public Renewer(JmDNSImpl jmDNSImpl) {
26 super(jmDNSImpl, defaultTTL());
27
28 this.setTaskState(DNSState.ANNOUNCED);
29 this.associate(DNSState.ANNOUNCED);
30 }
31
32
33
34
35
36 @Override
37 public String getName() {
38 return "Renewer(" + (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 if (!this.getDns().isCanceling() && !this.getDns().isCanceled()) {
57 timer.schedule(this, DNSConstants.ANNOUNCED_RENEWAL_TTL_INTERVAL, DNSConstants.ANNOUNCED_RENEWAL_TTL_INTERVAL);
58 }
59 }
60
61 @Override
62 public boolean cancel() {
63 this.removeAssociation();
64
65 return super.cancel();
66 }
67
68
69
70
71
72 @Override
73 public String getTaskDescription() {
74 return "renewing";
75 }
76
77
78
79
80
81 @Override
82 protected boolean checkRunCondition() {
83 return !this.getDns().isCanceling() && !this.getDns().isCanceled();
84 }
85
86
87
88
89
90 @Override
91 protected DNSOutgoing createOugoing() {
92 return new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA);
93 }
94
95
96
97
98
99 @Override
100 protected DNSOutgoing buildOutgoingForDNS(DNSOutgoing out) throws IOException {
101 DNSOutgoing newOut = out;
102 for (DNSRecord answer : this.getDns().getLocalHost().answers(DNSRecordClass.UNIQUE, this.getTTL())) {
103 newOut = this.addAnswer(newOut, null, answer);
104 }
105 return newOut;
106 }
107
108
109
110
111
112 @Override
113 protected DNSOutgoing buildOutgoingForInfo(ServiceInfoImpl info, DNSOutgoing out) throws IOException {
114 DNSOutgoing newOut = out;
115 for (DNSRecord answer : info.answers(DNSRecordClass.UNIQUE, this.getTTL(), this.getDns().getLocalHost())) {
116 newOut = this.addAnswer(newOut, null, answer);
117 }
118 return newOut;
119 }
120
121
122
123
124
125 @Override
126 protected void recoverTask(Throwable e) {
127 this.getDns().recover();
128 }
129
130
131
132
133
134 @Override
135 protected void advanceTask() {
136 this.setTaskState(this.getTaskState().advance());
137 if (!this.getTaskState().isAnnounced()) {
138 cancel();
139 }
140 }
141 }