Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/python3
|
||||
"""协议故障与半帧 fixture;不代替 SDK 验收。"""
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import struct
|
||||
import sys
|
||||
|
||||
def read():
|
||||
size = struct.unpack('>I', sys.stdin.buffer.read(4))[0]
|
||||
return json.loads(sys.stdin.buffer.read(size))
|
||||
|
||||
def write(value):
|
||||
body = json.dumps(value).encode()
|
||||
sys.stdout.buffer.write(struct.pack('>I', len(body)) + body)
|
||||
sys.stdout.buffer.flush()
|
||||
|
||||
hello = read()
|
||||
root = pathlib.Path(hello.pop('setting')['directory'])
|
||||
hello['kind'] = 'ready'
|
||||
write(hello)
|
||||
remaining = None
|
||||
while True:
|
||||
frame = read()
|
||||
kind = frame['kind']
|
||||
if kind == 'health':
|
||||
write(frame)
|
||||
elif kind == 'shutdown':
|
||||
(root / 'shutdown').touch()
|
||||
write(frame)
|
||||
break
|
||||
elif kind == 'cancel':
|
||||
(root / 'cancelled').touch()
|
||||
if remaining is not None:
|
||||
sys.stdout.buffer.write(remaining)
|
||||
sys.stdout.buffer.flush()
|
||||
remaining = None
|
||||
else:
|
||||
write(dict(kind='error', id=frame['id'], error='dever.cancelled', payload=None))
|
||||
elif kind == 'call':
|
||||
operation = frame['operation']
|
||||
if operation in ('hang', 'partial'):
|
||||
(root / 'started').touch()
|
||||
if operation == 'partial':
|
||||
body = json.dumps(dict(kind='result', id=frame['id'], payload={})).encode()
|
||||
sys.stdout.buffer.write(struct.pack('>I', len(body)) + body[:5])
|
||||
sys.stdout.buffer.flush()
|
||||
remaining = body[5:]
|
||||
elif operation == 'invalid':
|
||||
write(dict(kind='result', id=frame['id'] + 1, payload={}))
|
||||
else:
|
||||
write(dict(kind='result', id=frame['id'], payload=dict(value=frame['payload']['value'], pid=os.getpid())))
|
||||
Reference in new issue
Block a user