da294cf8a2cfa5efe9baeeaa8b91b8a8df70b717
[profile/ivi/GUPnP.git] / tests / test-server.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-root-device.h>
23 #include <libgupnp/gupnp-service.h>
24 #include <stdlib.h>
25 #include <locale.h>
26 #include <string.h>
27 #include <signal.h>
28
29 GMainLoop *main_loop;
30
31 static void
32 interrupt_signal_handler (int signum)
33 {
34         g_main_loop_quit (main_loop);
35 }
36
37 static void
38 notify_failed_cb (GUPnPService *service,
39                   const GList  *callback_urls,
40                   const GError *reason,
41                   gpointer      user_data)
42 {
43         g_print ("NOTIFY failed: %s\n", reason->message);
44 }
45
46 static gboolean
47 timeout (gpointer user_data)
48 {
49         gupnp_service_notify (GUPNP_SERVICE (user_data),
50                               "SystemUpdateID",
51                               G_TYPE_UINT,
52                               27182818,
53                               NULL);
54
55         return FALSE;
56 }
57
58 int
59 main (int argc, char **argv)
60 {
61         GError *error;
62         GUPnPContext *context;
63         GUPnPRootDevice *dev;
64         GUPnPServiceInfo *content_dir;
65         struct sigaction sig_action;
66
67         if (argc < 2) {
68                 g_printerr ("Usage: %s DESCRIPTION_FILE\n", argv[0]);
69
70                 return EXIT_FAILURE;
71         }
72
73         g_type_init ();
74         setlocale (LC_ALL, "");
75
76         error = NULL;
77         context = g_initable_new (GUPNP_TYPE_CONTEXT, NULL, &error, NULL);
78         if (error) {
79                 g_printerr ("Error creating the GUPnP context: %s\n",
80                             error->message);
81                 g_error_free (error);
82
83                 return EXIT_FAILURE;
84         }
85
86         g_print ("Running on port %d\n", gupnp_context_get_port (context));
87
88         /* Create root device */
89         dev = gupnp_root_device_new (context, "description.xml", ".");
90
91         /* Implement Browse action on ContentDirectory if available */
92         content_dir = gupnp_device_info_get_service
93                          (GUPNP_DEVICE_INFO (dev),
94                           "urn:schemas-upnp-org:service:ContentDirectory:1");
95
96         if (content_dir) {
97                 gupnp_service_signals_autoconnect (GUPNP_SERVICE (content_dir),
98                                                    NULL,
99                                                    &error);
100                 if (error) {
101                         g_warning ("Failed to autoconnect signals: %s",
102                                    error->message);
103
104                         g_error_free (error);
105                         error = NULL;
106                 }
107
108                 g_signal_connect (content_dir,
109                                   "notify-failed",
110                                   G_CALLBACK (notify_failed_cb),
111                                   NULL);
112
113                 g_timeout_add (5000, timeout, content_dir);
114         }
115
116         /* Run */
117         gupnp_root_device_set_available (dev, TRUE);
118
119         main_loop = g_main_loop_new (NULL, FALSE);
120
121         /* Hook the handler for SIGTERM */
122         memset (&sig_action, 0, sizeof (sig_action));
123         sig_action.sa_handler = interrupt_signal_handler;
124         sigaction (SIGINT, &sig_action, NULL);
125
126         g_main_loop_run (main_loop);
127         g_main_loop_unref (main_loop);
128
129         if (content_dir)
130                 g_object_unref (content_dir);
131
132         g_object_unref (dev);
133         g_object_unref (context);
134
135         return EXIT_SUCCESS;
136 }