00001 // Licensed under Apache License version 2.0 00002 package javax.jmdns.impl.tasks.resolver; 00003 00004 import java.io.IOException; 00005 import java.util.Timer; 00006 import java.util.logging.Level; 00007 import java.util.logging.Logger; 00008 00009 import javax.jmdns.impl.DNSOutgoing; 00010 import javax.jmdns.impl.JmDNSImpl; 00011 import javax.jmdns.impl.constants.DNSConstants; 00012 import javax.jmdns.impl.tasks.DNSTask; 00013 00019 public abstract class DNSResolverTask extends DNSTask { 00020 private static Logger logger = Logger.getLogger(DNSResolverTask.class.getName()); 00021 00025 protected int _count = 0; 00026 00030 public DNSResolverTask(JmDNSImpl jmDNSImpl) { 00031 super(jmDNSImpl); 00032 } 00033 00034 /* 00035 * (non-Javadoc) 00036 * @see java.lang.Object#toString() 00037 */ 00038 @Override 00039 public String toString() { 00040 return super.toString() + " count: " + _count; 00041 } 00042 00043 /* 00044 * (non-Javadoc) 00045 * @see javax.jmdns.impl.tasks.DNSTask#start(java.util.Timer) 00046 */ 00047 @Override 00048 public void start(Timer timer) { 00049 if (!this.getDns().isCanceling() && !this.getDns().isCanceled()) { 00050 timer.schedule(this, DNSConstants.QUERY_WAIT_INTERVAL, DNSConstants.QUERY_WAIT_INTERVAL); 00051 } 00052 } 00053 00054 /* 00055 * (non-Javadoc) 00056 * @see java.util.TimerTask#run() 00057 */ 00058 @Override 00059 public void run() { 00060 try { 00061 if (this.getDns().isCanceling() || this.getDns().isCanceled()) { 00062 this.cancel(); 00063 } else { 00064 if (_count++ < 3) { 00065 if (logger.isLoggable(Level.FINER)) { 00066 logger.finer(this.getName() + ".run() JmDNS " + this.description()); 00067 } 00068 DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_QUERY); 00069 out = this.addQuestions(out); 00070 if (this.getDns().isAnnounced()) { 00071 out = this.addAnswers(out); 00072 } 00073 if (!out.isEmpty()) { 00074 this.getDns().send(out); 00075 } 00076 } else { 00077 // After three queries, we can quit. 00078 this.cancel(); 00079 } 00080 } 00081 } catch (Throwable e) { 00082 logger.log(Level.WARNING, this.getName() + ".run() exception ", e); 00083 this.getDns().recover(); 00084 } 00085 } 00086 00096 protected abstract DNSOutgoing addQuestions(DNSOutgoing out) throws IOException; 00097 00107 protected abstract DNSOutgoing addAnswers(DNSOutgoing out) throws IOException; 00108 00114 protected abstract String description(); 00115 00116 }