refactored. Added Interface class
[sfa.git] / sfa / server / interface.py
1 import traceback
2 import os.path
3
4 from sfa.util.faults import *
5 from sfa.util.storage import XmlStorage
6 from sfa.util.xrn import get_authority, hrn_to_urn
7 from sfa.util.record import SfaRecord
8 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
9 import sfa.util.soapprotocol as soapprotocol
10 from sfa.trust.gid import GID
11
12 # GeniLight client support is optional
13 try:
14     from egeni.geniLight_client import *
15 except ImportError:
16     GeniClientLight = None            
17
18
19
20 class Interface:
21     
22     def __init__(self, hrn, addr, port, client_type='sfa'):
23         self.hrn = hrn
24         self.addr = addr
25         self.port = port
26         self.client_type = client_type
27   
28     def get_url(self):
29         address_parts = self.addr.split('/')
30         address_parts[0] = address_parts[0] + ":" + str(self.port)
31         url =  "http://%s" %  "/".join(address_parts)
32         return url
33
34     def get_server(self, key_file, cert_file, timeout=30):
35         server = None 
36         if  self.client_type ==  'geniclientlight' and GeniClientLight:
37             server = GeniClientLight(url, self.api.key_file, self.api.cert_file)
38         else:
39             server = xmlrpcprotocol.get_server(self.get_url(), key_file, cert_file, timeout) 
40  
41         return server       
42 ##
43 # In is a dictionary of registry connections keyed on the registry
44 # hrn
45
46 class Interfaces(dict):
47     """
48     Interfaces is a base class for managing information on the
49     peers we are federated with. Provides connections (xmlrpc or soap) to federated peers
50     """
51
52     # fields that must be specified in the config file
53     default_fields = {
54         'hrn': '',
55         'addr': '', 
56         'port': '', 
57     }
58
59     # defined by the class 
60     default_dict = {}
61
62     def __init__(self, conf_file):
63         dict.__init__(self, {})
64         # load config file
65         self.interface_info = XmlStorage(conf_file, self.default_dict)
66         self.interface_info.load()
67         records = self.interface_info.values()[0].values()[0]
68         if not isinstance(records, list):
69             records = [records]
70         
71         required_fields = self.default_fields.keys()
72         for record in records:
73             if not set(required_fields).issubset(record.keys()):
74                 continue
75             # port is appended onto the domain, before the path. Should look like:
76             # http://domain:port/path
77             hrn, address, port = record['hrn'], record['addr'], record['port']
78             interface = Interface(hrn, address, port) 
79             self[hrn] = interface
80
81     def get_server(self, hrn, key_file, cert_file, timeout=30):
82         return self[hrn].get_server(key_file, cert_file, timeout)