-
Notifications
You must be signed in to change notification settings - Fork 29
/
qimessaging-json
executable file
·154 lines (133 loc) · 5.44 KB
/
qimessaging-json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python2
import tornado
import tornadio2
import qi
import sys
import simplejson as json
import base64
URL = None
sid = 1
def is_member_of(o, mtype, name):
members = o.metaObject()[mtype]
for i in members:
if members[i]["name"] == name:
return True
return False
class QiMessagingHandler2(tornadio2.conn.SocketConnection):
class SetEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, bytearray):
return list(obj)
return json.JSONEncoder.default(self, obj)
def on_open(self, info):
global sid
self.objs = dict()
self.sid = sid
self.subs = dict()
sid = sid + 1
self.qim = qi.Session()
self.qim.connect(URL)
print("[%d] New connection from %s" % (self.sid, info.ip))
self.qim.disconnected.connect(lambda x: self.close())
def reply(self, idm, mtype, data):
try:
if qi.isinstance(data, qi.Object):
o = len(self.objs)
self.objs[o] = data
data = { "pyobject": o, "metaobject": data.metaObject() }
evt = dict(name = mtype, args = {})
if data is not None:
evt["args"]["result"] = data
if mtype != "signal":
evt["args"]["idm"] = idm
message = u'5:::%s' % (json.dumps(evt, cls=self.SetEncoder, allow_nan=False))
tornado.ioloop.IOLoop.instance().add_callback(self.session.send_message, message)
except (AttributeError, ValueError) as exc:
self.reply(idm, "error", str(exc))
def do_callback(self, service, signal, idm):
def cbk(*args):
self.reply(None, "signal",
{ "obj": service, "signal": signal, "data": args, "link": self.subs[idm] })
return cbk
def do_reply(self, idm, keepLink):
def rep(fut):
if fut.hasError():
self.reply(idm, "error", fut.error())
else:
if keepLink:
self.subs[idm] = fut.value()
self.reply(idm, "reply", fut.value())
return rep
@tornadio2.event
def call(self, idm, params):
try:
service = params["obj"]
member = params["member"]
args = params["args"]
if service == "ServiceDirectory" and member == "service":
fut = self.qim.service(str(*args), _async = True)
else:
obj = self.objs[service]
attr = getattr(obj, member)
if args != [] and args[0] == "connect" and is_member_of(obj, "signals", member):
self.subs[idm] = -1
fut = attr.connect(self.do_callback(service, member, idm), _async = True)
elif args != [] and args[0] == "disconnect" and is_member_of(obj, "signals", member):
fut = attr.disconnect(args[1], _async = True)
elif args != [] and args[0] == "value" and is_member_of(obj, "properties", member):
fut = attr.value(_async = True)
elif args != [] and args[0] == "setValue" and is_member_of(obj, "properties", member):
fut = attr.setValue(args[1], _async = True)
else:
fut = attr(*args, _async = True)
fut.addCallback(self.do_reply(idm, args != [] and args[0] == "connect"))
except (AttributeError, RuntimeError, Exception) as exc:
self.reply(idm, 'error', str(exc))
def on_close(self):
self.objs = dict()
self.subs = dict()
self.qim = None
print("[%d] Disconnected" % (self.sid))
class QiMessagingHandler1_0(QiMessagingHandler2):
class SetEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, bytearray):
return base64.b64encode(obj)
return json.JSONEncoder.default(self, obj)
@tornadio2.event
def call(self, idm, params):
try:
service = params["obj"]
method = params["method"]
args = params["args"]
if service == "ServiceDirectory" and method == "service":
fut = self.qim.service(str(args[0]), _async = True)
elif method == "registerEvent":
obj = self.objs[service]
self.subs[idm] = -1
evt = getattr(obj, args[0])
fut = evt.connect(self.do_callback(service, args[0], idm), _async = True)
elif method == "unregisterEvent":
obj = self.objs[service]
evt = getattr(obj, args[0])
fut = evt.disconnect(args[1], _async = True)
else:
obj = self.objs[service]
met = getattr(obj, method)
fut = met(*args, _async = True)
fut.addCallback(self.do_reply(idm, method == "registerEvent"))
except (AttributeError, RuntimeError) as exc:
self.reply(idm, 'error', str(exc))
if __name__ == "__main__":
if len(sys.argv) != 2:
URL = "tcp://127.0.0.1:9559"
else:
URL = sys.argv[1]
print("Will connect to " + URL)
QI_APP = qi.Application()
SOCK_APP = tornado.web.Application(
tornadio2.TornadioRouter(QiMessagingHandler1_0, namespace='1.0').urls +
tornadio2.TornadioRouter(QiMessagingHandler2, namespace='2').urls,
socket_io_port = 8002
)
tornadio2.server.SocketServer(SOCK_APP)