25 from __future__ import absolute_import |
25 from __future__ import absolute_import |
26 from __future__ import print_function |
26 from __future__ import print_function |
27 import time |
27 import time |
28 import json |
28 import json |
29 import os |
29 import os |
30 import inspect |
|
31 import re |
30 import re |
32 from autobahn.twisted import wamp |
31 from autobahn.twisted import wamp |
33 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
32 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
34 from autobahn.wamp import types, auth |
33 from autobahn.wamp import types, auth |
35 from autobahn.wamp.serializer import MsgPackSerializer |
34 from autobahn.wamp.serializer import MsgPackSerializer |
135 class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory): |
134 class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory): |
136 def __init__(self, config, *args, **kwargs): |
135 def __init__(self, config, *args, **kwargs): |
137 global _transportFactory |
136 global _transportFactory |
138 WampWebSocketClientFactory.__init__(self, *args, **kwargs) |
137 WampWebSocketClientFactory.__init__(self, *args, **kwargs) |
139 |
138 |
140 protocolOptions = config.extra.get('protocolOptions', None) |
139 try: |
141 if protocolOptions: |
140 protocolOptions = config.extra.get('protocolOptions', None) |
142 arguments = inspect.getargspec(self.setProtocolOptions).args |
141 if protocolOptions: |
143 validProtocolOptions = getValidOptins(protocolOptions, arguments) |
142 self.setProtocolOptions(**protocolOptions) |
144 if validProtocolOptions: |
143 _transportFactory = self |
145 self.setProtocolOptions(**validProtocolOptions) |
144 except Exception, e: |
146 #print(_("Added custom protocol options")) |
145 print(_("Custom protocol options failed :"), e) |
147 _transportFactory = self |
146 _transportFactory = None |
148 |
147 |
149 def buildProtocol(self, addr): |
148 def buildProtocol(self, addr): |
150 self.resetDelay() |
149 self.resetDelay() |
151 return ReconnectingClientFactory.buildProtocol(self, addr) |
150 return ReconnectingClientFactory.buildProtocol(self, addr) |
152 |
151 |
197 |
196 |
198 if saveChanges: |
197 if saveChanges: |
199 with open(os.path.realpath(_WampConf), 'w') as f: |
198 with open(os.path.realpath(_WampConf), 'w') as f: |
200 json.dump(WSClientConf, f, sort_keys=True, indent=4) |
199 json.dump(WSClientConf, f, sort_keys=True, indent=4) |
201 if 'active' in WSClientConf and WSClientConf['active']: |
200 if 'active' in WSClientConf and WSClientConf['active']: |
|
201 if _transportFactory and _WampSession: |
|
202 StopReconnectWampClient() |
202 StartReconnectWampClient() |
203 StartReconnectWampClient() |
203 else: |
204 else: |
204 StopReconnectWampClient() |
205 StopReconnectWampClient() |
205 |
206 |
206 return WSClientConf |
207 return WSClientConf |
260 session_factory = wamp.ApplicationSessionFactory( |
261 session_factory = wamp.ApplicationSessionFactory( |
261 config=component_config) |
262 config=component_config) |
262 session_factory.session = WampSession |
263 session_factory.session = WampSession |
263 |
264 |
264 # create a WAMP-over-WebSocket transport client factory |
265 # create a WAMP-over-WebSocket transport client factory |
265 transport_factory = ReconnectingWampWebSocketClientFactory( |
266 ReconnectingWampWebSocketClientFactory( |
266 component_config, |
267 component_config, |
267 session_factory, |
268 session_factory, |
268 url=WSClientConf["url"], |
269 url=WSClientConf["url"], |
269 serializers=[MsgPackSerializer()]) |
270 serializers=[MsgPackSerializer()]) |
270 |
271 |
271 # start the client from a Twisted endpoint |
272 # start the client from a Twisted endpoint |
272 conn = connectWS(transport_factory) |
273 if _transportFactory: |
273 print(_("WAMP client connecting to :"), WSClientConf["url"]) |
274 conn = connectWS(_transportFactory) |
274 return True |
275 print(_("WAMP client connecting to :"), WSClientConf["url"]) |
|
276 return True |
|
277 else: |
|
278 print(_("WAMP client can not connect to :"), WSClientConf["url"]) |
|
279 return False |
275 |
280 |
276 |
281 |
277 def StopReconnectWampClient(): |
282 def StopReconnectWampClient(): |
278 _transportFactory.stopTrying() |
283 _transportFactory.stopTrying() |
279 return _WampSession.leave() |
284 return _WampSession.leave() |