commit 0423ede0cfa8efa3224cc371581c5f0ca5d3bfff Author: David Fifield david@bamsoftware.com Date: Wed May 25 18:04:44 2011 -0700
Allow the facilitator to handle both TCP and RTMFP regs. --- facilitator.py | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 42 insertions(+), 12 deletions(-)
diff --git a/facilitator.py b/facilitator.py index 0333aee..c9859db 100755 --- a/facilitator.py +++ b/facilitator.py @@ -97,31 +97,61 @@ def format_addr(addr): else: return u"%s:%d" % (host, port)
-class Reg(object): +class TCPReg(object): + def __init__(self, host, port): + self.host = host + self.port = port + + def __unicode__(self): + return format_addr((self.host, self.port)) + + def __str__(self): + return unicode(self).encode("UTF-8") + + def __cmp__(self, other): + if isinstance(other, TCPReg): + return cmp((self.host, self.port), (other.host, other.port)) + else: + return False + +class RTMFPReg(object): def __init__(self, id): self.id = id
def __unicode__(self): - return u"%s" % (self.id) + return u"%s" % self.id
def __str__(self): return unicode(self).encode("UTF-8")
def __cmp__(self, other): - return cmp((self.id), (other.id)) + if isinstance(other, RTMFPReg): + return cmp(self.id, other.id) + else: + return False
+class Reg(object): @staticmethod def parse(spec, defhost = None, defport = None): - af, host, port = parse_addr_spec(spec, defhost, defport) try: - addrs = socket.getaddrinfo(host, port, af, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST) - except socket.gaierror, e: - raise ValueError("Bad host or port: "%s" "%s": %s" % (host, port, str(e))) - if not addrs: - raise ValueError("Bad host or port: "%s" "%s"" % (host, port)) + af, host, port = parse_addr_spec(spec, defhost, defport) + except ValueError: + pass + else: + try: + addrs = socket.getaddrinfo(host, port, af, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST) + except socket.gaierror, e: + raise ValueError("Bad host or port: "%s" "%s": %s" % (host, port, str(e))) + if not addrs: + raise ValueError("Bad host or port: "%s" "%s"" % (host, port)) + + host, port = socket.getnameinfo(addrs[0][4], socket.NI_NUMERICHOST | socket.NI_NUMERICSERV) + return TCPReg(host, int(port)) + + if re.match(ur'^[0-9A-Fa-f]{64}$', spec): + return RTMFPReg(spec)
- host, port = socket.getnameinfo(addrs[0][4], socket.NI_NUMERICHOST | socket.NI_NUMERICSERV) - return Reg(host, int(port)) + raise ValueError("Bad spec format: %s" % repr(spec))
class RegSet(object): def __init__(self): @@ -182,7 +212,7 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler): val = client_specs[0]
try: - reg = Reg(val) + reg = Reg.parse(val, self.client_address[0]) except ValueError, e: log(u"client %s syntax error in %s: %s" % (format_addr(self.client_address), repr(val), repr(str(e)))) return