review logging again; code that runs in client and/or server now logs in the right...
[sfa.git] / sfa / util / xmlrpcprotocol.py
1 # XMLRPC-specific code for SFA Client
2
3 import xmlrpclib
4
5 from sfa.util.sfalogging import sfa_logger
6
7 ##
8 # ServerException, ExceptionUnmarshaller
9 #
10 # Used to convert server exception strings back to an exception.
11 #    from usenet, Raghuram Devarakonda
12
13 class ServerException(Exception):
14     pass
15
16 class ExceptionUnmarshaller(xmlrpclib.Unmarshaller):
17     def close(self):
18         try:
19             return xmlrpclib.Unmarshaller.close(self)
20         except xmlrpclib.Fault, e:
21             raise ServerException(e.faultString)
22
23 ##
24 # XMLRPCTransport
25 #
26 # A transport for XMLRPC that works on top of HTTPS
27
28 class XMLRPCTransport(xmlrpclib.Transport):
29     key_file = None
30     cert_file = None
31     def make_connection(self, host):
32         # create a HTTPS connection object from a host descriptor
33         # host may be a string, or a (host, x509-dict) tuple
34         import httplib
35         host, extra_headers, x509 = self.get_host_info(host)
36         try:
37             HTTPS = httplib.HTTPS()
38         except AttributeError:
39             raise NotImplementedError(
40                 "your version of httplib doesn't support HTTPS"
41                 )
42         else:
43             return httplib.HTTPS(host, None, key_file=self.key_file, cert_file=self.cert_file) #**(x509 or {}))
44
45     def getparser(self):
46         unmarshaller = ExceptionUnmarshaller()
47         parser = xmlrpclib.ExpatParser(unmarshaller)
48         return parser, unmarshaller
49
50 class XMLRPCServerProxy(xmlrpclib.ServerProxy):
51     def __init__(self, url, transport, allow_none=True, options=None):
52         self.options = options
53         verbose = False
54         if self.options and self.options.debug:
55             verbose = True
56         xmlrpclib.ServerProxy.__init__(self, url, transport, allow_none=allow_none, verbose=verbose)
57
58     def __getattr__(self, attr):
59         sfa_logger().debug("Calling xml-rpc method:%s"%attr)
60         return xmlrpclib.ServerProxy.__getattr__(self, attr)
61
62
63 def get_server(url, key_file, cert_file, options=None):
64     transport = XMLRPCTransport()
65     transport.key_file = key_file
66     transport.cert_file = cert_file
67
68     return XMLRPCServerProxy(url, transport, allow_none=True, options=options)
69