forked from hstern/pynmsg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nmsg_message.pyx
380 lines (311 loc) · 13.4 KB
/
nmsg_message.pyx
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
cdef class message(object):
cdef msgmod _mod
cdef nmsg_message_t _instance
cdef bool changed
cdef readonly int vid
cdef readonly int msgtype
cdef public long time_sec
cdef public int time_nsec
cdef public long source
cdef public object operator
cdef public object group
cdef public bool has_source
cdef public bool has_operator
cdef public bool has_group
cdef readonly object fields
cdef readonly object field_types
cdef readonly object field_names
def __cinit__(self):
self._instance = NULL
self._mod = None
self.vid = 0
self.msgtype = 0
self.has_source = False
self.has_operator = False
self.has_group = False
self.changed = False
def __init__(self, unsigned vid, unsigned msgtype):
self.vid = vid
self.msgtype = msgtype
self._mod = msgmod(self.vid, self.msgtype)
self._instance = nmsg_message_init(self._mod._instance)
if self._instance == NULL:
raise Exception, 'nmsg_message_init() failed'
self.load_message()
def __dealloc__(self):
if self._instance != NULL:
nmsg_message_destroy(&self._instance)
cdef reinit(self):
if self._mod == None:
self._mod = msgmod(self.vid, self.msgtype)
if self._instance == NULL:
self._instance = nmsg_message_init(self._mod._instance)
if self._instance == NULL:
raise Exception, 'nmsg_message_init() failed'
self.changed = True
cdef set_instance(self, nmsg_message_t instance):
cdef char *a
cdef timespec ts
cdef uint32_t u
if self._instance != NULL:
nmsg_message_destroy(&self._instance)
self._instance = instance
self.vid = nmsg_message_get_vid(instance)
self.msgtype = nmsg_message_get_msgtype(instance)
nmsg_message_get_time(instance, &ts)
self.time_sec = ts.tv_sec
self.time_nsec = ts.tv_nsec
u = nmsg_message_get_source(instance)
if u != 0:
self.has_source = True
self.source = u
u = nmsg_message_get_operator(instance)
if u != 0:
self.has_operator = True
a = nmsg_alias_by_key(nmsg_alias_operator, u)
if a != NULL:
self.operator = a
else:
self.operator = '<UNKNOWN>'
else:
self.operator = None
u = nmsg_message_get_group(instance)
if u != 0:
self.has_group = True
a = nmsg_alias_by_key(nmsg_alias_group, u)
if a != NULL:
self.group = a
else:
self.group = '<UNKNOWN>'
else:
self.group = None
self.load_message()
cdef sync_fields(self):
cdef timespec ts
cdef unsigned u
if self._instance == NULL:
raise Exception, 'message not initialized'
ts.tv_sec = self.time_sec
ts.tv_nsec = self.time_nsec
nmsg_message_set_time(self._instance, &ts)
if self.has_source:
nmsg_message_set_source(self._instance, self.source)
else:
nmsg_message_set_source(self._instance, 0)
if self.has_operator:
u = nmsg_alias_by_value(nmsg_alias_operator, PyString_AsString(self.operator))
nmsg_message_set_operator(self._instance, u)
else:
nmsg_message_set_operator(self._instance, 0)
if self.has_group:
u = nmsg_alias_by_value(nmsg_alias_group, PyString_AsString(self.group))
nmsg_message_set_group(self._instance, u)
else:
nmsg_message_set_group(self._instance, 0)
cdef load_message(self):
cdef nmsg_res res
cdef size_t n_fields
cdef char *field_name
cdef nmsg_msgmod_field_type field_type
cdef unsigned field_flags
cdef unsigned val_enum
cdef char *str_enum
cdef int val_bool
cdef uint32_t val_uint32
cdef uint64_t val_uint64
cdef int32_t val_int32
cdef int64_t val_int64
cdef double val_double
cdef uint8_t *data
cdef size_t data_len
self.fields = {}
self.field_types = {}
self.field_names = set()
res = nmsg_message_get_num_fields(self._instance, &n_fields)
if res != nmsg_res_success:
raise Exception, 'nmsg_message_get_num_fields() failed'
for field_idx from 0 <= field_idx < n_fields:
res = nmsg_message_get_field_name(self._instance, field_idx, &field_name)
if res != nmsg_res_success:
raise Exception, 'nmsg_message_get_field_name() failed'
self.field_names.add(field_name)
res = nmsg_message_get_field_type_by_idx(self._instance, field_idx, &field_type)
if res != nmsg_res_success:
raise Exception, 'nmsg_message_get_field_type_by_idx() failed'
if res != nmsg_res_success:
raise Exception, 'nmsg_message_get_num_field_values_by_idx() failed'
val_list = []
val_idx = 0
while True:
res = nmsg_message_get_field_by_idx(self._instance, field_idx, val_idx, <void **> &data, &data_len)
if res != nmsg_res_success:
break
val_idx += 1
if field_type == nmsg_msgmod_ft_enum:
val_enum = (<unsigned *> data)[0]
res = nmsg_message_enum_value_to_name_by_idx(self._instance, field_idx, val_enum, &str_enum)
if res == nmsg_res_success:
s = PyString_FromStringAndSize(str_enum, strlen(str_enum))
val_list.append(s)
else:
val_list.append(val_enum)
elif field_type == nmsg_msgmod_ft_bytes:
s = PyString_FromStringAndSize(<char *> data, data_len)
val_list.append(s)
elif field_type == nmsg_msgmod_ft_string or \
field_type == nmsg_msgmod_ft_mlstring:
if data_len > 0 and data[data_len - 1] == '\x00':
data_len -= 1
s = PyString_FromStringAndSize(<char *> data, data_len)
val_list.append(s)
elif field_type == nmsg_msgmod_ft_ip:
ip = PyString_FromStringAndSize(<char *> data, data_len)
if data_len == 4:
sip = socket.inet_ntop(socket.AF_INET, ip)
elif data_len == 16:
sip = socket.inet_ntop(socket.AF_INET6, ip)
val_list.append(sip)
elif field_type == nmsg_msgmod_ft_uint16 or \
field_type == nmsg_msgmod_ft_uint32:
val_uint32 = (<uint32_t *> data)[0]
val_list.append(val_uint32)
elif field_type == nmsg_msgmod_ft_uint64:
val_uint64 = (<uint64_t *> data)[0]
val_list.append(val_uint64)
elif field_type == nmsg_msgmod_ft_int16 or \
field_type == nmsg_msgmod_ft_int32:
val_int32 = (<int32_t *> data)[0]
val_list.append(val_int32)
elif field_type == nmsg_msgmod_ft_int64:
val_int64 = (<int64_t *> data)[0]
val_list.append(val_int64)
elif field_type == nmsg_msgmod_ft_double:
val_double = (<double *> data)[0]
val_list.append(val_double)
elif field_type == nmsg_msgmod_ft_bool:
val_bool = (<int *> data)[0]
val_list.append(val_bool)
res = nmsg_message_get_field_flags_by_idx(self._instance, field_idx, &field_flags)
if res != nmsg_res_success:
raise Exception, 'nmsg_message_get_field_flags_by_idx() failed'
if len(val_list) > 0:
if field_flags & NMSG_MSGMOD_FIELD_REPEATED:
self.fields[field_name] = val_list
else:
self.fields[field_name] = val_list[0]
self.field_types[field_name] = field_type
cdef sync_message(self):
cdef nmsg_res res
cdef nmsg_msgmod_field_type field_type
cdef unsigned val_enum
cdef char *str_enum
cdef int val_bool
cdef uint16_t val_uint16
cdef uint32_t val_uint32
cdef uint64_t val_uint64
cdef int16_t val_int16
cdef int32_t val_int32
cdef int64_t val_int64
cdef double val_double
cdef char *val_buf
cdef Py_ssize_t val_buf_len
cdef uint8_t *data
cdef size_t data_len
if self._instance == NULL:
self.reinit()
for field_name in self.fields:
field_type = self.field_types[field_name]
if type(self.fields[field_name]) == list:
fields = self.fields[field_name]
else:
fields = [ self.fields[field_name] ]
for i in range(0, len(fields)):
if field_type == nmsg_msgmod_ft_enum:
if type(fields[i]) == int:
val_enum = fields[i]
data = <uint8_t *> &val_enum
data_len = sizeof(val_enum)
elif type(fields[i]) == str:
res = nmsg_message_enum_name_to_value(self._instance, field_name, fields[i], &val_enum)
if res != nmsg_res_success:
raise Exception, 'unknown enum value for field_name=%s: %s' % (field_name, fields[i])
data = <uint8_t *> &val_enum
data_len = sizeof(val_enum)
else:
raise Exception, 'unhandled python enum type: %s' % type(fields[i])
elif field_type == nmsg_msgmod_ft_bytes:
PyString_AsStringAndSize(fields[i], &val_buf, &val_buf_len)
data = <uint8_t *> val_buf
data_len = val_buf_len
elif field_type == nmsg_msgmod_ft_string or \
field_type == nmsg_msgmod_ft_mlstring:
tmp_string = fields[i] + '\x00'
PyString_AsStringAndSize(tmp_string, &val_buf, &val_buf_len)
data = <uint8_t *> val_buf
data_len = val_buf_len
elif field_type == nmsg_msgmod_ft_ip:
try:
ip = socket.inet_pton(socket.AF_INET, fields[i])
except:
ip = socket.inet_pton(socket.AF_INET6, fields[i])
PyString_AsStringAndSize(ip, &val_buf, &val_buf_len)
data = <uint8_t *> val_buf
data_len = val_buf_len
elif field_type == nmsg_msgmod_ft_uint16:
val_uint16 = fields[i]
data = <uint8_t *> &val_uint16
data_len = sizeof(val_uint16)
elif field_type == nmsg_msgmod_ft_int16:
val_int16 = fields[i]
data = <uint8_t *> &val_int16
data_len = sizeof(val_int16)
elif field_type == nmsg_msgmod_ft_uint32:
val_uint32 = fields[i]
data = <uint8_t *> &val_uint32
data_len = sizeof(val_uint32)
elif field_type == nmsg_msgmod_ft_int32:
val_int32 = fields[i]
data = <uint8_t *> &val_int32
data_len = sizeof(val_int32)
elif field_type == nmsg_msgmod_ft_uint64:
val_uint64 = fields[i]
data = <uint8_t *> &val_uint64
data_len = sizeof(val_uint64)
elif field_type == nmsg_msgmod_ft_int64:
val_int64 = fields[i]
data = <uint8_t *> &val_int64
data_len = sizeof(val_int64)
elif field_type == nmsg_msgmod_ft_double:
val_double = fields[i]
data = <uint8_t *> &val_double
data_len = sizeof(val_double)
elif field_type == nmsg_msgmod_ft_bool:
val_bool = fields[i]
data = <uint8_t *> &val_bool
data_len = sizeof(int)
else:
raise Exception, 'unknown field_type'
res = nmsg_message_set_field(self._instance, field_name, i, data, data_len)
if res != nmsg_res_success:
raise Exception, 'nmsg_message_set_field() failed'
self.changed = False
def __contains__(self, key):
return key in self.fields
def __getitem__(self, key):
return self.fields[key]
def __setitem__(self, key, value):
if key in self.field_names:
self.fields[key] = value
self.changed = True
else:
raise KeyError(key)
def __repr__(self):
return repr(self.fields)
def keys(self):
return self.fields.keys()
cdef class _recv_message(message):
def __init__(self):
pass
cdef class _meta_message(message):
def __init__(self):
message.__init__(self, self.__vid, self.__msgtype)