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 connection_hints_for_furl(furl):
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]))
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