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.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   * The Renewer is there to send renewal announcement when the record expire for ours infos.
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       * (non-Javadoc)
34       * @see javax.jmdns.impl.tasks.DNSTask#getName()
35       */
36      @Override
37      public String getName() {
38          return "Renewer(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
39      }
40  
41      /*
42       * (non-Javadoc)
43       * @see java.lang.Object#toString()
44       */
45      @Override
46      public String toString() {
47          return super.toString() + " state: " + this.getTaskState();
48      }
49  
50      /*
51       * (non-Javadoc)
52       * @see javax.jmdns.impl.tasks.DNSTask#start(java.util.Timer)
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       * (non-Javadoc)
70       * @see javax.jmdns.impl.tasks.state.DNSStateTask#getTaskDescription()
71       */
72      @Override
73      public String getTaskDescription() {
74          return "renewing";
75      }
76  
77      /*
78       * (non-Javadoc)
79       * @see javax.jmdns.impl.tasks.state.DNSStateTask#checkRunCondition()
80       */
81      @Override
82      protected boolean checkRunCondition() {
83          return !this.getDns().isCanceling() && !this.getDns().isCanceled();
84      }
85  
86      /*
87       * (non-Javadoc)
88       * @see javax.jmdns.impl.tasks.state.DNSStateTask#createOugoing()
89       */
90      @Override
91      protected DNSOutgoing createOugoing() {
92          return new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA);
93      }
94  
95      /*
96       * (non-Javadoc)
97       * @see javax.jmdns.impl.tasks.state.DNSStateTask#buildOutgoingForDNS(javax.jmdns.impl.DNSOutgoing)
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      * (non-Javadoc)
110      * @see javax.jmdns.impl.tasks.state.DNSStateTask#buildOutgoingForInfo(javax.jmdns.impl.ServiceInfoImpl, javax.jmdns.impl.DNSOutgoing)
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      * (non-Javadoc)
123      * @see javax.jmdns.impl.tasks.state.DNSStateTask#recoverTask(java.lang.Throwable)
124      */
125     @Override
126     protected void recoverTask(Throwable e) {
127         this.getDns().recover();
128     }
129 
130     /*
131      * (non-Javadoc)
132      * @see javax.jmdns.impl.tasks.state.DNSStateTask#advanceTask()
133      */
134     @Override
135     protected void advanceTask() {
136         this.setTaskState(this.getTaskState().advance());
137         if (!this.getTaskState().isAnnounced()) {
138             cancel();
139         }
140     }
141 }