-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviss_rpc_client.py
executable file
·294 lines (226 loc) · 9.02 KB
/
viss_rpc_client.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
# (C) 2020 Jaguar Land Rover.
#
# This program is licensed under the terms and conditions of the
# Mozilla Public License, version 2.0. The full text of the
# Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
#
# Author: Magnus Feuer ([email protected])
#
import asyncio
import websockets
import json
import sys
import getopt
pending_subscription_req = {}
pending_calls = {}
subscriptions = {}
def die(msg):
print(msg)
sys.exit(255)
def usage(name):
print(f"Usage {name} ws://<host>:<port> [-s signal ...] [<function>[argument ...]]")
print("signal: Signal(s) to subscribe to.")
print("function: DSTC function")
print("argument: Function argument. Format is:")
print(" <type>:<value> Single element - int32:4711")
print(" <type>:<len>:<value> Array element - int8:32:Hello world")
print(" <type> can be: int8, uint8, int16, uint16, int32, uint32")
print(" bool, float, double, string")
print(" Variable-length string argument type has the format: string:<string>")
print(f"Example: {sys.argv[0]} -S ws://localhost:8088 -s Vehicle.DriveTrain.FuelSystem.Level -c 'print_name_and_age string:1:Bob int32:42'")
def get_next_request_id():
get_next_request_id.request_id = get_next_request_id.request_id + 1
return str(get_next_request_id.request_id)
get_next_request_id.request_id = 0
def process_call_reply(json_obj):
global pending_calls
# Sanity check reply
if not 'requestId' in json_obj:
print("Error: Got a call reply with no 'requestId' element.")
sys.exit(255)
if 'error' in json_obj and not 'reply' in json_obj:
print("Error: Got a successful call reply with no 'reply' object.")
sys.exit(255)
# Caller has verified that requestId is in json_obj
request_id = json_obj['requestId']
if not request_id in pending_calls:
print("Error: Got a call reply for request ID {request_id} that was not pending.")
sys.exit(255)
# Remove from pending calls.
pending_calls.pop(request_id)
if 'error' in json_obj:
print(f"Call error reply requestId: {request_id} error: {json_obj['error']}")
else:
print(f"Call success reply requestId: {request_id} reply: {json_obj['reply']}")
def process_subscribe_reply(json_obj):
global subscriptions
global pending_subscription_req
# Sanity check reply
if not 'requestId' in json_obj:
print("Error: Got a subscribption reply with no 'requestId' element.")
sys.exit(255)
# Caller has verified that requestId is in json_obj
request_id = json_obj['requestId']
if not request_id in pending_subscription_req:
print("Error: Got a subscription reply for request ID {request_id} that I never used in a subscription.")
# Retrieve signal associated with request id that we sent with
# subscription request.
signal = pending_subscription_req[request_id]
pending_subscription_req.pop(request_id)
if not 'subscriptionId' in json_obj:
print("Error: Missing 'subscriptionId' in subscription notification sent from server")
subscription_id = 'MISSING'
else:
subscription_id = json_obj['subscriptionId']
subscriptions[subscription_id] = signal
print(f"Subscription reply Signal: {signal} SubscriptionID: {subscription_id}")
def display_subscription(json_obj):
global subscriptions
if not 'subscriptionId' in json_obj:
print("Error: Missing 'subscriptionId' in subscription notification sent from server")
else:
subscriptionId = 'MISSING'
# Retrieve the signal path from our subscriptions dictionary.
if not json_obj['subscriptionId'] in subscriptions:
print(f"Error: received subscriptionId that we dont recognize")
subscriptionId = 'MISSING'
else:
subscriptionId = subscriptions[json_obj['subscriptionId']]
if not "value" in json_obj:
print("Error: Missing 'value' in subscription notification sent from server")
value = 'MISSING'
else:
value = json_obj['value']
if not "timestamp" in json_obj:
print("Note: Missing 'timestamp' in subscription notification sent from server")
timestamp = 'MISSING'
else:
timestamp = json_obj['timestamp']
print(f"Received subscription: subscriptionId: {subscriptionId} Value: {value}")
def display_error_response(json_obj):
print(f"Received error reply for transaction {json_obj['requestId']}: {json_obj['error']['number']}: {json_obj['error']['reason']}: {json_obj['error']['message']}")
return False
async def process_websocket(ws):
raw_json = await ws.recv()
json_obj = json.loads(raw_json)
print("\n\n\nRECEIVED FROM server:")
print(json.dumps(json_obj, indent=2, sort_keys = False))
print("-----")
if not 'action' in json_obj:
print("Error: Missing 'action' in traffic from server")
if json_obj['action'] == 'subscribe':
process_subscribe_reply(json_obj)
elif json_obj['action'] == 'subscription':
display_subscription(json_obj)
elif json_obj['action'] == 'reply':
process_call_reply(json_obj)
else:
print('Error: received unknown action')
async def subscribe_to_signal(ws, signal):
global pending_subscription_req
req_id = get_next_request_id()
sub_cmd = {
"action": "subscribe",
"requestId": req_id,
"path": signal
}
pending_subscription_req[req_id] = signal
#print("Sending Subscribe: {}\n".format(json.dumps(sub_cmd, indent=2)))
await ws.send(json.dumps(sub_cmd))
async def process_rpc_call(ws, cmd_array):
func_name = cmd_array[0]
args = cmd_array[1:]
arg_arr = []
for arg in args:
first_colon = arg.find(':')
if first_colon == -1:
print(f"Argument {arg} lacks colon")
return None
arg_type = arg[:first_colon]
if arg_type not in ["int8", "uint8", "int16", "uint16", "int32", "uint32",
"bool", "float", "double", "string" ]:
print(f"Argument {arg} has an unknown type: {arg_type}")
print("Please use one of:")
print(" int8, uint8, int16, uint16, int32, uint32")
print(" bool, float, double, string")
return None
arg_val = arg[first_colon + 1:]
# Do we have a second colon?
# int32:4:1,2,3,4
second_colon = arg_val.find(':')
arg_sz = 1
if second_colon != -1:
arg_sz = arg_val[:second_colon]
arg_val = arg_val[second_colon + 1:]
if arg_type != "string":
arg_val = arg_val.split(",")
if len(arg_val) != arg_sz:
print(f"{arg}: {arg_sz} arguments specified, but {arg_val} arguments given.")
return None
# Add naked value to argument array
arg_arr.append({
"type": arg_type,
"size": arg_sz,
"value": arg_val
})
request_id = get_next_request_id()
json_cmd = {
"action": "call", # This is a remote procedure call
"requestId": request_id,
"function": func_name,
"arguments": arg_arr
}
pending_calls[request_id] = True
print("Sending call: {}\n".format(json.dumps(json_cmd, indent=2)))
await ws.send(json.dumps(json_cmd))
async def main_loop(server, call_array, signal_sub):
async with websockets.connect(server) as ws:
for signal in signal_sub:
await subscribe_to_signal(ws, signal)
# Send a command, if we have any.
for call in call_array:
call_argv = call.split(" ")
await process_rpc_call(ws, call_argv)
# Process the reply from the command sent above
# and signals. Abort with Ctrl-c
while True:
await process_websocket(ws)
if __name__ == "__main__":
signal_sub = []
server = None
calls = []
try:
opts, args = getopt.getopt(sys.argv[1:], "S:s:c:", ["subscribe=", "server=", "call="])
except getopt.GetoptError as err:
print(f"Error parsing arguments: {err}")
usage(sys.argv[0])
sys.exit(255)
for o, a in opts:
if o == "-s":
print("Subcribing to signal {}".format(a))
signal_sub.append(a)
elif o == "-S":
print("Server {}".format(a))
server = a
elif o == "-c":
print("Call {}".format(a))
calls.append(a)
else:
assert False, "unhandled option"
if not server:
print("\nNo -S server specified.\n")
usage(sys.argv[0])
sys.exit(255)
if len(calls) == 0:
print("""\nNo call using -c 'function [arg] [...]' specified.\n""")
usage(sys.argv[0])
sys.exit(255)
if not server:
usage(sys.argv[0])
sys.exit(255)
print(f"Arguments: {args}")
try:
asyncio.get_event_loop().run_until_complete(main_loop(server, calls, signal_sub))
except KeyboardInterrupt:
pass