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: 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 * Return value: 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, ":") + 1;
462 if (version != NULL &&
463 g_regex_match_simple (version_pattern, version, 0, 0)) {
464 strcpy (version, 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)
586 resource = user_data;
588 /* Steal the USN pointer from the resource as we need it for the signal
592 resource->usn = NULL;
594 g_hash_table_remove (resource->resource_browser->priv->resources, usn);
596 g_signal_emit (resource->resource_browser,
597 signals[RESOURCE_UNAVAILABLE],
606 resource_available (GSSDPResourceBrowser *resource_browser,
607 SoupMessageHeaders *headers)
615 GMainContext *context;
617 usn = soup_message_headers_get_one (headers, "USN");
619 return; /* No USN specified */
621 /* Get from cache, if possible */
622 resource = g_hash_table_lookup (resource_browser->priv->resources, usn);
624 /* Remove old timeout */
625 g_source_destroy (resource->timeout_src);
629 /* Create new Resource data structure */
630 resource = g_slice_new (Resource);
632 resource->resource_browser = resource_browser;
633 resource->usn = g_strdup (usn);
635 g_hash_table_insert (resource_browser->priv->resources,
642 /* Calculate new timeout */
643 header = soup_message_headers_get_one (headers, "Cache-Control");
650 for (list = soup_header_parse_list (header);
653 res = sscanf (list->data,
661 g_warning ("Invalid 'Cache-Control' header. Assuming "
662 "default max-age of %d.\n"
664 SSDP_DEFAULT_MAX_AGE,
667 timeout = SSDP_DEFAULT_MAX_AGE;
670 soup_header_free_list (list);
674 expires = soup_message_headers_get_one (headers, "Expires");
676 SoupDate *soup_exp_time;
677 time_t exp_time, cur_time;
679 soup_exp_time = soup_date_new_from_string (expires);
680 exp_time = soup_date_to_time_t (soup_exp_time);
681 soup_date_free (soup_exp_time);
683 cur_time = time (NULL);
685 if (exp_time > cur_time)
686 timeout = exp_time - cur_time;
688 g_warning ("Invalid 'Expires' header. Assuming "
689 "default max-age of %d.\n"
691 SSDP_DEFAULT_MAX_AGE,
694 timeout = SSDP_DEFAULT_MAX_AGE;
697 g_warning ("No 'Cache-Control' nor any 'Expires' "
698 "header was specified. Assuming default "
699 "max-age of %d.", SSDP_DEFAULT_MAX_AGE);
701 timeout = SSDP_DEFAULT_MAX_AGE;
705 resource->timeout_src = g_timeout_source_new_seconds (timeout);
706 g_source_set_callback (resource->timeout_src,
710 context = gssdp_client_get_main_context
711 (resource_browser->priv->client);
712 g_source_attach (resource->timeout_src, context);
714 g_source_unref (resource->timeout_src);
716 /* Only continue with signal emission if this resource was not
721 /* Build list of locations */
724 header = soup_message_headers_get_one (headers, "Location");
726 locations = g_list_append (locations, g_strdup (header));
728 header = soup_message_headers_get_one (headers, "AL");
730 /* Parse AL header. The format is:
732 const char *start, *end;
736 while ((start = strchr (start, '<'))) {
738 if (!start || !*start)
741 end = strchr (start, '>');
745 uri = g_strndup (start, end - start);
746 locations = g_list_append (locations, uri);
753 g_signal_emit (resource_browser,
754 signals[RESOURCE_AVAILABLE],
761 g_free (locations->data);
763 locations = g_list_delete_link (locations, locations);
768 resource_unavailable (GSSDPResourceBrowser *resource_browser,
769 SoupMessageHeaders *headers)
773 usn = soup_message_headers_get_one (headers, "USN");
775 return; /* No USN specified */
777 /* Only process if we were cached */
778 if (!g_hash_table_lookup (resource_browser->priv->resources, usn))
781 g_hash_table_remove (resource_browser->priv->resources, usn);
783 g_signal_emit (resource_browser,
784 signals[RESOURCE_UNAVAILABLE],
790 check_target_compat (GSSDPResourceBrowser *resource_browser,
793 return strcmp (resource_browser->priv->target,
794 GSSDP_ALL_RESOURCES) == 0 ||
795 g_regex_match (resource_browser->priv->target_regex,
802 received_discovery_response (GSSDPResourceBrowser *resource_browser,
803 SoupMessageHeaders *headers)
807 st = soup_message_headers_get_one (headers, "ST");
809 return; /* No target specified */
811 if (!check_target_compat (resource_browser, st))
812 return; /* Target doesn't match */
814 resource_available (resource_browser, headers);
818 received_announcement (GSSDPResourceBrowser *resource_browser,
819 SoupMessageHeaders *headers)
823 header = soup_message_headers_get_one (headers, "NT");
825 return; /* No target specified */
827 if (!check_target_compat (resource_browser, header))
828 return; /* Target doesn't match */
830 header = soup_message_headers_get_one (headers, "NTS");
832 return; /* No announcement type specified */
834 /* Check announcement type */
837 strlen (SSDP_ALIVE_NTS)) == 0)
838 resource_available (resource_browser, headers);
839 else if (strncmp (header,
841 strlen (SSDP_BYEBYE_NTS)) == 0)
842 resource_unavailable (resource_browser, headers);
849 message_received_cb (GSSDPClient *client,
852 _GSSDPMessageType type,
853 SoupMessageHeaders *headers,
856 GSSDPResourceBrowser *resource_browser;
858 resource_browser = GSSDP_RESOURCE_BROWSER (user_data);
860 if (!resource_browser->priv->active)
864 case _GSSDP_DISCOVERY_RESPONSE:
865 received_discovery_response (resource_browser, headers);
867 case _GSSDP_ANNOUNCEMENT:
868 received_announcement (resource_browser, headers);
876 * Free a Resource structure and its contained data
879 resource_free (gpointer data)
885 g_free (resource->usn);
887 g_source_destroy (resource->timeout_src);
889 g_slice_free (Resource, resource);
893 clear_cache_helper (gpointer key, gpointer value, gpointer data)
899 g_signal_emit (resource->resource_browser,
900 signals[RESOURCE_UNAVAILABLE],
908 * Clears the cached resources hash
911 clear_cache (GSSDPResourceBrowser *resource_browser)
914 g_hash_table_foreach_remove (resource_browser->priv->resources,
919 /* Sends discovery request */
921 send_discovery_request (GSSDPResourceBrowser *resource_browser)
925 message = g_strdup_printf (SSDP_DISCOVERY_REQUEST,
926 resource_browser->priv->target,
927 resource_browser->priv->mx,
928 g_get_application_name () ?: "");
930 _gssdp_client_send_message (resource_browser->priv->client,
939 discovery_timeout (gpointer data)
941 GSSDPResourceBrowser *resource_browser;
943 resource_browser = GSSDP_RESOURCE_BROWSER (data);
945 send_discovery_request (resource_browser);
947 resource_browser->priv->num_discovery += 1;
949 if (resource_browser->priv->num_discovery >= MAX_DISCOVERY_MESSAGES) {
950 resource_browser->priv->timeout_src = NULL;
951 resource_browser->priv->num_discovery = 0;
958 /* Starts sending discovery requests */
960 start_discovery (GSSDPResourceBrowser *resource_browser)
962 GMainContext *context;
965 send_discovery_request (resource_browser);
967 /* And schedule the rest for later */
968 resource_browser->priv->num_discovery = 1;
969 resource_browser->priv->timeout_src =
970 g_timeout_source_new (DISCOVERY_FREQUENCY);
971 g_source_set_callback (resource_browser->priv->timeout_src,
973 resource_browser, NULL);
975 context = gssdp_client_get_main_context
976 (resource_browser->priv->client);
977 g_source_attach (resource_browser->priv->timeout_src, context);
979 g_source_unref (resource_browser->priv->timeout_src);
982 /* Stops the sending of discovery messages */
984 stop_discovery (GSSDPResourceBrowser *resource_browser)
986 if (resource_browser->priv->timeout_src) {
987 g_source_destroy (resource_browser->priv->timeout_src);
988 resource_browser->priv->timeout_src = NULL;
989 resource_browser->priv->num_discovery = 0;