SsdpRootDevice.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using Rssdp.Infrastructure;
  7. namespace Rssdp
  8. {
  9. /// <summary>
  10. /// Represents a 'root' device, a device that has no parent. Used for publishing devices and for the root device in a tree of discovered devices.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>Child (embedded) devices are represented by the <see cref="SsdpDevice"/> in the <see cref="SsdpDevice.Devices"/> property.</para>
  14. /// <para>Root devices contain some information that applies to the whole device tree and is therefore not present on child devices, such as <see cref="CacheLifetime"/> and <see cref="Location"/>.</para>
  15. /// </remarks>
  16. public class SsdpRootDevice : SsdpDevice
  17. {
  18. #region Fields
  19. private Uri _UrlBase;
  20. #endregion
  21. #region Constructors
  22. /// <summary>
  23. /// Default constructor.
  24. /// </summary>
  25. public SsdpRootDevice() : base()
  26. {
  27. }
  28. /// <summary>
  29. /// Deserialisation constructor.
  30. /// </summary>
  31. /// <param name="location">The url from which the device description document was retrieved.</param>
  32. /// <param name="cacheLifetime">A <see cref="System.TimeSpan"/> representing the time maximum period of time the device description can be cached for.</param>
  33. /// <param name="deviceDescriptionXml">The device description XML as a string.</param>
  34. /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="deviceDescriptionXml"/> or <paramref name="location"/> arguments are null.</exception>
  35. /// <exception cref="System.ArgumentException">Thrown if the <paramref name="deviceDescriptionXml"/> argument is empty.</exception>
  36. public SsdpRootDevice(Uri location, TimeSpan cacheLifetime, string deviceDescriptionXml)
  37. : base(deviceDescriptionXml)
  38. {
  39. if (location == null) throw new ArgumentNullException("location");
  40. this.CacheLifetime = cacheLifetime;
  41. this.Location = location;
  42. LoadFromDescriptionDocument(deviceDescriptionXml);
  43. }
  44. #endregion
  45. #region Public Properties
  46. /// <summary>
  47. /// Specifies how long clients can cache this device's details for. Optional but defaults to <see cref="TimeSpan.Zero"/> which means no-caching. Recommended value is half an hour.
  48. /// </summary>
  49. /// <remarks>
  50. /// <para>Specifiy <see cref="TimeSpan.Zero"/> to indicate no caching allowed.</para>
  51. /// <para>Also used to specify how often to rebroadcast alive notifications.</para>
  52. /// <para>The UPnP/SSDP specifications indicate this should not be less than 1800 seconds (half an hour), but this is not enforced by this library.</para>
  53. /// </remarks>
  54. public TimeSpan CacheLifetime
  55. {
  56. get; set;
  57. }
  58. /// <summary>
  59. /// Gets or sets the URL used to retrieve the description document for this device/tree. Required.
  60. /// </summary>
  61. public Uri Location { get; set; }
  62. /// <summary>
  63. /// The base URL to use for all relative url's provided in other propertise (and those of child devices). Optional.
  64. /// </summary>
  65. /// <remarks>
  66. /// <para>Defines the base URL. Used to construct fully-qualified URLs. All relative URLs that appear elsewhere in the description are combined with this base URL. If URLBase is empty or not given, the base URL is the URL from which the device description was retrieved (which is the preferred implementation; use of URLBase is no longer recommended). Specified by UPnP vendor. Single URL.</para>
  67. /// </remarks>
  68. public Uri UrlBase
  69. {
  70. get
  71. {
  72. return _UrlBase ?? this.Location;
  73. }
  74. set
  75. {
  76. _UrlBase = value;
  77. }
  78. }
  79. #endregion
  80. #region Public Methods
  81. /// <summary>
  82. /// Saves the property values of this device object to an a string in the full UPnP device description XML format, including child devices and outer root node and XML document declaration.
  83. /// </summary>
  84. /// <returns>A string containing XML in the UPnP device description format</returns>
  85. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Dispsoing memory stream twice is 'safe' and easier to read than correct code for ensuring it is only closed once.")]
  86. public virtual string ToDescriptionDocument()
  87. {
  88. if (String.IsNullOrEmpty(this.Uuid)) throw new InvalidOperationException("Must provide a UUID value.");
  89. //This would have been so much nicer with Xml.Linq, but that's
  90. //not available until .NET 4.03 at the earliest, and I want to
  91. //target 4.0 :(
  92. using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
  93. {
  94. System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(ms, new XmlWriterSettings() { Encoding = System.Text.UTF8Encoding.UTF8, Indent = true, NamespaceHandling = NamespaceHandling.OmitDuplicates });
  95. writer.WriteStartDocument();
  96. writer.WriteStartElement("root", SsdpConstants.SsdpDeviceDescriptionXmlNamespace);
  97. writer.WriteStartElement("specVersion");
  98. writer.WriteElementString("major", "1");
  99. writer.WriteElementString("minor", "0");
  100. writer.WriteEndElement();
  101. if (this.UrlBase != null && this.UrlBase != this.Location)
  102. writer.WriteElementString("URLBase", this.UrlBase.ToString());
  103. WriteDeviceDescriptionXml(writer, this);
  104. writer.WriteEndElement();
  105. writer.Flush();
  106. ms.Seek(0, System.IO.SeekOrigin.Begin);
  107. using (var reader = new System.IO.StreamReader(ms))
  108. {
  109. return reader.ReadToEnd();
  110. }
  111. }
  112. }
  113. #endregion
  114. #region Private Methods
  115. #region Deserialisation Methods
  116. private void LoadFromDescriptionDocument(string deviceDescriptionXml)
  117. {
  118. using (var ms = new System.IO.MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(deviceDescriptionXml)))
  119. {
  120. var reader = XmlReader.Create(ms);
  121. while (!reader.EOF)
  122. {
  123. reader.Read();
  124. if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "root") continue;
  125. while (!reader.EOF)
  126. {
  127. reader.Read();
  128. if (reader.NodeType != XmlNodeType.Element) continue;
  129. if (reader.LocalName == "URLBase")
  130. {
  131. this.UrlBase = StringToUri(reader.ReadElementContentAsString());
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. #endregion
  139. #endregion
  140. }
  141. }