etisserant@203: #!/usr/bin/env python etisserant@203: # -*- coding: utf-8 -*- etisserant@203: # r@1455: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD etisserant@203: # r@1455: # See COPYING file for copyrights details. etisserant@203: # r@1455: # This library is free software; you can redistribute it and/or r@1455: # modify it under the terms of the GNU General Public r@1455: # License as published by the Free Software Foundation; either r@1455: # version 2.1 of the License, or (at your option) any later version. etisserant@203: # r@1455: # This library is distributed in the hope that it will be useful, r@1455: # but WITHOUT ANY WARRANTY; without even the implied warranty of r@1455: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU r@1455: # General Public License for more details. etisserant@203: # r@1455: # You should have received a copy of the GNU General Public r@1455: # License along with this library; if not, write to the Free Software r@1455: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA r@1455: import Pyro r@1455: import Pyro.core r@1455: import Pyro.util etisserant@203: from Pyro.errors import PyroError etisserant@203: import traceback etisserant@203: from time import sleep etisserant@231: import copy Edouard@763: import socket Edouard@763: service_type = '_PYRO._tcp.local.' r@1455: import os.path laurent@399: # this module attribute contains a list of DNS-SD (Zeroconf) service types Edouard@717: # supported by this connector confnode. laurent@399: # laurent@399: # for connectors that do not support DNS-SD, this attribute can be omitted laurent@399: # or set to an empty list. laurent@399: Edouard@717: def PYRO_connector_factory(uri, confnodesroot): etisserant@203: """ etisserant@203: This returns the connector to Pyro style PLCobject etisserant@203: """ r@1455: confnodesroot.logger.write(_("PYRO connecting to URI : %s\n") % uri) etisserant@203: etisserant@203: servicetype, location = uri.split("://") r@1455: if servicetype == "PYROS": r@1455: schemename = "PYROLOCSSL" r@1455: # Protect against name->IP substitution in Pyro3 r@1455: Pyro.config.PYRO_DNS_URI = True r@1455: # Beware Pyro lib need str path, not unicode r@1455: # don't rely on PYRO_STORAGE ! see documentation r@1455: Pyro.config.PYROSSL_CERTDIR = os.path.abspath(str(confnodesroot.ProjectPath) + '/certs') r@1455: if not os.path.exists(Pyro.config.PYROSSL_CERTDIR): r@1455: confnodesroot.logger.write_error( r@1455: 'Error : the directory %s is missing for SSL certificates (certs_dir).' r@1455: 'Please fix it in your project.\n' % Pyro.config.PYROSSL_CERTDIR) r@1455: return None r@1455: else: r@1455: confnodesroot.logger.write(_("PYRO using certificates in '%s' \n") r@1455: % (Pyro.config.PYROSSL_CERTDIR)) r@1455: Pyro.config.PYROSSL_CERT = "client.crt" r@1455: Pyro.config.PYROSSL_KEY = "client.key" r@1455: # Ugly Monkey Patching r@1455: def _gettimeout(self): r@1455: return self.timeout r@1455: r@1455: def _settimeout(self, timeout): r@1455: self.timeout = timeout r@1455: from M2Crypto.SSL import Connection r@1455: Connection.timeout = None r@1455: Connection.gettimeout = _gettimeout r@1455: Connection.settimeout = _settimeout r@1455: # M2Crypto.SSL.Checker.WrongHost: Peer certificate commonName does not r@1455: # match host, expected 127.0.0.1, got server r@1455: Connection.clientPostConnectionCheck = None r@1455: else: r@1455: schemename = "PYROLOC" Edouard@763: if location.find(service_type) != -1: r@1455: try: Edouard@763: from util.Zeroconf import Zeroconf Edouard@763: r = Zeroconf() r@1455: i = r.getServiceInfo(service_type, location) r@1455: if i is None: r@1455: raise Exception("'%s' not found" % location) Edouard@763: ip = str(socket.inet_ntoa(i.getAddress())) Edouard@763: port = str(i.getPort()) r@1455: newlocation = ip + ':' + port r@1455: confnodesroot.logger.write(_("'%s' is located at %s\n") % (location, newlocation)) Edouard@763: location = newlocation Edouard@763: r.close() Edouard@763: except Exception, msg: r@1455: confnodesroot.logger.write_error(_("MDNS resolution failure for '%s'\n") % location) Edouard@763: confnodesroot.logger.write_error(traceback.format_exc()) Edouard@763: return None Edouard@1434: etisserant@203: # Try to get the proxy object r@1455: try: r@1455: RemotePLCObjectProxy = Pyro.core.getAttrProxyForURI(schemename + "://" + location + "/PLCObject") etisserant@203: except Exception, msg: r@1455: confnodesroot.logger.write_error(_("Connection to '%s' failed.\n") % location) Edouard@717: confnodesroot.logger.write_error(traceback.format_exc()) etisserant@203: return None etisserant@203: etisserant@203: def PyroCatcher(func, default=None): etisserant@203: """ r@1455: A function that catch a Pyro exceptions, write error to logger r@1455: and return default value when it happen etisserant@203: """ r@1455: def catcher_func(*args, **kwargs): etisserant@203: try: r@1455: return func(*args, **kwargs) Laurent@1116: except Pyro.errors.ConnectionClosedError, e: Laurent@1116: confnodesroot.logger.write_error("Connection lost!\n") Laurent@1116: confnodesroot._SetConnector(None) laurent@493: except Pyro.errors.ProtocolError, e: r@1455: confnodesroot.logger.write_error("Pyro exception: " + str(e) + "\n") r@1455: except Exception, e: r@1455: # confnodesroot.logger.write_error(traceback.format_exc()) edouard@477: errmess = ''.join(Pyro.util.getPyroTraceback(e)) r@1455: confnodesroot.logger.write_error(errmess + "\n") edouard@477: print errmess Laurent@1116: confnodesroot._SetConnector(None) Edouard@1070: return default greg@218: return catcher_func etisserant@203: Edouard@1434: # Check connection is effective. etisserant@203: # lambda is for getattr of GetPLCstatus to happen inside catcher r@1455: if PyroCatcher(lambda: RemotePLCObjectProxy.GetPLCstatus())() is None: Edouard@717: confnodesroot.logger.write_error(_("Cannot get PLC status - connection failed.\n")) etisserant@203: return None etisserant@231: Edouard@1441: class PyroProxyProxy(object): etisserant@203: """ etisserant@203: A proxy proxy class to handle Beremiz Pyro interface specific behavior. r@1455: And to put Pyro exception catcher in between caller and Pyro proxy etisserant@203: """ etisserant@284: def __init__(self): etisserant@284: # for safe use in from debug thread, must create a copy etisserant@284: self.RemotePLCObjectProxyCopy = None etisserant@284: etisserant@203: def GetPyroProxy(self): etisserant@203: """ etisserant@203: This func returns the real Pyro Proxy. etisserant@203: Use this if you musn't keep reference to it. etisserant@203: """ etisserant@203: return RemotePLCObjectProxy etisserant@231: etisserant@235: def _PyroStartPLC(self, *args, **kwargs): etisserant@231: """ Edouard@1434: confnodesroot._connector.GetPyroProxy() is used etisserant@231: rather than RemotePLCObjectProxy because Edouard@1434: object is recreated meanwhile, etisserant@231: so we must not keep ref to it here etisserant@231: """ Laurent@969: current_status, log_count = confnodesroot._connector.GetPyroProxy().GetPLCstatus() edouard@465: if current_status == "Dirty": etisserant@231: """ etisserant@231: Some bad libs with static symbols may polute PLC etisserant@231: ask runtime to suicide and come back again etisserant@231: """ Edouard@717: confnodesroot.logger.write(_("Force runtime reload\n")) Edouard@717: confnodesroot._connector.GetPyroProxy().ForceReload() Edouard@717: confnodesroot._Disconnect() etisserant@231: # let remote PLC time to resurect.(freeze app) etisserant@231: sleep(0.5) Edouard@717: confnodesroot._Connect() Edouard@717: self.RemotePLCObjectProxyCopy = copy.copy(confnodesroot._connector.GetPyroProxy()) Edouard@717: return confnodesroot._connector.GetPyroProxy().StartPLC(*args, **kwargs) etisserant@231: StartPLC = PyroCatcher(_PyroStartPLC, False) etisserant@231: etisserant@231: def _PyroGetTraceVariables(self): etisserant@231: """ etisserant@231: for safe use in from debug thread, must use the copy etisserant@231: """ edouard@465: if self.RemotePLCObjectProxyCopy is None: Edouard@717: self.RemotePLCObjectProxyCopy = copy.copy(confnodesroot._connector.GetPyroProxy()) ed@446: return self.RemotePLCObjectProxyCopy.GetTraceVariables() r@1455: GetTraceVariables = PyroCatcher(_PyroGetTraceVariables, ("Broken", None)) etisserant@231: ed@446: def _PyroGetPLCstatus(self): ed@446: return RemotePLCObjectProxy.GetPLCstatus() r@1455: GetPLCstatus = PyroCatcher(_PyroGetPLCstatus, ("Broken", None)) ed@446: laurent@699: def _PyroRemoteExec(self, script, **kwargs): laurent@699: return RemotePLCObjectProxy.RemoteExec(script, **kwargs) laurent@699: RemoteExec = PyroCatcher(_PyroRemoteExec, (-1, "RemoteExec script failed!")) laurent@699: etisserant@203: def __getattr__(self, attrName): etisserant@231: member = self.__dict__.get(attrName, None) etisserant@231: if member is None: r@1455: def my_local_func(*args, **kwargs): r@1455: return RemotePLCObjectProxy.__getattr__(attrName)(*args, **kwargs) etisserant@231: member = PyroCatcher(my_local_func, None) etisserant@203: self.__dict__[attrName] = member etisserant@231: return member etisserant@231: etisserant@203: return PyroProxyProxy()