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@1881: from __future__ import absolute_import 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 Edouard@1919: from M2Crypto.SSL import Connection # pylint: disable=import-error 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: Edouard@1995: _special_return_funcs = { Edouard@1995: "StartPLC": False, Edouard@1995: "GetTraceVariables": ("Broken", None), Edouard@1995: "GetPLCstatus": ("Broken", None), Edouard@1995: "RemoteExec": (-1, "RemoteExec script failed!") Edouard@1995: } Edouard@1997: 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 __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) Edouard@1995: member = PyroCatcher(my_local_func, _special_return_funcs.get(attrName, None)) andrej@1731: self.__dict__[attrName] = member andrej@1731: return member andrej@1731: andrej@1731: return PyroProxyProxy()