Commit 976bc5a5 authored by Nick Mathewson's avatar Nick Mathewson
Browse files

r17266@catbus: nickm | 2007-12-19 23:49:57 -0500

 Implement support for networkstatus files.  Right now, we only use them if present, and only decline to hand outbridges that are listed and listed as non-running.  Later, we should decline to hand out unlisted bridges too.


svn:r12880
parent 7ed74c90
......@@ -106,6 +106,7 @@ class Bridge:
self.nickname = nickname
self.ip = ip
self.orport = orport
self.running = None
if id_digest is not None:
assert fingerprint is None
if len(id_digest) != DIGEST_LEN:
......@@ -137,6 +138,11 @@ class Bridge:
assert is_valid_fingerprint(self.fingerprint)
assert 1 <= self.orport <= 65535
def setStatus(self, running=None):
if running is not None:
self.running = running
def parseDescFile(f, bridge_purpose='bridge'):
"""Generator. Parses a cached-descriptors file 'f', and yields a Bridge
object for every entry whose purpose matches bridge_purpose.
......@@ -168,6 +174,28 @@ def parseDescFile(f, bridge_purpose='bridge'):
yield b
nickname = ip = orport = fingerprint = purpose = None
def parseStatusFile(f):
"""DOCDOC"""
result = None
ID = None
for line in f:
line = line.strip()
if line.startswith("opt "):
line = line[4:]
if line.startswith("r "):
try:
ID = binascii.a2b_base64(line.split()[2]+"=")
except binascii.Error:
logging.warn("Unparseable base64 ID %r", line.split()[2])
elif ID and line.startswith("s "):
flags = line.split()
if "Running" in flags:
yield ID, True
else:
yield ID, False
ID = None
class BridgeHolder:
"""Abstract base class for all classes that hold bridges."""
def insert(self, bridge):
......@@ -394,6 +422,10 @@ class BridgeSplitter(BridgeHolder):
assert self.rings
for s in self.statsHolders:
s.insert(bridge)
if bridge.running == False:
#XXXX Turn this to False or None.
return
bridgeID = bridge.getID()
ringname = self.store.get(bridgeID, "")
ring = self.ringsByName.get(ringname)
......
......@@ -35,6 +35,7 @@ CONFIG = Conf(
LOGLEVEL = "DEBUG",
BRIDGE_FILES = [ "./cached-descriptors", "./cached-descriptors.new" ],
STATUS_FILE = "networkstatus-bridges",
BRIDGE_PURPOSE = "bridge",
DB_FILE = "./bridgedist.db",
DB_LOG_FILE = "./bridgedist.log",
......@@ -113,9 +114,17 @@ def load(cfg, splitter):
"""Read all the bridge files from cfg, and pass them into a splitter
object.
"""
status = {}
if cfg.STATUS_FILE:
f = open(cfg.STATUS_FILE, 'r')
for ID, running in Bridges.parseStatusFile(f):
status[ID] = running
for fname in cfg.BRIDGE_FILES:
f = open(fname, 'r')
for bridge in Bridges.parseDescFile(f, cfg.BRIDGE_PURPOSE):
running = status.get(bridge.getID())
if running is not None:
bridge.setStatus(running=running)
splitter.insert(bridge)
f.close()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment