9964f10d5e6b1194d37836a00ee6a84f748fdb8a
[profile/ivi/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., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, 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_type_init ();
101         setlocale (LC_ALL, "");
102
103         error = NULL;
104         context = g_initable_new (GUPNP_TYPE_CONTEXT, NULL, &error, NULL);
105         if (error) {
106                 g_printerr ("Error creating the GUPnP context: %s\n",
107                             error->message);
108                 g_error_free (error);
109
110                 return EXIT_FAILURE;
111         }
112
113         /* We're interested in everything */
114         cp = gupnp_control_point_new (context, "ssdp:all");
115
116         g_signal_connect (cp,
117                           "device-proxy-available",
118                           G_CALLBACK (device_proxy_available_cb),
119                           NULL);
120         g_signal_connect (cp,
121                           "device-proxy-unavailable",
122                           G_CALLBACK (device_proxy_unavailable_cb),
123                           NULL);
124         g_signal_connect (cp,
125                           "service-proxy-available",
126                           G_CALLBACK (service_proxy_available_cb),
127                           NULL);
128         g_signal_connect (cp,
129                           "service-proxy-unavailable",
130                           G_CALLBACK (service_proxy_unavailable_cb),
131                           NULL);
132
133         gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
134
135         main_loop = g_main_loop_new (NULL, FALSE);
136
137         /* Hook the handler for SIGTERM */
138         memset (&sig_action, 0, sizeof (sig_action));
139         sig_action.sa_handler = interrupt_signal_handler;
140         sigaction (SIGINT, &sig_action, NULL);
141
142         g_main_loop_run (main_loop);
143         g_main_loop_unref (main_loop);
144
145         g_object_unref (cp);
146         g_object_unref (context);
147
148         return EXIT_SUCCESS;
149 }