24 |
24 |
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 inspect |
30 import inspect |
30 import re |
31 import re |
31 from autobahn.twisted import wamp |
32 from autobahn.twisted import wamp |
32 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
33 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
33 from autobahn.wamp import types, auth |
34 from autobahn.wamp import types, auth |
205 with open(os.path.realpath(wampconf), 'w') as f: |
206 with open(os.path.realpath(wampconf), 'w') as f: |
206 json.dump(WSClientConf, f) |
207 json.dump(WSClientConf, f) |
207 |
208 |
208 return WSClientConf |
209 return WSClientConf |
209 except ValueError, ve: |
210 except ValueError, ve: |
210 print(_("WAMP load error: "), ve) |
211 print(_("WAMP save error: "), ve) |
211 return None |
212 return None |
212 except Exception: |
213 except Exception, e: |
|
214 print(_("WAMP save error: "), e) |
213 return None |
215 return None |
214 |
216 |
215 def LoadWampSecret(secretfname): |
217 def LoadWampSecret(secretfname): |
216 try: |
218 try: |
217 WSClientWampSecret = open(secretfname, 'rb').read() |
219 WSClientWampSecret = open(secretfname, 'rb').read() |
270 # start the client from a Twisted endpoint |
272 # start the client from a Twisted endpoint |
271 conn = connectWS(transport_factory) |
273 conn = connectWS(transport_factory) |
272 print(_("WAMP client connecting to :"), WSClientConf["url"]) |
274 print(_("WAMP client connecting to :"), WSClientConf["url"]) |
273 return True # conn |
275 return True # conn |
274 |
276 |
275 |
277 def StopReconnectWampClient(): |
276 def ReconnectWampClient(active, url): |
278 _transportFactory.stopTrying() |
277 SaveWampClientConf(_WampConf, url, active) |
279 return _WampSession.leave() |
278 |
280 |
279 if not active and _WampSession: |
281 def StartReconnectWampClient(): |
280 # crossbar connection active is off, retry connection off |
282 if _WampSession: |
281 _transportFactory.stopTrying() |
283 # do reconnect |
282 return _WampSession.leave() |
|
283 elif _WampSession and active: |
|
284 # do reconnecting |
|
285 _WampSession.disconnect() |
284 _WampSession.disconnect() |
286 return True |
285 return True |
287 elif not _WampSession and active: |
286 elif not _WampSession: |
288 # crossbar connection active is on, do connect |
287 # do connect |
289 RegisterWampClient() |
288 RegisterWampClient() |
290 return True |
289 return True |
291 else: |
290 |
292 return False |
291 def ReconnectionWampClient(active, url): |
|
292 """ReconnectionWampClient function used for reconnecting to Crossbar router. |
|
293 |
|
294 Args: |
|
295 active (bool): Value in wampconf.json file. True: using Wamp connection. False: not using Wamp connection. |
|
296 url (str): Value in wampconf.json file. Url of Crossbar router. |
|
297 """ |
|
298 SaveWampClientConf(_WampConf, url, active) |
|
299 |
|
300 if active: |
|
301 StartReconnectWampClient() |
|
302 elif not active and _WampSession: |
|
303 StopReconnectWampClient() |
293 |
304 |
294 def GetSession(): |
305 def GetSession(): |
295 return _WampSession |
306 return _WampSession |
296 |
307 |
297 def StatusWampClient(): |
308 def StatusWampClient(): |