-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter_zmq.py
125 lines (112 loc) · 4.29 KB
/
adapter_zmq.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
import enum
import traceback
import zmq
from domain import Direction, Map, Material, Object, Player, Position
NoneType = type(None)
_COMMAND_TYPES = {
'activate': (str, NoneType),
'get_world_map': (NoneType, Map),
'join': (str, Player),
'leave': (str, NoneType),
'move': (Player, NoneType),
}
_EVENT_TYPES = {
'player': Player,
'player_left': str,
'world_map': Map,
}
def serialize(Type, v):
assert isinstance(v, Type), "{} is not a {}".format(v, Type)
if Type in [float, int, NoneType, str]:
return v
elif issubclass(Type, enum.Enum):
return v.name
elif Type in [Direction, Position]:
return [serialize(float, v.x), serialize(float, v.y)]
elif Type == Map:
return dict(materials=[serialize(Material, m) for m in v.materials],
objects=[[serialize(Object, o), serialize(Position, p)] for o, p in v.objects],
width=serialize(int, v.width))
elif Type == Player:
return dict(name=serialize(str, v.name),
position=serialize(Position, v.position),
forward=serialize(Direction, v.forward))
else:
raise Exception("Cannot serialize object of type {}".format(Type))
def deserialize(Type, v):
if Type in [float, int, NoneType, str]:
result = v
elif issubclass(Type, enum.Enum):
result = Type[v]
elif Type in [Direction, Position]:
x, y = v
result = Type(x=deserialize(float, x),
y=deserialize(float, y))
elif Type == Map:
result = Map(materials=[deserialize(Material, m) for m in v['materials']],
objects=[(deserialize(Object, o), deserialize(Position, p)) for o, p in v['objects']],
width=deserialize(int, v['width']))
elif Type == Player:
result = Player(name=deserialize(str, v['name']),
position=deserialize(Position, v['position']),
forward=deserialize(Direction, v['forward']))
else:
raise Exception("Cannot deserialize object of type {}".format(Type))
assert isinstance(result, Type), "{} is not a {}".format(v, Type)
return result
class ServerConnection:
def __init__(self, host, port):
self.__context = zmq.Context()
self.__commands = self.__context.socket(zmq.REQ)
self.__commands.connect("tcp://{}:{}".format(host, port))
self.__events = self.__context.socket(zmq.SUB)
self.__events.connect("tcp://{}:{}".format(host, port + 1))
self.__events.setsockopt_string(zmq.SUBSCRIBE, "")
def call(self, command_name, input_data=None):
InputType, OutputType = _COMMAND_TYPES[command_name]
input_json = {'command': command_name,
'input': serialize(InputType, input_data)}
self.__commands.send_json(input_json)
output_json = self.__commands.recv_json()
assert output_json['success']
output_data = deserialize(OutputType, output_json['output'])
return output_data
def poll_events(self):
while True:
try:
event_json = self.__events.recv_json(flags=zmq.NOBLOCK)
event_name = event_json['event']
EventType = _EVENT_TYPES[event_name]
event_data = deserialize(EventType, event_json['data'])
yield event_name, event_data
except zmq.ZMQError as e:
if e.errno == zmq.EAGAIN:
break
else:
raise
class Server:
def __init__(self, port):
self.__context = zmq.Context()
self.__commands = self.__context.socket(zmq.REP)
self.__commands.bind("tcp://*:{}".format(port))
self.__events = self.__context.socket(zmq.PUB)
self.__events.bind("tcp://*:{}".format(port + 1))
def serve(self, command_fn):
while True:
input_json = self.__commands.recv_json()
command_name = input_json['command']
InputType, OutputType = _COMMAND_TYPES[command_name]
input_data = deserialize(InputType, input_json['input'])
try:
output_data = command_fn(command_name, input_data)
output_json = {'success': True,
'output': serialize(OutputType, output_data)}
except:
traceback.print_exc()
output_json = {'success': False}
self.__commands.send_json(output_json)
def emit_event(self, event_name, event_data):
EventType = _EVENT_TYPES[event_name]
event_json = {'event': event_name,
'data': serialize(EventType, event_data)}
self.__events.send_json(event_json)