runtime/WampClient.py
changeset 1441 826730e60407
parent 1440 e8daabf2c438
child 1443 ff8a22d45c44
equal deleted inserted replaced
1440:e8daabf2c438 1441:826730e60407
     6 from autobahn.twisted import wamp
     6 from autobahn.twisted import wamp
     7 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS
     7 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS
     8 from twisted.internet.defer import inlineCallbacks
     8 from twisted.internet.defer import inlineCallbacks
     9 from autobahn.wamp import types
     9 from autobahn.wamp import types
    10 from autobahn.wamp.serializer import MsgPackSerializer
    10 from autobahn.wamp.serializer import MsgPackSerializer
       
    11 from twisted.internet.protocol import ReconnectingClientFactory
    11 import json
    12 import json
    12 
    13 
    13 _WampSession = None
    14 _WampSession = None
    14 _PySrv = None
    15 _PySrv = None
    15 
    16 
    21                 "MatchMD5",
    22                 "MatchMD5",
    22                 "SetTraceVariablesList",
    23                 "SetTraceVariablesList",
    23                 "GetTraceVariables",
    24                 "GetTraceVariables",
    24                 "RemoteExec",
    25                 "RemoteExec",
    25                 "GetLogMessage",
    26                 "GetLogMessage",
       
    27                 "ResetLogCount",
    26                 ]
    28                 ]
    27 
    29 
    28 def MakeCallee(name):
    30 def MakeCallee(name):
    29     global _PySrv
    31     global _PySrv
    30     def Callee(*args,**kwargs):
    32     def Callee(*args,**kwargs):
    45     def onLeave(self, details):
    47     def onLeave(self, details):
    46         global _WampSession
    48         global _WampSession
    47         _WampSession = None
    49         _WampSession = None
    48         print 'WAMP session left'
    50         print 'WAMP session left'
    49 
    51 
       
    52 class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory):
       
    53     def clientConnectionFailed(self, connector, reason):
       
    54         print("WAMP Client connection failed .. retrying ..")
       
    55         self.retry(connector)
       
    56     def clientConnectionLost(self, connector, reason):
       
    57         print("WAMP Client connection lost .. retrying ..")
       
    58         self.retry(connector)
       
    59 
    50 def RegisterWampClient(wampconf):
    60 def RegisterWampClient(wampconf):
    51 
    61 
    52     WSClientConf = json.load(open(wampconf))
    62     WSClientConf = json.load(open(wampconf))
    53 
    63 
    54     ## start logging to console
    64     ## start logging to console
    61     session_factory = wamp.ApplicationSessionFactory(
    71     session_factory = wamp.ApplicationSessionFactory(
    62         config = component_config)
    72         config = component_config)
    63     session_factory.session = WampSession
    73     session_factory.session = WampSession
    64 
    74 
    65     # create a WAMP-over-WebSocket transport client factory
    75     # create a WAMP-over-WebSocket transport client factory
    66     transport_factory = WampWebSocketClientFactory(
    76     transport_factory = ReconnectingWampWebSocketClientFactory(
    67         session_factory,
    77         session_factory,
    68         url = WSClientConf["url"],
    78         url = WSClientConf["url"],
    69         serializers = [MsgPackSerializer()],
    79         serializers = [MsgPackSerializer()],
    70         debug = False,
    80         debug = False,
    71         debug_wamp = False)
    81         debug_wamp = False)