8e7c5f8260ce209075a0a0d958af1d42b5ffe4cf
[sfa.git] / sfa / server / sfaserver.py
1 ##
2 # This module implements a general-purpose server layer for sfa.
3 # The same basic server should be usable on the registry, component, or
4 # other interfaces.
5 #
6 # TODO: investigate ways to combine this with existing PLC server?
7 ##
8
9 import threading
10
11 from sfa.server.threadedserver import ThreadedServer, SecureXMLRpcRequestHandler
12
13 from sfa.util.sfalogging import logger
14 from sfa.trust.certificate import Keypair, Certificate
15
16 ##
17 # Implements an HTTPS XML-RPC server. Generally it is expected that SFA
18 # functions will take a credential string, which is passed to
19 # decode_authentication. Decode_authentication() will verify the validity of
20 # the credential, and verify that the user is using the key that matches the
21 # GID supplied in the credential.
22
23 class SfaServer(threading.Thread):
24
25     ##
26     # Create a new SfaServer object.
27     #
28     # @param ip the ip address to listen on
29     # @param port the port to listen on
30     # @param key_file private key filename of registry
31     # @param cert_file certificate filename containing public key 
32     #   (could be a GID file)
33
34     def __init__(self, ip, port, key_file, cert_file, interface):
35         threading.Thread.__init__(self)
36         self.key = Keypair(filename = key_file)
37         self.cert = Certificate(filename = cert_file)
38         #self.server = SecureXMLRPCServer((ip, port), SecureXMLRpcRequestHandler, key_file, cert_file)
39         self.server = ThreadedServer((ip, int(port)), SecureXMLRpcRequestHandler, key_file, cert_file)
40         self.server.interface=interface
41         self.trusted_cert_list = None
42         self.register_functions()
43         logger.info("Starting SfaServer, interface=%s"%interface)
44
45     ##
46     # Register functions that will be served by the XMLRPC server. This
47     # function should be overridden by each descendant class.
48
49     def register_functions(self):
50         self.server.register_function(self.noop)
51
52     ##
53     # Sample no-op server function. The no-op function decodes the credential
54     # that was passed to it.
55
56     def noop(self, cred, anything):
57         return anything
58
59     ##
60     # Execute the server, serving requests forever. 
61
62     def run(self):
63         self.server.serve_forever()
64
65