use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class AbstractDNSClient method query.
public final DNSMessage query(DNSMessage requestMessage, InetAddress address, int port) throws IOException {
// See if we have the answer to this question already cached
DNSMessage responseMessage = (cache == null) ? null : cache.get(requestMessage);
if (responseMessage != null) {
return responseMessage;
}
final Question q = requestMessage.getQuestion();
final Level TRACE_LOG_LEVEL = Level.FINE;
LOGGER.log(TRACE_LOG_LEVEL, "Asking {0} on {1} for {2} with:\n{3}", new Object[] { address, port, q, requestMessage });
try {
responseMessage = dataSource.query(requestMessage, address, port);
} catch (IOException e) {
LOGGER.log(TRACE_LOG_LEVEL, "IOException {0} on {1} while resolving {2}: {3}", new Object[] { address, port, q, e });
throw e;
}
if (responseMessage != null) {
LOGGER.log(TRACE_LOG_LEVEL, "Response from {0} on {1} for {2}:\n{3}", new Object[] { address, port, q, responseMessage });
} else {
// TODO When should this ever happen?
LOGGER.log(Level.SEVERE, "NULL response from " + address + " on " + port + " for " + q);
}
if (responseMessage == null)
return null;
onResponseCallback.onResponse(requestMessage, responseMessage);
return responseMessage;
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class AbstractDNSClient method queryAsync.
public final MiniDnsFuture<DNSMessage, IOException> queryAsync(DNSMessage requestMessage, InetAddress address, int port) {
// See if we have the answer to this question already cached
DNSMessage responseMessage = (cache == null) ? null : cache.get(requestMessage);
if (responseMessage != null) {
return MiniDnsFuture.from(responseMessage);
}
final Question q = requestMessage.getQuestion();
final Level TRACE_LOG_LEVEL = Level.FINE;
LOGGER.log(TRACE_LOG_LEVEL, "Asynchronusly asking {0} on {1} for {2} with:\n{3}", new Object[] { address, port, q, requestMessage });
return dataSource.queryAsync(requestMessage, address, port, onResponseCallback);
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class AbstractDNSClient method getQueryFor.
protected DNSMessage getQueryFor(Question q) {
DNSMessage.Builder messageBuilder = buildMessage(q);
DNSMessage query = messageBuilder.build();
return query;
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class AbstractDNSClient method getCachedRecordsFor.
private <D extends Data> Set<D> getCachedRecordsFor(DNSName dnsName, TYPE type) {
Question dnsNameNs = new Question(dnsName, type);
DNSMessage queryDnsNameNs = getQueryFor(dnsNameNs);
DNSMessage cachedResult = cache.get(queryDnsNameNs);
if (cachedResult == null)
return Collections.emptySet();
return cachedResult.getAnswersFor(dnsNameNs);
}
use of org.minidns.dnsmessage.DNSMessage 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;
}
Aggregations