commit 27f05e1d4c67d8954b5cd2975781870a0dcdb9d9 Author: iwakeh iwakeh@torproject.org Date: Mon Jun 26 20:12:11 2017 +0000
All examples compile and work as explained in Metrics' web-site tutorials. Added a hint about where to find the examples to the README.md file. Implements task-22734. --- README.md | 6 +++ .../examples/ConsensusWeightByVersion.java | 45 +++++++++------------- .../resources/examples/PluggableTransports.java | 29 ++++++-------- 3 files changed, 36 insertions(+), 44 deletions(-)
diff --git a/README.md b/README.md index 0cc93d8..aa309ac 100644 --- a/README.md +++ b/README.md @@ -97,3 +97,9 @@ The Metrics website has a tutorial for getting started with metrics-lib:
https://metrics.torproject.org/metrics-lib.html
+The examples explained in the tutorials are available as source code in + +``` +src/main/resources/examples/ +``` + diff --git a/src/main/resources/examples/ConsensusWeightByVersion.java b/src/main/resources/examples/ConsensusWeightByVersion.java index 3b8495d..5fc7c1c 100644 --- a/src/main/resources/examples/ConsensusWeightByVersion.java +++ b/src/main/resources/examples/ConsensusWeightByVersion.java @@ -19,35 +19,26 @@ public class ConsensusWeightByVersion {
// Read descriptors from disk. DescriptorReader descriptorReader = DescriptorSourceFactory.createDescriptorReader(); - - // Add the directory with descriptors to the descriptor reader. - descriptorReader.addDirectory(new File("descriptors/recent/relay-descriptors/consensuses")); - Iterator<DescriptorFile> descriptorFiles = descriptorReader.readDescriptors(); - while (descriptorFiles.hasNext()) { // Iterate over all descriptor files found. - DescriptorFile descriptorFile = descriptorFiles.next(); - - // Now, iterate over the descriptors contained in the file. - for (Descriptor descriptor : descriptorFile.getDescriptors()) { - if (!(descriptor instanceof RelayNetworkStatusConsensus)) { - // We're only interested in consensuses. + for (Descriptor descriptor : descriptorReader.readDescriptors(new File("descriptors/recent/relay-descriptors/consensuses"))) { + if (!(descriptor instanceof RelayNetworkStatusConsensus)) { + // We're only interested in consensuses. + continue; + } + RelayNetworkStatusConsensus consensus = (RelayNetworkStatusConsensus) descriptor; + for (NetworkStatusEntry entry : consensus.getStatusEntries().values()) { + String version = entry.getVersion(); + if (!version.startsWith("Tor ") || version.length() < 9) { + // We're only interested in a.b.c type versions for this example. continue; } - RelayNetworkStatusConsensus consensus = (RelayNetworkStatusConsensus) descriptor; - for (NetworkStatusEntry entry : consensus.getStatusEntries().values()) { - String version = entry.getVersion(); - if (!version.startsWith("Tor ") || version.length() < 9) { - // We're only interested in a.b.c type versions for this example. - continue; - } - // Remove the 'Tor ' prefix and anything starting at the patch level. - version = version.substring(4, 9); - long bandwidth = entry.getBandwidth(); - totalBandwidth += bandwidth; - if (bandwidthByVersion.containsKey(version)) { - bandwidthByVersion.put(version, bandwidth + bandwidthByVersion.get(version)); - } else { - bandwidthByVersion.put(version, bandwidth); - } + // Remove the 'Tor ' prefix and anything starting at the patch level. + version = version.substring(4, 9); + long bandwidth = entry.getBandwidth(); + totalBandwidth += bandwidth; + if (bandwidthByVersion.containsKey(version)) { + bandwidthByVersion.put(version, bandwidth + bandwidthByVersion.get(version)); + } else { + bandwidthByVersion.put(version, bandwidth); } } } diff --git a/src/main/resources/examples/PluggableTransports.java b/src/main/resources/examples/PluggableTransports.java index 88e7eb6..65ce732 100644 --- a/src/main/resources/examples/PluggableTransports.java +++ b/src/main/resources/examples/PluggableTransports.java @@ -16,23 +16,18 @@ public class PluggableTransports { SortedMap<String, Integer> countedTransports = new TreeMap<>();
DescriptorReader descriptorReader = DescriptorSourceFactory.createDescriptorReader(); - descriptorReader.addDirectory(new File("descriptors/recent/bridge-descriptors/extra-infos")); - Iterator<DescriptorFile> descriptorFiles = descriptorReader.readDescriptors(); - while (descriptorFiles.hasNext()) { - DescriptorFile descriptorFile = descriptorFiles.next(); - for (Descriptor descriptor : descriptorFile.getDescriptors()) { - if (!(descriptor instanceof BridgeExtraInfoDescriptor)) { - continue; - } - BridgeExtraInfoDescriptor extraInfo = (BridgeExtraInfoDescriptor) descriptor; - String fingerprint = extraInfo.getFingerprint(); - if (observedFingerprints.add(fingerprint)) { - for (String transport : extraInfo.getTransports()) { - if (countedTransports.containsKey(transport)) { - countedTransports.put(transport, 1 + countedTransports.get(transport)); - } else { - countedTransports.put(transport, 1); - } + for (Descriptor descriptor : descriptorReader.readDescriptors(new File("descriptors/recent/bridge-descriptors/extra-infos"))) { + if (!(descriptor instanceof BridgeExtraInfoDescriptor)) { + continue; + } + BridgeExtraInfoDescriptor extraInfo = (BridgeExtraInfoDescriptor) descriptor; + String fingerprint = extraInfo.getFingerprint(); + if (observedFingerprints.add(fingerprint)) { + for (String transport : extraInfo.getTransports()) { + if (countedTransports.containsKey(transport)) { + countedTransports.put(transport, 1 + countedTransports.get(transport)); + } else { + countedTransports.put(transport, 1); } } }