-
Notifications
You must be signed in to change notification settings - Fork 3
/
object.c
170 lines (150 loc) · 5.61 KB
/
object.c
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
#include "object.h"
#include "common.h"
#include "connection.h"
const GDBusInterfaceVTable cDBusObject::_interface_vtable =
{
cDBusObject::handle_method_call,
NULL,
NULL
};
class cWorkerData
{
public:
static GThreadPool *_thread_pool;
cDBusObject *_object;
GDBusMethodInvocation *_invocation;
cWorkerData(cDBusObject *Object, GDBusMethodInvocation *Invocation)
{
_object = Object;
_invocation = Invocation;
};
};
GThreadPool *cWorkerData::_thread_pool = NULL;
void cDBusObject::FreeThreadPool(void)
{
if (cWorkerData::_thread_pool != NULL) {
g_thread_pool_free(cWorkerData::_thread_pool, FALSE, TRUE);
cWorkerData::_thread_pool = NULL;
isyslog("dbus2vdr: thread-pool for handling method-calls stopped");
}
}
void cDBusObject::do_work(gpointer data, gpointer user_data)
{
if (data == NULL)
return;
cWorkerData *workerData = (cWorkerData*)data;
const gchar *method_name = g_dbus_method_invocation_get_method_name(workerData->_invocation);
d4syslog("dbus2vdr: do_work on %s.%s", workerData->_object->Path(), method_name);
bool err = true;
for (cDBusMethod *m = workerData->_object->_methods.First(); m; m = workerData->_object->_methods.Next(m)) {
if (g_strcmp0(m->_name, method_name) == 0) {
m->_method(workerData->_object, g_dbus_method_invocation_get_parameters(workerData->_invocation), workerData->_invocation);
err = false;
break;
}
}
if (err) {
g_dbus_method_invocation_return_error(workerData->_invocation, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED,
"method '%s.%s' on object '%s' is not implemented yet",
g_dbus_method_invocation_get_interface_name(workerData->_invocation),
g_dbus_method_invocation_get_method_name(workerData->_invocation),
g_dbus_method_invocation_get_object_path(workerData->_invocation));
}
delete workerData;
}
void cDBusObject::handle_method_call(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data)
{
if (user_data == NULL)
return;
d4syslog("dbus2vdr: handle_method_call: sender '%s', object '%s', interface '%s', method '%s'", sender, object_path, interface_name, method_name);
cWorkerData *workerData = new cWorkerData((cDBusObject*)user_data, invocation);
if (cWorkerData::_thread_pool == NULL) {
GError *err = NULL;
cWorkerData::_thread_pool = g_thread_pool_new(do_work, NULL, 10, FALSE, &err);
if (err != NULL) {
esyslog("dbus2vdr: g_thread_pool_new reports: %s", err->message);
g_error_free(err);
if (cWorkerData::_thread_pool != NULL)
g_thread_pool_free(cWorkerData::_thread_pool, TRUE, FALSE);
cWorkerData::_thread_pool = NULL;
do_work(workerData, NULL);
return;
}
isyslog("dbus2vdr: thread-pool for handling method-calls started");
}
g_thread_pool_push(cWorkerData::_thread_pool, workerData, NULL);
}
cDBusObject::cDBusObject(const char *Path, const char *XmlNodeInfo)
{
_path = g_strdup(Path);
_registration_ids = NULL;
_connection = NULL;
GError *err = NULL;
_introspection_data = g_dbus_node_info_new_for_xml(XmlNodeInfo, &err);
if (err != NULL) {
esyslog("dbus2vdr: g_dbus_node_info_new_for_xml reports: %s", err->message);
g_error_free(err);
}
}
cDBusObject::~cDBusObject(void)
{
if (_introspection_data != NULL) {
g_dbus_node_info_unref(_introspection_data);
_introspection_data = NULL;
}
if (_path != NULL) {
g_free(_path);
_path = NULL;
}
if (_registration_ids != NULL) {
g_array_free(_registration_ids, TRUE);
_registration_ids = NULL;
}
}
void cDBusObject::Register(void)
{
if ((_connection == NULL) || (_connection->GetConnection() == NULL))
return;
if (_registration_ids != NULL)
Unregister();
int len = 0;
while (_introspection_data->interfaces[len] != NULL)
len++;
if (_registration_ids != NULL)
g_array_free(_registration_ids, TRUE);
_registration_ids = g_array_new(FALSE, TRUE, sizeof(guint));
for (int i = 0; i < len; i++) {
guint id = g_dbus_connection_register_object(_connection->GetConnection(), Path(), _introspection_data->interfaces[i], &_interface_vtable, this, NULL, NULL);
g_array_append_val(_registration_ids, id);
d4syslog("dbus2vdr: %s: register object %s with interface %s on id %d", _connection->Name(), Path(), _introspection_data->interfaces[i]->name, id);
}
}
void cDBusObject::Unregister(void)
{
if (_registration_ids != NULL) {
if ((_connection != NULL) && (_connection->GetConnection() != NULL)) {
for (guint i = 0; i < _registration_ids->len; i++) {
guint id = g_array_index(_registration_ids, guint, i);
if (id != 0) {
d4syslog("dbus2vdr: %s: unregister object %s with interface %s on id %d", _connection->Name(), Path(), _introspection_data->interfaces[i]->name, id);
g_dbus_connection_unregister_object(_connection->GetConnection(), id);
}
}
}
g_array_free(_registration_ids, TRUE);
_registration_ids = NULL;
}
}
void cDBusObject::SetPath(const char *Path)
{
if (_path != NULL) {
g_free(_path);
_path = NULL;
}
_path = g_strdup(Path);
}
void cDBusObject::AddMethod(const char *Name, cDBusMethodFunc Method)
{
if ((Name != NULL) && (Method != NULL))
_methods.Add(new cDBusMethod(Name, Method));
}