cleanup old code from aggregate_manager but preserve it in the max
[sfa.git] / sfa / client / sfaprotocol.py
1 # XMLRPC-specific code for SFA Client
2
3 import xmlrpclib
4 from httplib import HTTPS, HTTPSConnection
5
6 from sfa.util.sfalogging import logger
7
8 ##
9 # ServerException, ExceptionUnmarshaller
10 #
11 # Used to convert server exception strings back to an exception.
12 #    from usenet, Raghuram Devarakonda
13
14 class ServerException(Exception):
15     pass
16
17 class ExceptionUnmarshaller(xmlrpclib.Unmarshaller):
18     def close(self):
19         try:
20             return xmlrpclib.Unmarshaller.close(self)
21         except xmlrpclib.Fault, e:
22             raise ServerException(e.faultString)
23
24 ##
25 # XMLRPCTransport
26 #
27 # A transport for XMLRPC that works on top of HTTPS
28
29 # python 2.7 xmlrpclib has changed its internal code
30 # it now calls 'getresponse' on the obj returned by make_connection
31 # while it used to call 'getreply'
32 # regardless of the version, httplib.HTTPS does implement getreply, 
33 # while httplib.HTTPSConnection has getresponse
34 # so we create a dummy instance to check what's expected
35 need_HTTPSConnection=hasattr(xmlrpclib.Transport().make_connection('localhost'),'getresponse')
36
37 class XMLRPCTransport(xmlrpclib.Transport):
38     
39     def __init__(self, key_file=None, cert_file=None, timeout=None):
40         xmlrpclib.Transport.__init__(self)
41         self.timeout=timeout
42         self.key_file = key_file
43         self.cert_file = cert_file
44         
45     def make_connection(self, host):
46         # create a HTTPS connection object from a host descriptor
47         # host may be a string, or a (host, x509-dict) tuple
48         host, extra_headers, x509 = self.get_host_info(host)
49         if need_HTTPSConnection:
50             #conn = HTTPSConnection(host, None, key_file=self.key_file, cert_file=self.cert_file, timeout=self.timeout) #**(x509 or {}))
51             conn = HTTPSConnection(host, None, key_file=self.key_file, cert_file=self.cert_file) #**(x509 or {}))
52         else:
53             #conn = HTTPS(host, None, key_file=self.key_file, cert_file=self.cert_file, timeout=self.timeout) #**(x509 or {}))
54             conn = HTTPS(host, None, key_file=self.key_file, cert_file=self.cert_file) #**(x509 or {}))
55
56         if hasattr(conn, 'set_timeout'):
57             conn.set_timeout(self.timeout)
58
59         # Some logic to deal with timeouts. It appears that some (or all) versions
60         # of python don't set the timeout after the socket is created. We'll do it
61         # ourselves by forcing the connection to connect, finding the socket, and
62         # calling settimeout() on it. (tested with python 2.6)
63         if self.timeout:
64             if hasattr(conn, "_conn"):
65                 # HTTPS is a wrapper around HTTPSConnection
66                 real_conn = conn._conn
67             else:
68                 real_conn = conn
69             conn.connect()
70             if hasattr(real_conn, "sock") and hasattr(real_conn.sock, "settimeout"):
71                 real_conn.sock.settimeout(float(self.timeout))
72
73         return conn
74
75     def getparser(self):
76         unmarshaller = ExceptionUnmarshaller()
77         parser = xmlrpclib.ExpatParser(unmarshaller)
78         return parser, unmarshaller
79
80 class XMLRPCServerProxy(xmlrpclib.ServerProxy):
81     def __init__(self, url, transport, allow_none=True, verbose=False):
82         # remember url for GetVersion
83         self.url=url
84         xmlrpclib.ServerProxy.__init__(self, url, transport, allow_none=allow_none, verbose=verbose)
85
86     def __getattr__(self, attr):
87         logger.debug ("xml-rpc %s method:%s"%(self.url,attr))
88         return xmlrpclib.ServerProxy.__getattr__(self, attr)
89
90 def server_proxy(url, key_file, cert_file, timeout=None, verbose=False):
91     transport = XMLRPCTransport(key_file, cert_file, timeout)
92     return XMLRPCServerProxy(url, transport, allow_none=True, verbose=verbose)
93