-
Notifications
You must be signed in to change notification settings - Fork 3
/
homegear.cpp
530 lines (462 loc) · 20.9 KB
/
homegear.cpp
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* Copyright 2013-2019 Homegear GmbH
*
* Homegear is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Homegear is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Homegear. If not, see
* <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU Lesser General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#include <Python.h>
#include "IpcClient.h"
#include "PythonVariableConverter.h"
#include <unordered_set>
#if PY_MAJOR_VERSION > 3
#error "Python version > 3 is not supported."
#elif PY_MAJOR_VERSION < 3
#error "Python version < 3 is not supported."
#endif
static const std::unordered_set<std::string> kNodeMethods{
"nodeEvent",
"nodeOutput",
"nodeLog",
"setNodeData",
"setFlowData",
"setGlobalData",
"getNodeData",
"getFlowData",
"getGlobalData",
"getNodeCredentials",
"setNodeCredentials"
};
typedef struct {
PyObject_HEAD
std::string *socketPath = nullptr;
IpcClient *ipcClient = nullptr;
PyObject *eventCallback = nullptr;
std::mutex *onConnectWaitMutex = nullptr;
std::condition_variable *onConnectConditionVariable = nullptr;
// {{{ Variables and methods for use as Node-BLUE node
std::string *nodeId = nullptr;
PyObject *nodeInputCallback = nullptr;
// }}}
} HomegearObject;
static PyObject *Homegear_call(PyObject *object, PyObject *attrName);
static void Homegear_dealloc(HomegearObject *self);
static int Homegear_init(HomegearObject *self, PyObject *arg);
static PyObject *Homegear_new(PyTypeObject *type, PyObject *arg, PyObject *kw);
static PyMethodDef HomegearMethods[] = {
{nullptr, nullptr, 0, nullptr}
};
#if __GNUC__ > 7
static PyTypeObject HomegearObjectType = {
.ob_base = PyVarObject_HEAD_INIT(nullptr, 0)
.tp_name = "homegear.Homegear",
.tp_basicsize = sizeof(HomegearObject),
.tp_dealloc = (destructor)Homegear_dealloc,
.tp_getattro = Homegear_call,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "Class to locally communicate with Homegear.",
.tp_methods = HomegearMethods,
.tp_init = (initproc)Homegear_init,
.tp_new = Homegear_new,
};
#else
static PyTypeObject HomegearObjectType = {
PyVarObject_HEAD_INIT(nullptr, 0)
"homegear.Homegear", // tp_name (module name, object name)
sizeof(HomegearObject), // tp_basicsize
0, // tp_itemsize
(destructor)Homegear_dealloc, // tp_dealloc
#if PY_MINOR_VERSION >= 8
0, // tp_vectorcall_offset
#else
nullptr, // tp_print
#endif
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_as_async
nullptr, // tp_repr
nullptr, // tp_as_number
nullptr, // tp_as_sequence
nullptr, // tp_as_mapping
nullptr, // tp_hash
nullptr, // tp_call
nullptr, // tp_str
Homegear_call, // tp_getattro
nullptr, // tp_setattro
nullptr, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
"Class to locally communicate with Homegear.", // tp_doc
nullptr, // tp_traverse
nullptr, // tp_clear
nullptr, // tp_richcompare
0, // tp_weaklistoffset
nullptr, // tp_iter
nullptr, // tp_iternext
HomegearMethods, // tp_methods
nullptr, // tp_members
nullptr, // tp_getset
nullptr, // tp_base
nullptr, // tp_dict
nullptr, // tp_descr_get
nullptr, // tp_descr_set
0, // tp_dictoffset
(initproc)Homegear_init, // tp_init
nullptr, // tp_alloc
Homegear_new, // tp_new
nullptr, // tp_free
#if PY_MINOR_VERSION >= 8
nullptr, // tp_is_gc
#endif
};
#endif
typedef struct {
PyObject_HEAD
std::string *methodName = nullptr;
std::string *nodeId = nullptr;
IpcClient *ipcClient = nullptr;
} HomegearRpcMethod;
static PyObject *HomegearRpcMethod_call(PyObject *object, PyObject *args, PyObject *kw);
static void HomegearRpcMethod_dealloc(HomegearRpcMethod *self);
static PyObject *HomegearRpcMethod_new(PyTypeObject *type, PyObject *arg, PyObject *kw);
std::nullptr_t bla;
#if __GNUC__ > 7
static PyTypeObject HomegearRpcMethodType = {
.ob_base = PyVarObject_HEAD_INIT(nullptr, 0)
.tp_name = "homegear.HomegearRpcMethod", // (module name, object name)
.tp_basicsize = sizeof(HomegearRpcMethodType),
.tp_dealloc = (destructor)HomegearRpcMethod_dealloc,
.tp_call = HomegearRpcMethod_call,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = HomegearRpcMethod_new
};
#else
static PyTypeObject HomegearRpcMethodType = {
PyVarObject_HEAD_INIT(nullptr, 0)
"homegear.HomegearRpcMethod", // tp_name (module name, object name)
sizeof(HomegearRpcMethodType), // tp_basicsize
0, // tp_itemsize
(destructor)HomegearRpcMethod_dealloc, // tp_dealloc
#if PY_MINOR_VERSION >= 8
0, // tp_vectorcall_offset
#else
nullptr, // tp_print
#endif
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_as_async
nullptr, // tp_repr
nullptr, // tp_as_number
nullptr, // tp_as_sequence
nullptr, // tp_as_mapping
nullptr, // tp_hash
HomegearRpcMethod_call, // tp_call
nullptr, // tp_str
nullptr, // tp_getattro
nullptr, // tp_setattro
nullptr, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
nullptr, // tp_doc
nullptr, // tp_traverse
nullptr, // tp_clear
nullptr, // tp_richcompare
0, // tp_weaklistoffset
nullptr, // tp_iter
nullptr, // tp_iternext
nullptr, // tp_methods
nullptr, // tp_members
nullptr, // tp_getset
nullptr, // tp_base
nullptr, // tp_dict
nullptr, // tp_descr_get
nullptr, // tp_descr_set
0, // tp_dictoffset
nullptr, // tp_init
nullptr, // tp_alloc
HomegearRpcMethod_new, // tp_new
nullptr, // tp_free
#if PY_MINOR_VERSION >= 8
nullptr, // tp_is_gc
#endif
};
#endif
static PyObject *HomegearRpcMethod_new(PyTypeObject *type, PyObject *arg, PyObject *kw) {
const char *methodName = nullptr;
switch (PyTuple_Size(arg)) {
case 1:if (!PyArg_ParseTuple(arg, "s", &methodName)) return nullptr;
break;
default:return nullptr;
}
if (!methodName) return nullptr;
auto self = (HomegearRpcMethod *)type->tp_alloc(type, type->tp_basicsize);
if (!self) return nullptr;
//Py_INCREF(self); //valgrind does not complain if we don't do this and the dealloc is only called after setting the object to "None".
self->methodName = new std::string(methodName);
self->ipcClient = nullptr;
self->nodeId = nullptr;
return (PyObject *)self;
}
static void HomegearRpcMethod_dealloc(HomegearRpcMethod *self) {
if (self->methodName) {
delete self->methodName;
self->methodName = nullptr;
}
Py_TYPE(self)->tp_free(self);
}
static PyObject *HomegearRpcMethod_call(PyObject *object, PyObject *args, PyObject *kw) {
auto methodObject = (HomegearRpcMethod *)object;
if (!methodObject->ipcClient) {
Py_RETURN_NONE;
}
if (*methodObject->methodName == "connected") {
if (methodObject->ipcClient->connected()) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
if (!methodObject->ipcClient->connected()) Py_RETURN_NONE;
auto parameters = PythonVariableConverter::getVariable(args);
auto nodeMethodIterator = kNodeMethods.find(*methodObject->methodName);
if (nodeMethodIterator != kNodeMethods.end()) {
if (!methodObject->nodeId || methodObject->nodeId->empty()) {
PyErr_SetString(PyExc_Exception, "Node ID was not set in Object constructor.");
return nullptr;
}
auto newParameters = std::make_shared<Ipc::Array>();
newParameters->reserve(parameters->arrayValue->size() + 1);
newParameters->emplace_back(std::make_shared<Ipc::Variable>(*methodObject->nodeId));
newParameters->insert(newParameters->end(), parameters->arrayValue->begin(), parameters->arrayValue->end());
auto result = methodObject->ipcClient->invoke(*methodObject->methodName, newParameters);
if (result->errorStruct) {
PyErr_SetString(PyExc_Exception, result->structValue->at("faultString")->stringValue.c_str());
return nullptr;
}
return PythonVariableConverter::getPythonVariable(result);
} else {
auto result = methodObject->ipcClient->invoke(*methodObject->methodName, parameters->arrayValue);
if (result->errorStruct) {
PyErr_SetString(PyExc_Exception, result->structValue->at("faultString")->stringValue.c_str());
return nullptr;
}
return PythonVariableConverter::getPythonVariable(result);
}
}
static void Homegear_onConnect(HomegearObject *self) {
std::unique_lock<std::mutex> waitLock(*self->onConnectWaitMutex);
waitLock.unlock();
self->onConnectConditionVariable->notify_all();
}
static void Homegear_broadcastEvent(HomegearObject *self, std::string &eventSource, uint64_t peerId, int32_t channel, std::string &variableName, Ipc::PVariable value) {
if (!self->eventCallback) return;
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyObject *pythonValue = PythonVariableConverter::getPythonVariable(value);
if (pythonValue == nullptr) return;
PyObject *arglist = Py_BuildValue("(sKisO)", eventSource.c_str(), (unsigned long long)peerId, (int)channel, variableName.c_str(), pythonValue);
if (arglist == nullptr) {
Py_DECREF(pythonValue);
PyGILState_Release(gstate);
return;
}
PyObject *result = PyObject_Call(self->eventCallback, arglist, nullptr);
Py_DECREF(arglist);
Py_DECREF(pythonValue);
if (result == nullptr) {
PyGILState_Release(gstate);
return;
}
Py_DECREF(result);
PyGILState_Release(gstate);
}
static void Homegear_nodeInput(HomegearObject *self, const Ipc::PVariable &nodeInfo, uint32_t inputIndex, const Ipc::PVariable &message) {
if (!self->nodeInputCallback) return;
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyObject *pythonNodeInfo = PythonVariableConverter::getPythonVariable(nodeInfo);
PyObject *pythonMessage = PythonVariableConverter::getPythonVariable(message);
if (nodeInfo == nullptr || pythonMessage == nullptr) return;
PyObject *arglist = Py_BuildValue("(OIO)", pythonNodeInfo, (unsigned int)inputIndex, pythonMessage);
if (arglist == nullptr) {
Py_DECREF(pythonNodeInfo);
Py_DECREF(pythonMessage);
PyGILState_Release(gstate);
return;
}
PyObject *result = PyObject_Call(self->nodeInputCallback, arglist, nullptr);
Py_DECREF(arglist);
Py_DECREF(pythonNodeInfo);
Py_DECREF(pythonMessage);
if (result == nullptr) {
PyGILState_Release(gstate);
return;
}
Py_DECREF(result);
PyGILState_Release(gstate);
}
static PyObject *Homegear_new(PyTypeObject *type, PyObject *arg, PyObject *kw) {
const char *socketPath = nullptr;
PyObject *tempEventCallback = nullptr;
const char *nodeId = nullptr;
PyObject *tempNodeInputCallback = nullptr;
switch (PyTuple_Size(arg)) {
case 1: {
if (!PyArg_ParseTuple(arg, "s", &socketPath)) return nullptr;
break;
}
case 2: {
if (!PyArg_ParseTuple(arg, "sO:Homegear_new", &socketPath, &tempEventCallback)) return nullptr;
if (!PyCallable_Check(tempEventCallback)) {
PyErr_SetString(PyExc_TypeError, "Parameter eventCallback must be callable.");
return nullptr;
}
Py_XINCREF(tempEventCallback); /* Add a reference to new callback */
break;
}
case 4: {
if (!PyArg_ParseTuple(arg, "sOsO:Homegear_new", &socketPath, &tempEventCallback, &nodeId, &tempNodeInputCallback)) return nullptr;
if (!PyCallable_Check(tempEventCallback)) {
PyErr_SetString(PyExc_TypeError, "Parameter eventCallback must be callable.");
return nullptr;
}
Py_XINCREF(tempEventCallback); /* Add a reference to new callback */
if (!PyCallable_Check(tempNodeInputCallback)) {
PyErr_SetString(PyExc_TypeError, "Parameter nodeInputCallback must be callable.");
return nullptr;
}
Py_XINCREF(tempNodeInputCallback); /* Add a reference to new callback */
break;
}
default: {
return nullptr;
}
}
if (!socketPath) return nullptr;
auto self = (HomegearObject *)type->tp_alloc(type, 0);
if (!self) return nullptr;
//Py_INCREF(self); //valgrind does not complain if we don't do this and the dealloc is only called after setting the object to "None".
self->socketPath = new std::string(socketPath);
if (self->socketPath->front() == '"' && self->socketPath->back() == '"') *self->socketPath = self->socketPath->substr(1, self->socketPath->length() - 2);
self->eventCallback = tempEventCallback;
self->nodeInputCallback = tempNodeInputCallback;
if (nodeId) self->nodeId = new std::string(nodeId);
else self->nodeId = new std::string();
self->ipcClient = new IpcClient(*self->socketPath);
self->onConnectConditionVariable = new std::condition_variable;
self->onConnectWaitMutex = new std::mutex;
return (PyObject *)self;
}
static int Homegear_init(HomegearObject *self, PyObject *arg) {
if (self->eventCallback) {
self->ipcClient->setBroadcastEvent(std::function<void(std::string &, uint64_t, int32_t, std::string &, Ipc::PVariable)>(std::bind(&Homegear_broadcastEvent,
self,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3,
std::placeholders::_4,
std::placeholders::_5)));
}
if (self->nodeInputCallback) {
self->ipcClient->setNodeInput(std::function<void(const Ipc::PVariable &nodeInfo, uint32_t inputIndex, const Ipc::PVariable message)>(std::bind(&Homegear_nodeInput,
self,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3)));
}
self->ipcClient->setOnConnect(std::function<void(void)>(std::bind(&Homegear_onConnect, self)));
self->ipcClient->start();
std::unique_lock<std::mutex> waitLock(*self->onConnectWaitMutex);
int64_t startTime = Ipc::HelperFunctions::getTime();
while (!self->onConnectConditionVariable->wait_for(waitLock, std::chrono::milliseconds(2000), [&] {
if (Ipc::HelperFunctions::getTime() - startTime > 2000) return true;
else return self->ipcClient->connected();
}));
return 0;
}
static void Homegear_dealloc(HomegearObject *self) {
if (self->eventCallback) {
Py_XDECREF(self->eventCallback);
self->eventCallback = nullptr;
}
if (self->nodeInputCallback) {
Py_XDECREF(self->nodeInputCallback);
self->nodeInputCallback = nullptr;
}
if (self->ipcClient) {
delete self->ipcClient;
self->ipcClient = nullptr;
}
if (self->nodeId) {
delete self->nodeId;
self->nodeId = nullptr;
}
if (self->socketPath) {
delete self->socketPath;
self->socketPath = nullptr;
}
if (self->onConnectConditionVariable) {
delete self->onConnectConditionVariable;
self->onConnectConditionVariable = nullptr;
}
if (self->onConnectWaitMutex) {
delete self->onConnectWaitMutex;
self->onConnectWaitMutex = nullptr;
}
Py_TYPE(self)->tp_free(self);
}
static PyObject *Homegear_call(PyObject *object, PyObject *attrName) {
auto homegearObject = (HomegearObject *)object;
if (!PyUnicode_Check(attrName)) {
PyErr_Format(PyExc_TypeError, "Attribute name must be string, not '%.200s'", attrName->ob_type->tp_name);
return nullptr;
}
Py_ssize_t methodNameSize = 0;
const char *methodName = PyUnicode_AsUTF8AndSize(attrName, &methodNameSize);
if (!methodName) return nullptr;
auto homegearMethodObject = (HomegearRpcMethod *)HomegearRpcMethodType.tp_alloc(&HomegearRpcMethodType, 0);
if (!homegearMethodObject) return nullptr;
//Py_INCREF(homegearMethodObject); //valgrind does not complain if we don't do this and the dealloc is only called after setting the object to "None".
homegearMethodObject->methodName = new std::string(methodName, methodNameSize);
homegearMethodObject->ipcClient = homegearObject->ipcClient;
homegearMethodObject->nodeId = homegearObject->nodeId;
return (PyObject *)homegearMethodObject;
}
static struct PyModuleDef HomegearModule = {
PyModuleDef_HEAD_INIT,
"homegear", /* name of module */
nullptr, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
nullptr
};
PyMODINIT_FUNC PyInit_homegear(void) {
PyEval_InitThreads(); //Bugfix - Not needed in Python 3.6+, no-op when called for a second time. Must be called from the main thread.
if (PyType_Ready(&HomegearObjectType) < 0 || PyType_Ready(&HomegearRpcMethodType) < 0) return nullptr;
PyObject *m = PyModule_Create(&HomegearModule);
if (!m) return nullptr;
Py_INCREF(&HomegearObjectType);
PyModule_AddObject(m, "Homegear", (PyObject *)&HomegearObjectType);
Py_INCREF(&HomegearRpcMethodType);
PyModule_AddObject(m, "HomegearRpcMethod", (PyObject *)&HomegearRpcMethodType);
return m;
}