Search in sources :

Example 1 with InternalMiniDnsFuture

use of org.minidns.MiniDnsFuture.InternalMiniDnsFuture in project minidns by MiniDNS.

the class DNSClient method queryAsync.

@Override
protected MiniDnsFuture<DNSMessage, IOException> queryAsync(DNSMessage.Builder queryBuilder) {
    DNSMessage q = newQuestion(queryBuilder).build();
    // While this query method does in fact re-use query(Question, String)
    // we still do a cache lookup here in order to avoid unnecessary
    // findDNS()calls, which are expensive on Android. Note that we do not
    // put the results back into the Cache, as this is already done by
    // query(Question, String).
    DNSMessage responseMessage = (cache == null) ? null : cache.get(q);
    if (responseMessage != null) {
        return MiniDnsFuture.from(responseMessage);
    }
    final List<InetAddress> dnsServerAddresses = getServerAddresses();
    final InternalMiniDnsFuture<DNSMessage, IOException> future = new InternalMiniDnsFuture<>();
    final List<IOException> exceptions = Collections.synchronizedList(new ArrayList<IOException>(dnsServerAddresses.size()));
    // Filter loop.
    Iterator<InetAddress> it = dnsServerAddresses.iterator();
    while (it.hasNext()) {
        InetAddress dns = it.next();
        if (nonRaServers.contains(dns)) {
            it.remove();
            LOGGER.finer("Skipping " + dns + " because it was marked as \"recursion not available\"");
            continue;
        }
    }
    List<MiniDnsFuture<DNSMessage, IOException>> futures = new ArrayList<>(dnsServerAddresses.size());
    // "Main" loop.
    for (InetAddress dns : dnsServerAddresses) {
        if (future.isDone()) {
            for (MiniDnsFuture<DNSMessage, IOException> futureToCancel : futures) {
                futureToCancel.cancel(true);
            }
            break;
        }
        MiniDnsFuture<DNSMessage, IOException> f = queryAsync(q, dns);
        f.onSuccess(new SuccessCallback<DNSMessage>() {

            @Override
            public void onSuccess(DNSMessage result) {
                future.setResult(result);
            }
        });
        f.onError(new ExceptionCallback<IOException>() {

            @Override
            public void processException(IOException exception) {
                exceptions.add(exception);
                if (exceptions.size() == dnsServerAddresses.size()) {
                    future.setException(MultipleIoException.toIOException(exceptions));
                }
            }
        });
        futures.add(f);
    }
    return future;
}
Also used : InternalMiniDnsFuture(org.minidns.MiniDnsFuture.InternalMiniDnsFuture) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) InternalMiniDnsFuture(org.minidns.MiniDnsFuture.InternalMiniDnsFuture) IOException(java.io.IOException) InetAddress(java.net.InetAddress) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 2 with InternalMiniDnsFuture

use of org.minidns.MiniDnsFuture.InternalMiniDnsFuture in project minidns by MiniDNS.

the class DNSDataSource method queryAsync.

public MiniDnsFuture<DNSMessage, IOException> queryAsync(DNSMessage message, InetAddress address, int port, OnResponseCallback onResponseCallback) {
    InternalMiniDnsFuture<DNSMessage, IOException> future = new InternalMiniDnsFuture<>();
    DNSMessage result;
    try {
        result = query(message, address, port);
    } catch (IOException e) {
        future.setException(e);
        return future;
    }
    future.setResult(result);
    return future;
}
Also used : InternalMiniDnsFuture(org.minidns.MiniDnsFuture.InternalMiniDnsFuture) IOException(java.io.IOException) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 3 with InternalMiniDnsFuture

use of org.minidns.MiniDnsFuture.InternalMiniDnsFuture in project minidns by MiniDNS.

the class AbstractDNSClient method queryAsync.

/**
 * Default implementation of an asynchronous DNS query which just wraps the synchronous case.
 * <p>
 * Subclasses override this method to support true asynchronous queries.
 * </p>
 *
 * @param query the query.
 * @return a future for this query.
 */
protected MiniDnsFuture<DNSMessage, IOException> queryAsync(DNSMessage.Builder query) {
    InternalMiniDnsFuture<DNSMessage, IOException> future = new InternalMiniDnsFuture<>();
    DNSMessage result;
    try {
        result = query(query);
    } catch (IOException e) {
        future.setException(e);
        return future;
    }
    future.setResult(result);
    return future;
}
Also used : InternalMiniDnsFuture(org.minidns.MiniDnsFuture.InternalMiniDnsFuture) IOException(java.io.IOException) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Aggregations

IOException (java.io.IOException)3 InternalMiniDnsFuture (org.minidns.MiniDnsFuture.InternalMiniDnsFuture)3 DNSMessage (org.minidns.dnsmessage.DNSMessage)3 InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1