53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
#!/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())))
|