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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <qpid/dispatch/python_embedded.h>
#include <qpid/dispatch/threading.h>
#include <qpid/dispatch/log.h>
#include <qpid/dispatch/amqp.h>
#include <qpid/dispatch/alloc.h>
//===============================================================================
// Control Functions
//===============================================================================
static uint32_t ref_count = 0;
static sys_mutex_t *lock = 0;
static char *log_module = "PYTHON";
static void dx_python_setup();
void dx_python_initialize()
{
lock = sys_mutex();
}
void dx_python_finalize()
{
assert(ref_count == 0);
sys_mutex_free(lock);
}
void dx_python_start()
{
sys_mutex_lock(lock);
if (ref_count == 0) {
Py_Initialize();
dx_python_setup();
dx_log(log_module, LOG_TRACE, "Embedded Python Interpreter Initialized");
}
ref_count++;
sys_mutex_unlock(lock);
}
void dx_python_stop()
{
sys_mutex_lock(lock);
ref_count--;
if (ref_count == 0) {
Py_Finalize();
dx_log(log_module, LOG_TRACE, "Embedded Python Interpreter Shut Down");
}
sys_mutex_unlock(lock);
}
//===============================================================================
// Data Conversion Functions
//===============================================================================
static PyObject *parsed_to_py_string(dx_parsed_field_t *field)
{
switch (dx_parse_tag(field)) {
case DX_AMQP_VBIN8:
case DX_AMQP_VBIN32:
case DX_AMQP_STR8_UTF8:
case DX_AMQP_STR32_UTF8:
case DX_AMQP_SYM8:
case DX_AMQP_SYM32:
break;
default:
return Py_None;
}
#define SHORT_BUF 1024
uint8_t short_buf[SHORT_BUF];
PyObject *result;
dx_field_iterator_t *raw = dx_parse_raw(field);
dx_field_iterator_reset(raw);
uint32_t length = dx_field_iterator_remaining(raw);
uint8_t *buffer = short_buf;
uint8_t *ptr;
int alloc = 0;
if (length > SHORT_BUF) {
alloc = 1;
buffer = (uint8_t*) malloc(length);
}
ptr = buffer;
while (!dx_field_iterator_end(raw))
*(ptr++) = dx_field_iterator_octet(raw);
result = PyString_FromStringAndSize((char*) buffer, ptr - buffer);
if (alloc)
free(buffer);
return result;
}
void dx_py_to_composed(PyObject *value, dx_composed_field_t *field)
{
if (PyBool_Check(value))
dx_compose_insert_bool(field, PyInt_AS_LONG(value) ? 1 : 0);
//else if (PyFloat_Check(value))
// dx_compose_insert_double(field, PyFloat_AS_DOUBLE(value));
else if (PyInt_Check(value))
dx_compose_insert_long(field, (int64_t) PyInt_AS_LONG(value));
else if (PyLong_Check(value))
dx_compose_insert_long(field, (int64_t) PyLong_AsLongLong(value));
else if (PyString_Check(value))
dx_compose_insert_string(field, PyString_AS_STRING(value));
else if (PyDict_Check(value)) {
Py_ssize_t iter = 0;
PyObject *key;
PyObject *val;
dx_compose_start_map(field);
while (PyDict_Next(value, &iter, &key, &val)) {
dx_py_to_composed(key, field);
dx_py_to_composed(val, field);
}
dx_compose_end_map(field);
}
else if (PyList_Check(value)) {
Py_ssize_t count = PyList_Size(value);
dx_compose_start_list(field);
for (Py_ssize_t idx = 0; idx < count; idx++) {
PyObject *item = PyList_GetItem(value, idx);
dx_py_to_composed(item, field);
}
dx_compose_end_list(field);
}
else if (PyTuple_Check(value)) {
Py_ssize_t count = PyTuple_Size(value);
dx_compose_start_list(field);
for (Py_ssize_t idx = 0; idx < count; idx++) {
PyObject *item = PyTuple_GetItem(value, idx);
dx_py_to_composed(item, field);
}
dx_compose_end_list(field);
}
}
PyObject *dx_field_to_py(dx_parsed_field_t *field)
{
PyObject *result = Py_None;
uint8_t tag = dx_parse_tag(field);
switch (tag) {
case DX_AMQP_NULL:
result = Py_None;
break;
case DX_AMQP_BOOLEAN:
case DX_AMQP_TRUE:
case DX_AMQP_FALSE:
result = dx_parse_as_uint(field) ? Py_True : Py_False;
break;
case DX_AMQP_UBYTE:
case DX_AMQP_USHORT:
case DX_AMQP_UINT:
case DX_AMQP_SMALLUINT:
case DX_AMQP_UINT0:
result = PyInt_FromLong((long) dx_parse_as_uint(field));
break;
case DX_AMQP_ULONG:
case DX_AMQP_SMALLULONG:
case DX_AMQP_ULONG0:
case DX_AMQP_TIMESTAMP:
result = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) dx_parse_as_ulong(field));
break;
case DX_AMQP_BYTE:
case DX_AMQP_SHORT:
case DX_AMQP_INT:
case DX_AMQP_SMALLINT:
result = PyInt_FromLong((long) dx_parse_as_int(field));
break;
case DX_AMQP_LONG:
case DX_AMQP_SMALLLONG:
result = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) dx_parse_as_long(field));
break;
case DX_AMQP_FLOAT:
case DX_AMQP_DOUBLE:
case DX_AMQP_DECIMAL32:
case DX_AMQP_DECIMAL64:
case DX_AMQP_DECIMAL128:
case DX_AMQP_UTF32:
case DX_AMQP_UUID:
break;
case DX_AMQP_VBIN8:
case DX_AMQP_VBIN32:
case DX_AMQP_STR8_UTF8:
case DX_AMQP_STR32_UTF8:
case DX_AMQP_SYM8:
case DX_AMQP_SYM32:
result = parsed_to_py_string(field);
break;
case DX_AMQP_LIST0:
case DX_AMQP_LIST8:
case DX_AMQP_LIST32: {
uint32_t count = dx_parse_sub_count(field);
result = PyList_New(count);
for (uint32_t idx = 0; idx < count; idx++) {
dx_parsed_field_t *sub = dx_parse_sub_value(field, idx);
PyObject *pysub = dx_field_to_py(sub);
if (pysub == 0)
return 0;
PyList_SetItem(result, idx, pysub);
}
break;
}
case DX_AMQP_MAP8:
case DX_AMQP_MAP32: {
uint32_t count = dx_parse_sub_count(field);
result = PyDict_New();
for (uint32_t idx = 0; idx < count; idx++) {
dx_parsed_field_t *key = dx_parse_sub_key(field, idx);
dx_parsed_field_t *val = dx_parse_sub_value(field, idx);
PyObject *pykey = parsed_to_py_string(key);
PyObject *pyval = dx_field_to_py(val);
if (pyval == 0)
return 0;
PyDict_SetItem(result, pykey, pyval);
Py_DECREF(pykey);
Py_DECREF(pyval);
}
break;
}
case DX_AMQP_ARRAY8:
case DX_AMQP_ARRAY32:
break;
}
return result;
}
//===============================================================================
// Logging Object
//===============================================================================
typedef struct {
PyObject_HEAD
PyObject *module_name;
} LogAdapter;
static int LogAdapter_init(LogAdapter *self, PyObject *args, PyObject *kwds)
{
const char *text;
if (!PyArg_ParseTuple(args, "s", &text))
return -1;
self->module_name = PyString_FromString(text);
return 0;
}
static void LogAdapter_dealloc(LogAdapter* self)
{
Py_XDECREF(self->module_name);
self->ob_type->tp_free((PyObject*)self);
}
static PyObject* dx_python_log(PyObject *self, PyObject *args)
{
int level;
const char* text;
if (!PyArg_ParseTuple(args, "is", &level, &text))
return 0;
LogAdapter *self_ptr = (LogAdapter*) self;
char *logmod = PyString_AS_STRING(self_ptr->module_name);
dx_log(logmod, level, text);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef LogAdapter_methods[] = {
{"log", dx_python_log, METH_VARARGS, "Emit a Log Line"},
{0, 0, 0, 0}
};
static PyMethodDef empty_methods[] = {
{0, 0, 0, 0}
};
static PyTypeObject LogAdapterType = {
PyObject_HEAD_INIT(0)
0, /* ob_size*/
"dispatch.LogAdapter", /* tp_name*/
sizeof(LogAdapter), /* tp_basicsize*/
0, /* tp_itemsize*/
(destructor)LogAdapter_dealloc, /* tp_dealloc*/
0, /* tp_print*/
0, /* tp_getattr*/
0, /* tp_setattr*/
0, /* tp_compare*/
0, /* tp_repr*/
0, /* tp_as_number*/
0, /* tp_as_sequence*/
0, /* tp_as_mapping*/
0, /* tp_hash */
0, /* tp_call*/
0, /* tp_str*/
0, /* tp_getattro*/
0, /* tp_setattro*/
0, /* tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /* tp_flags*/
"Dispatch Log Adapter", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
LogAdapter_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)LogAdapter_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
0, /* tp_del */
0 /* tp_version_tag */
};
//===============================================================================
// Message IO Object
//===============================================================================
typedef struct dx_python_io_adapter {
int x;
} dx_python_io_adapter;
ALLOC_DECLARE(dx_python_io_adapter);
ALLOC_DEFINE(dx_python_io_adapter);
//static PyObject* dx_python_send(PyObject *self, PyObject *args)
//{
// return 0;
//}
//===============================================================================
// Initialization of Modules and Types
//===============================================================================
static void dx_python_setup()
{
//
// Add LogAdapter
//
LogAdapterType.tp_new = PyType_GenericNew;
if (PyType_Ready(&LogAdapterType) < 0) {
PyErr_Print();
dx_log(log_module, LOG_ERROR, "Unable to initialize LogAdapter");
assert(0);
} else {
PyObject *m = Py_InitModule3("dispatch", empty_methods, "Dispatch Adapter Module");
Py_INCREF(&LogAdapterType);
PyModule_AddObject(m, "LogAdapter", (PyObject*) &LogAdapterType);
PyObject *LogTrace = PyInt_FromLong((long) LOG_TRACE);
Py_INCREF(LogTrace);
PyModule_AddObject(m, "LOG_TRACE", LogTrace);
PyObject *LogError = PyInt_FromLong((long) LOG_ERROR);
Py_INCREF(LogError);
PyModule_AddObject(m, "LOG_ERROR", LogError);
PyObject *LogInfo = PyInt_FromLong((long) LOG_INFO);
Py_INCREF(LogInfo);
PyModule_AddObject(m, "LOG_INFO", LogInfo);
}
}
|