diff options
Diffstat (limited to 'src/lib/common/gnunet_dbus_lib_signal.c')
-rw-r--r-- | src/lib/common/gnunet_dbus_lib_signal.c | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/lib/common/gnunet_dbus_lib_signal.c b/src/lib/common/gnunet_dbus_lib_signal.c new file mode 100644 index 0000000..0597200 --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_signal.c | |||
@@ -0,0 +1,126 @@ | |||
1 | #include "config.h" | ||
2 | |||
3 | #include <gnunet/platform.h> | ||
4 | #include <gnunet/gnunet_common.h> | ||
5 | #include <gnunet/gnunet_container_lib.h> | ||
6 | |||
7 | #include "gnunet_dbus_lib.h" | ||
8 | |||
9 | #define LOG(kind, ...) GNUNET_log_from (kind, "dbus-signal", __VA_ARGS__) | ||
10 | |||
11 | struct GNUNET_DBUS_Signal | ||
12 | { | ||
13 | struct GNUNET_DBUS_ArgIterator *args_front; | ||
14 | struct GNUNET_DBUS_ArgIterator *args_back; | ||
15 | |||
16 | struct GNUNET_DBUS_Interface *owner; | ||
17 | |||
18 | char *name; | ||
19 | |||
20 | unsigned ref_count; | ||
21 | }; | ||
22 | |||
23 | struct GNUNET_DBUS_Signal * | ||
24 | GNUNET_DBUS_signal_create ( | ||
25 | const char *name) | ||
26 | { | ||
27 | struct GNUNET_DBUS_Signal *ret = GNUNET_new (struct GNUNET_DBUS_Signal); | ||
28 | ret->args_front = NULL; | ||
29 | ret->args_back = NULL; | ||
30 | ret->owner = NULL; | ||
31 | ret->name = strdup (name); | ||
32 | ret->ref_count = 1; | ||
33 | |||
34 | return ret; | ||
35 | } | ||
36 | |||
37 | void | ||
38 | GNUNET_DBUS_signal_ref ( | ||
39 | struct GNUNET_DBUS_Signal *signal) | ||
40 | { | ||
41 | signal->ref_count++; | ||
42 | } | ||
43 | |||
44 | void | ||
45 | GNUNET_DBUS_signal_unref ( | ||
46 | struct GNUNET_DBUS_Signal *signal) | ||
47 | { | ||
48 | if (signal->ref_count == 0) | ||
49 | { | ||
50 | LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference signal with ref count 0.\n"); | ||
51 | GNUNET_abort_ (); | ||
52 | } | ||
53 | if (0 == --(signal->ref_count)) | ||
54 | { | ||
55 | struct GNUNET_DBUS_ArgIterator *arg_it = signal->args_front; | ||
56 | while (arg_it) | ||
57 | { | ||
58 | struct GNUNET_DBUS_ArgIterator *next = arg_it->next; | ||
59 | GNUNET_DBUS_arg_unref (arg_it->arg); | ||
60 | GNUNET_free (arg_it); | ||
61 | arg_it = next; | ||
62 | } | ||
63 | |||
64 | GNUNET_free (signal->name); | ||
65 | GNUNET_free (signal); | ||
66 | } | ||
67 | }; | ||
68 | |||
69 | void | ||
70 | GNUNET_DBUS_signal_add_arg ( | ||
71 | struct GNUNET_DBUS_Signal *signal, | ||
72 | const char *name, | ||
73 | const char *signature) | ||
74 | { | ||
75 | struct GNUNET_DBUS_Arg *arg = GNUNET_DBUS_arg_create (name, signature); | ||
76 | struct GNUNET_DBUS_ArgIterator *arg_it = GNUNET_new (struct GNUNET_DBUS_ArgIterator); | ||
77 | arg_it->arg = arg; | ||
78 | GNUNET_CONTAINER_DLL_insert_tail (signal->args_front, | ||
79 | signal->args_back, | ||
80 | arg_it); | ||
81 | }; | ||
82 | |||
83 | const char * | ||
84 | GNUNET_DBUS_signal_get_name ( | ||
85 | const struct GNUNET_DBUS_Signal *signal) | ||
86 | { | ||
87 | return signal->name; | ||
88 | } | ||
89 | |||
90 | const struct GNUNET_DBUS_ArgIterator * | ||
91 | GNUNET_DBUS_signal_iterate_args ( | ||
92 | const struct GNUNET_DBUS_Signal *signal) | ||
93 | { | ||
94 | return signal->args_front; | ||
95 | } | ||
96 | |||
97 | DBusMessage * | ||
98 | GNUNET_DBUS_signal_spawn ( | ||
99 | const struct GNUNET_DBUS_Signal *signal, | ||
100 | const struct GNUNET_DBUS_ObjectPath *path, | ||
101 | const struct GNUNET_DBUS_Interface *interface, | ||
102 | bool pretty) | ||
103 | { | ||
104 | char *path_str = GNUNET_DBUS_object_path_to_string (path); | ||
105 | DBusMessage *ret = dbus_message_new_signal ( | ||
106 | path_str, | ||
107 | GNUNET_DBUS_interface_get_name (interface), | ||
108 | signal->name); | ||
109 | GNUNET_free (path_str); | ||
110 | GNUNET_DBUS_message_set_pretty (ret, pretty); | ||
111 | return ret; | ||
112 | }; | ||
113 | |||
114 | DBusMessage * | ||
115 | GNUNET_DBUS_signal_spawn_unicast ( | ||
116 | const struct GNUNET_DBUS_Signal *signal, | ||
117 | const struct GNUNET_DBUS_ObjectPath *path, | ||
118 | const struct GNUNET_DBUS_Interface *interface, | ||
119 | const struct GNUNET_DBUS_Client *client, | ||
120 | bool pretty) | ||
121 | { | ||
122 | DBusMessage *ret = GNUNET_DBUS_signal_spawn (signal, path, interface, pretty); | ||
123 | dbus_message_set_destination (ret, GNUNET_DBUS_client_get_unique_name (client)); | ||
124 | return ret; | ||
125 | } | ||
126 | |||