diff options
| author | 2016-02-03 14:12:23 +0900 | |
|---|---|---|
| committer | 2016-02-11 00:28:27 +0900 | |
| commit | 106cdf6c94c455462316555b7ac59ec79c3b40fa (patch) | |
| tree | 6aa22b87b9ebdd0fd73fafd6d5241680250709a0 | |
| parent | 2750b41d0b50eb66c8aada11d62666f934ca950d (diff) | |
Expose measurement results to interested callers.
With this it's possible for callers to:
Network network = ...
LinkProperties lp = ...
NetworkDiagnostics netdiag = new NetworkDiagnostics(network, lp, 5000);
netdiag.startMeasurements();
netdiag.waitForMeasurements();
boolean result = true;
for (Measurement m : netdiag.getMeasurements()) {
if (!m.checkSucceeded()) {
result = false;
// Additional error handling here.
}
}
return result;
Bug: 21832299
Bug: 26780890
Change-Id: Iaf7ff029e2c6a998b574c23d0dcde8d57a467a22
| -rw-r--r-- | services/core/java/com/android/server/connectivity/NetworkDiagnostics.java | 90 |
1 files changed, 63 insertions, 27 deletions
diff --git a/services/core/java/com/android/server/connectivity/NetworkDiagnostics.java b/services/core/java/com/android/server/connectivity/NetworkDiagnostics.java index 5fd39c02a10a..dc6260921a67 100644 --- a/services/core/java/com/android/server/connectivity/NetworkDiagnostics.java +++ b/services/core/java/com/android/server/connectivity/NetworkDiagnostics.java @@ -49,7 +49,9 @@ import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.Arrays; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Random; @@ -107,27 +109,33 @@ public class NetworkDiagnostics { // so callers can wait for completion. private final CountDownLatch mCountDownLatch; - private class Measurement { + public class Measurement { private static final String SUCCEEDED = "SUCCEEDED"; private static final String FAILED = "FAILED"; - // TODO: Refactor to make these private for better encapsulation. - public String description = ""; - public long startTime; - public long finishTime; - public String result = ""; - public Thread thread; + private boolean succeeded; - public void recordSuccess(String msg) { + // Package private. TODO: investigate better encapsulation. + String description = ""; + long startTime; + long finishTime; + String result = ""; + Thread thread; + + public boolean checkSucceeded() { return succeeded; } + + void recordSuccess(String msg) { maybeFixupTimes(); + succeeded = true; result = SUCCEEDED + ": " + msg; if (mCountDownLatch != null) { mCountDownLatch.countDown(); } } - public void recordFailure(String msg) { + void recordFailure(String msg) { maybeFixupTimes(); + succeeded = false; result = FAILED + ": " + msg; if (mCountDownLatch != null) { mCountDownLatch.countDown(); @@ -265,41 +273,69 @@ public class NetworkDiagnostics { } catch (InterruptedException ignored) {} } - public void dump(IndentingPrintWriter pw) { - pw.println(TAG + ":" + mDescription); - final long unfinished = mCountDownLatch.getCount(); - if (unfinished > 0) { - // This can't happen unless a caller forgets to call waitForMeasurements() - // or a measurement isn't implemented to correctly honor the timeout. - pw.println("WARNING: countdown wait incomplete: " - + unfinished + " unfinished measurements"); - } + public List<Measurement> getMeasurements() { + // TODO: Consider moving waitForMeasurements() in here to minimize the + // chance of caller errors. - pw.increaseIndent(); + ArrayList<Measurement> measurements = new ArrayList(totalMeasurementCount()); + + // Sort measurements IPv4 first. for (Map.Entry<InetAddress, Measurement> entry : mIcmpChecks.entrySet()) { if (entry.getKey() instanceof Inet4Address) { - pw.println(entry.getValue().toString()); + measurements.add(entry.getValue()); + } + } + for (Map.Entry<Pair<InetAddress, InetAddress>, Measurement> entry : + mExplicitSourceIcmpChecks.entrySet()) { + if (entry.getKey().first instanceof Inet4Address) { + measurements.add(entry.getValue()); + } + } + for (Map.Entry<InetAddress, Measurement> entry : mDnsUdpChecks.entrySet()) { + if (entry.getKey() instanceof Inet4Address) { + measurements.add(entry.getValue()); } } + + // IPv6 measurements second. for (Map.Entry<InetAddress, Measurement> entry : mIcmpChecks.entrySet()) { if (entry.getKey() instanceof Inet6Address) { - pw.println(entry.getValue().toString()); + measurements.add(entry.getValue()); } } for (Map.Entry<Pair<InetAddress, InetAddress>, Measurement> entry : mExplicitSourceIcmpChecks.entrySet()) { - pw.println(entry.getValue().toString()); - } - for (Map.Entry<InetAddress, Measurement> entry : mDnsUdpChecks.entrySet()) { - if (entry.getKey() instanceof Inet4Address) { - pw.println(entry.getValue().toString()); + if (entry.getKey().first instanceof Inet6Address) { + measurements.add(entry.getValue()); } } for (Map.Entry<InetAddress, Measurement> entry : mDnsUdpChecks.entrySet()) { if (entry.getKey() instanceof Inet6Address) { - pw.println(entry.getValue().toString()); + measurements.add(entry.getValue()); } } + + return measurements; + } + + public void dump(IndentingPrintWriter pw) { + pw.println(TAG + ":" + mDescription); + final long unfinished = mCountDownLatch.getCount(); + if (unfinished > 0) { + // This can't happen unless a caller forgets to call waitForMeasurements() + // or a measurement isn't implemented to correctly honor the timeout. + pw.println("WARNING: countdown wait incomplete: " + + unfinished + " unfinished measurements"); + } + + pw.increaseIndent(); + + String prefix; + for (Measurement m : getMeasurements()) { + prefix = m.checkSucceeded() ? "." : "F"; + pw.println(prefix + " " + m.toString()); + } + pw.decreaseIndent(); } |