-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessaging.py
348 lines (261 loc) · 10.3 KB
/
messaging.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# coding: utf-8
import struct
import re
import json
import hashlib
import textwrap
import zlib
from typing import *
"""
Messaging serialization useful e.g. in combination with zeromq sockets
There are three main concepts:
1. Declarative definition of messages, i.e.
@register(b'range-request') # header of message
class RangeRequest(Message): # messages must be subclasses of Message
start_idx = '<I' # message-fields, consisting label and struct.pack/unpack format
count = '<I'
2. Registering of a new Message
Registers a message in the messaging system by looking at the message declaration and building appropriate
serializing and deserializing structures (Message._type and Message._fields).
These structures are class variables and thus do not impact each message instance. Hence, the run-time overhead is
negligible (neɡlɪdʒəb(ə)l).
3. (de-)serialization
serialize(msg: Message) turns a Message into a list, where each message-field is automatically serialized depending
on the given format string:
'json': json.dumps(field[idx]).encode('utf-8')
'blob': field[idx]
'<I': struct.pack('<I', field[idx])
Additionally, the Ellipsis object (...) can be used as a placeholder for any additional elements (in the form of bytes).
deserialize(msg) turns a list into a Message, where each element the concrete message subtype is determined using
the first element in the list and the remaining fields are deserialized according to the format string in the
message-fields:
'json': json.loads(msg[idx].decode('utf-8'))
'blob': msg[idx]
'<I': struct.unpack('<I', msg[idx])
"""
MSGTYPE_HASH_LENGTH = 4
message_ctors = dict()
class MessageError(Exception):
""" Base class for errors in messaging system """
pass
class UnknownMessageError(MessageError):
""" Error if message type is unknown """
pass
def constructor(type_hash):
return message_ctors[type_hash]
def deserialize_message(msg):
type_hash = msg[:MSGTYPE_HASH_LENGTH]
try:
ctor = message_ctors[type_hash] # TODO: Eigenen Error-Type für unbekannte Messages
msg = ctor(msg)
return msg
except KeyError:
raise UnknownMessageError(f'Unknown message-type hash: {type_hash}.') from None
def get_type_hash(msg_raw):
return msg_raw[:MSGTYPE_HASH_LENGTH]
def register(msgtype_hash=None, compress=False):
def do_register(cls):
fields = dict()
names = list(enumerate(filter(lambda f: not f.startswith('_'), cls.__dict__.keys())))
def to_field(fmt):
if fmt == 'json':
return JsonField()
elif fmt == 'blob':
return BlobField()
elif fmt == 'utf-8':
return Utf8Field()
elif fmt == 'boolean':
return BooleanField()
elif isinstance(fmt, dict):
return DictField({field_name: to_field(field_fmt) for field_name, field_fmt in fmt.items()})
elif isinstance(fmt, str):
return StructField(fmt)
elif isinstance(fmt, list):
assert len(fmt) == 1
return ListField(to_field(fmt[0]))
elif Message in fmt.__bases__:
return MessageField(fmt)
for i, field_name in names:
fmt = cls.__dict__[field_name]
field = to_field(fmt)
fields[field_name] = field
delattr(cls, field_name)
cls._fields = fields
# create messsagetype-hash from message-definition
if msgtype_hash is not None:
if not isinstance(msgtype_hash, bytes) or len(msgtype_hash) != MSGTYPE_HASH_LENGTH:
raise MessageError(f'Messagetype-Hash must be bytes of length {MSGTYPE_HASH_LENGTH}.')
typehash = msgtype_hash
else:
clsdefinition = cls.definition().encode('utf-8')
typehash = hashlib.blake2b(clsdefinition, digest_size=MSGTYPE_HASH_LENGTH).digest()
if typehash in message_ctors:
raise MessageError(f'Hash collision in message definitions. Messagetype-Hash {typehash} is already defined by another message ({message_ctors[typehash].__name__}).')
cls._typehash = typehash
cls._compress = compress
message_ctors[cls._typehash] = cls
return cls
return do_register
class Message:
_typehash = None
_fields = None
_compress = False
def __init__(self, payload):
self.payload = payload
self._cache = None
def __getattr__(self, item):
if self._cache is None:
self._cache = dict()
buffer = self.payload[MSGTYPE_HASH_LENGTH:]
if self._compress:
buffer = zlib.decompress(buffer)
self.deserialize(buffer)
return self._cache[item]
def deserialize(self, buffer):
length = len(buffer)
offset = 0
for field_name, field in self._fields.items():
value, offset = field.deserialize(buffer, offset)
self._cache[field_name] = value
assert offset == length
@classmethod
def serialize(cls, **kwargs):
msg = b''
for field_name in cls._fields:
value = kwargs[field_name]
field = cls._fields[field_name]
msg += field.serialize(value)
if cls._compress:
msg = zlib.compress(msg)
return cls._typehash + msg
@classmethod
def definition(cls):
header = f'class {cls.__name__}(Message):'
body = []
for field_name, field in cls._fields.items():
body.append(f'{field_name} = {field}')
body = '\n'.join(body)
cls_def = f'{header}\n{textwrap.indent(body, " ")}'
return cls_def
def __str__(self):
max_field_len = 20
fields = []
for field_name in self._fields.keys():
value = str(getattr(self, field_name))
fields.append(f'{field_name}={value}')
fields = ', '.join(fields)
return f'<{self.__class__.__name__} {fields}>'
class UnknownMessage:
def __init__(self, msg):
self.msg = msg
class Field:
def serialize(self, value) -> bytes:
raise NotImplementedError
def deserialize(self, buffer: bytes, offset: int) -> (object, int):
raise NotImplementedError
def __str__(self):
raise NotImplementedError
class BooleanField(Field):
def serialize(self, value):
return struct.pack('<?', value)
def deserialize(self, buffer, offset):
return struct.unpack_from('<?', buffer, offset)[0], offset + 1
def __str__(self):
return "'boolean'"
class StructField(Field):
def __init__(self, fmt):
self.fmt = fmt
self.nitems = len(re.sub("[0-9@=<>!]", "", fmt))
self.size = struct.calcsize(self.fmt)
def serialize(self, values):
if self.nitems > 1:
return struct.pack(self.fmt, *values)
return struct.pack(self.fmt, values)
def deserialize(self, buffer, offset):
data = struct.unpack_from(self.fmt, buffer, offset)
if self.nitems == 1:
data = data[0]
return data, offset+self.size
def __str__(self):
return f"'{self.fmt}'"
class BlobField(Field):
def serialize(self, value):
return struct.pack('<I', len(value)) + value
def deserialize(self, buffer, offset):
sz = struct.calcsize('<I')
length = struct.unpack_from('<I', buffer, offset)[0]
return buffer[offset+sz:offset+sz+length], offset+sz+length
def __str__(self):
return "'blob'"
class Utf8Field(Field):
def serialize(self, value: str):
return struct.pack('<I', len(value)) + value.encode('utf-8')
def deserialize(self, buffer, offset):
sz = struct.calcsize('<I')
length = struct.unpack_from('<I', buffer, offset)[0]
return buffer[offset+sz:offset+sz+length].decode('utf-8'), offset+sz+length
def __str__(self):
return "'utf-8'"
class JsonField(Field):
def serialize(self, value):
dumped = json.dumps(value).encode('utf-8')
return struct.pack('<I', len(dumped)) + dumped
def deserialize(self, buffer, offset):
sz = struct.calcsize('<I')
length = struct.unpack_from('<I', buffer, offset)[0]
json_data = buffer[offset+sz:offset+sz+length]
return json.loads(bytes(json_data)), offset+sz+length
def __str__(self):
return "'json'"
class DictField(Field):
def __init__(self, nested: Dict[str, Field]):
self.nested = nested
def serialize(self, value) -> bytes:
msg = b''
for field_name, field in self.nested.items():
msg += field.serialize(value[field_name])
return msg
def deserialize(self, buffer, offset):
obj = {}
for field_name, field in self.nested.items():
value, offset = field.deserialize(buffer, offset)
obj[field_name] = value
return obj, offset
def __str__(self):
items = []
for field_name, field in self.nested.items():
items.append(f"'{field_name}': {field},")
items = '\n'.join(items)
return f'{{\n{textwrap.indent(items, " ")}\n}}'
class ListField(Field):
def __init__(self, elem_field: Field):
self.elem_field = elem_field
def serialize(self, values):
msg = struct.pack('<I', len(values))
for value in values:
msg += self.elem_field.serialize(value)
return msg
def deserialize(self, buffer, offset):
sz = struct.calcsize('<I')
count = struct.unpack_from('<I', buffer, offset)[0]
offset = offset+sz
values = []
for i in range(count):
value, offset = self.elem_field.deserialize(buffer, offset)
values.append(value)
return values, offset
def __str__(self):
return f'[{self.elem_field}]'
class MessageField(Field):
def __init__(self, msg_cls):
self.msg_cls = msg_cls
def serialize(self, value):
data = value
return struct.pack('<I', len(data)) + data
def deserialize(self, buffer, offset):
sz = struct.calcsize('<I')
length = struct.unpack_from('<I', buffer, offset)[0]
msg = self.msg_cls(buffer[offset+sz:offset+sz+length])
return msg, offset+sz+length
def __str__(self):
return self.msg_cls.__name__