[tor-commits] [flashproxy/master] Move util classes to fac.py since they'll be needed later, and rename "Reg" to more general "Endpoint"
infinity0 at torproject.org
infinity0 at torproject.org
Mon Oct 28 14:47:41 UTC 2013
commit e9bdc091ca2c35f013f338552ea01cf966806eb9
Author: Ximin Luo <infinity0 at gmx.com>
Date: Tue Oct 8 16:50:46 2013 +0100
Move util classes to fac.py since they'll be needed later, and rename "Reg" to more general "Endpoint"
---
facilitator/fac.py | 31 +++++++++++++++++++++++++++++++
facilitator/facilitator | 35 +++--------------------------------
facilitator/facilitator-test | 15 ++++++++-------
3 files changed, 42 insertions(+), 39 deletions(-)
diff --git a/facilitator/fac.py b/facilitator/fac.py
index 9cbfa79..cedadd8 100644
--- a/facilitator/fac.py
+++ b/facilitator/fac.py
@@ -5,6 +5,7 @@ import socket
import stat
import subprocess
import pwd
+from collections import namedtuple
# Return true iff the given fd is readable, writable, and executable only by its
# owner.
@@ -146,6 +147,36 @@ def format_addr(addr):
raise ValueError("host and port may not both be None")
return u"%s%s" % (host_str, port_str)
+
+class Transport(namedtuple("Transport", "prefix suffix")):
+ @classmethod
+ def parse(cls, transport):
+ if isinstance(transport, cls):
+ return transport
+ elif type(transport) == str:
+ if "|" in transport:
+ prefix, suffix = transport.rsplit("|", 1)
+ else:
+ prefix, suffix = "", transport
+ return cls(prefix, suffix)
+ else:
+ raise ValueError("could not parse transport: %s" % transport)
+
+ def __init__(self, prefix, suffix):
+ if not suffix:
+ raise ValueError("suffix (proxy) part of transport must be non-empty: %s" % str(self))
+
+ def __str__(self):
+ return "%s|%s" % (self.prefix, self.suffix) if self.prefix else self.suffix
+
+
+class Endpoint(namedtuple("Endpoint", "addr transport")):
+ @classmethod
+ def parse(cls, spec, transport, defhost = None, defport = None):
+ host, port = parse_addr_spec(spec, defhost, defport)
+ return cls((host, port), Transport.parse(transport))
+
+
def skip_space(pos, line):
"""Skip a (possibly empty) sequence of space characters (the ASCII character
'\x20' exactly). Returns a pair (pos, num_skipped)."""
diff --git a/facilitator/facilitator b/facilitator/facilitator
index 9c8de92..f3ae79b 100755
--- a/facilitator/facilitator
+++ b/facilitator/facilitator
@@ -8,9 +8,9 @@ import sys
import threading
import time
import traceback
-from collections import namedtuple
import fac
+from fac import Transport, Endpoint
LISTEN_ADDRESS = "127.0.0.1"
DEFAULT_LISTEN_PORT = 9002
@@ -80,35 +80,6 @@ def log(msg):
options.log_file.flush()
-class Transport(namedtuple("Transport", "prefix suffix")):
- @classmethod
- def parse(cls, transport):
- if isinstance(transport, cls):
- return transport
- elif type(transport) == str:
- if "|" in transport:
- prefix, suffix = transport.rsplit("|", 1)
- else:
- prefix, suffix = "", transport
- return cls(prefix, suffix)
- else:
- raise ValueError("could not parse transport: %s" % transport)
-
- def __init__(self, prefix, suffix):
- if not suffix:
- raise ValueError("suffix (proxy) part of transport must be non-empty: %s" % str(self))
-
- def __str__(self):
- return "%s|%s" % (self.prefix, self.suffix) if self.prefix else self.suffix
-
-
-class Reg(namedtuple("Reg", "addr transport")):
- @classmethod
- def parse(cls, spec, transport, defhost = None, defport = None):
- host, port = fac.parse_addr_spec(spec, defhost, defport)
- return cls((host, port), Transport.parse(transport))
-
-
class Endpoints(object):
"""
Tracks endpoints (either client/server) and the transports they support.
@@ -261,7 +232,7 @@ class Endpoints(object):
# assume servers never run out
client_transport = ptsClient._endpoints[client_addr]
server_transport = ptsServer._endpoints[server_addr]
- return Reg(client_addr, client_transport), Reg(server_addr, server_transport)
+ return Endpoint(client_addr, client_transport), Endpoint(server_addr, server_transport)
class Handler(SocketServer.StreamRequestHandler):
@@ -398,7 +369,7 @@ class Handler(SocketServer.StreamRequestHandler):
return self.error(u"PUT missing CLIENT param")
try:
- reg = Reg.parse(client_spec, transport)
+ reg = Endpoint.parse(client_spec, transport)
except (UnknownTransport, ValueError) as e:
# XXX should we throw a better error message to the client? Is it possible?
return self.error(u"syntax error in %s: %%(cause)s" % safe_str(repr(client_spec)), e)
diff --git a/facilitator/facilitator-test b/facilitator/facilitator-test
index b81b84e..6709221 100755
--- a/facilitator/facilitator-test
+++ b/facilitator/facilitator-test
@@ -7,8 +7,9 @@ import tempfile
import time
import unittest
-from facilitator import Transport, Reg, Endpoints, parse_relay_file
+from facilitator import Endpoints, parse_relay_file
import fac
+from fac import Transport, Endpoint
FACILITATOR_HOST = "127.0.0.1"
FACILITATOR_PORT = 39002 # diff port to not conflict with production service
@@ -114,7 +115,7 @@ class EndpointsTest(unittest.TestCase):
self.pts2.addEndpoint("B", "a|p")
self.pts2.addEndpoint("C", "b|p")
self.pts2.addEndpoint("D", "a|q")
- expected = (Reg("A", Transport("a","p")), Reg("B", Transport("a","p")))
+ expected = (Endpoint("A", Transport("a","p")), Endpoint("B", Transport("a","p")))
empty = Endpoints.EMPTY_MATCH
self.assertEquals(expected, Endpoints.match(self.pts, self.pts2, ["p"]))
self.assertEquals(empty, Endpoints.match(self.pts, self.pts2, ["x"]))
@@ -123,7 +124,7 @@ class EndpointsTest(unittest.TestCase):
self.pts.addEndpoint("A", "a|p")
self.pts2 = Endpoints(af=socket.AF_INET)
self.pts2.addEndpoint("B", "a|q")
- expected = (Reg("A", Transport("a","p")), Reg("B", Transport("a","q")))
+ expected = (Endpoint("A", Transport("a","p")), Endpoint("B", Transport("a","q")))
empty = Endpoints.EMPTY_MATCH
self.assertEquals(expected, Endpoints.match(self.pts, self.pts2, ["p", "q"]))
self.assertEquals(empty, Endpoints.match(self.pts, self.pts2, ["p"]))
@@ -134,7 +135,7 @@ class EndpointsTest(unittest.TestCase):
self.pts.addEndpoint("A", "p")
self.pts2 = Endpoints(af=socket.AF_INET)
self.pts2.addEndpoint("B", "p")
- expected = (Reg("A", Transport("","p")), Reg("B", Transport("","p")))
+ expected = (Endpoint("A", Transport("","p")), Endpoint("B", Transport("","p")))
empty = Endpoints.EMPTY_MATCH
self.assertEquals(expected, Endpoints.match(self.pts, self.pts2, ["p"]))
self.assertEquals(empty, Endpoints.match(self.pts, self.pts2, ["x"]))
@@ -149,9 +150,9 @@ class EndpointsTest(unittest.TestCase):
self.pts2.addEndpoint("F", "p")
# this test ensures we have a sane policy for selecting between prefix pools
expected = set()
- expected.add((Reg("A", Transport("a","p")), Reg("D", Transport("a","p"))))
- expected.add((Reg("B", Transport("b","p")), Reg("E", Transport("b","p"))))
- expected.add((Reg("C", Transport("","p")), Reg("F", Transport("","p"))))
+ expected.add((Endpoint("A", Transport("a","p")), Endpoint("D", Transport("a","p"))))
+ expected.add((Endpoint("B", Transport("b","p")), Endpoint("E", Transport("b","p"))))
+ expected.add((Endpoint("C", Transport("","p")), Endpoint("F", Transport("","p"))))
result = set()
result.add(Endpoints.match(self.pts, self.pts2, ["p"]))
result.add(Endpoints.match(self.pts, self.pts2, ["p"]))
More information about the tor-commits
mailing list