commit 24cdd9815c79f0d29db58fbab5dc1af8fef85b57 Author: Karsten Loesing karsten.loesing@gmx.net Date: Wed Jul 23 17:51:07 2014 +0200
Create two runner classes for updating files. --- src/org/torproject/onionoo/cron/Main.java | 84 +++----------------- .../onionoo/updater/StatusUpdateRunner.java | 51 ++++++++++++ .../onionoo/writer/DocumentWriterRunner.java | 37 +++++++++ 3 files changed, 98 insertions(+), 74 deletions(-)
diff --git a/src/org/torproject/onionoo/cron/Main.java b/src/org/torproject/onionoo/cron/Main.java index 94dfa00..ba905fa 100644 --- a/src/org/torproject/onionoo/cron/Main.java +++ b/src/org/torproject/onionoo/cron/Main.java @@ -2,28 +2,13 @@ * See LICENSE for licensing information */ package org.torproject.onionoo.cron;
-import java.io.File; - import org.torproject.onionoo.docs.DocumentStore; -import org.torproject.onionoo.updater.BandwidthStatusUpdater; -import org.torproject.onionoo.updater.ClientsStatusUpdater; import org.torproject.onionoo.updater.DescriptorSource; -import org.torproject.onionoo.updater.LookupService; -import org.torproject.onionoo.updater.NodeDetailsStatusUpdater; -import org.torproject.onionoo.updater.ReverseDomainNameResolver; -import org.torproject.onionoo.updater.StatusUpdater; -import org.torproject.onionoo.updater.UptimeStatusUpdater; -import org.torproject.onionoo.updater.WeightsStatusUpdater; +import org.torproject.onionoo.updater.StatusUpdateRunner; import org.torproject.onionoo.util.ApplicationFactory; import org.torproject.onionoo.util.LockFile; import org.torproject.onionoo.util.Logger; -import org.torproject.onionoo.writer.BandwidthDocumentWriter; -import org.torproject.onionoo.writer.ClientsDocumentWriter; -import org.torproject.onionoo.writer.DetailsDocumentWriter; -import org.torproject.onionoo.writer.DocumentWriter; -import org.torproject.onionoo.writer.SummaryDocumentWriter; -import org.torproject.onionoo.writer.UptimeDocumentWriter; -import org.torproject.onionoo.writer.WeightsDocumentWriter; +import org.torproject.onionoo.writer.DocumentWriterRunner;
/* Update search data and status data files. */ public class Main { @@ -48,38 +33,10 @@ public class Main { Logger.printStatusTime("Initialized descriptor source"); DocumentStore ds = ApplicationFactory.getDocumentStore(); Logger.printStatusTime("Initialized document store"); - LookupService ls = new LookupService(new File("geoip")); - Logger.printStatusTime("Initialized Geoip lookup service"); - ReverseDomainNameResolver rdnr = new ReverseDomainNameResolver(); - Logger.printStatusTime("Initialized reverse domain name resolver"); - NodeDetailsStatusUpdater ndsu = new NodeDetailsStatusUpdater(rdnr, - ls); - Logger.printStatusTime("Initialized node data writer"); - BandwidthStatusUpdater bsu = new BandwidthStatusUpdater(); - Logger.printStatusTime("Initialized bandwidth status updater"); - WeightsStatusUpdater wsu = new WeightsStatusUpdater(); - Logger.printStatusTime("Initialized weights status updater"); - ClientsStatusUpdater csu = new ClientsStatusUpdater(); - Logger.printStatusTime("Initialized clients status updater"); - UptimeStatusUpdater usu = new UptimeStatusUpdater(); - Logger.printStatusTime("Initialized uptime status updater"); - StatusUpdater[] sus = new StatusUpdater[] { ndsu, bsu, wsu, csu, - usu }; - - SummaryDocumentWriter sdw = new SummaryDocumentWriter(); - Logger.printStatusTime("Initialized summary document writer"); - DetailsDocumentWriter ddw = new DetailsDocumentWriter(); - Logger.printStatusTime("Initialized details document writer"); - BandwidthDocumentWriter bdw = new BandwidthDocumentWriter(); - Logger.printStatusTime("Initialized bandwidth document writer"); - WeightsDocumentWriter wdw = new WeightsDocumentWriter(); - Logger.printStatusTime("Initialized weights document writer"); - ClientsDocumentWriter cdw = new ClientsDocumentWriter(); - Logger.printStatusTime("Initialized clients document writer"); - UptimeDocumentWriter udw = new UptimeDocumentWriter(); - Logger.printStatusTime("Initialized uptime document writer"); - DocumentWriter[] dws = new DocumentWriter[] { sdw, ddw, bdw, wdw, cdw, - udw }; + StatusUpdateRunner sur = new StatusUpdateRunner(); + Logger.printStatusTime("Initialized status update runner"); + DocumentWriterRunner dwr = new DocumentWriterRunner(); + Logger.printStatusTime("Initialized document writer runner");
Logger.printStatus("Downloading descriptors."); dso.downloadDescriptors(); @@ -88,16 +45,10 @@ public class Main { dso.readDescriptors();
Logger.printStatus("Updating internal status files."); - for (StatusUpdater su : sus) { - su.updateStatuses(); - Logger.printStatusTime(su.getClass().getSimpleName() - + " updated status files"); - } + sur.updateStatuses();
Logger.printStatus("Updating document files."); - for (DocumentWriter dw : dws) { - dw.writeDocuments(); - } + dwr.writeDocuments();
Logger.printStatus("Shutting down."); dso.writeHistoryFiles(); @@ -106,25 +57,10 @@ public class Main { Logger.printStatusTime("Flushed document cache");
Logger.printStatus("Gathering statistics."); - for (StatusUpdater su : sus) { - String statsString = su.getStatsString(); - if (statsString != null) { - Logger.printStatistics(su.getClass().getSimpleName(), - statsString); - } - } - for (DocumentWriter dw : dws) { - String statsString = dw.getStatsString(); - if (statsString != null) { - Logger.printStatistics(dw.getClass().getSimpleName(), - statsString); - } - } + sur.logStatistics(); + dwr.logStatistics(); Logger.printStatistics("Descriptor source", dso.getStatsString()); Logger.printStatistics("Document store", ds.getStatsString()); - Logger.printStatistics("GeoIP lookup service", ls.getStatsString()); - Logger.printStatistics("Reverse domain name resolver", - rdnr.getStatsString());
Logger.printStatus("Releasing lock."); if (lf.releaseLock()) { diff --git a/src/org/torproject/onionoo/updater/StatusUpdateRunner.java b/src/org/torproject/onionoo/updater/StatusUpdateRunner.java new file mode 100644 index 0000000..09dd952 --- /dev/null +++ b/src/org/torproject/onionoo/updater/StatusUpdateRunner.java @@ -0,0 +1,51 @@ +/* Copyright 2014 The Tor Project + * See LICENSE for licensing information */ +package org.torproject.onionoo.updater; + +import java.io.File; + +import org.torproject.onionoo.util.Logger; + +public class StatusUpdateRunner { + + private LookupService ls; + + private ReverseDomainNameResolver rdnr; + + private StatusUpdater[] statusUpdaters; + + public StatusUpdateRunner() { + this.ls = new LookupService(new File("geoip")); + this.rdnr = new ReverseDomainNameResolver(); + NodeDetailsStatusUpdater ndsu = new NodeDetailsStatusUpdater( + this.rdnr, this.ls); + BandwidthStatusUpdater bsu = new BandwidthStatusUpdater(); + WeightsStatusUpdater wsu = new WeightsStatusUpdater(); + ClientsStatusUpdater csu = new ClientsStatusUpdater(); + UptimeStatusUpdater usu = new UptimeStatusUpdater(); + this.statusUpdaters = new StatusUpdater[] { ndsu, bsu, wsu, csu, + usu }; + } + + public void updateStatuses() { + for (StatusUpdater su : this.statusUpdaters) { + su.updateStatuses(); + Logger.printStatusTime(su.getClass().getSimpleName() + + " updated status files"); + } + } + + public void logStatistics() { + for (StatusUpdater su : this.statusUpdaters) { + String statsString = su.getStatsString(); + if (statsString != null) { + Logger.printStatistics(su.getClass().getSimpleName(), + statsString); + } + } + Logger.printStatistics("GeoIP lookup service", + this.ls.getStatsString()); + Logger.printStatistics("Reverse domain name resolver", + this.rdnr.getStatsString()); + } +} diff --git a/src/org/torproject/onionoo/writer/DocumentWriterRunner.java b/src/org/torproject/onionoo/writer/DocumentWriterRunner.java new file mode 100644 index 0000000..559206f --- /dev/null +++ b/src/org/torproject/onionoo/writer/DocumentWriterRunner.java @@ -0,0 +1,37 @@ +/* Copyright 2014 The Tor Project + * See LICENSE for licensing information */ +package org.torproject.onionoo.writer; + +import org.torproject.onionoo.util.Logger; + +public class DocumentWriterRunner { + + private DocumentWriter[] documentWriters; + + public DocumentWriterRunner() { + SummaryDocumentWriter sdw = new SummaryDocumentWriter(); + DetailsDocumentWriter ddw = new DetailsDocumentWriter(); + BandwidthDocumentWriter bdw = new BandwidthDocumentWriter(); + WeightsDocumentWriter wdw = new WeightsDocumentWriter(); + ClientsDocumentWriter cdw = new ClientsDocumentWriter(); + UptimeDocumentWriter udw = new UptimeDocumentWriter(); + this.documentWriters = new DocumentWriter[] { sdw, ddw, bdw, wdw, cdw, + udw }; + } + + public void writeDocuments() { + for (DocumentWriter dw : this.documentWriters) { + dw.writeDocuments(); + } + } + + public void logStatistics() { + for (DocumentWriter dw : this.documentWriters) { + String statsString = dw.getStatsString(); + if (statsString != null) { + Logger.printStatistics(dw.getClass().getSimpleName(), + statsString); + } + } + } +}