5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <gweb/gweb.h>
34 static GMainLoop *main_loop;
36 static void web_debug(const char *str, void *data)
38 g_print("%s: %s\n", (const char *) data, str);
41 static void sig_term(int sig)
43 g_main_loop_quit(main_loop);
46 static guint request_id;
48 static void web_result(guint16 status, GWebResult *result, gpointer user_data)
52 elapsed = g_timer_elapsed(timer, NULL);
54 g_print("elapse: %f seconds\n", elapsed);
56 g_print("status: %03u\n", status);
58 g_main_loop_quit(main_loop);
61 static gboolean option_debug = FALSE;
62 static gchar *option_nameserver = NULL;
63 static gchar *option_url = NULL;
65 static GOptionEntry options[] = {
66 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
67 "Enable debug output" },
68 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
69 "Specify nameserver", "ADDRESS" },
70 { "url", 'u', 0, G_OPTION_ARG_STRING, &option_url,
71 "Specify arbitrary request", "URL" },
75 int main(int argc, char *argv[])
77 GOptionContext *context;
83 context = g_option_context_new(NULL);
84 g_option_context_add_main_entries(context, options, NULL);
86 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
88 g_printerr("%s\n", error->message);
91 g_printerr("An unknown error occurred\n");
95 g_option_context_free(context);
97 web = g_web_new(index);
99 fprintf(stderr, "Failed to create web service\n");
103 if (option_debug == TRUE)
104 g_web_set_debug(web, web_debug, "WEB");
106 main_loop = g_main_loop_new(NULL, FALSE);
108 if (option_nameserver != NULL) {
109 g_web_add_nameserver(web, option_nameserver);
110 g_free(option_nameserver);
113 g_web_set_accept(web, NULL);
114 g_web_set_user_agent(web, "SmartClient/%s wispr", VERSION);
115 g_web_set_close_connection(web, TRUE);
117 if (option_url == NULL)
118 option_url = g_strdup("http://connman.net/");
120 timer = g_timer_new();
122 request_id = g_web_request(web, G_WEB_METHOD_GET, option_url,
127 if (request_id == 0) {
128 fprintf(stderr, "Failed to start request\n");
132 memset(&sa, 0, sizeof(sa));
133 sa.sa_handler = sig_term;
134 sigaction(SIGINT, &sa, NULL);
135 sigaction(SIGTERM, &sa, NULL);
137 g_main_loop_run(main_loop);
139 g_timer_destroy(timer);
143 g_main_loop_unref(main_loop);