runtime/WampClient.py
author dporopat <denis.poropat@smarteh.si>
Wed, 09 May 2018 09:59:53 +0200
branch#2486
changeset 2203 c8a32ad27c0a
parent 2202 237c1a2de1c8
child 2204 25dafeb98b55
permissions -rw-r--r--
#2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
     3
1667
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
     4
# This file is part of Beremiz runtime.
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1446
diff changeset
     5
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1446
diff changeset
     6
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1446
diff changeset
     7
#
1667
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
     8
# See COPYING.Runtime file for copyrights details.
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1446
diff changeset
     9
#
1667
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    10
# This library is free software; you can redistribute it and/or
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    11
# modify it under the terms of the GNU Lesser General Public
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    12
# License as published by the Free Software Foundation; either
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    13
# version 2.1 of the License, or (at your option) any later version.
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    14
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    15
# This library is distributed in the hope that it will be useful,
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1446
diff changeset
    16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1667
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    18
# Lesser General Public License for more details.
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    19
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    20
# You should have received a copy of the GNU Lesser General Public
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    21
# License along with this library; if not, write to the Free Software
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    22
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1446
diff changeset
    23
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    24
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1878
diff changeset
    25
from __future__ import absolute_import
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    26
from __future__ import print_function
1899
ff4410112b67 runtime/WAMP : was 'Added time print on session left.' from dporopat. Purpose is to log (dis/re)connection, to help debugging connection problem
Edouard Tisserant
parents: 1898
diff changeset
    27
import time
1893
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
    28
import json
2203
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
    29
import os
2195
2cef75d036c2 #2486 Added option to use websocket protocol options.
dporopat <denis.poropat@smarteh.si>
parents: 2194
diff changeset
    30
import inspect
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
    31
import re
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
    32
from autobahn.twisted import wamp
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
    33
from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS
1893
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
    34
from autobahn.wamp import types, auth
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    35
from autobahn.wamp.serializer import MsgPackSerializer
1834
cd42b426028b fix ungrouped imports from package X
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1832
diff changeset
    36
from twisted.internet.defer import inlineCallbacks
1441
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
    37
from twisted.internet.protocol import ReconnectingClientFactory
1832
0f1081928d65 fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    38
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
    39
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
    40
_transportFactory = None
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
    41
_WampSession = None
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
    42
_PySrv = None
2202
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
    43
_WampConf = None
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
    44
_WampSecret = None
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
    45
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    46
ExposedCalls = [
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    47
    "StartPLC",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    48
    "StopPLC",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    49
    "ForceReload",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    50
    "GetPLCstatus",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    51
    "NewPLC",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    52
    "MatchMD5",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    53
    "SetTraceVariablesList",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    54
    "GetTraceVariables",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    55
    "RemoteExec",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    56
    "GetLogMessage",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    57
    "ResetLogCount",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    58
]
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    59
2198
4a5ac299f550 #2486 Added ExposedProgressCalls list using details argument for registration progress calls.
dporopat <denis.poropat@smarteh.si>
parents: 2197
diff changeset
    60
ExposedProgressCalls = []
4a5ac299f550 #2486 Added ExposedProgressCalls list using details argument for registration progress calls.
dporopat <denis.poropat@smarteh.si>
parents: 2197
diff changeset
    61
1898
d2dbdeffc426 runtime/Wamp : adapt to unicode being enforced in latest crossbar API. Updated some documentation, to explain why there is unused definitions like SubscribedEvents or DoOnJoin.
Edouard Tisserant
parents: 1894
diff changeset
    62
# Those two lists are meant to be filled by customized runtime
1901
e8cf68d69447 Enforced check_source.sh pep8 and pylint rules.
Edouard Tisserant
parents: 1900
diff changeset
    63
# or User python code.
1898
d2dbdeffc426 runtime/Wamp : adapt to unicode being enforced in latest crossbar API. Updated some documentation, to explain why there is unused definitions like SubscribedEvents or DoOnJoin.
Edouard Tisserant
parents: 1894
diff changeset
    64
1901
e8cf68d69447 Enforced check_source.sh pep8 and pylint rules.
Edouard Tisserant
parents: 1900
diff changeset
    65
""" crossbar Events to register to """
1446
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
    66
SubscribedEvents = []
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
    67
1901
e8cf68d69447 Enforced check_source.sh pep8 and pylint rules.
Edouard Tisserant
parents: 1900
diff changeset
    68
""" things to do on join (callables) """
1446
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
    69
DoOnJoin = []
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
    70
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    71
1445
e25479e3fe11 More versatile registering of wamp callees
Edouard Tisserant
parents: 1443
diff changeset
    72
def GetCallee(name):
1446
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
    73
    """ Get Callee or Subscriber corresponding to '.' spearated object path """
1445
e25479e3fe11 More versatile registering of wamp callees
Edouard Tisserant
parents: 1443
diff changeset
    74
    names = name.split('.')
e25479e3fe11 More versatile registering of wamp callees
Edouard Tisserant
parents: 1443
diff changeset
    75
    obj = _PySrv.plcobj
1756
08e4394ff4fb clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1753
diff changeset
    76
    while names:
08e4394ff4fb clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1753
diff changeset
    77
        obj = getattr(obj, names.pop(0))
1445
e25479e3fe11 More versatile registering of wamp callees
Edouard Tisserant
parents: 1443
diff changeset
    78
    return obj
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    79
2196
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
    80
def getValidOptins(options, arguments):
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
    81
    validOptions = {}
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
    82
    for key in options:
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
    83
        if key in arguments:
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
    84
            validOptions[key] = options[key]
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
    85
    if len(validOptions) > 0:
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
    86
        return validOptions
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
    87
    else:
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
    88
        return None
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    89
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
    90
class WampSession(wamp.ApplicationSession):
1893
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
    91
    def onConnect(self):
1901
e8cf68d69447 Enforced check_source.sh pep8 and pylint rules.
Edouard Tisserant
parents: 1900
diff changeset
    92
        if "secret" in self.config.extra:
2194
35d6b9321ffd #2486 Updated wamp test project to work with the latest Crossbar version.
dporopat <denis.poropat@smarteh.si>
parents: 2193
diff changeset
    93
            user = self.config.extra["ID"]
1893
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
    94
            self.join(u"Automation", [u"wampcra"], user)
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
    95
        else:
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
    96
            self.join(u"Automation")
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
    97
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
    98
    def onChallenge(self, challenge):
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
    99
        if challenge.method == u"wampcra":
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   100
            secret = self.config.extra["secret"].encode('utf8')
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   101
            signature = auth.compute_wcs(secret, challenge.extra['challenge'].encode('utf8'))
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   102
            return signature.decode("ascii")
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   103
        else:
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   104
            raise Exception("don't know how to handle authmethod {}".format(challenge.method))
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
   105
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
   106
    @inlineCallbacks
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   107
    def onJoin(self, details):
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   108
        global _WampSession
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   109
        _WampSession = self
1901
e8cf68d69447 Enforced check_source.sh pep8 and pylint rules.
Edouard Tisserant
parents: 1900
diff changeset
   110
        ID = self.config.extra["ID"]
2199
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   111
        validRegisterOptions = {}
2196
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
   112
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
   113
        registerOptions = self.config.extra.get('registerOptions', None)
2199
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   114
        if registerOptions:
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   115
            arguments = inspect.getargspec(types.RegisterOptions.__init__).args
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   116
            validRegisterOptions = getValidOptins(registerOptions, arguments)
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   117
            if validRegisterOptions:
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   118
                registerOptions = types.RegisterOptions(**validRegisterOptions)
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   119
                #print(_("Added custom register options"))
2196
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
   120
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
   121
        for name in ExposedCalls:
2197
bec80ff35282 #2486 Unnecessary variable removed.
dporopat <denis.poropat@smarteh.si>
parents: 2196
diff changeset
   122
            yield self.register(GetCallee(name), u'.'.join((ID, name)), registerOptions)
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   123
2198
4a5ac299f550 #2486 Added ExposedProgressCalls list using details argument for registration progress calls.
dporopat <denis.poropat@smarteh.si>
parents: 2197
diff changeset
   124
        if ExposedProgressCalls:
4a5ac299f550 #2486 Added ExposedProgressCalls list using details argument for registration progress calls.
dporopat <denis.poropat@smarteh.si>
parents: 2197
diff changeset
   125
            validRegisterOptions["details_arg"] = 'details'
4a5ac299f550 #2486 Added ExposedProgressCalls list using details argument for registration progress calls.
dporopat <denis.poropat@smarteh.si>
parents: 2197
diff changeset
   126
            registerOptions = types.RegisterOptions(**validRegisterOptions)
4a5ac299f550 #2486 Added ExposedProgressCalls list using details argument for registration progress calls.
dporopat <denis.poropat@smarteh.si>
parents: 2197
diff changeset
   127
            # using progress, details argument must be added
4a5ac299f550 #2486 Added ExposedProgressCalls list using details argument for registration progress calls.
dporopat <denis.poropat@smarteh.si>
parents: 2197
diff changeset
   128
            for name in ExposedProgressCalls:
4a5ac299f550 #2486 Added ExposedProgressCalls list using details argument for registration progress calls.
dporopat <denis.poropat@smarteh.si>
parents: 2197
diff changeset
   129
                yield self.register(GetCallee(name), u'.'.join((ID, name)), registerOptions)
4a5ac299f550 #2486 Added ExposedProgressCalls list using details argument for registration progress calls.
dporopat <denis.poropat@smarteh.si>
parents: 2197
diff changeset
   130
1446
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
   131
        for name in SubscribedEvents:
1898
d2dbdeffc426 runtime/Wamp : adapt to unicode being enforced in latest crossbar API. Updated some documentation, to explain why there is unused definitions like SubscribedEvents or DoOnJoin.
Edouard Tisserant
parents: 1894
diff changeset
   132
            yield self.subscribe(GetCallee(name), unicode(name))
1446
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
   133
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
   134
        for func in DoOnJoin:
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
   135
            yield func(self)
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
   136
2194
35d6b9321ffd #2486 Updated wamp test project to work with the latest Crossbar version.
dporopat <denis.poropat@smarteh.si>
parents: 2193
diff changeset
   137
        print(_('WAMP session joined (%s) by:' % time.ctime()), ID)
35d6b9321ffd #2486 Updated wamp test project to work with the latest Crossbar version.
dporopat <denis.poropat@smarteh.si>
parents: 2193
diff changeset
   138
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   139
    def onLeave(self, details):
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   140
        global _WampSession, _transportFactory
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   141
        super(WampSession, self).onLeave(details)
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   142
        _WampSession = None
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   143
        _transportFactory = None
1890
916099cb8d22 non significant print syntax change. ready for py3.
Edouard Tisserant
parents: 1881
diff changeset
   144
        print(_('WAMP session left'))
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   145
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
   146
1441
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
   147
class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory):
2195
2cef75d036c2 #2486 Added option to use websocket protocol options.
dporopat <denis.poropat@smarteh.si>
parents: 2194
diff changeset
   148
    def __init__(self, config, *args, **kwargs):
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   149
        global _transportFactory
2195
2cef75d036c2 #2486 Added option to use websocket protocol options.
dporopat <denis.poropat@smarteh.si>
parents: 2194
diff changeset
   150
        WampWebSocketClientFactory.__init__(self, *args, **kwargs)
2cef75d036c2 #2486 Added option to use websocket protocol options.
dporopat <denis.poropat@smarteh.si>
parents: 2194
diff changeset
   151
2196
9017e830ae70 #2486 Added option to use websocket register options. Added function getValidOptins to read options from wampconf.json.
dporopat <denis.poropat@smarteh.si>
parents: 2195
diff changeset
   152
        protocolOptions = config.extra.get('protocolOptions', None)
2199
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   153
        if protocolOptions:
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   154
            arguments = inspect.getargspec(self.setProtocolOptions).args
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   155
            validProtocolOptions = getValidOptins(protocolOptions, arguments)
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   156
            if validProtocolOptions:
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   157
                self.setProtocolOptions(**validProtocolOptions)
8fd73c6c8aa9 #2486 Fix if protocol, register options are not present in wapmconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2198
diff changeset
   158
                #print(_("Added custom protocol options"))
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   159
        _transportFactory = self
2195
2cef75d036c2 #2486 Added option to use websocket protocol options.
dporopat <denis.poropat@smarteh.si>
parents: 2194
diff changeset
   160
2193
259df9ecf9ca #2486 Reset delay function added. It resets the delay and the retry counter on reconnect.
dporopat <denis.poropat@smarteh.si>
parents: 1955
diff changeset
   161
    def buildProtocol(self, addr):
259df9ecf9ca #2486 Reset delay function added. It resets the delay and the retry counter on reconnect.
dporopat <denis.poropat@smarteh.si>
parents: 1955
diff changeset
   162
        self.resetDelay()
259df9ecf9ca #2486 Reset delay function added. It resets the delay and the retry counter on reconnect.
dporopat <denis.poropat@smarteh.si>
parents: 1955
diff changeset
   163
        return ReconnectingClientFactory.buildProtocol(self, addr)
259df9ecf9ca #2486 Reset delay function added. It resets the delay and the retry counter on reconnect.
dporopat <denis.poropat@smarteh.si>
parents: 1955
diff changeset
   164
1441
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
   165
    def clientConnectionFailed(self, connector, reason):
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   166
        if self.continueTrying:
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   167
            print(_("WAMP Client connection failed (%s) .. retrying .." % time.ctime()))
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   168
            super(ReconnectingWampWebSocketClientFactory, self).clientConnectionFailed(connector, reason)
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   169
        else:
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   170
            del connector
1751
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1748
diff changeset
   171
1441
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
   172
    def clientConnectionLost(self, connector, reason):
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   173
        if self.continueTrying:
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   174
            print(_("WAMP Client connection lost (%s) .. retrying .." % time.ctime()))
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   175
            super(ReconnectingWampWebSocketClientFactory, self).clientConnectionFailed(connector, reason)
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   176
        else:
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   177
            del connector
1441
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
   178
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
   179
1446
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
   180
def LoadWampClientConf(wampconf):
1893
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   181
    try:
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   182
        WSClientConf = json.load(open(wampconf))
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   183
        return WSClientConf
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   184
    except ValueError, ve:
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   185
        print(_("WAMP load error: "), ve)
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   186
        return None
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   187
    except Exception:
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   188
        return None
1446
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
   189
2202
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   190
def SaveWampClientConf(wampconf, url, active):
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   191
    try:
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   192
        WSClientConf = LoadWampClientConf(wampconf)
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   193
        change = False
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   194
        if url:
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   195
            oldUrl = WSClientConf.get('url', None)
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   196
            if oldUrl != url:
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   197
                WSClientConf['url'] = url
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   198
                change = True
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   199
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   200
        oldActive = WSClientConf.get('active', False)
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   201
        if oldActive != active:
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   202
            WSClientConf['active'] = active
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   203
            change = True
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   204
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   205
        if change:
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   206
            with open(os.path.realpath(wampconf), 'w') as f:
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   207
                json.dump(WSClientConf, f)
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   208
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   209
        return WSClientConf
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   210
    except ValueError, ve:
2203
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   211
        print(_("WAMP save error: "), ve)
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   212
        return None
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   213
    except Exception, e:
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   214
        print(_("WAMP save error: "), e)
2202
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   215
        return None
1901
e8cf68d69447 Enforced check_source.sh pep8 and pylint rules.
Edouard Tisserant
parents: 1900
diff changeset
   216
1893
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   217
def LoadWampSecret(secretfname):
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   218
    try:
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   219
        WSClientWampSecret = open(secretfname, 'rb').read()
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   220
        return WSClientWampSecret
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   221
    except ValueError, ve:
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   222
        print(_("Wamp secret load error:"), ve)
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   223
        return None
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   224
    except Exception:
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   225
        return None
1446
4963e3816641 Split runtime's twisted modules import and setup to ease runtime extensions hotpatching
Edouard Tisserant
parents: 1445
diff changeset
   226
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
   227
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   228
def IsCorrectUri(uri):
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   229
    if re.match(r'w{1}s{1,2}:{1}/{2}.+:{1}[0-9]+/{1}.+', uri):
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   230
        return True
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   231
    else:
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   232
        return False
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   233
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   234
2202
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   235
def RegisterWampClient(wampconf = None, secretfname = None):
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   236
    if wampconf:
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   237
        WSClientConf = LoadWampClientConf(wampconf)
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   238
    else:
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   239
        WSClientConf = LoadWampClientConf(_WampConf)
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   240
1893
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   241
    if not WSClientConf:
1894
f224383cc883 Fixed changes from dporopat and agregorcic about WAMP CRA and config.
Edouard Tisserant
parents: 1893
diff changeset
   242
        print(_("WAMP client connection not established!"))
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   243
        return False
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   244
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   245
    if not IsCorrectUri(WSClientConf["url"]):
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   246
        print(_("WAMP url {} is not correct!".format(WSClientConf["url"])))
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   247
        return False
1893
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   248
2202
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   249
    if secretfname:
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   250
        WampSecret = LoadWampSecret(secretfname)
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   251
    else:
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   252
        WampSecret = LoadWampSecret(_WampSecret)
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   253
1900
9d1547578f55 runtime/WAMP: Fixed secret loading into configuration. Fixed secret commandline switch handling.
Edouard Tisserant
parents: 1899
diff changeset
   254
    if WampSecret is not None:
9d1547578f55 runtime/WAMP: Fixed secret loading into configuration. Fixed secret commandline switch handling.
Edouard Tisserant
parents: 1899
diff changeset
   255
        WSClientConf["secret"] = WampSecret
9d1547578f55 runtime/WAMP: Fixed secret loading into configuration. Fixed secret commandline switch handling.
Edouard Tisserant
parents: 1899
diff changeset
   256
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
   257
    # create a WAMP application session factory
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   258
    component_config = types.ComponentConfig(
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   259
        realm=WSClientConf["realm"],
1901
e8cf68d69447 Enforced check_source.sh pep8 and pylint rules.
Edouard Tisserant
parents: 1900
diff changeset
   260
        extra=WSClientConf)
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   261
    session_factory = wamp.ApplicationSessionFactory(
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   262
        config=component_config)
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   263
    session_factory.session = WampSession
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   264
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
   265
    # create a WAMP-over-WebSocket transport client factory
1441
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
   266
    transport_factory = ReconnectingWampWebSocketClientFactory(
2195
2cef75d036c2 #2486 Added option to use websocket protocol options.
dporopat <denis.poropat@smarteh.si>
parents: 2194
diff changeset
   267
        component_config,
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   268
        session_factory,
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   269
        url=WSClientConf["url"],
1893
971de876b1af WAMP runtime : changes from dporopat and agregorcic, adding CRA (-s) and changed way to load WAMP conf (-c)
Edouard Tisserant
parents: 1892
diff changeset
   270
        serializers=[MsgPackSerializer()])
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   271
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
   272
    # start the client from a Twisted endpoint
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   273
    conn = connectWS(transport_factory)
1894
f224383cc883 Fixed changes from dporopat and agregorcic about WAMP CRA and config.
Edouard Tisserant
parents: 1893
diff changeset
   274
    print(_("WAMP client connecting to :"), WSClientConf["url"])
2201
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   275
    return True # conn
4e511f5aad19 #2486 Added global TransportFactory, needed in WebInterface reconnecting function.
dporopat <denis.poropat@smarteh.si>
parents: 2199
diff changeset
   276
2203
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   277
def StopReconnectWampClient():
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   278
    _transportFactory.stopTrying()
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   279
    return _WampSession.leave()
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   280
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   281
def StartReconnectWampClient():
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   282
    if _WampSession:
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   283
        # do reconnect
2202
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   284
        _WampSession.disconnect()
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   285
        return True
2203
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   286
    elif not _WampSession:
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   287
        # do connect
2202
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   288
        RegisterWampClient()
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   289
        return True
2203
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   290
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   291
def ReconnectionWampClient(active, url):
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   292
    """ReconnectionWampClient function used for reconnecting to Crossbar router.
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   293
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   294
    Args:
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   295
        active (bool): Value in wampconf.json file. True: using Wamp connection. False: not using Wamp connection.
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   296
        url (str): Value in wampconf.json file. Url of Crossbar router.
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   297
    """
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   298
    SaveWampClientConf(_WampConf, url, active)
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   299
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   300
    if active:
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   301
        StartReconnectWampClient()
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   302
    elif not active and _WampSession:
c8a32ad27c0a #2486 Modified reconnect function. Added 'active' parameter to wampconf.json file.
dporopat <denis.poropat@smarteh.si>
parents: 2202
diff changeset
   303
        StopReconnectWampClient()
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
   304
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   305
def GetSession():
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   306
    return _WampSession
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   307
2202
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   308
def StatusWampClient():
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   309
    return _WampSession and _WampSession.is_attached()
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   310
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   311
def SetServer(pysrv, wampconf = None, wampsecret = None):
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   312
    global _PySrv, _WampConf, _WampSecret
1439
a68cd4253259 Added stub code for runtime WAMP client. Added runtime command line switch to select WAMP url and Nevow web site port. Web port is now fixed, next port number is not tested if bind fails.
Edouard Tisserant
parents:
diff changeset
   313
    _PySrv = pysrv
2202
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   314
    _WampConf = wampconf
237c1a2de1c8 #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
dporopat <denis.poropat@smarteh.si>
parents: 2201
diff changeset
   315
    _WampSecret = wampsecret