sfaprotocol is renamed into sfaserverproxy, with class SfaServerProxy
[sfa.git] / sfa / server / interface.py
1 from sfa.client.sfaserverproxy import SfaServerProxy
2 from sfa.util.xml import XML
3
4 # GeniLight client support is optional
5 try:
6     from egeni.geniLight_client import *
7 except ImportError:
8     GeniClientLight = None            
9
10 class Interface:
11     """
12     Interface to another SFA service, typically a peer, or the local aggregate
13     can retrieve a xmlrpclib.ServerProxy object for issuing calls there
14     """
15     def __init__(self, hrn, addr, port, client_type='sfa'):
16         self.hrn = hrn
17         self.addr = addr
18         self.port = port
19         self.client_type = client_type
20   
21     def get_url(self):
22         address_parts = self.addr.split('/')
23         address_parts[0] = address_parts[0] + ":" + str(self.port)
24         url =  "http://%s" %  "/".join(address_parts)
25         return url
26
27     def server_proxy(self, key_file, cert_file, timeout=30):
28         server = None 
29         if  self.client_type ==  'geniclientlight' and GeniClientLight:
30             # xxx url and self.api are undefined
31             server = GeniClientLight(url, self.api.key_file, self.api.cert_file)
32         else:
33             server = SfaServerProxy(self.get_url(), key_file, cert_file, timeout) 
34  
35         return server       
36 ##
37 # In is a dictionary of registry connections keyed on the registry
38 # hrn
39
40 class Interfaces(dict):
41     """
42     Interfaces is a base class for managing information on the
43     peers we are federated with. Provides connections (xmlrpc or soap) to federated peers
44     """
45
46     # fields that must be specified in the config file
47     default_fields = {
48         'hrn': '',
49         'addr': '', 
50         'port': '', 
51     }
52
53     # defined by the class 
54     default_dict = {}
55
56     def __init__(self, conf_file):
57         dict.__init__(self, {})
58         # load config file
59         required_fields = set(self.default_fields.keys())
60         self.interface_info = XML(conf_file).todict()
61         for value in self.interface_info.values():
62             if isinstance(value, list):
63                 for record in value:
64                     if isinstance(record, dict) and \
65                       required_fields.issubset(record.keys()):
66                         hrn, address, port = record['hrn'], record['addr'], record['port']
67                         # sometime this is called at a very early stage with no config loaded
68                         # avoid to remember this instance in such a case
69                         if not address or not port:
70                             continue     
71                         interface = Interface(hrn, address, port)
72                         self[hrn] = interface   
73
74     def server_proxy(self, hrn, key_file, cert_file, timeout=30):
75         return self[hrn].server_proxy(key_file, cert_file, timeout)