runtime/WampClient.py
author Edouard Tisserant
Tue, 09 Jan 2018 15:08:08 +0100
changeset 1893 971de876b1af
parent 1892 daf40a1e7607
child 1894 f224383cc883
permissions -rw-r--r--
WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
- added Challenge Response Authentication,
- use -s commandline switch to give the shared secret.
- WAMP configuration now either given as -c commandline switch
or taken as default wampconf.json in project files.
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of Beremiz runtime.
#
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
#
# See COPYING.Runtime file for copyrights details.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


from __future__ import absolute_import
from __future__ import print_function
import json

import os
import json
from autobahn.twisted import wamp
from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS
from autobahn.wamp import types, auth
from autobahn.wamp.serializer import MsgPackSerializer
from autobahn.wamp.exception import ApplicationError
from twisted.internet.defer import inlineCallbacks
from twisted.internet.protocol import ReconnectingClientFactory


_WampSession = None
_PySrv = None

ExposedCalls = [
    "StartPLC",
    "StopPLC",
    "ForceReload",
    "GetPLCstatus",
    "NewPLC",
    "MatchMD5",
    "SetTraceVariablesList",
    "GetTraceVariables",
    "RemoteExec",
    "GetLogMessage",
    "ResetLogCount",
]

SubscribedEvents = []

DoOnJoin = []


def GetCallee(name):
    """ Get Callee or Subscriber corresponding to '.' spearated object path """
    names = name.split('.')
    obj = _PySrv.plcobj
    while names:
        obj = getattr(obj, names.pop(0))
    return obj


class WampSession(wamp.ApplicationSession):
    def onConnect(self):
        secret = self.config.extra["secret"]
        if secret:
            user = self.config.extra["ID"].encode('utf8')
            self.join(u"Automation", [u"wampcra"], user)
        else:
            self.join(u"Automation")

    def onChallenge(self, challenge):
        if challenge.method == u"wampcra":
            secret = self.config.extra["secret"].encode('utf8')
            signature = auth.compute_wcs(secret, challenge.extra['challenge'].encode('utf8'))
            return signature.decode("ascii")
        else:
            raise Exception("don't know how to handle authmethod {}".format(challenge.method))

    @inlineCallbacks
    def onJoin(self, details):
        global _WampSession
        _WampSession = self
        ID = self.config.extra["ID"]
        print('WAMP session joined by :', ID)
        for name in ExposedCalls:
            regoption = types.RegisterOptions(u'exact',u'last',None, None)
            yield self.register(GetCallee(name), '.'.join((ID, name)), regoption)

        for name in SubscribedEvents:
            yield self.subscribe(GetCallee(name), name)

        for func in DoOnJoin:
            yield func(self)

    def onLeave(self, details):
        global _WampSession
        _WampSession = None
        print(_('WAMP session left'))


class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory):
    def clientConnectionFailed(self, connector, reason):
        print(_("WAMP Client connection failed .. retrying .."))
        ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)

    def clientConnectionLost(self, connector, reason):
        print(_("WAMP Client connection lost .. retrying .."))
        ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)


def LoadWampClientConf(wampconf):
    try:
        WSClientConf = json.load(open(wampconf))
        return WSClientConf
    except ValueError, ve:
        print(_("WAMP load error: "), ve)
        return None
    except Exception:
        return None

def LoadWampSecret(secretfname):
    try:
        WSClientWampSecret = open(secretfname, 'rb').read()
        return WSClientWampSecret
    except ValueError, ve:
        print(_("Wamp secret load error:"), ve)
        return None
    except Exception:
        return None


def RegisterWampClient(wampconf, secretfname):

    WSClientConf = LoadWampClientConf(wampconf)

    if not WSClientConf:
        print _("WAMP client connection not established!")
        return

    WampSecret = LoadWampSecret(secretfname)

    # create a WAMP application session factory
    component_config = types.ComponentConfig(
        realm=WSClientConf["realm"],
        extra=WSClientConf)
    session_factory = wamp.ApplicationSessionFactory(
        config=component_config)
    session_factory.session = WampSession

    # create a WAMP-over-WebSocket transport client factory
    transport_factory = ReconnectingWampWebSocketClientFactory(
        session_factory,
        url=WSClientConf["url"],
        serializers=[MsgPackSerializer()])

    # start the client from a Twisted endpoint
    conn = connectWS(transport_factory)
    print(_("WAMP client connecting to :"), _WSClientConf["url"])
    return conn


def GetSession():
    return _WampSession


def SetServer(pysrv):
    global _PySrv
    _PySrv = pysrv