commit ee1976649c74cb408dd7d6d334acc95e4e71c417 Author: Karsten Loesing karsten.loesing@gmx.net Date: Sun Jan 8 16:55:56 2012 +0100
Detect relay descriptor type from first lines. --- .../torproject/descriptor/impl/DescriptorImpl.java | 37 ++++++++++++++++++++ .../descriptor/impl/RelayDescriptorReaderImpl.java | 26 +------------- 2 files changed, 38 insertions(+), 25 deletions(-)
diff --git a/src/org/torproject/descriptor/impl/DescriptorImpl.java b/src/org/torproject/descriptor/impl/DescriptorImpl.java index 8165d69..cade4cf 100644 --- a/src/org/torproject/descriptor/impl/DescriptorImpl.java +++ b/src/org/torproject/descriptor/impl/DescriptorImpl.java @@ -14,6 +14,43 @@ import org.torproject.descriptor.Descriptor;
public abstract class DescriptorImpl implements Descriptor {
+ protected static List<Descriptor> parseRelayDescriptors( + byte[] rawDescriptorBytes) throws DescriptorParseException { + List<Descriptor> parsedDescriptors = new ArrayList<Descriptor>(); + byte[] first100Chars = new byte[Math.min(100, + rawDescriptorBytes.length)]; + System.arraycopy(rawDescriptorBytes, 0, first100Chars, 0, + first100Chars.length); + String firstLines = new String(first100Chars); + if (firstLines.startsWith("network-status-version 3\n") || + firstLines.contains("\nnetwork-status-version 3\n")) { + if (firstLines.contains("\nvote-status consensus\n")) { + parsedDescriptors.addAll(RelayNetworkStatusConsensusImpl. + parseConsensuses(rawDescriptorBytes)); + } else if (firstLines.contains("\nvote-status consensus\n")) { + parsedDescriptors.addAll(RelayNetworkStatusVoteImpl. + parseVotes(rawDescriptorBytes)); + } else { + throw new DescriptorParseException("Could not detect relay " + + "network status type in descriptor starting with '" + + firstLines + "'."); + } + } else if (firstLines.startsWith("router ") || + firstLines.contains("\nrouter ")) { + parsedDescriptors.addAll(RelayServerDescriptorImpl. + parseDescriptors(rawDescriptorBytes)); + } else if (firstLines.startsWith("extra-info ") || + firstLines.contains("\nextra-info ")) { + parsedDescriptors.addAll(RelayExtraInfoDescriptorImpl. + parseDescriptors(rawDescriptorBytes)); + } else { + throw new DescriptorParseException("Could not detect relay " + + "descriptor type in descriptor starting with '" + firstLines + + "'."); + } + return parsedDescriptors; + } + protected static List<byte[]> splitRawDescriptorBytes( byte[] rawDescriptorBytes, String startToken) { List<byte[]> rawDescriptors = new ArrayList<byte[]>(); diff --git a/src/org/torproject/descriptor/impl/RelayDescriptorReaderImpl.java b/src/org/torproject/descriptor/impl/RelayDescriptorReaderImpl.java index f0efbb3..b0e2b81 100644 --- a/src/org/torproject/descriptor/impl/RelayDescriptorReaderImpl.java +++ b/src/org/torproject/descriptor/impl/RelayDescriptorReaderImpl.java @@ -110,31 +110,7 @@ public class RelayDescriptorReaderImpl implements RelayDescriptorReader { } bis.close(); byte[] rawDescriptorBytes = baos.toByteArray(); - /* TODO This consensus/vote detection is a hack. */ - boolean isConsensus = false; - if (file.getName().contains("consensus")) { - isConsensus = true; - } else if (file.getName().contains("vote")) { - } else { - throw new RuntimeException("Unknown descriptor type in '" - + file.getAbsolutePath() + "."); - } - return this.parseDescriptors(rawDescriptorBytes, isConsensus); - } - private List<Descriptor> parseDescriptors(byte[] rawDescriptorBytes, - boolean isConsensus) throws DescriptorParseException { - List<Descriptor> parsedDescriptors = new ArrayList<Descriptor>(); - if (isConsensus) { - List<RelayNetworkStatusConsensus> parsedConsensuses = - RelayNetworkStatusConsensusImpl.parseConsensuses( - rawDescriptorBytes); - parsedDescriptors.addAll(parsedConsensuses); - } else { - List<RelayNetworkStatusVote> parsedVotes = - RelayNetworkStatusVoteImpl.parseVotes(rawDescriptorBytes); - parsedDescriptors.addAll(parsedVotes); - } - return parsedDescriptors; + return DescriptorImpl.parseRelayDescriptors(rawDescriptorBytes); } } }