[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [metrics-lib/master] Add an in-memory descriptor parser as source.



commit 91aeedc3ab30a389b0652bcf3a810ea9e02f7237
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Wed May 9 12:15:09 2012 +0200

    Add an in-memory descriptor parser as source.
---
 .../torproject/descriptor/DescriptorParser.java    |   26 ++++++++++++++++++++
 .../descriptor/DescriptorSourceFactory.java        |    6 ++++
 .../descriptor/impl/DescriptorParserImpl.java      |   26 ++++++++++++++++++++
 .../descriptor/impl/DescriptorReaderImpl.java      |   18 +++++++-------
 4 files changed, 67 insertions(+), 9 deletions(-)

diff --git a/src/org/torproject/descriptor/DescriptorParser.java b/src/org/torproject/descriptor/DescriptorParser.java
new file mode 100644
index 0000000..09ae593
--- /dev/null
+++ b/src/org/torproject/descriptor/DescriptorParser.java
@@ -0,0 +1,26 @@
+/* Copyright 2012 The Tor Project
+ * See LICENSE for licensing information */
+package org.torproject.descriptor;
+
+import java.util.List;
+
+import org.torproject.descriptor.impl.DescriptorParseException;
+
+/* Parse descriptors that are already in memory instead of using the
+ * descriptor reader or downloader. */
+public interface DescriptorParser {
+
+  /* Fail descriptor parsing when encountering an unrecognized line.  This
+   * is not set by default, because the Tor specifications allow for new
+   * lines to be added that shall be ignored by older Tor versions.  But
+   * some applications may want to handle unrecognized descriptor lines
+   * explicitly. */
+  public void setFailUnrecognizedDescriptorLines(
+      boolean failUnrecognizedDescriptorLines);
+
+  /* Parse descriptors in the given byte array, possibly parsing the
+   * publication time from the file name (depending on the descriptor
+   * type). */
+  public List<Descriptor> parseDescriptors(byte[] rawDescriptorBytes,
+      String fileName) throws DescriptorParseException;
+}
diff --git a/src/org/torproject/descriptor/DescriptorSourceFactory.java b/src/org/torproject/descriptor/DescriptorSourceFactory.java
index 102bfce..bcd7d60 100644
--- a/src/org/torproject/descriptor/DescriptorSourceFactory.java
+++ b/src/org/torproject/descriptor/DescriptorSourceFactory.java
@@ -3,11 +3,17 @@
 package org.torproject.descriptor;
 
 import org.torproject.descriptor.impl.DescriptorDownloaderImpl;
+import org.torproject.descriptor.impl.DescriptorParserImpl;
 import org.torproject.descriptor.impl.DescriptorReaderImpl;
 
 /* Create descriptor source instances. */
 public class DescriptorSourceFactory {
 
+  /* Create a descriptor parser. */
+  public static DescriptorParser createDescriptorParser() {
+    return new DescriptorParserImpl();
+  }
+
   /* Create a descriptor reader. */
   public static DescriptorReader createDescriptorReader() {
     return new DescriptorReaderImpl();
diff --git a/src/org/torproject/descriptor/impl/DescriptorParserImpl.java b/src/org/torproject/descriptor/impl/DescriptorParserImpl.java
new file mode 100644
index 0000000..2ee6f40
--- /dev/null
+++ b/src/org/torproject/descriptor/impl/DescriptorParserImpl.java
@@ -0,0 +1,26 @@
+/* Copyright 2012 The Tor Project
+ * See LICENSE for licensing information */
+package org.torproject.descriptor.impl;
+
+import java.util.List;
+
+import org.torproject.descriptor.Descriptor;
+import org.torproject.descriptor.DescriptorParser;
+
+public class DescriptorParserImpl implements DescriptorParser {
+
+  private boolean failUnrecognizedDescriptorLines;
+
+  public void setFailUnrecognizedDescriptorLines(
+      boolean failUnrecognizedDescriptorLines) {
+    this.failUnrecognizedDescriptorLines =
+        failUnrecognizedDescriptorLines;
+  }
+
+  public List<Descriptor> parseDescriptors(byte[] rawDescriptorBytes,
+      String fileName) throws DescriptorParseException {
+    return DescriptorImpl.parseRelayOrBridgeDescriptors(
+        rawDescriptorBytes, fileName,
+        this.failUnrecognizedDescriptorLines);
+  }
+}
diff --git a/src/org/torproject/descriptor/impl/DescriptorReaderImpl.java b/src/org/torproject/descriptor/impl/DescriptorReaderImpl.java
index e82d039..ddb6ea6 100644
--- a/src/org/torproject/descriptor/impl/DescriptorReaderImpl.java
+++ b/src/org/torproject/descriptor/impl/DescriptorReaderImpl.java
@@ -25,6 +25,7 @@ import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
 import org.torproject.descriptor.Descriptor;
 import org.torproject.descriptor.DescriptorFile;
+import org.torproject.descriptor.DescriptorParser;
 import org.torproject.descriptor.DescriptorReader;
 
 public class DescriptorReaderImpl implements DescriptorReader {
@@ -87,7 +88,7 @@ public class DescriptorReaderImpl implements DescriptorReader {
     private List<File> tarballs;
     private BlockingIteratorImpl<DescriptorFile> descriptorQueue;
     private File historyFile;
-    private boolean failUnrecognizedDescriptorLines;
+    private DescriptorParser descriptorParser;
     private DescriptorReaderRunnable(List<File> directories,
         List<File> tarballs,
         BlockingIteratorImpl<DescriptorFile> descriptorQueue,
@@ -96,8 +97,9 @@ public class DescriptorReaderImpl implements DescriptorReader {
       this.tarballs = tarballs;
       this.descriptorQueue = descriptorQueue;
       this.historyFile = historyFile;
-      this.failUnrecognizedDescriptorLines =
-          failUnrecognizedDescriptorLines;
+      this.descriptorParser = new DescriptorParserImpl();
+      this.descriptorParser.setFailUnrecognizedDescriptorLines(
+          failUnrecognizedDescriptorLines);
     }
     public void run() {
       this.readOldHistory();
@@ -245,9 +247,8 @@ public class DescriptorReaderImpl implements DescriptorReader {
                 String fileName = tae.getName().substring(
                     tae.getName().lastIndexOf("/") + 1);
                 List<Descriptor> parsedDescriptors =
-                    DescriptorImpl.parseRelayOrBridgeDescriptors(
-                    rawDescriptorBytes, fileName,
-                    this.failUnrecognizedDescriptorLines);
+                    this.descriptorParser.parseDescriptors(
+                    rawDescriptorBytes, fileName);
                 descriptorFile.setDescriptors(parsedDescriptors);
               } catch (DescriptorParseException e) {
                 descriptorFile.setException(e);
@@ -272,9 +273,8 @@ public class DescriptorReaderImpl implements DescriptorReader {
       }
       bis.close();
       byte[] rawDescriptorBytes = baos.toByteArray();
-      return DescriptorImpl.parseRelayOrBridgeDescriptors(
-          rawDescriptorBytes, file.getName(),
-          this.failUnrecognizedDescriptorLines);
+      return this.descriptorParser.parseDescriptors(rawDescriptorBytes,
+          file.getName());
     }
   }
 }

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits