tizen 2.3.1 release
[external/gupnp.git] / tests / test-proxy.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 subscription_lost_cb (GUPnPServiceProxy *proxy,
38                       const GError      *reason,
39                       gpointer           user_data)
40 {
41         g_print ("Lost subscription: %s\n", reason->message);
42 }
43
44 static void
45 notify_cb (GUPnPServiceProxy *proxy,
46            const char        *variable,
47            GValue            *value,
48            gpointer           user_data)
49 {
50         g_print ("Received a notification for variable '%s':\n", variable);
51         g_print ("\tvalue:     %d\n", g_value_get_uint (value));
52         g_print ("\tuser_data: %s\n", (const char *) user_data);
53 }
54
55 static void
56 service_proxy_available_cb (GUPnPControlPoint *cp,
57                             GUPnPServiceProxy *proxy)
58 {
59         const char *location;
60         char *result = NULL;
61         guint count, total;
62         GError *error = NULL;
63
64         location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));
65
66         g_print ("ContentDirectory available:\n");
67         g_print ("\tlocation: %s\n", location);
68
69         /* We want to be notified whenever SystemUpdateID (of type uint)
70          * changes */
71         gupnp_service_proxy_add_notify (proxy,
72                                         "SystemUpdateID",
73                                         G_TYPE_UINT,
74                                         notify_cb,
75                                         (gpointer) "Test");
76
77         /* Subscribe */
78         g_signal_connect (proxy,
79                           "subscription-lost",
80                           G_CALLBACK (subscription_lost_cb),
81                           NULL);
82
83         gupnp_service_proxy_set_subscribed (proxy, TRUE);
84
85         /* And test action IO */
86         gupnp_service_proxy_send_action (proxy,
87                                          "Browse",
88                                          &error,
89                                          /* IN args */
90                                          "ObjectID",
91                                                 G_TYPE_STRING,
92                                                 "0",
93                                          "BrowseFlag",
94                                                 G_TYPE_STRING,
95                                                 "BrowseDirectChildren",
96                                          "Filter",
97                                                 G_TYPE_STRING,
98                                                 "*",
99                                          "StartingIndex",
100                                                 G_TYPE_UINT,
101                                                 0,
102                                          "RequestedCount",
103                                                 G_TYPE_UINT,
104                                                 0,
105                                          "SortCriteria",
106                                                 G_TYPE_STRING,
107                                                 "",
108                                          NULL,
109                                          /* OUT args */
110                                          "Result",
111                                                 G_TYPE_STRING,
112                                                 &result,
113                                          "NumberReturned",
114                                                 G_TYPE_UINT,
115                                                 &count,
116                                          "TotalMatches",
117                                                 G_TYPE_UINT,
118                                                 &total,
119                                          NULL);
120
121         if (error) {
122                 g_printerr ("Error: %s\n", error->message);
123                 g_error_free (error);
124
125                 return;
126         }
127
128         g_print ("Browse returned:\n");
129         g_print ("\tResult:         %s\n", result);
130         g_print ("\tNumberReturned: %u\n", count);
131         g_print ("\tTotalMatches:   %u\n", total);
132
133         g_free (result);
134 }
135
136 static void
137 service_proxy_unavailable_cb (GUPnPControlPoint *cp,
138                               GUPnPServiceProxy *proxy)
139 {
140         const char *location;
141
142         location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));
143
144         g_print ("ContentDirectory unavailable:\n");
145         g_print ("\tlocation: %s\n", location);
146 }
147
148 int
149 main (int argc, char **argv)
150 {
151         GError *error;
152         GUPnPContext *context;
153         GUPnPControlPoint *cp;
154         struct sigaction sig_action;
155
156         g_thread_init (NULL);
157         g_type_init ();
158         setlocale (LC_ALL, "");
159
160         error = NULL;
161         context = gupnp_context_new (NULL, NULL, 0, &error);
162         if (error) {
163                 g_printerr ("Error creating the GUPnP context: %s\n",
164                             error->message);
165                 g_error_free (error);
166
167                 return EXIT_FAILURE;
168         }
169
170         /* We're interested in everything */
171         cp = gupnp_control_point_new
172                 (context, "urn:schemas-upnp-org:service:ContentDirectory:1");
173
174         g_signal_connect (cp,
175                           "service-proxy-available",
176                           G_CALLBACK (service_proxy_available_cb),
177                           NULL);
178         g_signal_connect (cp,
179                           "service-proxy-unavailable",
180                           G_CALLBACK (service_proxy_unavailable_cb),
181                           NULL);
182
183         gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
184
185         main_loop = g_main_loop_new (NULL, FALSE);
186
187         /* Hook the handler for SIGTERM */
188         memset (&sig_action, 0, sizeof (sig_action));
189         sig_action.sa_handler = interrupt_signal_handler;
190         sigaction (SIGINT, &sig_action, NULL);
191
192         g_main_loop_run (main_loop);
193         g_main_loop_unref (main_loop);
194
195         g_object_unref (cp);
196         g_object_unref (context);
197
198         return EXIT_SUCCESS;
199 }