20 # You should have received a copy of the GNU Lesser General Public |
20 # You should have received a copy of the GNU Lesser General Public |
21 # License along with this library; if not, write to the Free Software |
21 # License along with this library; if not, write to the Free Software |
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 |
23 |
24 import sys |
24 import sys |
25 #from twisted.python import log |
|
26 from autobahn.twisted import wamp |
25 from autobahn.twisted import wamp |
27 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
26 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
28 from twisted.internet.defer import inlineCallbacks |
27 from twisted.internet.defer import inlineCallbacks |
29 from autobahn.wamp import types |
28 from autobahn.wamp import types |
30 from autobahn.wamp.serializer import MsgPackSerializer |
29 from autobahn.wamp.serializer import MsgPackSerializer |
49 |
48 |
50 SubscribedEvents = [] |
49 SubscribedEvents = [] |
51 |
50 |
52 DoOnJoin = [] |
51 DoOnJoin = [] |
53 |
52 |
|
53 |
54 def GetCallee(name): |
54 def GetCallee(name): |
55 """ Get Callee or Subscriber corresponding to '.' spearated object path """ |
55 """ Get Callee or Subscriber corresponding to '.' spearated object path """ |
56 global _PySrv |
56 global _PySrv |
57 names = name.split('.') |
57 names = name.split('.') |
58 obj = _PySrv.plcobj |
58 obj = _PySrv.plcobj |
59 while names: obj = getattr(obj, names.pop(0)) |
59 while names: |
|
60 obj = getattr(obj, names.pop(0)) |
60 return obj |
61 return obj |
|
62 |
61 |
63 |
62 class WampSession(wamp.ApplicationSession): |
64 class WampSession(wamp.ApplicationSession): |
63 |
65 |
64 @inlineCallbacks |
66 @inlineCallbacks |
65 def onJoin(self, details): |
67 def onJoin(self, details): |
66 global _WampSession |
68 global _WampSession |
67 _WampSession = self |
69 _WampSession = self |
68 ID = self.config.extra["ID"] |
70 ID = self.config.extra["ID"] |
69 print 'WAMP session joined by :', ID |
71 print 'WAMP session joined by :', ID |
70 for name in ExposedCalls: |
72 for name in ExposedCalls: |
71 reg = yield self.register(GetCallee(name), '.'.join((ID,name))) |
73 reg = yield self.register(GetCallee(name), '.'.join((ID, name))) |
72 |
74 |
73 for name in SubscribedEvents: |
75 for name in SubscribedEvents: |
74 reg = yield self.subscribe(GetCallee(name), name) |
76 reg = yield self.subscribe(GetCallee(name), name) |
75 |
77 |
76 for func in DoOnJoin: |
78 for func in DoOnJoin: |
79 def onLeave(self, details): |
81 def onLeave(self, details): |
80 global _WampSession |
82 global _WampSession |
81 _WampSession = None |
83 _WampSession = None |
82 print 'WAMP session left' |
84 print 'WAMP session left' |
83 |
85 |
|
86 |
84 class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory): |
87 class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory): |
85 def clientConnectionFailed(self, connector, reason): |
88 def clientConnectionFailed(self, connector, reason): |
86 print("WAMP Client connection failed .. retrying ..") |
89 print("WAMP Client connection failed .. retrying ..") |
87 self.retry(connector) |
90 self.retry(connector) |
|
91 |
88 def clientConnectionLost(self, connector, reason): |
92 def clientConnectionLost(self, connector, reason): |
89 print("WAMP Client connection lost .. retrying ..") |
93 print("WAMP Client connection lost .. retrying ..") |
90 self.retry(connector) |
94 self.retry(connector) |
|
95 |
91 |
96 |
92 def LoadWampClientConf(wampconf): |
97 def LoadWampClientConf(wampconf): |
93 |
98 |
94 WSClientConf = json.load(open(wampconf)) |
99 WSClientConf = json.load(open(wampconf)) |
95 return WSClientConf |
100 return WSClientConf |
96 |
101 |
|
102 |
97 def RegisterWampClient(wampconf): |
103 def RegisterWampClient(wampconf): |
98 |
104 |
99 WSClientConf = LoadWampClientConf(wampconf) |
105 WSClientConf = LoadWampClientConf(wampconf) |
100 |
106 |
101 ## start logging to console |
107 # start logging to console |
102 # log.startLogging(sys.stdout) |
108 # log.startLogging(sys.stdout) |
103 |
109 |
104 # create a WAMP application session factory |
110 # create a WAMP application session factory |
105 component_config = types.ComponentConfig( |
111 component_config = types.ComponentConfig( |
106 realm = WSClientConf["realm"], |
112 realm=WSClientConf["realm"], |
107 extra = {"ID":WSClientConf["ID"]}) |
113 extra={"ID": WSClientConf["ID"]}) |
108 session_factory = wamp.ApplicationSessionFactory( |
114 session_factory = wamp.ApplicationSessionFactory( |
109 config = component_config) |
115 config=component_config) |
110 session_factory.session = WampSession |
116 session_factory.session = WampSession |
111 |
117 |
112 # create a WAMP-over-WebSocket transport client factory |
118 # create a WAMP-over-WebSocket transport client factory |
113 transport_factory = ReconnectingWampWebSocketClientFactory( |
119 transport_factory = ReconnectingWampWebSocketClientFactory( |
114 session_factory, |
120 session_factory, |
115 url = WSClientConf["url"], |
121 url=WSClientConf["url"], |
116 serializers = [MsgPackSerializer()], |
122 serializers=[MsgPackSerializer()], |
117 debug = False, |
123 debug=False, |
118 debug_wamp = False) |
124 debug_wamp=False) |
119 |
125 |
120 # start the client from a Twisted endpoint |
126 # start the client from a Twisted endpoint |
121 conn = connectWS(transport_factory) |
127 conn = connectWS(transport_factory) |
122 print "WAMP client connecting to :",WSClientConf["url"] |
128 print "WAMP client connecting to :", WSClientConf["url"] |
123 return conn |
129 return conn |
|
130 |
124 |
131 |
125 def GetSession(): |
132 def GetSession(): |
126 global _WampSession |
133 global _WampSession |
127 return _WampSession |
134 return _WampSession |
128 |
135 |
|
136 |
129 def SetServer(pysrv): |
137 def SetServer(pysrv): |
130 global _PySrv |
138 global _PySrv |
131 _PySrv = pysrv |
139 _PySrv = pysrv |
132 |
|