]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/util/rrefutil.py
introducer: stop tracking hints for subscribed clients
[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 hosts_for_furl(furl, ignore_localhost=True):
31     advertised = []
32     for hint in SturdyRef(furl).locationHints:
33         assert not isinstance(hint, str), hint
34         if hint[0] == "ipv4":
35             host = hint[1]
36             if ignore_localhost and host == "127.0.0.1":
37                 continue
38             advertised.append(host)
39     return advertised
40
41 def stringify_remote_address(rref):
42     remote = rref.getPeer()
43     if isinstance(remote, address.IPv4Address):
44         return "%s:%d" % (remote.host, remote.port)
45     # loopback is a non-IPv4Address
46     return str(remote)