diff options
Diffstat (limited to 'src/services/gnunet-service-namecache-dbus.c')
-rw-r--r-- | src/services/gnunet-service-namecache-dbus.c | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/src/services/gnunet-service-namecache-dbus.c b/src/services/gnunet-service-namecache-dbus.c new file mode 100644 index 0000000..7eded44 --- /dev/null +++ b/src/services/gnunet-service-namecache-dbus.c | |||
@@ -0,0 +1,158 @@ | |||
1 | #include "config.h" | ||
2 | |||
3 | #include <gnunet/platform.h> | ||
4 | #include <gnunet/gnunet_common.h> | ||
5 | #include <gnunet/gnunet_configuration_lib.h> | ||
6 | #include <gnunet/gnunet_getopt_lib.h> | ||
7 | #include <gnunet/gnunet_strings_lib.h> | ||
8 | #include <gnunet/gnunet_program_lib.h> | ||
9 | #include <gnunet/gnunet_namecache_service.h> | ||
10 | |||
11 | #include "gnunet_dbus_lib.h" | ||
12 | |||
13 | static void | ||
14 | block_cache_return ( | ||
15 | void *cls, | ||
16 | int32_t success, | ||
17 | const char *emsg) | ||
18 | { | ||
19 | struct GNUNET_DBUS_MethodContext *mc = (struct GNUNET_DBUS_MethodContext *)cls; | ||
20 | |||
21 | DBusMessage *message = mc->message; | ||
22 | DBusMessage *reply = dbus_message_new_method_return (message); | ||
23 | DBusMessageIter reply_iter; | ||
24 | dbus_message_iter_init_append (reply, &reply_iter); | ||
25 | GNUNET_DBUS_message_iter_push_int32 (&reply_iter, success); | ||
26 | GNUNET_DBUS_message_iter_push_string (&reply_iter, emsg); | ||
27 | |||
28 | GNUNET_DBUS_method_context_send_reply (mc, reply); | ||
29 | GNUNET_DBUS_method_context_unref (mc); | ||
30 | }; | ||
31 | |||
32 | static void | ||
33 | block_cache ( | ||
34 | struct GNUNET_DBUS_MethodContext *mc) | ||
35 | { | ||
36 | struct GNUNET_GNSRECORD_Block block; | ||
37 | |||
38 | DBusMessage *dbus_message = mc->message; | ||
39 | DBusMessageIter iter; | ||
40 | dbus_message_iter_init (dbus_message, &iter); | ||
41 | DBusMessage *reply = GNUNET_DBUS_message_iter_pop_gnsrecord_block (dbus_message, &iter, "block", &block); | ||
42 | if (reply) | ||
43 | { | ||
44 | GNUNET_DBUS_method_context_send_reply (mc, reply); | ||
45 | return; | ||
46 | }; | ||
47 | |||
48 | GNUNET_DBUS_method_context_ref (mc); | ||
49 | struct GNUNET_NAMECACHE_Handle *handle = GNUNET_DBUS_client_get_data (mc->client); | ||
50 | GNUNET_NAMECACHE_block_cache (handle, &block, block_cache_return, mc); | ||
51 | }; | ||
52 | |||
53 | static void | ||
54 | client_connects ( | ||
55 | struct GNUNET_DBUS_Service *service, | ||
56 | struct GNUNET_DBUS_Client *client) | ||
57 | { | ||
58 | const struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_DBUS_service_get_config (service); | ||
59 | struct GNUNET_NAMECACHE_Handle *handle = GNUNET_NAMECACHE_connect (cfg); | ||
60 | GNUNET_DBUS_client_set_data (client, handle); | ||
61 | }; | ||
62 | |||
63 | static void | ||
64 | client_disconnects ( | ||
65 | struct GNUNET_DBUS_Service *service, | ||
66 | struct GNUNET_DBUS_Client *client) | ||
67 | { | ||
68 | (void)service; | ||
69 | GNUNET_NAMECACHE_disconnect (GNUNET_DBUS_client_get_data (client)); | ||
70 | }; | ||
71 | |||
72 | static void | ||
73 | shutdown_task ( | ||
74 | void *cls, | ||
75 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
76 | { | ||
77 | (void)tc; | ||
78 | |||
79 | struct GNUNET_DBUS_Service *gns_service = (struct GNUNET_DBUS_Service *)cls; | ||
80 | GNUNET_DBUS_service_destroy (gns_service); | ||
81 | |||
82 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Exiting.\n"); | ||
83 | }; | ||
84 | |||
85 | static void | ||
86 | run ( | ||
87 | void *cls, | ||
88 | char *const *args, | ||
89 | const char *configfile, | ||
90 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
91 | { | ||
92 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running.\n"); | ||
93 | |||
94 | struct GNUNET_DBUS_Service *namecache_service = GNUNET_DBUS_service_create (cfg, "namecache"); | ||
95 | GNUNET_DBUS_service_set_client_handlers (namecache_service, client_connects, client_disconnects); | ||
96 | |||
97 | struct GNUNET_DBUS_Object *namecache_object = GNUNET_DBUS_object_create ("/namecache", NULL); | ||
98 | int err = GNUNET_DBUS_service_add_object (namecache_service, namecache_object); | ||
99 | if (GNUNET_OK != err) | ||
100 | { | ||
101 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to add /namecache object. GNUNET_DBUS_service_add_object returned %d\n", err); | ||
102 | GNUNET_abort_ (); | ||
103 | }; | ||
104 | |||
105 | struct GNUNET_DBUS_Interface *namecache_interface = GNUNET_DBUS_interface_create ("gnu.gnunet.namecache"); | ||
106 | GNUNET_DBUS_object_add_interface (namecache_object, GNUNET_DBUS_interface_introspectable ()); | ||
107 | GNUNET_DBUS_object_add_interface (namecache_object, namecache_interface); | ||
108 | |||
109 | struct GNUNET_DBUS_Method *namecache_method_block_cache = GNUNET_DBUS_method_create ("block_cache", block_cache); | ||
110 | GNUNET_DBUS_interface_add_method (namecache_interface, namecache_method_block_cache); | ||
111 | GNUNET_DBUS_method_add_arg (namecache_method_block_cache, "block", GNUNET_DBUS_SIGNATURE_GNSRECORD_BLOCK); | ||
112 | |||
113 | GNUNET_DBUS_method_add_return_arg (namecache_method_block_cache, "success", GNUNET_DBUS_SIGNATURE_INT32); | ||
114 | GNUNET_DBUS_method_add_return_arg (namecache_method_block_cache, "error_msg", GNUNET_DBUS_SIGNATURE_STRING); | ||
115 | |||
116 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, namecache_service); | ||
117 | }; | ||
118 | |||
119 | int | ||
120 | main ( | ||
121 | int argc, | ||
122 | char *const *argv) | ||
123 | { | ||
124 | int ret; | ||
125 | |||
126 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
127 | GNUNET_GETOPT_OPTION_END | ||
128 | }; | ||
129 | |||
130 | static const char bin_name[] = "gnunet-service-namecache-dbus [OPTIONS]"; | ||
131 | static const char bin_help[] = "DBus proxy for gnunet-service-namecache"; | ||
132 | |||
133 | ret = GNUNET_log_setup ("gnunet-service-namecache-dbus", "DEBUG", NULL); | ||
134 | if (GNUNET_OK != ret) | ||
135 | { | ||
136 | fprintf (stderr, "ERROR: Failed to setup logging. GNUNET_log_setup returned %d\n", ret); | ||
137 | return 1; | ||
138 | }; | ||
139 | |||
140 | ret = GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv); | ||
141 | if (GNUNET_OK != ret) | ||
142 | { | ||
143 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to parse command line options. GNUNET_STRINGS_get_utf8_args returned %d\n", ret); | ||
144 | return 1; | ||
145 | }; | ||
146 | |||
147 | ret = GNUNET_PROGRAM_run (argc, argv, bin_name, bin_help, options, run, NULL); | ||
148 | if (GNUNET_OK != ret) | ||
149 | { | ||
150 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to run program. GNUNET_PROGRAM_run returned %d\n", ret); | ||
151 | return 1; | ||
152 | }; | ||
153 | |||
154 | printf ("leaving main()\n"); | ||
155 | GNUNET_free ((void *)argv); | ||
156 | return 0; | ||
157 | } | ||
158 | |||