iptables-test: Support for user defined chain jumps
[framework/connectivity/connman.git] / tools / web-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30
31 #include <gweb/gweb.h>
32
33 static GTimer *timer;
34
35 static GMainLoop *main_loop;
36
37 static void web_debug(const char *str, void *data)
38 {
39         g_print("%s: %s\n", (const char *) data, str);
40 }
41
42 static void sig_term(int sig)
43 {
44         g_main_loop_quit(main_loop);
45 }
46
47 static void web_result(uint16_t status, gpointer user_data)
48 {
49         gdouble elapsed;
50
51         elapsed = g_timer_elapsed(timer, NULL);
52
53         g_print("elapse: %f seconds\n", elapsed);
54
55         g_print("status: %03u\n", status);
56
57         g_main_loop_quit(main_loop);
58 }
59
60 static gboolean option_debug = FALSE;
61 static gchar *option_nameserver = NULL;
62
63 static GOptionEntry options[] = {
64         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
65                                         "Enable debug output" },
66         { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
67                                         "Specify nameserver", "ADDRESS" },
68         { NULL },
69 };
70
71 int main(int argc, char *argv[])
72 {
73         GOptionContext *context;
74         GError *error = NULL;
75         struct sigaction sa;
76         GWeb *web;
77         int index = 0;
78
79         context = g_option_context_new(NULL);
80         g_option_context_add_main_entries(context, options, NULL);
81
82         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
83                 if (error != NULL) {
84                         g_printerr("%s\n", error->message);
85                         g_error_free(error);
86                 } else
87                         g_printerr("An unknown error occurred\n");
88                 exit(1);
89         }
90
91         g_option_context_free(context);
92
93         if (argc < 2) {
94                 printf("missing argument\n");
95                 return 1;
96         }
97
98         web = g_web_new(index);
99         if (web == NULL) {
100                 printf("failed to web service\n");
101                 return 1;
102         }
103
104         if (option_debug == TRUE)
105                 g_web_set_debug(web, web_debug, "WEB");
106
107         main_loop = g_main_loop_new(NULL, FALSE);
108
109         if (option_nameserver != NULL) {
110                 g_web_add_nameserver(web, option_nameserver);
111                 g_free(option_nameserver);
112         }
113
114         timer = g_timer_new();
115
116         if (g_web_request(web, G_WEB_METHOD_GET, argv[1],
117                                         web_result, NULL) == 0) {
118                 printf("failed to start request\n");
119                 return 1;
120         }
121
122         memset(&sa, 0, sizeof(sa));
123         sa.sa_handler = sig_term;
124         sigaction(SIGINT, &sa, NULL);
125         sigaction(SIGTERM, &sa, NULL);
126
127         g_main_loop_run(main_loop);
128
129         g_timer_destroy(timer);
130
131         g_web_unref(web);
132
133         g_main_loop_unref(main_loop);
134
135         return 0;
136 }