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