[tor-commits] [metrics-lib/master] Add basic support for or-address lines.

karsten at torproject.org karsten at torproject.org
Mon Jan 23 09:42:30 UTC 2012


commit 1679980f9f7f9b2ed4e75a7f6feb267d9c4190c7
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Mon Jan 23 10:11:27 2012 +0100

    Add basic support for or-address lines.
---
 .../torproject/descriptor/NetworkStatusEntry.java  |    6 +++++
 .../torproject/descriptor/ServerDescriptor.java    |    5 ++++
 .../descriptor/impl/NetworkStatusEntryImpl.java    |   22 +++++++++++++++++++-
 .../descriptor/impl/ServerDescriptorImpl.java      |   18 ++++++++++++++++
 4 files changed, 50 insertions(+), 1 deletions(-)

diff --git a/src/org/torproject/descriptor/NetworkStatusEntry.java b/src/org/torproject/descriptor/NetworkStatusEntry.java
index 38b7862..ba5939c 100644
--- a/src/org/torproject/descriptor/NetworkStatusEntry.java
+++ b/src/org/torproject/descriptor/NetworkStatusEntry.java
@@ -2,6 +2,7 @@
  * See LICENSE for licensing information */
 package org.torproject.descriptor;
 
+import java.util.List;
 import java.util.SortedSet;
 
 public interface NetworkStatusEntry {
@@ -30,6 +31,11 @@ public interface NetworkStatusEntry {
   /* Return the DirPort. */
   public int getDirPort();
 
+  /* Return the relay's additional OR addresses and ports contained in
+   * or-address lines, or an empty list if the network status doesn't
+   * contain such lines. */
+  public List<String> getOrAddresses();
+
   /* Return the relay flags or null if the status entry didn't contain any
    * relay flags. */
   public SortedSet<String> getFlags();
diff --git a/src/org/torproject/descriptor/ServerDescriptor.java b/src/org/torproject/descriptor/ServerDescriptor.java
index 631779e..2aa9364 100644
--- a/src/org/torproject/descriptor/ServerDescriptor.java
+++ b/src/org/torproject/descriptor/ServerDescriptor.java
@@ -22,6 +22,11 @@ public interface ServerDescriptor extends Descriptor {
   /* Return the relay's directory port. */
   public int getDirPort();
 
+  /* Return the relay's additional OR addresses and ports contained in
+   * or-address lines, or an empty list if the descriptor doesn't contain
+   * such lines. */
+  public List<String> getOrAddresses();
+
   /* Return the average bandwidth in bytes per second that the relay is
    * willing to sustain over long periods. */
   public int getBandwidthRate();
diff --git a/src/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java b/src/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java
index ef122fe..ce235ab 100644
--- a/src/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java
+++ b/src/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java
@@ -5,6 +5,8 @@ package org.torproject.descriptor.impl;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.SortedMap;
 import java.util.SortedSet;
 import java.util.TreeSet;
@@ -59,7 +61,9 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry {
         String[] parts = !line.startsWith("opt ") ? line.split(" ") :
             line.substring("opt ".length()).split(" ");
         String keyword = parts[0];
-        if (keyword.equals("s")) {
+        if (keyword.equals("a")) {
+          this.parseALine(line, parts);
+        } else if (keyword.equals("s")) {
           this.parseSLine(line, parts);
         } else if (keyword.equals("v")) {
           this.parseVLine(line, parts);
@@ -102,6 +106,17 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry {
     this.dirPort = ParseHelper.parsePort(line, parts[8]);
   }
 
+  private void parseALine(String line, String[] parts)
+      throws DescriptorParseException {
+    if (parts.length != 2) {
+      throw new DescriptorParseException("Invalid line '" + line + "' in "
+          + "status entry.");
+    }
+    /* TODO Add more checks. */
+    /* TODO Add tests. */
+    this.orAddresses.add(parts[1]);
+  }
+
   private void parseSLine(String line, String[] parts)
       throws DescriptorParseException {
     this.parsedAtMostOnceKeyword("s");
@@ -212,6 +227,11 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry {
     return this.dirPort;
   }
 
+  private List<String> orAddresses = new ArrayList<String>();
+  public List<String> getOrAddresses() {
+    return new ArrayList<String>(this.orAddresses);
+  }
+
   private SortedSet<String> flags;
   public SortedSet<String> getFlags() {
     return new TreeSet<String>(this.flags);
diff --git a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
index 47e6166..09e9123 100644
--- a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
@@ -78,6 +78,8 @@ public class ServerDescriptorImpl extends DescriptorImpl
         String keyword = partsNoOpt[0];
         if (keyword.equals("router")) {
           this.parseRouterLine(line, lineNoOpt, partsNoOpt);
+        } else if (keyword.equals("or-address")) {
+          this.parseOrAddressLine(line, lineNoOpt, partsNoOpt);
         } else if (keyword.equals("bandwidth")) {
           this.parseBandwidthLine(line, lineNoOpt, partsNoOpt);
         } else if (keyword.equals("platform")) {
@@ -154,6 +156,17 @@ public class ServerDescriptorImpl extends DescriptorImpl
     this.dirPort = ParseHelper.parsePort(line, partsNoOpt[5]);
   }
 
+  private void parseOrAddressLine(String line, String lineNoOpt,
+      String[] partsNoOpt) throws DescriptorParseException {
+    if (partsNoOpt.length != 2) {
+      throw new DescriptorParseException("Wrong number of values in line "
+          + "'" + line + "'.");
+    }
+    /* TODO Add more checks. */
+    /* TODO Add tests. */
+    this.orAddresses.add(partsNoOpt[1]);
+  }
+
   private void parseBandwidthLine(String line, String lineNoOpt,
       String[] partsNoOpt) throws DescriptorParseException {
     if (partsNoOpt.length != 4) {
@@ -432,6 +445,11 @@ public class ServerDescriptorImpl extends DescriptorImpl
     return this.dirPort;
   }
 
+  private List<String> orAddresses = new ArrayList<String>();
+  public List<String> getOrAddresses() {
+    return new ArrayList<String>(this.orAddresses);
+  }
+
   private int bandwidthRate;
   public int getBandwidthRate() {
     return this.bandwidthRate;





More information about the tor-commits mailing list