1// Copyright 2003-2005 Arthur van Hoff, Rick Blair
2// Licensed under Apache License version 2.0
3// Original license LGPL
4
5package javax.jmdns.impl.tasks.state;
6
7import java.io.IOException;
8import java.util.Timer;
9import java.util.logging.Logger;
10
11import javax.jmdns.impl.DNSOutgoing;
12import javax.jmdns.impl.DNSRecord;
13import javax.jmdns.impl.JmDNSImpl;
14import javax.jmdns.impl.ServiceInfoImpl;
15import javax.jmdns.impl.constants.DNSConstants;
16import javax.jmdns.impl.constants.DNSRecordClass;
17import javax.jmdns.impl.constants.DNSState;
18
19/**
20 * The Renewer is there to send renewal announcement when the record expire for ours infos.
21 */
22public 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            // BEGIN android-changed
58            // Schedule the renewer based on this task's TTL, not the default TTL
59            timer.schedule(this, getTTL() * 500, getTTL() * 500);
60            // END android-changed
61        }
62    }
63
64    @Override
65    public boolean cancel() {
66        this.removeAssociation();
67
68        return super.cancel();
69    }
70
71    /*
72     * (non-Javadoc)
73     * @see javax.jmdns.impl.tasks.state.DNSStateTask#getTaskDescription()
74     */
75    @Override
76    public String getTaskDescription() {
77        return "renewing";
78    }
79
80    /*
81     * (non-Javadoc)
82     * @see javax.jmdns.impl.tasks.state.DNSStateTask#checkRunCondition()
83     */
84    @Override
85    protected boolean checkRunCondition() {
86        return !this.getDns().isCanceling() && !this.getDns().isCanceled();
87    }
88
89    /*
90     * (non-Javadoc)
91     * @see javax.jmdns.impl.tasks.state.DNSStateTask#createOugoing()
92     */
93    @Override
94    protected DNSOutgoing createOugoing() {
95        return new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA);
96    }
97
98    /*
99     * (non-Javadoc)
100     * @see javax.jmdns.impl.tasks.state.DNSStateTask#buildOutgoingForDNS(javax.jmdns.impl.DNSOutgoing)
101     */
102    @Override
103    protected DNSOutgoing buildOutgoingForDNS(DNSOutgoing out) throws IOException {
104        DNSOutgoing newOut = out;
105        for (DNSRecord answer : this.getDns().getLocalHost().answers(DNSRecordClass.UNIQUE, this.getTTL())) {
106            newOut = this.addAnswer(newOut, null, answer);
107        }
108        return newOut;
109    }
110
111    /*
112     * (non-Javadoc)
113     * @see javax.jmdns.impl.tasks.state.DNSStateTask#buildOutgoingForInfo(javax.jmdns.impl.ServiceInfoImpl, javax.jmdns.impl.DNSOutgoing)
114     */
115    @Override
116    protected DNSOutgoing buildOutgoingForInfo(ServiceInfoImpl info, DNSOutgoing out) throws IOException {
117        DNSOutgoing newOut = out;
118        for (DNSRecord answer : info.answers(DNSRecordClass.UNIQUE, this.getTTL(), this.getDns().getLocalHost())) {
119            newOut = this.addAnswer(newOut, null, answer);
120        }
121        return newOut;
122    }
123
124    /*
125     * (non-Javadoc)
126     * @see javax.jmdns.impl.tasks.state.DNSStateTask#recoverTask(java.lang.Throwable)
127     */
128    @Override
129    protected void recoverTask(Throwable e) {
130        this.getDns().recover();
131    }
132
133    /*
134     * (non-Javadoc)
135     * @see javax.jmdns.impl.tasks.state.DNSStateTask#advanceTask()
136     */
137    @Override
138    protected void advanceTask() {
139        this.setTaskState(this.getTaskState().advance());
140        if (!this.getTaskState().isAnnounced()) {
141            cancel();
142        }
143    }
144}