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: Edouard@2313: zeroconf_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": Edouard@2312: import connectors.PYRO.PSK_Adapter Edouard@2314: schemename = "PYROLOCPSK" Edouard@2313: url, ID = location.split('#') Edouard@2312: # load PSK from project Edouard@2312: secpath = os.path.join(str(confnodesroot.ProjectPath), 'psk', ID+'.secret') Edouard@2312: if not os.path.exists(secpath): andrej@1731: confnodesroot.logger.write_error( Edouard@2312: 'Error: Pre-Shared-Key Secret in %s is missing!\n' % secpath) andrej@1731: return None Edouard@2313: Pyro.config.PYROPSK = (open(secpath).read(), ID) Edouard@2313: # strip ID from URL, so that pyro can understand it. Edouard@2313: location = url andrej@1731: else: andrej@1731: schemename = "PYROLOC" Edouard@2312: Edouard@2313: if location.find(zeroconf_service_type) != -1: andrej@1731: try: andrej@1830: from zeroconf import Zeroconf andrej@1731: r = Zeroconf() Edouard@2313: i = r.get_service_info(zeroconf_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() Edouard@2312: