diff options
Diffstat (limited to 'src/lib/common/gnunet_dbus_lib_client.c')
-rw-r--r-- | src/lib/common/gnunet_dbus_lib_client.c | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/src/lib/common/gnunet_dbus_lib_client.c b/src/lib/common/gnunet_dbus_lib_client.c new file mode 100644 index 0000000..72b3aef --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_client.c | |||
@@ -0,0 +1,119 @@ | |||
1 | #include "config.h" | ||
2 | |||
3 | #include <gnunet/platform.h> | ||
4 | #include <gnunet/gnunet_common.h> | ||
5 | |||
6 | #include "gnunet_dbus_lib.h" | ||
7 | |||
8 | #define LOG(kind, ...) GNUNET_log_from (kind, "dbus-client", __VA_ARGS__) | ||
9 | |||
10 | struct GNUNET_DBUS_Client | ||
11 | { | ||
12 | char *unique_name; | ||
13 | void *data; | ||
14 | bool prefers_pretty_encodings; | ||
15 | unsigned ref_count; | ||
16 | }; | ||
17 | |||
18 | struct GNUNET_DBUS_Client * | ||
19 | GNUNET_DBUS_client_create ( | ||
20 | const char *unique_name) | ||
21 | { | ||
22 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating client with unique name %s\n", unique_name); | ||
23 | |||
24 | struct GNUNET_DBUS_Client *client = GNUNET_new (struct GNUNET_DBUS_Client); | ||
25 | client->unique_name = GNUNET_strdup (unique_name); | ||
26 | client->data = NULL; | ||
27 | client->prefers_pretty_encodings = false; | ||
28 | client->ref_count = 1; | ||
29 | |||
30 | return client; | ||
31 | }; | ||
32 | |||
33 | void | ||
34 | GNUNET_DBUS_client_ref ( | ||
35 | struct GNUNET_DBUS_Client *client) | ||
36 | { | ||
37 | client->ref_count++; | ||
38 | } | ||
39 | |||
40 | void | ||
41 | GNUNET_DBUS_client_unref ( | ||
42 | struct GNUNET_DBUS_Client *client) | ||
43 | { | ||
44 | if (client->ref_count == 0) | ||
45 | { | ||
46 | LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unref client with ref count 0\n"); | ||
47 | LOG (GNUNET_ERROR_TYPE_ERROR, " unique_name == %s\n", client->unique_name); | ||
48 | GNUNET_abort_ (); | ||
49 | } | ||
50 | |||
51 | if (0 == --(client->ref_count)) | ||
52 | { | ||
53 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying client with unique name %s\n", client->unique_name); | ||
54 | |||
55 | GNUNET_free (client->unique_name); | ||
56 | GNUNET_free (client); | ||
57 | } | ||
58 | }; | ||
59 | |||
60 | const char * | ||
61 | GNUNET_DBUS_client_get_unique_name ( | ||
62 | const struct GNUNET_DBUS_Client *client) | ||
63 | { | ||
64 | return client->unique_name; | ||
65 | }; | ||
66 | |||
67 | void | ||
68 | GNUNET_DBUS_client_set_data ( | ||
69 | struct GNUNET_DBUS_Client *client, | ||
70 | void *data) | ||
71 | { | ||
72 | client->data = data; | ||
73 | }; | ||
74 | |||
75 | void * | ||
76 | GNUNET_DBUS_client_get_data ( | ||
77 | const struct GNUNET_DBUS_Client *client) | ||
78 | { | ||
79 | return client->data; | ||
80 | }; | ||
81 | |||
82 | void | ||
83 | GNUNET_DBUS_client_set_prefers_pretty_encodings ( | ||
84 | struct GNUNET_DBUS_Client *client, | ||
85 | bool prefers_pretty_encodings) | ||
86 | { | ||
87 | client->prefers_pretty_encodings = prefers_pretty_encodings; | ||
88 | } | ||
89 | |||
90 | bool | ||
91 | GNUNET_DBUS_client_get_prefers_pretty_encodings ( | ||
92 | const struct GNUNET_DBUS_Client *client) | ||
93 | { | ||
94 | return client->prefers_pretty_encodings; | ||
95 | }; | ||
96 | |||
97 | DBusMessage * | ||
98 | GNUNET_DBUS_client_create_method_call ( | ||
99 | struct GNUNET_DBUS_Client *client, | ||
100 | const char *object_path, | ||
101 | const char *interface, | ||
102 | const char *method, | ||
103 | bool pretty, | ||
104 | void (*return_callback)(DBusMessage *message)) | ||
105 | { | ||
106 | DBusMessage *ret = dbus_message_new_method_call ( | ||
107 | client->unique_name, | ||
108 | object_path, | ||
109 | interface, | ||
110 | method); | ||
111 | GNUNET_DBUS_message_set_pretty (ret, pretty); | ||
112 | if (return_callback) | ||
113 | { | ||
114 | LOG (GNUNET_ERROR_TYPE_ERROR, "Method calls on external objects not implemented yet.\n"); | ||
115 | GNUNET_abort_ (); | ||
116 | }; | ||
117 | return ret; | ||
118 | } | ||
119 | |||