182 print(_("WAMP load error: "), ve) |
184 print(_("WAMP load error: "), ve) |
183 return None |
185 return None |
184 except Exception: |
186 except Exception: |
185 return None |
187 return None |
186 |
188 |
|
189 def SaveWampClientConf(wampconf, url, active): |
|
190 try: |
|
191 WSClientConf = LoadWampClientConf(wampconf) |
|
192 change = False |
|
193 if url: |
|
194 oldUrl = WSClientConf.get('url', None) |
|
195 if oldUrl != url: |
|
196 WSClientConf['url'] = url |
|
197 change = True |
|
198 |
|
199 oldActive = WSClientConf.get('active', False) |
|
200 if oldActive != active: |
|
201 WSClientConf['active'] = active |
|
202 change = True |
|
203 |
|
204 if change: |
|
205 with open(os.path.realpath(wampconf), 'w') as f: |
|
206 json.dump(WSClientConf, f) |
|
207 |
|
208 return WSClientConf |
|
209 except ValueError, ve: |
|
210 print(_("WAMP load error: "), ve) |
|
211 return None |
|
212 except Exception: |
|
213 return None |
187 |
214 |
188 def LoadWampSecret(secretfname): |
215 def LoadWampSecret(secretfname): |
189 try: |
216 try: |
190 WSClientWampSecret = open(secretfname, 'rb').read() |
217 WSClientWampSecret = open(secretfname, 'rb').read() |
191 return WSClientWampSecret |
218 return WSClientWampSecret |
201 return True |
228 return True |
202 else: |
229 else: |
203 return False |
230 return False |
204 |
231 |
205 |
232 |
206 def RegisterWampClient(wampconf, secretfname): |
233 def RegisterWampClient(wampconf = None, secretfname = None): |
207 WSClientConf = LoadWampClientConf(wampconf) |
234 if wampconf: |
|
235 WSClientConf = LoadWampClientConf(wampconf) |
|
236 else: |
|
237 WSClientConf = LoadWampClientConf(_WampConf) |
208 |
238 |
209 if not WSClientConf: |
239 if not WSClientConf: |
210 print(_("WAMP client connection not established!")) |
240 print(_("WAMP client connection not established!")) |
211 return False |
241 return False |
212 |
242 |
213 if not IsCorrectUri(WSClientConf["url"]): |
243 if not IsCorrectUri(WSClientConf["url"]): |
214 print(_("WAMP url {} is not correct!".format(WSClientConf["url"]))) |
244 print(_("WAMP url {} is not correct!".format(WSClientConf["url"]))) |
215 return False |
245 return False |
216 |
246 |
217 WampSecret = LoadWampSecret(secretfname) |
247 if secretfname: |
|
248 WampSecret = LoadWampSecret(secretfname) |
|
249 else: |
|
250 WampSecret = LoadWampSecret(_WampSecret) |
218 |
251 |
219 if WampSecret is not None: |
252 if WampSecret is not None: |
220 WSClientConf["secret"] = WampSecret |
253 WSClientConf["secret"] = WampSecret |
221 |
254 |
222 # create a WAMP application session factory |
255 # create a WAMP application session factory |
238 conn = connectWS(transport_factory) |
271 conn = connectWS(transport_factory) |
239 print(_("WAMP client connecting to :"), WSClientConf["url"]) |
272 print(_("WAMP client connecting to :"), WSClientConf["url"]) |
240 return True # conn |
273 return True # conn |
241 |
274 |
242 |
275 |
243 def GetTransportFactory(): |
276 def ReconnectWampClient(active, url): |
244 global _transportFactory |
277 SaveWampClientConf(_WampConf, url, active) |
245 return _transportFactory |
278 |
246 |
279 if not active and _WampSession: |
|
280 # crossbar connection active is off, retry connection off |
|
281 _transportFactory.stopTrying() |
|
282 return _WampSession.leave() |
|
283 elif _WampSession and active: |
|
284 # do reconnecting |
|
285 _WampSession.disconnect() |
|
286 return True |
|
287 elif not _WampSession and active: |
|
288 # crossbar connection active is on, do connect |
|
289 RegisterWampClient() |
|
290 return True |
|
291 else: |
|
292 return False |
247 |
293 |
248 def GetSession(): |
294 def GetSession(): |
249 global _WampSession |
|
250 return _WampSession |
295 return _WampSession |
251 |
296 |
252 |
297 def StatusWampClient(): |
253 def SetServer(pysrv): |
298 return _WampSession and _WampSession.is_attached() |
254 global _PySrv |
299 |
|
300 def SetServer(pysrv, wampconf = None, wampsecret = None): |
|
301 global _PySrv, _WampConf, _WampSecret |
255 _PySrv = pysrv |
302 _PySrv = pysrv |
|
303 _WampConf = wampconf |
|
304 _WampSecret = wampsecret |