runtime/WampClient.py
changeset 1439 a68cd4253259
child 1440 e8daabf2c438
equal deleted inserted replaced
1438:19ebe96b41c0 1439:a68cd4253259
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 import sys
       
     5 from twisted.python import log
       
     6 
       
     7 from twisted.internet import reactor, ssl
       
     8 from autobahn.twisted import wamp
       
     9 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS
       
    10 from autobahn.wamp import types
       
    11 import json
       
    12 
       
    13 _WampSession = None
       
    14 _PySrv = None
       
    15 
       
    16 class WampSession(wamp.ApplicationSession):
       
    17     def onJoin(self, details):
       
    18         global _WampSession
       
    19         _WampSession = self
       
    20         print 'WAMP session joined by :', self.config.extra["ID"]
       
    21 
       
    22     def onLeave(self, details):
       
    23         global _WampSession
       
    24         _WampSession = None
       
    25         print 'WAMP session left'
       
    26 
       
    27 
       
    28 def RegisterWampClient(wampconf):
       
    29 
       
    30     WSClientConf = json.load(open(wampconf))
       
    31 
       
    32     ## TODO log to PLC console instead
       
    33     ## 0) start logging to console
       
    34     log.startLogging(sys.stdout)
       
    35 
       
    36     ## 1) create a WAMP application session factory
       
    37     component_config = types.ComponentConfig(
       
    38         realm = WSClientConf["realm"],
       
    39         extra = {"ID":WSClientConf["ID"]})
       
    40     session_factory = wamp.ApplicationSessionFactory(
       
    41         config = component_config)
       
    42     session_factory.session = WampSession
       
    43 
       
    44     ## TODO select optimum serializer for passing session lists
       
    45     ## optional: use specific set of serializers
       
    46     #from autobahn.wamp.serializer import *
       
    47     #serializers = []
       
    48     ##serializers.append(JsonSerializer(batched = True))
       
    49     ##serializers.append(MsgPackSerializer(batched = True))
       
    50     #serializers.append(JsonSerializer())
       
    51     ##serializers.append(MsgPackSerializer())
       
    52     serializers = None
       
    53 
       
    54     ## 2) create a WAMP-over-WebSocket transport client factory
       
    55     transport_factory = WampWebSocketClientFactory(
       
    56         session_factory,
       
    57         url = WSClientConf["url"],
       
    58         serializers = serializers,
       
    59         debug = False,
       
    60         debug_wamp = False)
       
    61 
       
    62     ## 3) start the client from a Twisted endpoint
       
    63     conn = connectWS(transport_factory)
       
    64     print "WAMP clien connecting to :",WSClientConf["url"]
       
    65     return conn
       
    66 
       
    67 def GetSession():
       
    68     global _WampSession
       
    69     return _WampSession
       
    70 
       
    71 def SetServer(pysrv):
       
    72     global _PySrv
       
    73     _PySrv = pysrv
       
    74