waf: fix the include path workaround
[ladish.git] / daemon / graph_dict.c
bloba66e49c234309e8af9f8947f8b93fad5840ec512
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains interface to the D-Bus graph dict interface helpers
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "graph_dict.h"
28 #include "../dbus/error.h"
29 #include "../dbus_constants.h"
30 #include "graph.h"
31 #include "dict.h"
33 #define graph_handle ((ladish_graph_handle)call_ptr->iface_context)
35 bool find_dict(struct dbus_method_call * call_ptr, uint32_t object_type, uint64_t object_id, ladish_dict_handle * dict_handle_ptr)
37 ladish_client_handle client;
38 ladish_port_handle port;
39 ladish_dict_handle dict;
41 switch (object_type)
43 case GRAPH_DICT_OBJECT_TYPE_GRAPH:
44 *dict_handle_ptr = ladish_graph_get_dict(graph_handle);
45 return true;
46 case GRAPH_DICT_OBJECT_TYPE_CLIENT:
47 client = ladish_graph_find_client_by_id(graph_handle, object_id);
48 if (client == NULL)
50 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "cannot find client %"PRIu64".", object_id);
51 return false;
53 *dict_handle_ptr = ladish_client_get_dict(client);
54 return true;
55 case GRAPH_DICT_OBJECT_TYPE_PORT:
56 port = ladish_graph_find_port_by_id(graph_handle, object_id);
57 if (port == NULL)
59 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "cannot find port %"PRIu64".", object_id);
60 return false;
62 *dict_handle_ptr = ladish_port_get_dict(port);
63 return true;
64 case GRAPH_DICT_OBJECT_TYPE_CONNECTION:
65 dict = ladish_graph_get_connection_dict(graph_handle, object_id);
66 if (dict == NULL)
68 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "cannot find connection %"PRIu64".", object_id);
71 *dict_handle_ptr = dict;
72 return false;
75 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "find_dict() not implemented for object type %"PRIu32".", object_type);
76 return false;
79 #undef graph_handle
81 void ladish_dict_set_dbus(struct dbus_method_call * call_ptr)
83 uint32_t object_type;
84 uint64_t object_id;
85 ladish_dict_handle dict;
86 const char * key;
87 const char * value;
89 if (!dbus_message_get_args(
90 call_ptr->message,
91 &g_dbus_error,
92 DBUS_TYPE_UINT32, &object_type,
93 DBUS_TYPE_UINT64, &object_id,
94 DBUS_TYPE_STRING, &key,
95 DBUS_TYPE_STRING, &value,
96 DBUS_TYPE_INVALID))
98 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
99 dbus_error_free(&g_dbus_error);
100 return;
103 if (!find_dict(call_ptr, object_type, object_id, &dict))
105 return;
108 log_info("%s <- %s", key, value);
110 if (!ladish_dict_set(dict, key, value))
112 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_dict_set(\"%s\", \"%s\") failed.");
113 return;
116 method_return_new_void(call_ptr);
119 void ladish_dict_get_dbus(struct dbus_method_call * call_ptr)
121 uint32_t object_type;
122 uint64_t object_id;
123 ladish_dict_handle dict;
124 const char * key;
125 const char * value;
127 if (!dbus_message_get_args(
128 call_ptr->message,
129 &g_dbus_error,
130 DBUS_TYPE_UINT32, &object_type,
131 DBUS_TYPE_UINT64, &object_id,
132 DBUS_TYPE_STRING, &key,
133 DBUS_TYPE_INVALID))
135 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
136 dbus_error_free(&g_dbus_error);
137 return;
140 if (!find_dict(call_ptr, object_type, object_id, &dict))
142 return;
145 value = ladish_dict_get(dict, key);
146 if (value == NULL)
148 //lash_dbus_error(call_ptr, LASH_DBUS_ERROR_KEY_NOT_FOUND, "Key '%s' not found", key);
149 return;
152 method_return_new_single(call_ptr, DBUS_TYPE_STRING, &value);
155 void ladish_dict_drop_dbus(struct dbus_method_call * call_ptr)
157 uint32_t object_type;
158 uint64_t object_id;
159 ladish_dict_handle dict;
160 const char * key;
162 if (!dbus_message_get_args(
163 call_ptr->message,
164 &g_dbus_error,
165 DBUS_TYPE_UINT32, &object_type,
166 DBUS_TYPE_UINT64, &object_id,
167 DBUS_TYPE_STRING, &key,
168 DBUS_TYPE_INVALID))
170 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
171 dbus_error_free(&g_dbus_error);
172 return;
175 if (!find_dict(call_ptr, object_type, object_id, &dict))
177 return;
180 ladish_dict_drop(dict, key);
182 method_return_new_void(call_ptr);
185 METHOD_ARGS_BEGIN(Set, "Set value for specified key")
186 METHOD_ARG_DESCRIBE_IN("object_type", "u", "Type of object, 0 - graph, 1 - client, 2 - port, 3 - connection")
187 METHOD_ARG_DESCRIBE_IN("object_id", "t", "ID of the object")
188 METHOD_ARG_DESCRIBE_IN("key", "s", "Key to set")
189 METHOD_ARG_DESCRIBE_IN("value", "s", "Value to set")
190 METHOD_ARGS_END
192 METHOD_ARGS_BEGIN(Get, "Get value for specified key")
193 METHOD_ARG_DESCRIBE_IN("object_type", "u", "Type of object, 0 - graph, 1 - client, 2 - port, 3 - connection")
194 METHOD_ARG_DESCRIBE_IN("object_id", "t", "ID of the object")
195 METHOD_ARG_DESCRIBE_IN("key", "s", "Key to query")
196 METHOD_ARG_DESCRIBE_OUT("value", "s", "Value associated with the key")
197 METHOD_ARGS_END
199 METHOD_ARGS_BEGIN(Drop, "Drop entry, if key exists")
200 METHOD_ARG_DESCRIBE_IN("object_type", "u", "Type of object, 0 - graph, 1 - client, 2 - port, 3 - connection")
201 METHOD_ARG_DESCRIBE_IN("object_id", "t", "ID of the object")
202 METHOD_ARG_DESCRIBE_IN("key", "s", "Key of the entry to drop")
203 METHOD_ARGS_END
205 METHODS_BEGIN
206 METHOD_DESCRIBE(Set, ladish_dict_set_dbus)
207 METHOD_DESCRIBE(Get, ladish_dict_get_dbus)
208 METHOD_DESCRIBE(Drop, ladish_dict_drop_dbus)
209 METHODS_END
211 INTERFACE_BEGIN(g_iface_graph_dict, IFACE_GRAPH_DICT)
212 INTERFACE_DEFAULT_HANDLER
213 INTERFACE_EXPOSE_METHODS
214 INTERFACE_END