[tor-commits] [metrics-lib/master] Improve downloader a bit.
karsten at torproject.org
karsten at torproject.org
Thu Jan 19 16:49:09 UTC 2012
commit 1bf8a7f914458f5b3968c453abc91dd24808aef6
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date: Thu Jan 19 17:36:13 2012 +0100
Improve downloader a bit.
---
.../torproject/descriptor/DescriptorRequest.java | 4 +++
.../torproject/descriptor/impl/DescriptorImpl.java | 3 ++
.../descriptor/impl/DescriptorRequestImpl.java | 8 ++++++
.../descriptor/impl/DirectoryDownloader.java | 8 +++++-
.../descriptor/impl/DownloadCoordinatorImpl.java | 24 ++++++-------------
5 files changed, 30 insertions(+), 17 deletions(-)
diff --git a/src/org/torproject/descriptor/DescriptorRequest.java b/src/org/torproject/descriptor/DescriptorRequest.java
index b0c2c02..f68332f 100644
--- a/src/org/torproject/descriptor/DescriptorRequest.java
+++ b/src/org/torproject/descriptor/DescriptorRequest.java
@@ -15,6 +15,10 @@ public interface DescriptorRequest {
* previously configured. */
public String getDirectoryNickname();
+ /* Return the first exception that was thrown when making this request
+ * or parsing the response, or null if no exception was thrown. */
+ public Exception getException();
+
/* Return the response code that the directory mirror or authority
* returned. */
public int getResponseCode();
diff --git a/src/org/torproject/descriptor/impl/DescriptorImpl.java b/src/org/torproject/descriptor/impl/DescriptorImpl.java
index 7e7abee..2d20fa7 100644
--- a/src/org/torproject/descriptor/impl/DescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/DescriptorImpl.java
@@ -19,6 +19,9 @@ public abstract class DescriptorImpl implements Descriptor {
byte[] rawDescriptorBytes, String fileName)
throws DescriptorParseException {
List<Descriptor> parsedDescriptors = new ArrayList<Descriptor>();
+ if (rawDescriptorBytes == null) {
+ return parsedDescriptors;
+ }
byte[] first100Chars = new byte[Math.min(100,
rawDescriptorBytes.length)];
System.arraycopy(rawDescriptorBytes, 0, first100Chars, 0,
diff --git a/src/org/torproject/descriptor/impl/DescriptorRequestImpl.java b/src/org/torproject/descriptor/impl/DescriptorRequestImpl.java
index 6926e59..6c44a79 100644
--- a/src/org/torproject/descriptor/impl/DescriptorRequestImpl.java
+++ b/src/org/torproject/descriptor/impl/DescriptorRequestImpl.java
@@ -87,5 +87,13 @@ public class DescriptorRequestImpl implements DescriptorRequest {
public List<Descriptor> getDescriptors() {
return this.descriptors;
}
+
+ private Exception exception;
+ protected void setException(Exception exception) {
+ this.exception = exception;
+ }
+ public Exception getException() {
+ return this.exception;
+ }
}
diff --git a/src/org/torproject/descriptor/impl/DirectoryDownloader.java b/src/org/torproject/descriptor/impl/DirectoryDownloader.java
index d07c5c9..b6d9b05 100644
--- a/src/org/torproject/descriptor/impl/DirectoryDownloader.java
+++ b/src/org/torproject/descriptor/impl/DirectoryDownloader.java
@@ -66,8 +66,11 @@ public class DirectoryDownloader implements Runnable {
byte[] responseBytes = baos.toByteArray();
request.setResponseBytes(responseBytes);
request.setRequestEnd(System.currentTimeMillis());
+ request.setDescriptors(DescriptorImpl.
+ parseRelayOrBridgeDescriptors(responseBytes, null));
}
- } catch (IOException e) {
+ } catch (Exception e) {
+ request.setException(e);
/* Stop downloading from this directory if there are any
* problems, e.g., refused connections. */
keepRunning = false;
@@ -83,6 +86,9 @@ public class DirectoryDownloader implements Runnable {
}
/* Interrupt a download request if it takes longer than a given time. */
+ /* TODO Also look at URLConnection.setConnectTimeout() and
+ * URLConnection.setReadTimeout() instead of implementing this
+ * ourselves. */
private static class RequestTimeout implements Runnable {
private long timeoutMillis;
private Thread downloaderThread;
diff --git a/src/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java b/src/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java
index 5d4ca21..0c0ebc3 100644
--- a/src/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java
+++ b/src/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java
@@ -190,17 +190,16 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator {
if (response.getDescriptorType().equals("consensus")) {
this.requestingConsensuses.remove(nickname);
if (response.getResponseCode() == 200) {
- List<RelayNetworkStatusConsensus> parsedConsensuses =
- RelayNetworkStatusConsensusImpl.parseConsensuses(
- response.getResponseBytes());
- List<Descriptor> parsedDescriptors =
- new ArrayList<Descriptor>(parsedConsensuses);
- response.setDescriptors(parsedDescriptors);
if (this.includeCurrentReferencedVotes) {
/* TODO Only add votes if the consensus is not older than one
* hour. Or does that make no sense? */
- for (RelayNetworkStatusConsensus parsedConsensus :
- parsedConsensuses) {
+ for (Descriptor parsedDescriptor : response.getDescriptors()) {
+ if (!(parsedDescriptor instanceof
+ RelayNetworkStatusConsensus)) {
+ continue;
+ }
+ RelayNetworkStatusConsensus parsedConsensus =
+ (RelayNetworkStatusConsensus) parsedDescriptor;
for (DirSourceEntry dirSource :
parsedConsensus.getDirSourceEntries().values()) {
String identity = dirSource.getIdentity();
@@ -226,14 +225,7 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator {
}
} else if (response.getDescriptorType().equals("vote")) {
String requestedVote = requestingVotes.remove(nickname);
- if (response.getResponseCode() == 200) {
- List<RelayNetworkStatusVote> parsedVotes =
- RelayNetworkStatusVoteImpl.parseVotes(
- response.getResponseBytes());
- List<Descriptor> parsedDescriptors =
- new ArrayList<Descriptor>(parsedVotes);
- response.setDescriptors(parsedDescriptors);
- } else {
+ if (response.getResponseCode() != 200) {
this.missingVotes.add(requestedVote);
}
}
More information about the tor-commits
mailing list