diff options
Diffstat (limited to 'src/lib/common/gnunet_dbus_lib_method.c')
-rw-r--r-- | src/lib/common/gnunet_dbus_lib_method.c | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/lib/common/gnunet_dbus_lib_method.c b/src/lib/common/gnunet_dbus_lib_method.c new file mode 100644 index 0000000..ebf631d --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_method.c | |||
@@ -0,0 +1,149 @@ | |||
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-method", __VA_ARGS__) | ||
10 | |||
11 | struct GNUNET_DBUS_Method | ||
12 | { | ||
13 | struct GNUNET_DBUS_ArgIterator *args_front; | ||
14 | struct GNUNET_DBUS_ArgIterator *args_back; | ||
15 | |||
16 | struct GNUNET_DBUS_ArgIterator *return_args_front; | ||
17 | struct GNUNET_DBUS_ArgIterator *return_args_back; | ||
18 | |||
19 | char *name; | ||
20 | |||
21 | void (*underlying_method)(struct GNUNET_DBUS_MethodContext *mc); | ||
22 | |||
23 | unsigned ref_count; | ||
24 | }; | ||
25 | |||
26 | struct GNUNET_DBUS_Method * | ||
27 | GNUNET_DBUS_method_create ( | ||
28 | const char *name, | ||
29 | void (*underlying_method)(struct GNUNET_DBUS_MethodContext *mc)) | ||
30 | { | ||
31 | struct GNUNET_DBUS_Method *method = GNUNET_new (struct GNUNET_DBUS_Method); | ||
32 | method->name = GNUNET_strdup (name); | ||
33 | method->underlying_method = underlying_method; | ||
34 | method->ref_count = 1; | ||
35 | |||
36 | return method; | ||
37 | }; | ||
38 | |||
39 | void | ||
40 | GNUNET_DBUS_method_ref ( | ||
41 | struct GNUNET_DBUS_Method *method) | ||
42 | { | ||
43 | method->ref_count++; | ||
44 | }; | ||
45 | |||
46 | void | ||
47 | GNUNET_DBUS_method_unref ( | ||
48 | struct GNUNET_DBUS_Method *method) | ||
49 | { | ||
50 | if (method->ref_count == 0) | ||
51 | { | ||
52 | LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference method with ref count 0\n"); | ||
53 | GNUNET_abort_ (); | ||
54 | }; | ||
55 | if (0 == --(method->ref_count)) | ||
56 | { | ||
57 | struct GNUNET_DBUS_ArgIterator *arg_it = method->args_front; | ||
58 | while (arg_it) | ||
59 | { | ||
60 | struct GNUNET_DBUS_ArgIterator *next = arg_it->next; | ||
61 | GNUNET_DBUS_arg_unref (arg_it->arg); | ||
62 | GNUNET_free (arg_it); | ||
63 | arg_it = next; | ||
64 | } | ||
65 | |||
66 | arg_it = method->return_args_front; | ||
67 | while (arg_it) | ||
68 | { | ||
69 | struct GNUNET_DBUS_ArgIterator *next = arg_it->next; | ||
70 | GNUNET_DBUS_arg_unref (arg_it->arg); | ||
71 | GNUNET_free (arg_it); | ||
72 | arg_it = next; | ||
73 | } | ||
74 | |||
75 | GNUNET_free (method->name); | ||
76 | GNUNET_free (method); | ||
77 | } | ||
78 | }; | ||
79 | |||
80 | void | ||
81 | GNUNET_DBUS_method_add_arg ( | ||
82 | struct GNUNET_DBUS_Method *method, | ||
83 | const char *name, | ||
84 | const char *signature) | ||
85 | { | ||
86 | struct GNUNET_DBUS_Arg *arg = GNUNET_DBUS_arg_create (name, signature); | ||
87 | struct GNUNET_DBUS_ArgIterator *arg_it = GNUNET_new (struct GNUNET_DBUS_ArgIterator); | ||
88 | arg_it->arg = arg; | ||
89 | GNUNET_CONTAINER_DLL_insert_tail (method->args_front, | ||
90 | method->args_back, | ||
91 | arg_it); | ||
92 | }; | ||
93 | |||
94 | void | ||
95 | GNUNET_DBUS_method_add_return_arg ( | ||
96 | struct GNUNET_DBUS_Method *method, | ||
97 | const char *name, | ||
98 | const char *signature) | ||
99 | { | ||
100 | struct GNUNET_DBUS_Arg *arg = GNUNET_DBUS_arg_create (name, signature); | ||
101 | struct GNUNET_DBUS_ArgIterator *arg_it = GNUNET_new (struct GNUNET_DBUS_ArgIterator); | ||
102 | arg_it->arg = arg; | ||
103 | GNUNET_CONTAINER_DLL_insert_tail (method->return_args_front, | ||
104 | method->return_args_back, | ||
105 | arg_it); | ||
106 | }; | ||
107 | |||
108 | const char * | ||
109 | GNUNET_DBUS_method_get_name ( | ||
110 | const struct GNUNET_DBUS_Method *method) | ||
111 | { | ||
112 | return method->name; | ||
113 | }; | ||
114 | |||
115 | const struct GNUNET_DBUS_ArgIterator * | ||
116 | GNUNET_DBUS_method_iterate_args ( | ||
117 | const struct GNUNET_DBUS_Method *method) | ||
118 | { | ||
119 | return method->args_front; | ||
120 | }; | ||
121 | |||
122 | const struct GNUNET_DBUS_ArgIterator * | ||
123 | GNUNET_DBUS_method_iterate_return_args ( | ||
124 | const struct GNUNET_DBUS_Method *method) | ||
125 | { | ||
126 | return method->return_args_front; | ||
127 | }; | ||
128 | |||
129 | void | ||
130 | GNUNET_DBUS_method_call ( | ||
131 | struct GNUNET_DBUS_Method *method, | ||
132 | struct GNUNET_DBUS_MethodContext *mc) | ||
133 | { | ||
134 | method->underlying_method (mc); | ||
135 | }; | ||
136 | |||
137 | const struct GNUNET_DBUS_MethodIterator * | ||
138 | GNUNET_DBUS_method_find ( | ||
139 | const struct GNUNET_DBUS_MethodIterator *meth_it, | ||
140 | const char *name) | ||
141 | { | ||
142 | for (; meth_it; meth_it = meth_it->next) | ||
143 | { | ||
144 | if (! strcmp (name, meth_it->method->name)) | ||
145 | return meth_it; | ||
146 | } | ||
147 | return NULL; | ||
148 | } | ||
149 | |||