tizen 2.3.1 release
[external/gupnp.git] / tests / test-browsing.c
1 /*
2  * Copyright (C) 2007 OpenedHand Ltd.
3  *
4  * Author: Jorn Baayen <jorn@openedhand.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <libgupnp/gupnp-control-point.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <locale.h>
26 #include <signal.h>
27
28 GMainLoop *main_loop;
29
30 static void
31 interrupt_signal_handler (int signum)
32 {
33         g_main_loop_quit (main_loop);
34 }
35
36 static void
37 device_proxy_available_cb (GUPnPControlPoint *cp,
38                            GUPnPDeviceProxy  *proxy)
39 {
40         const char *type, *location;
41
42         type = gupnp_device_info_get_device_type (GUPNP_DEVICE_INFO (proxy));
43         location = gupnp_device_info_get_location (GUPNP_DEVICE_INFO (proxy));
44
45         g_print ("Device available:\n");
46         g_print ("\ttype:     %s\n", type);
47         g_print ("\tlocation: %s\n", location);
48 }
49
50 static void
51 device_proxy_unavailable_cb (GUPnPControlPoint *cp,
52                              GUPnPDeviceProxy  *proxy)
53 {
54         const char *type, *location;
55
56         type = gupnp_device_info_get_device_type (GUPNP_DEVICE_INFO (proxy));
57         location = gupnp_device_info_get_location (GUPNP_DEVICE_INFO (proxy));
58
59         g_print ("Device unavailable:\n");
60         g_print ("\ttype:     %s\n", type);
61         g_print ("\tlocation: %s\n", location);
62 }
63
64 static void
65 service_proxy_available_cb (GUPnPControlPoint *cp,
66                             GUPnPServiceProxy *proxy)
67 {
68         const char *type, *location;
69
70         type = gupnp_service_info_get_service_type (GUPNP_SERVICE_INFO (proxy));
71         location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));
72
73         g_print ("Service available:\n");
74         g_print ("\ttype:     %s\n", type);
75         g_print ("\tlocation: %s\n", location);
76 }
77
78 static void
79 service_proxy_unavailable_cb (GUPnPControlPoint *cp,
80                               GUPnPServiceProxy *proxy)
81 {
82         const char *type, *location;
83
84         type = gupnp_service_info_get_service_type (GUPNP_SERVICE_INFO (proxy));
85         location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));
86
87         g_print ("Service unavailable:\n");
88         g_print ("\ttype:     %s\n", type);
89         g_print ("\tlocation: %s\n", location);
90 }
91
92 int
93 main (int argc, char **argv)
94 {
95         GError *error;
96         GUPnPContext *context;
97         GUPnPControlPoint *cp;
98         struct sigaction sig_action;
99
100         g_thread_init (NULL);
101         g_type_init ();
102         setlocale (LC_ALL, "");
103
104         error = NULL;
105         context = gupnp_context_new (NULL, NULL, 0, &error);
106         if (error) {
107                 g_printerr ("Error creating the GUPnP context: %s\n",
108                             error->message);
109                 g_error_free (error);
110
111                 return EXIT_FAILURE;
112         }
113
114         /* We're interested in everything */
115         cp = gupnp_control_point_new (context, "ssdp:all");
116
117         g_signal_connect (cp,
118                           "device-proxy-available",
119                           G_CALLBACK (device_proxy_available_cb),
120                           NULL);
121         g_signal_connect (cp,
122                           "device-proxy-unavailable",
123                           G_CALLBACK (device_proxy_unavailable_cb),
124                           NULL);
125         g_signal_connect (cp,
126                           "service-proxy-available",
127                           G_CALLBACK (service_proxy_available_cb),
128                           NULL);
129         g_signal_connect (cp,
130                           "service-proxy-unavailable",
131                           G_CALLBACK (service_proxy_unavailable_cb),
132                           NULL);
133
134         gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
135
136         main_loop = g_main_loop_new (NULL, FALSE);
137
138         /* Hook the handler for SIGTERM */
139         memset (&sig_action, 0, sizeof (sig_action));
140         sig_action.sa_handler = interrupt_signal_handler;
141         sigaction (SIGINT, &sig_action, NULL);
142
143         g_main_loop_run (main_loop);
144         g_main_loop_unref (main_loop);
145
146         g_object_unref (cp);
147         g_object_unref (context);
148
149         return EXIT_SUCCESS;
150 }