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 inspect |
29 import inspect |
|
30 import re |
30 from autobahn.twisted import wamp |
31 from autobahn.twisted import wamp |
31 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
32 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
32 from autobahn.wamp import types, auth |
33 from autobahn.wamp import types, auth |
33 from autobahn.wamp.serializer import MsgPackSerializer |
34 from autobahn.wamp.serializer import MsgPackSerializer |
34 from twisted.internet.defer import inlineCallbacks |
35 from twisted.internet.defer import inlineCallbacks |
35 from twisted.internet.protocol import ReconnectingClientFactory |
36 from twisted.internet.protocol import ReconnectingClientFactory |
36 |
37 |
37 |
38 |
|
39 _transportFactory = None |
38 _WampSession = None |
40 _WampSession = None |
39 _PySrv = None |
41 _PySrv = None |
40 |
42 |
41 ExposedCalls = [ |
43 ExposedCalls = [ |
42 "StartPLC", |
44 "StartPLC", |
130 yield func(self) |
132 yield func(self) |
131 |
133 |
132 print(_('WAMP session joined (%s) by:' % time.ctime()), ID) |
134 print(_('WAMP session joined (%s) by:' % time.ctime()), ID) |
133 |
135 |
134 def onLeave(self, details): |
136 def onLeave(self, details): |
135 global _WampSession |
137 global _WampSession, _transportFactory |
|
138 super(WampSession, self).onLeave(details) |
136 _WampSession = None |
139 _WampSession = None |
|
140 _transportFactory = None |
137 print(_('WAMP session left')) |
141 print(_('WAMP session left')) |
138 |
142 |
139 |
143 |
140 class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory): |
144 class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory): |
141 def __init__(self, config, *args, **kwargs): |
145 def __init__(self, config, *args, **kwargs): |
|
146 global _transportFactory |
142 WampWebSocketClientFactory.__init__(self, *args, **kwargs) |
147 WampWebSocketClientFactory.__init__(self, *args, **kwargs) |
143 |
148 |
144 protocolOptions = config.extra.get('protocolOptions', None) |
149 protocolOptions = config.extra.get('protocolOptions', None) |
145 if protocolOptions: |
150 if protocolOptions: |
146 arguments = inspect.getargspec(self.setProtocolOptions).args |
151 arguments = inspect.getargspec(self.setProtocolOptions).args |
147 validProtocolOptions = getValidOptins(protocolOptions, arguments) |
152 validProtocolOptions = getValidOptins(protocolOptions, arguments) |
148 if validProtocolOptions: |
153 if validProtocolOptions: |
149 self.setProtocolOptions(**validProtocolOptions) |
154 self.setProtocolOptions(**validProtocolOptions) |
150 #print(_("Added custom protocol options")) |
155 #print(_("Added custom protocol options")) |
|
156 _transportFactory = self |
151 |
157 |
152 def buildProtocol(self, addr): |
158 def buildProtocol(self, addr): |
153 self.resetDelay() |
159 self.resetDelay() |
154 return ReconnectingClientFactory.buildProtocol(self, addr) |
160 return ReconnectingClientFactory.buildProtocol(self, addr) |
155 |
161 |
156 def clientConnectionFailed(self, connector, reason): |
162 def clientConnectionFailed(self, connector, reason): |
157 print(_("WAMP Client connection failed (%s) .. retrying .." % time.ctime())) |
163 if self.continueTrying: |
158 ReconnectingClientFactory.clientConnectionFailed(self, connector, reason) |
164 print(_("WAMP Client connection failed (%s) .. retrying .." % time.ctime())) |
|
165 super(ReconnectingWampWebSocketClientFactory, self).clientConnectionFailed(connector, reason) |
|
166 else: |
|
167 del connector |
159 |
168 |
160 def clientConnectionLost(self, connector, reason): |
169 def clientConnectionLost(self, connector, reason): |
161 print(_("WAMP Client connection lost (%s) .. retrying .." % time.ctime())) |
170 if self.continueTrying: |
162 ReconnectingClientFactory.clientConnectionFailed(self, connector, reason) |
171 print(_("WAMP Client connection lost (%s) .. retrying .." % time.ctime())) |
|
172 super(ReconnectingWampWebSocketClientFactory, self).clientConnectionFailed(connector, reason) |
|
173 else: |
|
174 del connector |
163 |
175 |
164 |
176 |
165 def LoadWampClientConf(wampconf): |
177 def LoadWampClientConf(wampconf): |
166 try: |
178 try: |
167 WSClientConf = json.load(open(wampconf)) |
179 WSClientConf = json.load(open(wampconf)) |
182 return None |
194 return None |
183 except Exception: |
195 except Exception: |
184 return None |
196 return None |
185 |
197 |
186 |
198 |
|
199 def IsCorrectUri(uri): |
|
200 if re.match(r'w{1}s{1,2}:{1}/{2}.+:{1}[0-9]+/{1}.+', uri): |
|
201 return True |
|
202 else: |
|
203 return False |
|
204 |
|
205 |
187 def RegisterWampClient(wampconf, secretfname): |
206 def RegisterWampClient(wampconf, secretfname): |
188 |
|
189 WSClientConf = LoadWampClientConf(wampconf) |
207 WSClientConf = LoadWampClientConf(wampconf) |
190 |
208 |
191 if not WSClientConf: |
209 if not WSClientConf: |
192 print(_("WAMP client connection not established!")) |
210 print(_("WAMP client connection not established!")) |
193 return |
211 return False |
|
212 |
|
213 if not IsCorrectUri(WSClientConf["url"]): |
|
214 print(_("WAMP url {} is not correct!".format(WSClientConf["url"]))) |
|
215 return False |
194 |
216 |
195 WampSecret = LoadWampSecret(secretfname) |
217 WampSecret = LoadWampSecret(secretfname) |
196 |
218 |
197 if WampSecret is not None: |
219 if WampSecret is not None: |
198 WSClientConf["secret"] = WampSecret |
220 WSClientConf["secret"] = WampSecret |