$search
00001 // Copyright 2003-2005 Arthur van Hoff, Rick Blair 00002 // Licensed under Apache License version 2.0 00003 // Original license LGPL 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 * (non-Javadoc) 00039 * @see javax.jmdns.impl.tasks.DNSTask#getName() 00040 */ 00041 @Override 00042 public String getName() { 00043 return "Prober(" + (this.getDns() != null ? this.getDns().getName() : "") + ")"; 00044 } 00045 00046 /* 00047 * (non-Javadoc) 00048 * @see java.lang.Object#toString() 00049 */ 00050 @Override 00051 public String toString() { 00052 return super.toString() + " state: " + this.getTaskState(); 00053 } 00054 00055 /* 00056 * (non-Javadoc) 00057 * @see javax.jmdns.impl.tasks.DNSTask#start(java.util.Timer) 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 * (non-Javadoc) 00085 * @see javax.jmdns.impl.tasks.state.DNSStateTask#getTaskDescription() 00086 */ 00087 @Override 00088 public String getTaskDescription() { 00089 return "probing"; 00090 } 00091 00092 /* 00093 * (non-Javadoc) 00094 * @see javax.jmdns.impl.tasks.state.DNSStateTask#checkRunCondition() 00095 */ 00096 @Override 00097 protected boolean checkRunCondition() { 00098 return !this.getDns().isCanceling() && !this.getDns().isCanceled(); 00099 } 00100 00101 /* 00102 * (non-Javadoc) 00103 * @see javax.jmdns.impl.tasks.state.DNSStateTask#createOugoing() 00104 */ 00105 @Override 00106 protected DNSOutgoing createOugoing() { 00107 return new DNSOutgoing(DNSConstants.FLAGS_QR_QUERY); 00108 } 00109 00110 /* 00111 * (non-Javadoc) 00112 * @see javax.jmdns.impl.tasks.state.DNSStateTask#buildOutgoingForDNS(javax.jmdns.impl.DNSOutgoing) 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 * (non-Javadoc) 00126 * @see javax.jmdns.impl.tasks.state.DNSStateTask#buildOutgoingForInfo(javax.jmdns.impl.ServiceInfoImpl, javax.jmdns.impl.DNSOutgoing) 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 // the "unique" flag should be not set here because these answers haven't been proven unique yet this means the record will not exactly match the announcement record 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 * (non-Javadoc) 00140 * @see javax.jmdns.impl.tasks.state.DNSStateTask#recoverTask(java.lang.Throwable) 00141 */ 00142 @Override 00143 protected void recoverTask(Throwable e) { 00144 this.getDns().recover(); 00145 } 00146 00147 /* 00148 * (non-Javadoc) 00149 * @see javax.jmdns.impl.tasks.state.DNSStateTask#advanceTask() 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 }