From 484c660cf9b34458e3fe908ea158c7aeb188bfb4 Mon Sep 17 00:00:00 2001 From: Emil Ivov Date: Wed, 24 Nov 2010 21:27:27 +0000 Subject: [PATCH] Work in progress on the implementation of parallel DNS queries. --- .../communicator/util/dns/ParallelLookup.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/net/java/sip/communicator/util/dns/ParallelLookup.java diff --git a/src/net/java/sip/communicator/util/dns/ParallelLookup.java b/src/net/java/sip/communicator/util/dns/ParallelLookup.java new file mode 100644 index 000000000..9b1d2cfca --- /dev/null +++ b/src/net/java/sip/communicator/util/dns/ParallelLookup.java @@ -0,0 +1,108 @@ +/* + * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package net.java.sip.communicator.util.dns; + +import java.text.*; + +import net.java.sip.communicator.util.*; + +import org.xbill.DNS.*; + +/** + * + * + * @author Emil Ivov + */ +public class ParallelLookup +{ + /** + * The Logger used by the ParallelLookup + * class and its instances for logging output. + */ + private static final Logger logger = Logger.getLogger(ParallelLookup.class + .getName()); + + /** + * + * + * @param domain + * @return + */ + public Record[] lookup(String domain) + + throws ParseException + { + Record[] records = null; + Lookup lookup; + try + { + lookup = new Lookup(domain, Type.SRV); + + new LookupThread(lookup).start(); + } + catch (TextParseException tpe) + { + logger.error("Failed to parse domain="+domain, tpe); + throw new ParseException(tpe.getMessage(), 0); + } + + return records; + } + + /** + * The {@link Thread} that executes a single {@link Lookup} in a + * non-blocking way. + */ + private final class LookupThread extends Thread + { + Lookup lookup; + + boolean done = false; + + public LookupThread(Lookup lookup) + { + this.lookup = lookup; + } + + /** + * + */ + public void run() + { + lookup.run(); + + synchronized(this) + { + done = true; + notify(); + } + } + + public Record[] lookUpAndWaitFor(long patience) + { + this.start(); + + synchronized(this) + { + if ( !done ) + { + try + { + wait(patience); + } + catch(InterruptedException iexc) + { + } + } + + done = true; + } + + return lookup.getAnswers(); + } + } +}