diff options
author | Leandro A. F. Pereira <leandro@hardinfo.org> | 2010-05-03 09:25:43 -0300 |
---|---|---|
committer | Leandro A. F. Pereira <leandro@hardinfo.org> | 2010-05-03 09:25:43 -0300 |
commit | 9a50155ec3e27aa6cedf3f118196f1947c769a29 (patch) | |
tree | c23cc6949b227b9e09432af038a44ccb81d92ce6 /hardinfo2/remote/xmlrpc-client.c | |
parent | ca5a3e84296a34c2391374942001cfaf1d8395a6 (diff) |
Use CMake instead of ToscoConf.
Diffstat (limited to 'hardinfo2/remote/xmlrpc-client.c')
-rw-r--r-- | hardinfo2/remote/xmlrpc-client.c | 247 |
1 files changed, 247 insertions, 0 deletions
diff --git a/hardinfo2/remote/xmlrpc-client.c b/hardinfo2/remote/xmlrpc-client.c new file mode 100644 index 00000000..d363729b --- /dev/null +++ b/hardinfo2/remote/xmlrpc-client.c @@ -0,0 +1,247 @@ +/* + * XMLRPC Client + * HardInfo - Displays System Information + * Copyright (C) 2003-2009 Leandro A. F. Pereira <leandro@hardinfo.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <string.h> +#include "config.h" +#include "xmlrpc-client.h" + +#ifdef HAS_LIBSOUP +static GMainLoop *loop = NULL; +static SoupSession *session = NULL; +static gboolean lock = FALSE; + +void xmlrpc_init(void) +{ + if (!loop) { + loop = g_main_loop_new(FALSE, FALSE); + } + + if (!session) { + session = soup_session_async_new_with_options(SOUP_SESSION_TIMEOUT, 10, NULL); + } +} + +static void xmlrpc_response_get_integer(SoupSession *s, + SoupMessage *m, + gpointer user_data) +{ + gint *response = user_data; + + *response = -1; + + if (SOUP_STATUS_IS_SUCCESSFUL(m->status_code)) { + soup_xmlrpc_extract_method_response(m->response_body->data, + m->response_body->length, + NULL, + G_TYPE_INT, response); + } + + g_main_quit(loop); + lock = FALSE; +} + +gint xmlrpc_get_integer(gchar *addr, + gchar *method, + const gchar *param_types, + ...) +{ + gint integer; + GValueArray *params; + SoupMessage *msg; + gchar *body; + + msg = soup_message_new("POST", addr); + + params = g_value_array_new(1); + + if (param_types && *param_types) { + va_list ap; + + va_start(ap, param_types); + while (*param_types) { + switch (*param_types) { + case '%': + break; + case 'i': + soup_value_array_append(params, G_TYPE_INT, va_arg(ap, int)); + break; + case 's': + default: + soup_value_array_append(params, G_TYPE_STRING, va_arg(ap, char *)); + break; + } + + param_types++; + } + + va_end(ap); + } + + body = soup_xmlrpc_build_method_call(method, params->values, params->n_values); + g_value_array_free(params); + + soup_message_set_request(msg, "text/xml", + SOUP_MEMORY_TAKE, body, strlen(body)); + + while (lock) + g_main_iteration(FALSE); + + lock = TRUE; + soup_session_queue_message(session, msg, xmlrpc_response_get_integer, &integer); + g_main_run(loop); + + return integer; +} + +static void xmlrpc_response_get_string(SoupSession *s, + SoupMessage *m, + gpointer user_data) +{ + if (SOUP_STATUS_IS_SUCCESSFUL(m->status_code)) { + soup_xmlrpc_extract_method_response(m->response_body->data, + m->response_body->length, + NULL, + G_TYPE_STRING, user_data); + } + + g_main_quit(loop); + lock = FALSE; +} + +gchar *xmlrpc_get_string(gchar *addr, + gchar *method, + const gchar *param_types, + ...) +{ + GValueArray *params; + SoupMessage *msg; + gchar *body, *string = NULL; + + msg = soup_message_new("POST", addr); + + params = g_value_array_new(1); + + if (param_types && *param_types) { + va_list ap; + + va_start(ap, param_types); + while (*param_types) { + switch (*param_types) { + case '%': + break; + case 'i': + soup_value_array_append(params, G_TYPE_INT, va_arg(ap, int)); + break; + case 's': + default: + soup_value_array_append(params, G_TYPE_STRING, va_arg(ap, char *)); + break; + } + + param_types++; + } + + va_end(ap); + } + + body = soup_xmlrpc_build_method_call(method, params->values, params->n_values); + g_value_array_free(params); + + soup_message_set_request(msg, "text/xml", + SOUP_MEMORY_TAKE, body, strlen(body)); + + while (lock) + g_main_iteration(FALSE); + + lock = TRUE; + soup_session_queue_message(session, msg, xmlrpc_response_get_string, &string); + g_main_run(loop); + + return string; +} + +static void xmlrpc_response_get_array(SoupSession *s, + SoupMessage *m, + gpointer user_data) +{ + if (SOUP_STATUS_IS_SUCCESSFUL(m->status_code)) { + soup_xmlrpc_extract_method_response(m->response_body->data, + m->response_body->length, + NULL, + G_TYPE_VALUE_ARRAY, user_data); + } + + g_main_quit(loop); + lock = FALSE; +} + +GValueArray *xmlrpc_get_array(gchar *addr, + gchar *method, + const gchar *param_types, + ...) +{ + GValueArray *params, *answer = NULL; + SoupMessage *msg; + gchar *body; + + msg = soup_message_new("POST", addr); + + params = g_value_array_new(1); + + if (param_types && *param_types) { + va_list ap; + + va_start(ap, param_types); + while (*param_types) { + switch (*param_types) { + case '%': + break; + case 'i': + soup_value_array_append(params, G_TYPE_INT, va_arg(ap, int)); + break; + case 's': + default: + soup_value_array_append(params, G_TYPE_STRING, va_arg(ap, char *)); + break; + } + + param_types++; + } + + va_end(ap); + } + + body = soup_xmlrpc_build_method_call(method, params->values, params->n_values); + g_value_array_free(params); + + soup_message_set_request(msg, "text/xml", + SOUP_MEMORY_TAKE, body, strlen(body)); + + while (lock) + g_main_iteration(FALSE); + + lock = TRUE; + soup_session_queue_message(session, msg, xmlrpc_response_get_array, &answer); + g_main_run(loop); + + return answer; +} + +#endif /* HAS_LIBSOUP */ + |