runtime/WampClient.py
branch#2486
changeset 2204 25dafeb98b55
parent 2203 c8a32ad27c0a
child 2205 4c74218b42e1
equal deleted inserted replaced
2203:c8a32ad27c0a 2204:25dafeb98b55
   175             super(ReconnectingWampWebSocketClientFactory, self).clientConnectionFailed(connector, reason)
   175             super(ReconnectingWampWebSocketClientFactory, self).clientConnectionFailed(connector, reason)
   176         else:
   176         else:
   177             del connector
   177             del connector
   178 
   178 
   179 
   179 
   180 def LoadWampClientConf(wampconf):
   180 def LoadWampClientConf(items=None):
   181     try:
   181     try:
   182         WSClientConf = json.load(open(wampconf))
   182         WSClientConf = json.load(open(_WampConf))
       
   183         if items and isinstance(items, list):
       
   184             WSClientConfItems = {}
       
   185             for item in items:
       
   186                 wampconf_value = WSClientConf.get(item, None)
       
   187                 if wampconf_value is not None:
       
   188                     WSClientConfItems[item] = wampconf_value
       
   189             if WSClientConfItems:
       
   190                 return WSClientConfItems
   183         return WSClientConf
   191         return WSClientConf
   184     except ValueError, ve:
   192     except ValueError, ve:
   185         print(_("WAMP load error: "), ve)
   193         print(_("WAMP load error: "), ve)
   186         return None
   194         return None
   187     except Exception:
   195     except Exception, e:
   188         return None
   196         print(_("WAMP load error: "), e)
   189 
   197         return None
   190 def SaveWampClientConf(wampconf, url, active):
   198 
       
   199 def SaveWampClientConf(items):
   191     try:
   200     try:
   192         WSClientConf = LoadWampClientConf(wampconf)
   201         WSClientConf = LoadWampClientConf()
   193         change = False
   202         saveChanges = False
   194         if url:
   203         if items:
   195             oldUrl = WSClientConf.get('url', None)
   204             for itemKey in items.keys():
   196             if oldUrl != url:
   205                 wampconf_value = WSClientConf.get(itemKey, None)
   197                 WSClientConf['url'] = url
   206                 if (wampconf_value is not None) and (items[itemKey] is not None) and (wampconf_value != items[itemKey]):
   198                 change = True
   207                     WSClientConf[itemKey] = items[itemKey]
   199 
   208                     saveChanges = True
   200         oldActive = WSClientConf.get('active', False)
   209 
   201         if oldActive != active:
   210         if saveChanges:
   202             WSClientConf['active'] = active
   211             with open(os.path.realpath(_WampConf), 'w') as f:
   203             change = True
   212                 json.dump(WSClientConf, f, sort_keys=True, indent=4)
   204 
   213             if 'active' in WSClientConf and WSClientConf['active']:
   205         if change:
   214                 StartReconnectWampClient()
   206             with open(os.path.realpath(wampconf), 'w') as f:
   215             else:
   207                 json.dump(WSClientConf, f)
   216                 StopReconnectWampClient()
   208 
   217 
   209         return WSClientConf
   218         return WSClientConf
   210     except ValueError, ve:
   219     except ValueError, ve:
   211         print(_("WAMP save error: "), ve)
   220         print(_("WAMP save error: "), ve)
   212         return None
   221         return None
   230         return True
   239         return True
   231     else:
   240     else:
   232         return False
   241         return False
   233 
   242 
   234 
   243 
   235 def RegisterWampClient(wampconf = None, secretfname = None):
   244 def RegisterWampClient(wampconf=None, secretfname=None):
       
   245     global _WampConf
   236     if wampconf:
   246     if wampconf:
   237         WSClientConf = LoadWampClientConf(wampconf)
   247         _WampConf = wampconf
   238     else:
   248         WSClientConf = LoadWampClientConf()
   239         WSClientConf = LoadWampClientConf(_WampConf)
   249     else:
       
   250         WSClientConf = LoadWampClientConf()
   240 
   251 
   241     if not WSClientConf:
   252     if not WSClientConf:
   242         print(_("WAMP client connection not established!"))
   253         print(_("WAMP client connection not established!"))
   243         return False
   254         return False
   244 
   255 
   270         serializers=[MsgPackSerializer()])
   281         serializers=[MsgPackSerializer()])
   271 
   282 
   272     # start the client from a Twisted endpoint
   283     # start the client from a Twisted endpoint
   273     conn = connectWS(transport_factory)
   284     conn = connectWS(transport_factory)
   274     print(_("WAMP client connecting to :"), WSClientConf["url"])
   285     print(_("WAMP client connecting to :"), WSClientConf["url"])
   275     return True # conn
   286     return True
       
   287 
   276 
   288 
   277 def StopReconnectWampClient():
   289 def StopReconnectWampClient():
   278     _transportFactory.stopTrying()
   290     _transportFactory.stopTrying()
   279     return _WampSession.leave()
   291     return _WampSession.leave()
       
   292 
   280 
   293 
   281 def StartReconnectWampClient():
   294 def StartReconnectWampClient():
   282     if _WampSession:
   295     if _WampSession:
   283         # do reconnect
   296         # do reconnect
   284         _WampSession.disconnect()
   297         _WampSession.disconnect()
   285         return True
   298         return True
   286     elif not _WampSession:
   299     else:
   287         # do connect
   300         # do connect
   288         RegisterWampClient()
   301         RegisterWampClient()
   289         return True
   302         return True
   290 
   303 
   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()
       
   304 
   304 
   305 def GetSession():
   305 def GetSession():
   306     return _WampSession
   306     return _WampSession
   307 
   307 
       
   308 
   308 def StatusWampClient():
   309 def StatusWampClient():
   309     return _WampSession and _WampSession.is_attached()
   310     return _WampSession and _WampSession.is_attached()
   310 
   311 
   311 def SetServer(pysrv, wampconf = None, wampsecret = None):
   312 
       
   313 def SetServer(pysrv, wampconf=None, wampsecret=None):
   312     global _PySrv, _WampConf, _WampSecret
   314     global _PySrv, _WampConf, _WampSecret
   313     _PySrv = pysrv
   315     _PySrv = pysrv
   314     _WampConf = wampconf
   316     _WampConf = wampconf
   315     _WampSecret = wampsecret
   317     _WampSecret = wampsecret