]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/util/rrefutil.py
introweb: fix connection hints for server announcements
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / util / rrefutil.py
1
2 from twisted.internet import address
3 from foolscap.api import Violation, RemoteException, DeadReferenceError, \
4      SturdyRef
5
6 def add_version_to_remote_reference(rref, default):
7     """I try to add a .version attribute to the given RemoteReference. I call
8     the remote get_version() method to learn its version. I'll add the
9     default value if the remote side doesn't appear to have a get_version()
10     method."""
11     d = rref.callRemote("get_version")
12     def _got_version(version):
13         rref.version = version
14         return rref
15     def _no_get_version(f):
16         f.trap(Violation, RemoteException)
17         rref.version = default
18         return rref
19     d.addCallbacks(_got_version, _no_get_version)
20     return d
21
22 def trap_and_discard(f, *errorTypes):
23     f.trap(*errorTypes)
24     pass
25
26 def trap_deadref(f):
27     return trap_and_discard(f, DeadReferenceError)
28
29
30 def connection_hints_for_furl(furl):
31     hints = []
32     for h in SturdyRef(furl).locationHints:
33         # Foolscap-0.2.5 and earlier used strings in .locationHints, 0.2.6
34         # through 0.6.4 used tuples of ("ipv4",host,port), 0.6.5 through
35         # 0.8.0 used tuples of ("tcp",host,port), and >=0.9.0 uses strings
36         # again. Tolerate them all.
37         if isinstance(h, tuple):
38             hints.append(":".join([str(s) for s in h]))
39         else:
40             hints.append(h)
41     return hints
42
43 def stringify_remote_address(rref):
44     remote = rref.getPeer()
45     if isinstance(remote, address.IPv4Address):
46         return "%s:%d" % (remote.host, remote.port)
47     # loopback is a non-IPv4Address
48     return str(remote)