etisserant@203: #!/usr/bin/env python etisserant@203: # -*- coding: utf-8 -*- etisserant@203: # etisserant@203: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD etisserant@203: # etisserant@203: #See COPYING file for copyrights details. etisserant@203: # etisserant@203: #This library is free software; you can redistribute it and/or etisserant@203: #modify it under the terms of the GNU General Public etisserant@203: #License as published by the Free Software Foundation; either etisserant@203: #version 2.1 of the License, or (at your option) any later version. etisserant@203: # etisserant@203: #This library is distributed in the hope that it will be useful, etisserant@203: #but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@203: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@203: #General Public License for more details. etisserant@203: # etisserant@203: #You should have received a copy of the GNU General Public etisserant@203: #License along with this library; if not, write to the Free Software etisserant@203: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@203: import Pyro.core as pyro etisserant@203: from Pyro.errors import PyroError edouard@477: import Pyro.util 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.' etisserant@203: 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: """ Edouard@717: confnodesroot.logger.write(_("Connecting to URI : %s\n")%uri) etisserant@203: etisserant@203: servicetype, location = uri.split("://") Edouard@763: if location.find(service_type) != -1: Edouard@763: try : Edouard@763: from util.Zeroconf import Zeroconf Edouard@763: r = Zeroconf() Edouard@763: i=r.getServiceInfo(service_type, location) Edouard@1035: if i is None : raise Exception, "'%s' not found"%location Edouard@763: ip = str(socket.inet_ntoa(i.getAddress())) Edouard@763: port = str(i.getPort()) Edouard@763: newlocation = ip+':'+port Edouard@763: confnodesroot.logger.write(_("'%s' is located at %s\n")%(location, newlocation)) Edouard@763: location = newlocation Edouard@763: r.close() Edouard@763: except Exception, msg: Edouard@763: confnodesroot.logger.write_error(_("MDNS resolution failure for '%s'\n")%location) Edouard@763: confnodesroot.logger.write_error(traceback.format_exc()) Edouard@763: return None etisserant@203: etisserant@203: # Try to get the proxy object etisserant@203: try : etisserant@203: RemotePLCObjectProxy = pyro.getAttrProxyForURI("PYROLOC://"+location+"/PLCObject") etisserant@203: except Exception, msg: Edouard@763: 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: """ etisserant@203: A function that catch a pyro exceptions, write error to logger etisserant@203: and return defaul value when it happen etisserant@203: """ greg@218: def catcher_func(*args,**kwargs): etisserant@203: try: etisserant@203: 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: Edouard@1070: confnodesroot.logger.write_error("Pyro exception: "+str(e)+"\n") edouard@477: except Exception,e: Edouard@717: #confnodesroot.logger.write_error(traceback.format_exc()) edouard@477: errmess = ''.join(Pyro.util.getPyroTraceback(e)) Edouard@717: 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: etisserant@203: # Check connection is effective. etisserant@203: # lambda is for getattr of GetPLCstatus to happen inside catcher Edouard@923: 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: etisserant@284: etisserant@203: class PyroProxyProxy: etisserant@203: """ etisserant@203: A proxy proxy class to handle Beremiz Pyro interface specific behavior. etisserant@203: 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@717: confnodesroot._connector.GetPyroProxy() is used etisserant@231: rather than RemotePLCObjectProxy because etisserant@231: 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: 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() Edouard@917: GetTraceVariables = PyroCatcher(_PyroGetTraceVariables,("Broken",None,None)) etisserant@231: ed@446: def _PyroGetPLCstatus(self): ed@446: return RemotePLCObjectProxy.GetPLCstatus() Edouard@923: 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: etisserant@231: def my_local_func(*args,**kwargs): etisserant@231: 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() etisserant@203: etisserant@203: