2 * Copyright (C) 2006, 2007, 2008 OpenedHand Ltd.
4 * Author: Jorn Baayen <jorn@openedhand.com>
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.
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.
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.
23 * SECTION:gssdp-resource-browser
24 * @short_description: Class handling resource discovery.
26 * #GUPnPResourceBrowser handles resource discovery. After creating a browser
27 * and activating it, the ::resource-available and ::resource-unavailable
28 * signals will be emitted whenever the availability of a resource matching the
29 * specified discovery target changes. A discovery request is sent out
30 * automatically when activating the browser.
34 #include <libsoup/soup.h>
38 #include "gssdp-resource-browser.h"
39 #include "gssdp-client-private.h"
40 #include "gssdp-protocol.h"
41 #include "gssdp-marshal.h"
43 #define MAX_DISCOVERY_MESSAGES 3
44 #define DISCOVERY_FREQUENCY 500 /* 500 ms */
46 G_DEFINE_TYPE (GSSDPResourceBrowser,
47 gssdp_resource_browser,
50 struct _GSSDPResourceBrowserPrivate {
60 gulong message_received_id;
62 GHashTable *resources;
82 static guint signals[LAST_SIGNAL];
85 GSSDPResourceBrowser *resource_browser;
90 /* Function prototypes */
92 gssdp_resource_browser_set_client (GSSDPResourceBrowser *resource_browser,
95 message_received_cb (GSSDPClient *client,
98 _GSSDPMessageType type,
99 SoupMessageHeaders *headers,
102 resource_free (gpointer data);
104 clear_cache (GSSDPResourceBrowser *resource_browser);
106 send_discovery_request (GSSDPResourceBrowser *resource_browser);
108 discovery_timeout (gpointer data);
110 start_discovery (GSSDPResourceBrowser *resource_browser);
112 stop_discovery (GSSDPResourceBrowser *resource_browser);
115 gssdp_resource_browser_init (GSSDPResourceBrowser *resource_browser)
117 resource_browser->priv = G_TYPE_INSTANCE_GET_PRIVATE
119 GSSDP_TYPE_RESOURCE_BROWSER,
120 GSSDPResourceBrowserPrivate);
122 resource_browser->priv->mx = SSDP_DEFAULT_MX;
124 resource_browser->priv->resources =
125 g_hash_table_new_full (g_str_hash,
132 gssdp_resource_browser_get_property (GObject *object,
137 GSSDPResourceBrowser *resource_browser;
139 resource_browser = GSSDP_RESOURCE_BROWSER (object);
141 switch (property_id) {
145 gssdp_resource_browser_get_client (resource_browser));
150 gssdp_resource_browser_get_target (resource_browser));
155 gssdp_resource_browser_get_mx (resource_browser));
160 gssdp_resource_browser_get_active (resource_browser));
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
169 gssdp_resource_browser_set_property (GObject *object,
174 GSSDPResourceBrowser *resource_browser;
176 resource_browser = GSSDP_RESOURCE_BROWSER (object);
178 switch (property_id) {
180 gssdp_resource_browser_set_client (resource_browser,
181 g_value_get_object (value));
184 gssdp_resource_browser_set_target (resource_browser,
185 g_value_get_string (value));
188 gssdp_resource_browser_set_mx (resource_browser,
189 g_value_get_uint (value));
192 gssdp_resource_browser_set_active (resource_browser,
193 g_value_get_boolean (value));
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
202 gssdp_resource_browser_dispose (GObject *object)
204 GSSDPResourceBrowser *resource_browser;
206 resource_browser = GSSDP_RESOURCE_BROWSER (object);
208 if (resource_browser->priv->client) {
209 if (g_signal_handler_is_connected
210 (resource_browser->priv->client,
211 resource_browser->priv->message_received_id)) {
212 g_signal_handler_disconnect
213 (resource_browser->priv->client,
214 resource_browser->priv->message_received_id);
217 stop_discovery (resource_browser);
219 g_object_unref (resource_browser->priv->client);
220 resource_browser->priv->client = NULL;
223 clear_cache (resource_browser);
225 G_OBJECT_CLASS (gssdp_resource_browser_parent_class)->dispose (object);
229 gssdp_resource_browser_finalize (GObject *object)
231 GSSDPResourceBrowser *resource_browser;
233 resource_browser = GSSDP_RESOURCE_BROWSER (object);
235 if (resource_browser->priv->target_regex)
236 g_regex_unref (resource_browser->priv->target_regex);
238 g_free (resource_browser->priv->target);
240 g_hash_table_destroy (resource_browser->priv->resources);
242 G_OBJECT_CLASS (gssdp_resource_browser_parent_class)->finalize (object);
246 gssdp_resource_browser_class_init (GSSDPResourceBrowserClass *klass)
248 GObjectClass *object_class;
250 object_class = G_OBJECT_CLASS (klass);
252 object_class->set_property = gssdp_resource_browser_set_property;
253 object_class->get_property = gssdp_resource_browser_get_property;
254 object_class->dispose = gssdp_resource_browser_dispose;
255 object_class->finalize = gssdp_resource_browser_finalize;
257 g_type_class_add_private (klass, sizeof (GSSDPResourceBrowserPrivate));
260 * GSSDPResourceBrowser:client
262 * The #GSSDPClient to use.
264 g_object_class_install_property
270 "The associated client.",
272 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
273 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
274 G_PARAM_STATIC_BLURB));
277 * GSSDPResourceBrowser:target
279 * The discovery target.
281 g_object_class_install_property
287 "The discovery target.",
290 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
291 G_PARAM_STATIC_BLURB));
294 * GSSDPResourceBrowser:mx
296 * The maximum number of seconds in which to request other parties
299 g_object_class_install_property
305 "The maximum number of seconds in which to request "
306 "other parties to respond.",
311 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
312 G_PARAM_STATIC_BLURB));
315 * GSSDPResourceBrowser:active
317 * Whether this browser is active or not.
319 g_object_class_install_property
325 "TRUE if the resource browser is active.",
328 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
329 G_PARAM_STATIC_BLURB));
332 * GSSDPResourceBrowser::resource-available
333 * @resource_browser: The #GSSDPResourceBrowser that received the
335 * @usn: The USN of the discovered resource
336 * @locations: (type GList*) (transfer none) (element-type utf8): A #GList of strings describing the locations of the
337 * discovered resource.
339 * The ::resource-available signal is emitted whenever a new resource
340 * has become available.
342 signals[RESOURCE_AVAILABLE] =
343 g_signal_new ("resource-available",
344 GSSDP_TYPE_RESOURCE_BROWSER,
346 G_STRUCT_OFFSET (GSSDPResourceBrowserClass,
349 gssdp_marshal_VOID__STRING_POINTER,
356 * GSSDPResourceBrowser::resource-unavailable
357 * @resource_browser: The #GSSDPResourceBrowser that received the
359 * @usn: The USN of the resource
361 * The ::resource-unavailable signal is emitted whenever a resource
362 * is not available any more.
364 signals[RESOURCE_UNAVAILABLE] =
365 g_signal_new ("resource-unavailable",
366 GSSDP_TYPE_RESOURCE_BROWSER,
368 G_STRUCT_OFFSET (GSSDPResourceBrowserClass,
369 resource_unavailable),
371 gssdp_marshal_VOID__STRING,
378 * gssdp_resource_browser_new
379 * @client: The #GSSDPClient to associate with
381 * Return value: A new #GSSDPResourceBrowser object.
383 GSSDPResourceBrowser *
384 gssdp_resource_browser_new (GSSDPClient *client,
387 return g_object_new (GSSDP_TYPE_RESOURCE_BROWSER,
394 * Sets the #GSSDPClient @resource_browser is associated with to @client
397 gssdp_resource_browser_set_client (GSSDPResourceBrowser *resource_browser,
400 g_return_if_fail (GSSDP_IS_RESOURCE_BROWSER (resource_browser));
401 g_return_if_fail (GSSDP_IS_CLIENT (client));
403 resource_browser->priv->client = g_object_ref (client);
405 resource_browser->priv->message_received_id =
406 g_signal_connect_object (resource_browser->priv->client,
408 G_CALLBACK (message_received_cb),
412 g_object_notify (G_OBJECT (resource_browser), "client");
416 * gssdp_resource_browser_get_client
417 * @resource_browser: A #GSSDPResourceBrowser
419 * Returns: (transfer none): The #GSSDPClient @resource_browser is associated with.
422 gssdp_resource_browser_get_client (GSSDPResourceBrowser *resource_browser)
424 g_return_val_if_fail (GSSDP_IS_RESOURCE_BROWSER (resource_browser),
427 return resource_browser->priv->client;
431 * gssdp_resource_browser_set_target
432 * @resource_browser: A #GSSDPResourceBrowser
433 * @target: The browser target
435 * Sets the browser target of @resource_browser to @target.
438 gssdp_resource_browser_set_target (GSSDPResourceBrowser *resource_browser,
443 char *version_pattern;
446 g_return_if_fail (GSSDP_IS_RESOURCE_BROWSER (resource_browser));
447 g_return_if_fail (target != NULL);
448 g_return_if_fail (!resource_browser->priv->active);
450 g_free (resource_browser->priv->target);
451 resource_browser->priv->target = g_strdup (target);
453 if (resource_browser->priv->target_regex)
454 g_regex_unref (resource_browser->priv->target_regex);
456 version_pattern = "[0-9]+";
457 /* Make sure we have enough room for version pattern */
458 pattern = g_strndup (target,
459 strlen (target) + strlen (version_pattern));
461 version = g_strrstr (pattern, ":");
462 if (version != NULL &&
463 g_regex_match_simple (version_pattern, version + 1, 0, 0)) {
464 strcpy (version + 1, version_pattern);
468 resource_browser->priv->target_regex = g_regex_new (pattern,
473 g_warning ("Error compiling regular expression '%s': %s",
477 g_error_free (error);
481 g_object_notify (G_OBJECT (resource_browser), "target");
485 * gssdp_resource_browser_get_target
486 * @resource_browser: A #GSSDPResourceBrowser
488 * Return value: The browser target.
491 gssdp_resource_browser_get_target (GSSDPResourceBrowser *resource_browser)
493 g_return_val_if_fail (GSSDP_IS_RESOURCE_BROWSER (resource_browser),
496 return resource_browser->priv->target;
500 * gssdp_resource_browser_set_mx
501 * @resource_browser: A #GSSDPResourceBrowser
502 * @mx: The to be used MX value
504 * Sets the used MX value of @resource_browser to @mx.
507 gssdp_resource_browser_set_mx (GSSDPResourceBrowser *resource_browser,
510 g_return_if_fail (GSSDP_IS_RESOURCE_BROWSER (resource_browser));
512 if (resource_browser->priv->mx == mx)
515 resource_browser->priv->mx = mx;
517 g_object_notify (G_OBJECT (resource_browser), "mx");
521 * gssdp_resource_browser_get_mx
522 * @resource_browser: A #GSSDPResourceBrowser
524 * Return value: The used MX value.
527 gssdp_resource_browser_get_mx (GSSDPResourceBrowser *resource_browser)
529 g_return_val_if_fail (GSSDP_IS_RESOURCE_BROWSER (resource_browser), 0);
531 return resource_browser->priv->mx;
535 * gssdp_resource_browser_set_active
536 * @resource_browser: A #GSSDPResourceBrowser
537 * @active: TRUE to activate @resource_browser
539 * (De)activates @resource_browser.
542 gssdp_resource_browser_set_active (GSSDPResourceBrowser *resource_browser,
545 g_return_if_fail (GSSDP_IS_RESOURCE_BROWSER (resource_browser));
547 if (resource_browser->priv->active == active)
550 resource_browser->priv->active = active;
553 start_discovery (resource_browser);
555 stop_discovery (resource_browser);
557 clear_cache (resource_browser);
560 g_object_notify (G_OBJECT (resource_browser), "active");
564 * gssdp_resource_browser_get_active
565 * @resource_browser: A #GSSDPResourceBrowser
567 * Return value: TRUE if @resource_browser is active.
570 gssdp_resource_browser_get_active (GSSDPResourceBrowser *resource_browser)
572 g_return_val_if_fail (GSSDP_IS_RESOURCE_BROWSER (resource_browser), 0);
574 return resource_browser->priv->active;
578 * Resource expired: Remove
581 resource_expire (gpointer user_data)
583 GSSDPResourceBrowser *resource_browser;
587 resource = user_data;
588 resource_browser = resource->resource_browser;
590 /* Steal the USN pointer from the resource as we need it for the signal
594 resource->usn = NULL;
596 g_hash_table_remove (resource->resource_browser->priv->resources, usn);
598 g_signal_emit (resource_browser,
599 signals[RESOURCE_UNAVAILABLE],
608 resource_available (GSSDPResourceBrowser *resource_browser,
609 SoupMessageHeaders *headers)
617 GMainContext *context;
619 usn = soup_message_headers_get_one (headers, "USN");
621 return; /* No USN specified */
623 /* Get from cache, if possible */
624 resource = g_hash_table_lookup (resource_browser->priv->resources, usn);
626 /* Remove old timeout */
627 g_source_destroy (resource->timeout_src);
631 /* Create new Resource data structure */
632 resource = g_slice_new (Resource);
634 resource->resource_browser = resource_browser;
635 resource->usn = g_strdup (usn);
637 g_hash_table_insert (resource_browser->priv->resources,
644 /* Calculate new timeout */
645 header = soup_message_headers_get_one (headers, "Cache-Control");
652 for (list = soup_header_parse_list (header);
655 res = sscanf (list->data,
663 g_warning ("Invalid 'Cache-Control' header. Assuming "
664 "default max-age of %d.\n"
666 SSDP_DEFAULT_MAX_AGE,
669 timeout = SSDP_DEFAULT_MAX_AGE;
672 soup_header_free_list (list);
676 expires = soup_message_headers_get_one (headers, "Expires");
678 SoupDate *soup_exp_time;
679 time_t exp_time, cur_time;
681 soup_exp_time = soup_date_new_from_string (expires);
682 exp_time = soup_date_to_time_t (soup_exp_time);
683 soup_date_free (soup_exp_time);
685 cur_time = time (NULL);
687 if (exp_time > cur_time)
688 timeout = exp_time - cur_time;
690 g_warning ("Invalid 'Expires' header. Assuming "
691 "default max-age of %d.\n"
693 SSDP_DEFAULT_MAX_AGE,
696 timeout = SSDP_DEFAULT_MAX_AGE;
699 g_warning ("No 'Cache-Control' nor any 'Expires' "
700 "header was specified. Assuming default "
701 "max-age of %d.", SSDP_DEFAULT_MAX_AGE);
703 timeout = SSDP_DEFAULT_MAX_AGE;
707 resource->timeout_src = g_timeout_source_new_seconds (timeout);
708 g_source_set_callback (resource->timeout_src,
712 context = gssdp_client_get_main_context
713 (resource_browser->priv->client);
714 g_source_attach (resource->timeout_src, context);
716 g_source_unref (resource->timeout_src);
718 /* Only continue with signal emission if this resource was not
723 /* Build list of locations */
726 header = soup_message_headers_get_one (headers, "Location");
728 locations = g_list_append (locations, g_strdup (header));
730 header = soup_message_headers_get_one (headers, "AL");
732 /* Parse AL header. The format is:
734 const char *start, *end;
738 while ((start = strchr (start, '<'))) {
740 if (!start || !*start)
743 end = strchr (start, '>');
747 uri = g_strndup (start, end - start);
748 locations = g_list_append (locations, uri);
755 g_signal_emit (resource_browser,
756 signals[RESOURCE_AVAILABLE],
763 g_free (locations->data);
765 locations = g_list_delete_link (locations, locations);
770 resource_unavailable (GSSDPResourceBrowser *resource_browser,
771 SoupMessageHeaders *headers)
775 usn = soup_message_headers_get_one (headers, "USN");
777 return; /* No USN specified */
779 /* Only process if we were cached */
780 if (!g_hash_table_lookup (resource_browser->priv->resources, usn))
783 g_hash_table_remove (resource_browser->priv->resources, usn);
785 g_signal_emit (resource_browser,
786 signals[RESOURCE_UNAVAILABLE],
792 check_target_compat (GSSDPResourceBrowser *resource_browser,
795 return strcmp (resource_browser->priv->target,
796 GSSDP_ALL_RESOURCES) == 0 ||
797 g_regex_match (resource_browser->priv->target_regex,
804 received_discovery_response (GSSDPResourceBrowser *resource_browser,
805 SoupMessageHeaders *headers)
809 st = soup_message_headers_get_one (headers, "ST");
811 return; /* No target specified */
813 if (!check_target_compat (resource_browser, st))
814 return; /* Target doesn't match */
816 resource_available (resource_browser, headers);
820 received_announcement (GSSDPResourceBrowser *resource_browser,
821 SoupMessageHeaders *headers)
825 header = soup_message_headers_get_one (headers, "NT");
827 return; /* No target specified */
829 if (!check_target_compat (resource_browser, header))
830 return; /* Target doesn't match */
832 header = soup_message_headers_get_one (headers, "NTS");
834 return; /* No announcement type specified */
836 /* Check announcement type */
839 strlen (SSDP_ALIVE_NTS)) == 0)
840 resource_available (resource_browser, headers);
841 else if (strncmp (header,
843 strlen (SSDP_BYEBYE_NTS)) == 0)
844 resource_unavailable (resource_browser, headers);
851 message_received_cb (GSSDPClient *client,
854 _GSSDPMessageType type,
855 SoupMessageHeaders *headers,
858 GSSDPResourceBrowser *resource_browser;
860 resource_browser = GSSDP_RESOURCE_BROWSER (user_data);
862 if (!resource_browser->priv->active)
866 case _GSSDP_DISCOVERY_RESPONSE:
867 received_discovery_response (resource_browser, headers);
869 case _GSSDP_ANNOUNCEMENT:
870 received_announcement (resource_browser, headers);
878 * Free a Resource structure and its contained data
881 resource_free (gpointer data)
887 g_free (resource->usn);
889 g_source_destroy (resource->timeout_src);
891 g_slice_free (Resource, resource);
895 clear_cache_helper (gpointer key, gpointer value, gpointer data)
901 g_signal_emit (resource->resource_browser,
902 signals[RESOURCE_UNAVAILABLE],
910 * Clears the cached resources hash
913 clear_cache (GSSDPResourceBrowser *resource_browser)
916 g_hash_table_foreach_remove (resource_browser->priv->resources,
921 /* Sends discovery request */
923 send_discovery_request (GSSDPResourceBrowser *resource_browser)
927 message = g_strdup_printf (SSDP_DISCOVERY_REQUEST,
928 resource_browser->priv->target,
929 resource_browser->priv->mx,
930 g_get_application_name () ?: "");
932 _gssdp_client_send_message (resource_browser->priv->client,
941 discovery_timeout (gpointer data)
943 GSSDPResourceBrowser *resource_browser;
945 resource_browser = GSSDP_RESOURCE_BROWSER (data);
947 send_discovery_request (resource_browser);
949 resource_browser->priv->num_discovery += 1;
951 if (resource_browser->priv->num_discovery >= MAX_DISCOVERY_MESSAGES) {
952 resource_browser->priv->timeout_src = NULL;
953 resource_browser->priv->num_discovery = 0;
960 /* Starts sending discovery requests */
962 start_discovery (GSSDPResourceBrowser *resource_browser)
964 GMainContext *context;
967 send_discovery_request (resource_browser);
969 /* And schedule the rest for later */
970 resource_browser->priv->num_discovery = 1;
971 resource_browser->priv->timeout_src =
972 g_timeout_source_new (DISCOVERY_FREQUENCY);
973 g_source_set_callback (resource_browser->priv->timeout_src,
975 resource_browser, NULL);
977 context = gssdp_client_get_main_context
978 (resource_browser->priv->client);
979 g_source_attach (resource_browser->priv->timeout_src, context);
981 g_source_unref (resource_browser->priv->timeout_src);
984 /* Stops the sending of discovery messages */
986 stop_discovery (GSSDPResourceBrowser *resource_browser)
988 if (resource_browser->priv->timeout_src) {
989 g_source_destroy (resource_browser->priv->timeout_src);
990 resource_browser->priv->timeout_src = NULL;
991 resource_browser->priv->num_discovery = 0;