Go to the documentation of this file.00001
00002
00003
00004
00005 package javax.jmdns.impl.tasks.state;
00006
00007 import java.io.IOException;
00008 import java.util.Timer;
00009 import java.util.logging.Logger;
00010
00011 import javax.jmdns.impl.DNSOutgoing;
00012 import javax.jmdns.impl.DNSQuestion;
00013 import javax.jmdns.impl.DNSRecord;
00014 import javax.jmdns.impl.JmDNSImpl;
00015 import javax.jmdns.impl.ServiceInfoImpl;
00016 import javax.jmdns.impl.constants.DNSConstants;
00017 import javax.jmdns.impl.constants.DNSRecordClass;
00018 import javax.jmdns.impl.constants.DNSRecordType;
00019 import javax.jmdns.impl.constants.DNSState;
00020
00027 public class Prober extends DNSStateTask {
00028 static Logger logger = Logger.getLogger(Prober.class.getName());
00029
00030 public Prober(JmDNSImpl jmDNSImpl) {
00031 super(jmDNSImpl, defaultTTL());
00032
00033 this.setTaskState(DNSState.PROBING_1);
00034 this.associate(DNSState.PROBING_1);
00035 }
00036
00037
00038
00039
00040
00041 @Override
00042 public String getName() {
00043 return "Prober(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
00044 }
00045
00046
00047
00048
00049
00050 @Override
00051 public String toString() {
00052 return super.toString() + " state: " + this.getTaskState();
00053 }
00054
00055
00056
00057
00058
00059 @Override
00060 public void start(Timer timer) {
00061 long now = System.currentTimeMillis();
00062 if (now - this.getDns().getLastThrottleIncrement() < DNSConstants.PROBE_THROTTLE_COUNT_INTERVAL) {
00063 this.getDns().setThrottle(this.getDns().getThrottle() + 1);
00064 } else {
00065 this.getDns().setThrottle(1);
00066 }
00067 this.getDns().setLastThrottleIncrement(now);
00068
00069 if (this.getDns().isAnnounced() && this.getDns().getThrottle() < DNSConstants.PROBE_THROTTLE_COUNT) {
00070 timer.schedule(this, JmDNSImpl.getRandom().nextInt(1 + DNSConstants.PROBE_WAIT_INTERVAL), DNSConstants.PROBE_WAIT_INTERVAL);
00071 } else if (!this.getDns().isCanceling() && !this.getDns().isCanceled()) {
00072 timer.schedule(this, DNSConstants.PROBE_CONFLICT_INTERVAL, DNSConstants.PROBE_CONFLICT_INTERVAL);
00073 }
00074 }
00075
00076 @Override
00077 public boolean cancel() {
00078 this.removeAssociation();
00079
00080 return super.cancel();
00081 }
00082
00083
00084
00085
00086
00087 @Override
00088 public String getTaskDescription() {
00089 return "probing";
00090 }
00091
00092
00093
00094
00095
00096 @Override
00097 protected boolean checkRunCondition() {
00098 return !this.getDns().isCanceling() && !this.getDns().isCanceled();
00099 }
00100
00101
00102
00103
00104
00105 @Override
00106 protected DNSOutgoing createOugoing() {
00107 return new DNSOutgoing(DNSConstants.FLAGS_QR_QUERY);
00108 }
00109
00110
00111
00112
00113
00114 @Override
00115 protected DNSOutgoing buildOutgoingForDNS(DNSOutgoing out) throws IOException {
00116 DNSOutgoing newOut = out;
00117 newOut.addQuestion(DNSQuestion.newQuestion(this.getDns().getLocalHost().getName(), DNSRecordType.TYPE_ANY, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
00118 for (DNSRecord answer : this.getDns().getLocalHost().answers(DNSRecordClass.NOT_UNIQUE, this.getTTL())) {
00119 newOut = this.addAuthoritativeAnswer(newOut, answer);
00120 }
00121 return newOut;
00122 }
00123
00124
00125
00126
00127
00128 @Override
00129 protected DNSOutgoing buildOutgoingForInfo(ServiceInfoImpl info, DNSOutgoing out) throws IOException {
00130 DNSOutgoing newOut = out;
00131 newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(info.getQualifiedName(), DNSRecordType.TYPE_ANY, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
00132
00133 newOut = this.addAuthoritativeAnswer(newOut, new DNSRecord.Service(info.getQualifiedName(), DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, this.getTTL(), info.getPriority(), info.getWeight(), info.getPort(), this.getDns().getLocalHost()
00134 .getName()));
00135 return newOut;
00136 }
00137
00138
00139
00140
00141
00142 @Override
00143 protected void recoverTask(Throwable e) {
00144 this.getDns().recover();
00145 }
00146
00147
00148
00149
00150
00151 @Override
00152 protected void advanceTask() {
00153 this.setTaskState(this.getTaskState().advance());
00154 if (!this.getTaskState().isProbing()) {
00155 cancel();
00156
00157 this.getDns().startAnnouncer();
00158 }
00159 }
00160
00161 }