streamlined dns helper by ignoring weight

This commit is contained in:
Daniel Gultsch 2015-06-04 02:48:07 +02:00
parent 6b794eca2c
commit 8f07e4c441

View file

@ -47,88 +47,43 @@ public class DNSHelper {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
try { try {
String qname = "_xmpp-client._tcp." + host; String qname = "_xmpp-client._tcp." + host;
Log.d(Config.LOGTAG, Log.d(Config.LOGTAG, "using dns server: " + dnsServer.getHostAddress() + " to look up " + host);
"using dns server: " + dnsServer.getHostAddress() DNSMessage message = client.query(qname, TYPE.SRV, CLASS.IN, dnsServer.getHostAddress());
+ " to look up " + host);
DNSMessage message = client.query(qname, TYPE.SRV, CLASS.IN,
dnsServer.getHostAddress());
// How should we handle priorities and weight?
// Wikipedia has a nice article about priorities vs. weights:
// https://en.wikipedia.org/wiki/SRV_record#Provisioning_for_high_service_availability
// we bucket the SRV records based on priority, pick per priority
// a random order respecting the weight, and dump that priority by
// priority
TreeMap<Integer, ArrayList<SRV>> priorities = new TreeMap<>(); TreeMap<Integer, ArrayList<SRV>> priorities = new TreeMap<>();
TreeMap<String, ArrayList<String>> ips4 = new TreeMap<>(); TreeMap<String, ArrayList<String>> ips4 = new TreeMap<>();
TreeMap<String, ArrayList<String>> ips6 = new TreeMap<>(); TreeMap<String, ArrayList<String>> ips6 = new TreeMap<>();
for (Record[] rrset : new Record[][] { message.getAnswers(), for (Record[] rrset : new Record[][] { message.getAnswers(), message.getAdditionalResourceRecords() }) {
message.getAdditionalResourceRecords() }) {
for (Record rr : rrset) { for (Record rr : rrset) {
Data d = rr.getPayload(); Data d = rr.getPayload();
if (d instanceof SRV if (d instanceof SRV && NameUtil.idnEquals(qname, rr.getName())) {
&& NameUtil.idnEquals(qname, rr.getName())) {
SRV srv = (SRV) d; SRV srv = (SRV) d;
if (!priorities.containsKey(srv.getPriority())) { if (!priorities.containsKey(srv.getPriority())) {
priorities.put(srv.getPriority(), priorities.put(srv.getPriority(),new ArrayList<SRV>());
new ArrayList<SRV>(2));
} }
priorities.get(srv.getPriority()).add(srv); priorities.get(srv.getPriority()).add(srv);
} }
if (d instanceof A) { if (d instanceof A) {
A arecord = (A) d; A a = (A) d;
if (!ips4.containsKey(rr.getName())) { if (!ips4.containsKey(rr.getName())) {
ips4.put(rr.getName(), new ArrayList<String>(3)); ips4.put(rr.getName(), new ArrayList<String>());
} }
ips4.get(rr.getName()).add(arecord.toString()); ips4.get(rr.getName()).add(a.toString());
} }
if (d instanceof AAAA) { if (d instanceof AAAA) {
AAAA aaaa = (AAAA) d; AAAA aaaa = (AAAA) d;
if (!ips6.containsKey(rr.getName())) { if (!ips6.containsKey(rr.getName())) {
ips6.put(rr.getName(), new ArrayList<String>(3)); ips6.put(rr.getName(), new ArrayList<String>());
} }
ips6.get(rr.getName()).add("[" + aaaa.toString() + "]"); ips6.get(rr.getName()).add("[" + aaaa.toString() + "]");
} }
} }
} }
Random rnd = new Random(); ArrayList<SRV> result = new ArrayList<>();
ArrayList<SRV> result = new ArrayList<>(
priorities.size() * 2 + 1);
for (ArrayList<SRV> s : priorities.values()) { for (ArrayList<SRV> s : priorities.values()) {
// trivial case
if (s.size() <= 1) {
result.addAll(s);
continue;
}
long totalweight = 0l;
for (SRV srv : s) {
totalweight += srv.getWeight();
}
while (totalweight > 0l && s.size() > 0) {
long p = (rnd.nextLong() & 0x7fffffffffffffffl)
% totalweight;
int i = 0;
while (p > 0) {
p -= s.get(i++).getPriority();
}
if (i>0) i--;
// remove is expensive, but we have only a few entries
// anyway
SRV srv = s.remove(i);
totalweight -= srv.getWeight();
result.add(srv);
}
Collections.shuffle(s, rnd);
result.addAll(s); result.addAll(s);
} }
ArrayList<Bundle> values = new ArrayList<>(); ArrayList<Bundle> values = new ArrayList<>();