remote connections timeout after 30 seconds
[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 # In is a dictionary of registry connections keyed on the registry
21 # hrn
22
23 class Interfaces(dict):
24     """
25     Interfaces is a base class for managing information on the
26     peers we are federated with. It is responsible for the following:
27
28     1) Makes sure a record exist in the local registry for the each 
29        fedeated peer   
30     2) Attempts to fetch and install trusted gids   
31     3) Provides connections (xmlrpc or soap) to federated peers
32     """
33
34     # fields that must be specified in the config file
35     default_fields = {
36         'hrn': '',
37         'addr': '', 
38         'port': '', 
39     }
40
41     # defined by the class 
42     default_dict = {}
43
44     types = ['authority']
45
46     def __init__(self, api, conf_file, type='authority'):
47         if type not in self.types:
48             raise SfaInfaildArgument('Invalid type %s: must be in %s' % (type, self.types))    
49         dict.__init__(self, {})
50         self.api = api
51         self.type = type  
52         # load config file
53         self.interface_info = XmlStorage(conf_file, self.default_dict)
54         self.interface_info.load()
55         interfaces = self.interface_info.values()[0].values()[0]
56         if not isinstance(interfaces, list):
57             interfaces = [self.interfaces]
58         # set the url and urn 
59         for interface in interfaces:
60             # port is appended onto the domain, before the path. Should look like:
61             # http://domain:port/path
62             hrn, address, port = interface['hrn'], interface['addr'], interface['port']
63             address_parts = address.split('/')
64             address_parts[0] = address_parts[0] + ":" + str(port)
65             url =  "http://%s" %  "/".join(address_parts)
66             interface['url'] = url
67             interface['urn'] = hrn_to_urn(hrn, 'authority')
68     
69         self.interfaces = {}
70         required_fields = self.default_fields.keys()
71         for interface in interfaces:
72             valid = True
73             # skp any interface definition that has a null hrn, 
74             # address or port
75             for field in required_fields:
76                 if field not in interface or not interface[field]:
77                     valid = False
78                     break
79             if valid:     
80                 self.interfaces[interface['hrn']] = interface
81
82
83     def get_connections(self):
84         """
85         read connection details for the trusted peer registries from file return 
86         a dictionary of connections keyed on interface hrn. 
87         """
88         connections = {}
89         required_fields = self.default_fields.keys()
90         for interface in self.interfaces.values():
91             url = interface['url']
92 #            sfa_logger().debug("Interfaces.get_connections - looping on neighbour %s"%url)
93             # check which client we should use
94             # sfa.util.xmlrpcprotocol is default
95             client_type = 'xmlrpcprotocol'
96             if interface.has_key('client') and \
97                interface['client'] in ['geniclientlight'] and \
98                GeniClientLight:
99                 client_type = 'geniclientlight'
100                 connections[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file) 
101             else:
102                 connections[interface['hrn']] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file, timeout=30)
103
104         return connections