runtime/WampClient.py
author Edouard Tisserant
Sun, 08 Feb 2015 22:39:17 +0100
changeset 1441 826730e60407
parent 1440 e8daabf2c438
child 1443 ff8a22d45c44
permissions -rw-r--r--
Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
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
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
     4
import sys
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
     5
#from twisted.python import log
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
     6
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
     7
from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
     8
from twisted.internet.defer import 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
     9
from autobahn.wamp import types
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    10
from autobahn.wamp.serializer import MsgPackSerializer
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
    11
from twisted.internet.protocol import ReconnectingClientFactory
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
    12
import json
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
    13
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
    14
_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
    15
_PySrv = 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
    16
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    17
ExposedCalls = ["StartPLC",
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    18
                "StopPLC",
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    19
                "ForceReload",
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    20
                "GetPLCstatus",
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    21
                "NewPLC",
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    22
                "MatchMD5",
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    23
                "SetTraceVariablesList",
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    24
                "GetTraceVariables",
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    25
                "RemoteExec",
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    26
                "GetLogMessage",
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
    27
                "ResetLogCount",
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    28
                ]
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    29
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    30
def MakeCallee(name):
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    31
    global _PySrv
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    32
    def Callee(*args,**kwargs):
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    33
        return getattr(_PySrv.plcobj, name)(*args,**kwargs)
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    34
    return Callee
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    35
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    36
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
    37
class WampSession(wamp.ApplicationSession):
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    38
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    39
    @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
    40
    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
    41
        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
    42
        _WampSession = self
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
    43
        print 'WAMP session joined by :', self.config.extra["ID"]
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    44
        for name in ExposedCalls:
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    45
            reg = yield self.register(MakeCallee(name), name)
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
    46
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
    47
    def onLeave(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
    48
        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
    49
        _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
    50
        print 'WAMP session left'
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
    51
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
    52
class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory):
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
    53
    def clientConnectionFailed(self, connector, reason):
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
    54
        print("WAMP Client connection failed .. retrying ..")
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
    55
        self.retry(connector)
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
    56
    def clientConnectionLost(self, connector, reason):
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
    57
        print("WAMP Client connection lost .. retrying ..")
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
    58
        self.retry(connector)
826730e60407 Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents: 1440
diff changeset
    59
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
    60
def RegisterWampClient(wampconf):
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
    61
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
    62
    WSClientConf = json.load(open(wampconf))
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
    63
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    64
    ## start logging to console
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    65
    # log.startLogging(sys.stdout)
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
    66
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    67
    # 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
    68
    component_config = types.ComponentConfig(
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
    69
        realm = WSClientConf["realm"],
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
    70
        extra = {"ID":WSClientConf["ID"]})
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
    71
    session_factory = wamp.ApplicationSessionFactory(
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
    72
        config = component_config)
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
    73
    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
    74
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    75
    # 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
    76
    transport_factory = ReconnectingWampWebSocketClientFactory(
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
    77
        session_factory,
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
    78
        url = WSClientConf["url"],
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    79
        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
    80
        debug = False,
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
    81
        debug_wamp = False)
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
    82
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    83
    # 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
    84
    conn = connectWS(transport_factory)
1440
e8daabf2c438 Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents: 1439
diff changeset
    85
    print "WAMP client connecting to :",WSClientConf["url"]
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
    86
    return conn
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
    87
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
    88
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
    89
    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
    90
    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
    91
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
    92
def SetServer(pysrv):
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
    93
    global _PySrv
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
    94
    _PySrv = pysrv
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
    95