2 from twisted.internet import address
3 from foolscap.api import Violation, RemoteException, DeadReferenceError, \
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()
11 d = rref.callRemote("get_version")
12 def _got_version(version):
13 rref.version = version
15 def _no_get_version(f):
16 f.trap(Violation, RemoteException)
17 rref.version = default
19 d.addCallbacks(_got_version, _no_get_version)
22 def trap_and_discard(f, *errorTypes):
27 return trap_and_discard(f, DeadReferenceError)
30 def hosts_for_furl(furl, ignore_localhost=True):
32 for hint in SturdyRef(furl).locationHints:
33 assert not isinstance(hint, str), hint
36 if ignore_localhost and host == "127.0.0.1":
38 advertised.append(host)
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