[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] [ernie/master] Start using a config file.
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Fri, 19 Feb 2010 21:08:03 +0100
Subject: Start using a config file.
Commit: ee37fcc1c81b238e649a83514ae8a9851382a598
---
config | 72 ++++++++++++++++++
src/Configuration.java | 192 ++++++++++++++++++++++++++++++++++++++++++++++++
src/Main.java | 3 +
3 files changed, 267 insertions(+), 0 deletions(-)
create mode 100644 config
create mode 100644 src/Configuration.java
diff --git a/config b/config
new file mode 100644
index 0000000..5289c2a
--- /dev/null
+++ b/config
@@ -0,0 +1,72 @@
+##
+## ERNIE performs its tasks in three phases:
+##
+## 1. Import files from the import/ directory that we didn't import yet
+##
+## 2. Download files that we want to learn about but didn't from importing
+##
+## 3. Write to disk what we learned
+##
+## Configuration options below are structured by these phases, starting
+## with the data we care about and want to write to disk (3.), followed by
+## the data we want to import (1.) or download (2.). Note that the default
+## for downloading data is set to off!
+############## TODO rethink this order
+##
+
+#### Data that we want to write to disk ####
+
+## Write consensus stats to disk
+#WriteConsensusStats 1
+
+## Write dirreq stats to disk
+#WriteDirreqStats 1
+
+## Comma-separated set of countries to be included in dirreq and bridge
+## graphs; note that after adding new countries, an import of the relevant
+## descriptor archives (relay and/or bridge) is necessary!
+#DirreqBridgeCountries bh,cn,cu,et,ir,mm,sa,sy,tn,tm,uz,vn,ye
+
+## Comma-separated set of fingerprints of directory mirrors to be included
+## in dirreq and bridge graphs; note that after adding new directories, an
+## import of the relevant descriptor archives (relay and/or bridge) is
+## necessary!
+#DirreqDirectories 8522EB98C91496E80EC238E732594D1509158E77,9695DFC35FFEB861329B9F1AB04C46397020CE31
+
+## Write bridge stats to disk
+#WriteBridgeStats 1
+
+## Write torperf stats to disk
+#WriteTorperfStats 1
+
+## Write GetTor stats to disk
+#WriteGettorStats 1
+
+## Write directory archives to disk
+#WriteDirectoryArchives 1
+
+## Import directory archives from disk, if available
+#ImportDirectoryArchives 1
+
+## Import sanitized bridges from disk, if available
+#ImportSanitizedBridges 1
+
+## Import bridge snapshots from disk, if available
+#ImportBridgeSnapshots 1
+
+## Import torperf stats from disk, if available
+#ImportTorperfStats 1
+
+## Download relay descriptors from directory authorities, if required
+#DownloadRelayDescriptors 0
+
+## Comma separated list of directory authority addresses (IP[:port]) to
+## download missing relay descriptors from
+#DownloadFromDirectoryAuthorities 86.59.21.38,194.109.206.212,80.190.246.100:8180
+
+## Download GetTor stats
+#DownloadGetTorStats 0
+
+## URL to download GetTor stats from
+#GetTorStatsURL = http://gettor.torproject.org:8080/~gettor/gettor_stats.txt
+
diff --git a/src/Configuration.java b/src/Configuration.java
new file mode 100644
index 0000000..b08debc
--- /dev/null
+++ b/src/Configuration.java
@@ -0,0 +1,192 @@
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import java.util.logging.*;
+/**
+ * Initialize configuration with hard-coded defaults, overwrite with
+ * configuration in config config file, if exists, and answer Main.java
+ * about our configuration.
+ */
+public class Configuration {
+ private boolean writeStats = true;
+ private boolean writeConsensusStats = true;
+ private boolean writeDirreqStats = true;
+ private SortedSet<String> dirreqBridgeCountries = new TreeSet<String>(
+ Arrays.asList("bh,cn,cu,et,ir,mm,sa,sy,tn,tm,uz,vn,ye".split(",")));
+ private SortedSet<String> dirreqDirectories = new TreeSet<String>(
+ Arrays.asList("8522EB98C91496E80EC238E732594D1509158E77,"
+ + "9695DFC35FFEB861329B9F1AB04C46397020CE31"));
+ private boolean writeBridgeStats = true;
+ private boolean writeTorperfStats = true;
+ private boolean writeGettorStats = true;
+ private boolean writeDirectoryArchives = true;
+ private boolean importDirectoryArchives = true;
+ private boolean importSanitizedBridges = true;
+ private boolean importBridgeSnapshots = true;
+ private boolean importTorperfStats = true;
+ private boolean downloadRelayDescriptors = false;
+ private List<String> downloadFromDirectoryAuthorities = Arrays.asList(
+ "86.59.21.38,194.109.206.212,80.190.246.100:8180".split(","));
+ private boolean downloadGetTorStats = false;
+ private String getTorStatsUrl = "http://gettor.torproject.org:8080/"
+ + "~gettor/gettor_stats.txt";
+ public Configuration() {
+ Logger logger = Logger.getLogger(Configuration.class.getName());
+ File configFile = new File("config");
+ if (!configFile.exists()) {
+ return;
+ }
+ String line = null;
+ try {
+ BufferedReader br = new BufferedReader(new FileReader(configFile));
+ while ((line = br.readLine()) != null) {
+ if (line.startsWith("#") || line.length() < 1) {
+ continue;
+ } else if (line.startsWith("WriteConsensusStats")) {
+ this.writeConsensusStats = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("WriteDirreqStats")) {
+ this.writeDirreqStats = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("DirreqBridgeCountries")) {
+ this.dirreqBridgeCountries = new TreeSet<String>();
+ for (String country : line.split(" ")[1].split(",")) {
+ if (country.length() != 2) {
+ logger.severe("Configuration file contains illegal country "
+ + "code in line '" + line + "'! Exiting!");
+ System.exit(1);
+ }
+ this.dirreqBridgeCountries.add(country);
+ }
+ } else if (line.startsWith("DirreqDirectories")) {
+ this.dirreqDirectories = new TreeSet<String>(
+ Arrays.asList(line.split(" ")[1].split(",")));
+ } else if (line.startsWith("WriteBridgeStats")) {
+ this.writeBridgeStats = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("WriteTorperfStats")) {
+ this.writeTorperfStats = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("WriteGettorStats")) {
+ this.writeGettorStats = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("WriteDirectoryArchives")) {
+ this.writeDirectoryArchives = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("ImportDirectoryArchives")) {
+ this.importDirectoryArchives = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("ImportSanitizedBridges")) {
+ this.importSanitizedBridges = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("ImportBridgeSnapshots")) {
+ this.importBridgeSnapshots = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("ImportTorperfStats")) {
+ this.importTorperfStats = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("DownloadRelayDescriptors")) {
+ this.downloadRelayDescriptors = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("DownloadFromDirectoryAuthorities")) {
+ this.downloadFromDirectoryAuthorities = new ArrayList<String>();
+ for (String dir : line.split(" ")[1].split(",")) {
+ // test if IP:port pair has correct format
+ if (dir.length() < 1) {
+ logger.severe("Configuration file contains directory "
+ + "authority IP:port of length 0 in line '" + line
+ + "'! Exiting!");
+ System.exit(1);
+ }
+ new URL("http://" + dir + "/");
+ this.downloadFromDirectoryAuthorities.add(dir);
+ }
+ } else if (line.startsWith("DownloadGetTorStats")) {
+ this.downloadGetTorStats = Integer.parseInt(
+ line.split(" ")[1]) != 0;
+ } else if (line.startsWith("GetTorStatsURL")) {
+ String newUrl = line.split(" ")[1];
+ // test if URL has correct format
+ new URL(newUrl);
+ this.getTorStatsUrl = newUrl;
+ } else {
+ logger.severe("Configuration file contains unrecognized "
+ + "configuration key in line '" + line + "'! Exiting!");
+ System.exit(1);
+ }
+ }
+ br.close();
+ } catch (ArrayIndexOutOfBoundsException e) {
+ logger.severe("Configuration file contains configuration key "
+ + "without value in line '" + line + "'. Exiting!");
+ System.exit(1);
+ } catch (MalformedURLException e) {
+ logger.severe("Configuration file contains illegal URL or IP:port "
+ + "pair in line '" + line + "'. Exiting!");
+ System.exit(1);
+ } catch (NumberFormatException e) {
+ logger.severe("Configuration file contains illegal value in line '"
+ + line + "' with legal values being 0 or 1. Exiting!");
+ System.exit(1);
+ } catch (IOException e) {
+ logger.log(Level.SEVERE, "Unknown problem while reading config "
+ + "file! Exiting!", e);
+ System.exit(1);
+ }
+ }
+ public boolean getWriteStats() {
+ return this.writeStats;
+ }
+ public boolean getWriteConsensusStats() {
+ return this.writeConsensusStats;
+ }
+ public boolean getWriteDirreqStats() {
+ return this.writeDirreqStats;
+ }
+ public SortedSet<String> getDirreqBridgeCountries() {
+ return this.dirreqBridgeCountries;
+ }
+ public SortedSet<String> getDirreqDirectories() {
+ return this.dirreqDirectories;
+ }
+ public boolean getWriteBridgeStats() {
+ return this.writeBridgeStats;
+ }
+ public boolean getWriteTorperfStats() {
+ return this.writeTorperfStats;
+ }
+ public boolean getWriteGettorStats() {
+ return this.writeGettorStats;
+ }
+ public boolean getWriteDirectoryArchives() {
+ return this.writeDirectoryArchives;
+ }
+ public boolean getImportDirectoryArchives() {
+ return this.importDirectoryArchives;
+ }
+ public boolean getImportSanitizedBridges() {
+ return this.importSanitizedBridges;
+ }
+ public boolean getImportBridgeSnapshots() {
+ return this.importBridgeSnapshots;
+ }
+ public boolean getImportTorperfStats() {
+ return this.importTorperfStats;
+ }
+ public boolean getDownloadRelayDescriptors() {
+ return this.downloadRelayDescriptors;
+ }
+ public List<String> getDownloadFromDirectoryAuthorities() {
+ return this.downloadFromDirectoryAuthorities;
+ }
+ public boolean getDownloadGetTorStats() {
+ return this.downloadGetTorStats;
+ }
+ public String getGetTorStatsUrl() {
+ return this.getTorStatsUrl;
+ }
+ public static void main(String[] args) {
+ new Configuration();
+ }
+}
+
diff --git a/src/Main.java b/src/Main.java
index 1685995..6144f55 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -13,6 +13,9 @@ public class Main {
Logger logger = Logger.getLogger(Main.class.getName());
logger.info("Starting ERNIE...");
+ // Initialize configuration
+ Configuration config = new Configuration();
+
// Use lock file to avoid overlapping runs
LockFile lf = new LockFile();
if (!lf.acquireLock()) {
--
1.6.5