ThreadedServer and SfaServer in separate files
[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 #should be passed to threadedserver
16 #from sfa.plc.api import SfaAPI
17
18 ##
19 # Implements an HTTPS XML-RPC server. Generally it is expected that SFA
20 # functions will take a credential string, which is passed to
21 # decode_authentication. Decode_authentication() will verify the validity of
22 # the credential, and verify that the user is using the key that matches the
23 # GID supplied in the credential.
24
25 class SfaServer(threading.Thread):
26
27     ##
28     # Create a new SfaServer object.
29     #
30     # @param ip the ip address to listen on
31     # @param port the port to listen on
32     # @param key_file private key filename of registry
33     # @param cert_file certificate filename containing public key 
34     #   (could be a GID file)
35
36     def __init__(self, ip, port, key_file, cert_file,interface):
37         threading.Thread.__init__(self)
38         self.key = Keypair(filename = key_file)
39         self.cert = Certificate(filename = cert_file)
40         #self.server = SecureXMLRPCServer((ip, port), SecureXMLRpcRequestHandler, key_file, cert_file)
41         self.server = ThreadedServer((ip, port), SecureXMLRpcRequestHandler, key_file, cert_file)
42         self.server.interface=interface
43         self.trusted_cert_list = None
44         self.register_functions()
45         logger.info("Starting SfaServer, interface=%s"%interface)
46
47     ##
48     # Register functions that will be served by the XMLRPC server. This
49     # function should be overridden by each descendant class.
50
51     def register_functions(self):
52         self.server.register_function(self.noop)
53
54     ##
55     # Sample no-op server function. The no-op function decodes the credential
56     # that was passed to it.
57
58     def noop(self, cred, anything):
59         self.decode_authentication(cred)
60         return anything
61
62     ##
63     # Execute the server, serving requests forever. 
64
65     def run(self):
66         self.server.serve_forever()
67
68