tizen 2.3.1 release
[external/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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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_thread_init (NULL);
74         g_type_init ();
75         setlocale (LC_ALL, "");
76
77         error = NULL;
78         context = gupnp_context_new (NULL, NULL, 0, &error);
79         if (error) {
80                 g_printerr ("Error creating the GUPnP context: %s\n",
81                             error->message);
82                 g_error_free (error);
83
84                 return EXIT_FAILURE;
85         }
86
87         g_print ("Running on port %d\n", gupnp_context_get_port (context));
88
89         /* Create root device */
90         dev = gupnp_root_device_new (context, "description.xml", ".");
91
92         /* Implement Browse action on ContentDirectory if available */
93         content_dir = gupnp_device_info_get_service
94                          (GUPNP_DEVICE_INFO (dev),
95                           "urn:schemas-upnp-org:service:ContentDirectory:1");
96
97         if (content_dir) {
98                 gupnp_service_signals_autoconnect (GUPNP_SERVICE (content_dir),
99                                                    NULL,
100                                                    &error);
101                 if (error) {
102                         g_warning ("Failed to autoconnect signals: %s",
103                                    error->message);
104
105                         g_error_free (error);
106                         error = NULL;
107                 }
108
109                 g_signal_connect (content_dir,
110                                   "notify-failed",
111                                   G_CALLBACK (notify_failed_cb),
112                                   NULL);
113
114                 g_timeout_add (5000, timeout, content_dir);
115         }
116
117         /* Run */
118         gupnp_root_device_set_available (dev, TRUE);
119
120         main_loop = g_main_loop_new (NULL, FALSE);
121
122         /* Hook the handler for SIGTERM */
123         memset (&sig_action, 0, sizeof (sig_action));
124         sig_action.sa_handler = interrupt_signal_handler;
125         sigaction (SIGINT, &sig_action, NULL);
126
127         g_main_loop_run (main_loop);
128         g_main_loop_unref (main_loop);
129
130         if (content_dir)
131                 g_object_unref (content_dir);
132
133         g_object_unref (dev);
134         g_object_unref (context);
135
136         return EXIT_SUCCESS;
137 }