Merge branch 'master' into senslab2
[sfa.git] / sfa / plc / plshell.py
index 863472f..d40e0b2 100644 (file)
@@ -1,4 +1,9 @@
+import sys
 import xmlrpclib
+import socket
+from urlparse import urlparse
+
+from sfa.util.sfalogging import logger
 
 class PlShell:
     """
@@ -24,16 +29,49 @@ class PlShell:
                     'get_nodes':'GetNodes',
                     }
 
+
+    # use the 'capability' auth mechanism for higher performance when the PLC db is local    
     def __init__ ( self, config ) :
-        self.plauth = {'Username': config.SFA_PLC_USER,
-                       'AuthMethod': 'password',
-                       'AuthString': config.SFA_PLC_PASSWORD}
-        
-        self.url = config.SFA_PLC_URL
-        self.plauth = {'Username': 'root@test.onelab.eu',
-                       'AuthMethod': 'password',
-                       'AuthString': 'test++'}
-        self.proxy_server = xmlrpclib.Server(self.url, verbose = 0, allow_none = True)
+        url = config.SFA_PLC_URL
+        # try to figure if the url is local
+        hostname=urlparse(url).hostname
+        is_local=False
+        if hostname == 'localhost': is_local=True
+        # otherwise compare IP addresses; 
+        # this might fail for any number of reasons, so let's harden that
+        try:
+            # xxx todo this seems to result in a DNS request for each incoming request to the AM
+            # should be cached or improved
+            url_ip=socket.gethostbyname(hostname)
+            local_ip=socket.gethostbyname(socket.gethostname())
+            if url_ip==local_ip: is_local=True
+        except:
+            pass
+
+        if is_local:
+            try:
+                # too bad this is not installed properly
+                plcapi_path="/usr/share/plc_api"
+                if plcapi_path not in sys.path: sys.path.append(plcapi_path)
+                import PLC.Shell
+                plc_direct_access=True
+            except:
+                plc_direct_access=False
+        if is_local and plc_direct_access:
+            logger.debug('plshell access - capability')
+            self.plauth = { 'AuthMethod': 'capability',
+                            'Username':   config.SFA_PLC_USER,
+                            'AuthString': config.SFA_PLC_PASSWORD,
+                            }
+            self.proxy = PLC.Shell.Shell ()
+
+        else:
+            logger.debug('plshell access - xmlrpc')
+            self.plauth = { 'AuthMethod': 'password',
+                            'Username':   config.SFA_PLC_USER,
+                            'AuthString': config.SFA_PLC_PASSWORD,
+                            }
+            self.proxy = xmlrpclib.Server(url, verbose = False, allow_none = True)
 
     def __getattr__(self, name):
         def func(*args, **kwds):
@@ -42,5 +80,7 @@ class PlShell:
             if name in PlShell.alias_calls: actual_name=PlShell.alias_calls[name]
             if not actual_name:
                 raise Exception, "Illegal method call %s for PL driver"%(name)
-            return getattr(self.proxy_server, actual_name)(self.plauth, *args, **kwds)
+            result=getattr(self.proxy, actual_name)(self.plauth, *args, **kwds)
+            logger.debug('%s (%s) returned ... '%(name,actual_name))
+            return result
         return func