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@2339: import PSKManagement as PSK
andrej@2543: import connectors.PYRO.PSK_Adapter
andrej@2416: from runtime import PlcStatus
andrej@1832: 
andrej@2543: 
andrej@2543: def switch_pyro_adapter(use_ssl):
andrej@2543:     """
andrej@2543:     Reloads Pyro module with new settings.
andrej@2543:     This is workaround for Pyro, because it doesn't work with SSL wrapper.
andrej@2543:     """
andrej@2543:     # Pyro.config.PYRO_BROKEN_MSGWAITALL = use_ssl
andrej@2543:     reload(Pyro.protocol)
andrej@2543:     if use_ssl:
andrej@2543:         connectors.PYRO.PSK_Adapter.setupPSKAdapter()
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: 
Edouard@2338:     scheme, location = uri.split("://")
andrej@2543:     use_ssl = scheme == "PYROS"
andrej@2543:     switch_pyro_adapter(use_ssl)
andrej@2543:     if use_ssl:
Edouard@2314:         schemename = "PYROLOCPSK"
edouard@2492:         url, ID = location.split('#')  # TODO fix exception when # not found
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@2324:         secret = open(secpath).read().partition(':')[2].rstrip('\n\r')
Edouard@2324:         Pyro.config.PYROPSK = (secret, 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: 
andrej@1731:     # Try to get the proxy object
andrej@1731:     try:
andrej@1731:         RemotePLCObjectProxy = Pyro.core.getAttrProxyForURI(schemename + "://" + location + "/PLCObject")
andrej@2536:     except Exception as e:
edouard@2492:         confnodesroot.logger.write_error(
edouard@2492:             _("Connection to {loc} failed with exception {ex}\n").format(
andrej@2538:                 loc=location, ex=str(e)))
andrej@1731:         return None
andrej@1731: 
Edouard@2468:     RemotePLCObjectProxy.adapter.setTimeout(60)
Edouard@2468: 
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@2418:             except Pyro.errors.ConnectionClosedError as e:
andrej@1731:                 confnodesroot.logger.write_error(_("Connection lost!\n"))
andrej@1731:                 confnodesroot._SetConnector(None)
andrej@2418:             except Pyro.errors.ProtocolError as e:
andrej@1731:                 confnodesroot.logger.write_error(_("Pyro exception: %s\n") % e)
andrej@2418:             except Exception as 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
Edouard@2324:     IDPSK = PyroCatcher(RemotePLCObjectProxy.GetPLCID)()
Edouard@2324:     if IDPSK is None:
edouard@2430:         confnodesroot.logger.write_warning(_("PLC did not provide identity and security infomation.\n"))
edouard@2430:     else:
edouard@2492:         ID, secret = IDPSK
edouard@2430:         PSK.UpdateID(confnodesroot.ProjectPath, ID, secret, uri)
andrej@1731: 
Edouard@1995:     _special_return_funcs = {
Edouard@1995:         "StartPLC": False,
edouard@2429:         "GetTraceVariables": (PlcStatus.Broken, None),
andrej@2416:         "GetPLCstatus": (PlcStatus.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: 
Edouard@2463:     return PyroProxyProxy