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 Announcer sends an accumulated query of all announces, and advances the state of all serviceInfos, for which it has sent an announce. The Announcer also sends announcements and advances the state of JmDNS itself.
21   * <p/>
22   * When the announcer has run two times, it finishes.
23   */
24  public class Announcer extends DNSStateTask {
25      static Logger logger = Logger.getLogger(Announcer.class.getName());
26  
27      public Announcer(JmDNSImpl jmDNSImpl) {
28          super(jmDNSImpl, defaultTTL());
29  
30          this.setTaskState(DNSState.ANNOUNCING_1);
31          this.associate(DNSState.ANNOUNCING_1);
32      }
33  
34      /*
35       * (non-Javadoc)
36       * @see javax.jmdns.impl.tasks.DNSTask#getName()
37       */
38      @Override
39      public String getName() {
40          return "Announcer(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
41      }
42  
43      /*
44       * (non-Javadoc)
45       * @see java.lang.Object#toString()
46       */
47      @Override
48      public String toString() {
49          return super.toString() + " state: " + this.getTaskState();
50      }
51  
52      /*
53       * (non-Javadoc)
54       * @see javax.jmdns.impl.tasks.DNSTask#start(java.util.Timer)
55       */
56      @Override
57      public void start(Timer timer) {
58          if (!this.getDns().isCanceling() && !this.getDns().isCanceled()) {
59              timer.schedule(this, DNSConstants.ANNOUNCE_WAIT_INTERVAL, DNSConstants.ANNOUNCE_WAIT_INTERVAL);
60          }
61      }
62  
63      @Override
64      public boolean cancel() {
65          this.removeAssociation();
66  
67          return super.cancel();
68      }
69  
70      /*
71       * (non-Javadoc)
72       * @see javax.jmdns.impl.tasks.state.DNSStateTask#getTaskDescription()
73       */
74      @Override
75      public String getTaskDescription() {
76          return "announcing";
77      }
78  
79      /*
80       * (non-Javadoc)
81       * @see javax.jmdns.impl.tasks.state.DNSStateTask#checkRunCondition()
82       */
83      @Override
84      protected boolean checkRunCondition() {
85          return !this.getDns().isCanceling() && !this.getDns().isCanceled();
86      }
87  
88      /*
89       * (non-Javadoc)
90       * @see javax.jmdns.impl.tasks.state.DNSStateTask#createOugoing()
91       */
92      @Override
93      protected DNSOutgoing createOugoing() {
94          return new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA);
95      }
96  
97      /*
98       * (non-Javadoc)
99       * @see javax.jmdns.impl.tasks.state.DNSStateTask#buildOutgoingForDNS(javax.jmdns.impl.DNSOutgoing)
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      * (non-Javadoc)
112      * @see javax.jmdns.impl.tasks.state.DNSStateTask#buildOutgoingForInfo(javax.jmdns.impl.ServiceInfoImpl, javax.jmdns.impl.DNSOutgoing)
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      * (non-Javadoc)
125      * @see javax.jmdns.impl.tasks.state.DNSStateTask#recoverTask(java.lang.Throwable)
126      */
127     @Override
128     protected void recoverTask(Throwable e) {
129         this.getDns().recover();
130     }
131 
132     /*
133      * (non-Javadoc)
134      * @see javax.jmdns.impl.tasks.state.DNSStateTask#advanceTask()
135      */
136     @Override
137     protected void advanceTask() {
138         this.setTaskState(this.getTaskState().advance());
139         if (!this.getTaskState().isAnnouncing()) {
140             this.cancel();
141 
142             this.getDns().startRenewer();
143         }
144     }
145 
146 }