[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] [ernie/master] Clean up Main class a bit.
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Fri, 26 Feb 2010 15:57:56 +0100
Subject: Clean up Main class a bit.
Commit: 822fba12848a2a93e5f72000206eb9f0179117df
---
src/ArchiveReader.java | 28 ++++++++++++---------
src/BridgeDescriptorParser.java | 20 +++++++++++---
src/CachedRelayDescriptorReader.java | 12 ++++++---
src/Main.java | 46 +++++++++++++++++++++++-----------
src/RelayDescriptorParser.java | 26 ++++++++++++------
5 files changed, 87 insertions(+), 45 deletions(-)
diff --git a/src/ArchiveReader.java b/src/ArchiveReader.java
index 63eae80..a375d9e 100644
--- a/src/ArchiveReader.java
+++ b/src/ArchiveReader.java
@@ -10,10 +10,12 @@ public class ArchiveReader {
public ArchiveReader(RelayDescriptorParser rdp, String archivesDir) {
Logger logger = Logger.getLogger(ArchiveReader.class.getName());
if (new File(archivesDir).exists()) {
- try {
- rdp.initialize();
- } catch (IOException e) {
- return;
+ if (rdp != null) {
+ try {
+ rdp.initialize();
+ } catch (IOException e) {
+ return;
+ }
}
logger.info("Importing files in directory " + archivesDir
+ "/...");
@@ -27,14 +29,16 @@ public class ArchiveReader {
filesInInputDir.add(f);
}
} else {
- try {
- BufferedReader br = new BufferedReader(new FileReader(pop));
- rdp.parse(br);
- br.close();
- } catch (IOException e) {
- problems.add(pop);
- if (problems.size() > 3) {
- break;
+ if (rdp != null) {
+ try {
+ BufferedReader br = new BufferedReader(new FileReader(pop));
+ rdp.parse(br);
+ br.close();
+ } catch (IOException e) {
+ problems.add(pop);
+ if (problems.size() > 3) {
+ break;
+ }
}
}
}
diff --git a/src/BridgeDescriptorParser.java b/src/BridgeDescriptorParser.java
index 220cc8c..9021e5d 100644
--- a/src/BridgeDescriptorParser.java
+++ b/src/BridgeDescriptorParser.java
@@ -18,8 +18,12 @@ public class BridgeDescriptorParser {
Logger.getLogger(BridgeDescriptorParser.class.getName());
}
public void initialize() throws IOException {
- this.csfh.initialize();
- this.bsfh.initialize();
+ if (this.csfh != null) {
+ this.csfh.initialize();
+ }
+ if (this.bsfh != null) {
+ this.bsfh.initialize();
+ }
}
public void parse(BufferedReader br, String dateTime, boolean sanitized)
throws IOException, ParseException {
@@ -38,11 +42,15 @@ public class BridgeDescriptorParser {
runningBridges++;
}
}
- csfh.addBridgeConsensusResults(dateTime, runningBridges);
+ if (this.csfh != null) {
+ this.csfh.addBridgeConsensusResults(dateTime, runningBridges);
+ }
} else if (line.startsWith("extra-info ")) {
hashedIdentity = sanitized ? line.split(" ")[2]
: DigestUtils.shaHex(line.split(" ")[2]).toUpperCase();
- skip = bsfh.isKnownRelay(hashedIdentity);
+ if (this.bsfh != null) {
+ skip = this.bsfh.isKnownRelay(hashedIdentity);
+ }
} else if (!skip && line.startsWith("published ")) {
publishedLine = line;
} else if (!skip && line.startsWith("geoip-start-time ")) {
@@ -75,7 +83,9 @@ public class BridgeDescriptorParser {
}
String date = publishedLine.split(" ")[1];
String time = publishedLine.split(" ")[2];
- bsfh.addObs(hashedIdentity, date, time, obs);
+ if (this.bsfh != null) {
+ bsfh.addObs(hashedIdentity, date, time, obs);
+ }
}
}
}
diff --git a/src/CachedRelayDescriptorReader.java b/src/CachedRelayDescriptorReader.java
index 13bd02c..ea4d1a7 100644
--- a/src/CachedRelayDescriptorReader.java
+++ b/src/CachedRelayDescriptorReader.java
@@ -10,15 +10,19 @@ import org.apache.commons.codec.digest.*;
public class CachedRelayDescriptorReader {
public CachedRelayDescriptorReader(RelayDescriptorParser rdp,
ArchiveWriter aw) {
+ // TODO check if files are stale; print out warning that Tor process
+ // might have died
Logger logger = Logger.getLogger(
CachedRelayDescriptorReader.class.getName());
File cachedDescDir = new File("cacheddesc");
if (cachedDescDir.exists()) {
logger.info("Reading cacheddesc/ directory.");
- try {
- rdp.initialize(); // TODO get rid of this non-sense
- } catch (IOException e) {
- return;
+ if (rdp != null) {
+ try {
+ rdp.initialize(); // TODO get rid of this non-sense
+ } catch (IOException e) {
+ return;
+ }
}
for (File f : cachedDescDir.listFiles()) {
try {
diff --git a/src/Main.java b/src/Main.java
index 8d8b2a1..cc05614 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -28,8 +28,7 @@ public class Main {
SortedSet<String> countries = config.getDirreqBridgeCountries();
SortedSet<String> directories = config.getDirreqDirectories();
- // Prepare stats file handlers which will be initialized by the
- // importing/downloading classes
+ // Prepare stats file handlers (only if we are writing stats)
String statsDirectory = "stats";
ConsensusStatsFileHandler csfh = config.getWriteConsensusStats() ?
new ConsensusStatsFileHandler(statsDirectory) : null;
@@ -38,20 +37,19 @@ public class Main {
DirreqStatsFileHandler dsfh = config.getWriteDirreqStats() ?
new DirreqStatsFileHandler(statsDirectory, countries) : null;
- // Prepare parsers
- // TODO handle cases bsfh==NULL, csfh==NULL, dsfh==NULL
- RelayDescriptorParser rdp = new RelayDescriptorParser(statsDirectory,
- csfh, bsfh, dsfh, countries, directories);
- BridgeDescriptorParser bdp = new BridgeDescriptorParser(csfh, bsfh,
- countries);
+ // Prepare relay descriptor parser (only if we are writing the
+ // stats)
+ RelayDescriptorParser rdp = config.getWriteConsensusStats() &&
+ config.getWriteBridgeStats() && config.getWriteDirreqStats() ?
+ new RelayDescriptorParser(statsDirectory, csfh, bsfh, dsfh,
+ countries, directories) : null;
// Prepare writing relay descriptor archive to disk
ArchiveWriter aw = config.getWriteDirectoryArchives() ?
new ArchiveWriter(statsDirectory,
config.getV3DirectoryAuthorities()) : null;
- // TODO handle case aw==NULL below
- // import and/or download relay and bridge descriptors
+ // Import/download relay descriptors from the various sources
if (config.getImportCachedRelayDescriptors()) {
new CachedRelayDescriptorReader(rdp, aw);
}
@@ -63,6 +61,27 @@ public class Main {
config.getDownloadFromDirectoryAuthorities(),
directories);
}
+
+ // Write output to disk that only depends on relay descriptors
+ if (aw != null) {
+ aw.writeFile();
+ aw = null;
+ }
+ if (rdp != null) {
+ rdp.writeFile();
+ rdp = null;
+ }
+ if (dsfh != null) {
+ dsfh.writeFile();
+ dsfh = null;
+ }
+
+ // Prepare bridge descriptor parser
+ BridgeDescriptorParser bdp = config.getWriteConsensusStats() &&
+ config.getWriteBridgeStats() ? new BridgeDescriptorParser(
+ csfh, bsfh, countries) : null;
+
+ // Import bridge descriptors
if (config.getImportSanitizedBridges()) {
new SanitizedBridgesReader(bdp, "bridges", countries);
}
@@ -74,15 +93,12 @@ public class Main {
// Write updated stats files to disk
if (bsfh != null) {
bsfh.writeFile();
+ bsfh = null;
}
if (csfh != null) {
csfh.writeFile();
+ csfh = null;
}
- if (dsfh != null) {
- dsfh.writeFile();
- }
- rdp.writeFile();
- aw.writeFile();
// Import and process torperf stats
if (config.getImportWriteTorperfStats()) {
diff --git a/src/RelayDescriptorParser.java b/src/RelayDescriptorParser.java
index 2139f48..b17c1e7 100644
--- a/src/RelayDescriptorParser.java
+++ b/src/RelayDescriptorParser.java
@@ -40,9 +40,15 @@ public class RelayDescriptorParser {
if (this.initialized) {
return;
}
- this.csfh.initialize();
- this.bsfh.initialize();
- this.dsfh.initialize();
+ if (this.csfh != null) {
+ this.csfh.initialize();
+ }
+ if (this.bsfh != null) {
+ this.bsfh.initialize();
+ }
+ if (this.dsfh != null) {
+ this.dsfh.initialize();
+ }
this.lastParsedConsensus = null;
this.lastParsedExtraInfos = new TreeMap<String, String>();
if (this.relayDescriptorParseHistory.exists()) {
@@ -83,10 +89,10 @@ public class RelayDescriptorParser {
}
} else if (line.equals("vote-status vote")) {
return;
- } else if (line.startsWith("r ")) {
+ } else if (line.startsWith("r ") && this.bsfh != null) {
String hashedRelay = DigestUtils.shaHex(Base64.decodeBase64(
line.split(" ")[2] + "=")).toUpperCase();
- bsfh.addHashedRelay(hashedRelay);
+ this.bsfh.addHashedRelay(hashedRelay);
} else if (line.startsWith("s ")) {
if (line.contains(" Running")) {
exit += line.contains(" Exit") ? 1 : 0;
@@ -97,12 +103,14 @@ public class RelayDescriptorParser {
}
}
}
- csfh.addConsensusResults(validAfter, exit, fast, guard, running,
+ if (this.csfh != null) {
+ csfh.addConsensusResults(validAfter, exit, fast, guard, running,
stable);
+ }
} else if (line.startsWith("router ")) {
// in case we want to parse server descriptors in the future
- } else if (line.startsWith("extra-info ")
- && directories.contains(line.split(" ")[2])) {
+ } else if (line.startsWith("extra-info ") && this.dsfh != null &&
+ directories.contains(line.split(" ")[2])) {
String dir = line.split(" ")[2];
String statsEnd = null, date = null, v3ips = null;
boolean skip = false;
@@ -129,7 +137,7 @@ public class RelayDescriptorParser {
}
String share = line.substring("dirreq-v3-share ".length(),
line.length() - 1);
- dsfh.addObs(dir, date, obs, share);
+ this.dsfh.addObs(dir, date, obs, share);
if (!this.lastParsedExtraInfos.containsKey(dir) ||
statsEnd.compareTo(
this.lastParsedExtraInfos.get(dir)) > 0) {
--
1.6.5