UdpAnnouncementEndpointElement.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // Author: Atsushi Enomoto <[email protected]>
  3. //
  4. // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining
  7. // a copy of this software and associated documentation files (the
  8. // "Software"), to deal in the Software without restriction, including
  9. // without limitation the rights to use, copy, modify, merge, publish,
  10. // distribute, sublicense, and/or sell copies of the Software, and to
  11. // permit persons to whom the Software is furnished to do so, subject to
  12. // the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #if NET_4_0
  26. using System;
  27. using System.ComponentModel;
  28. using System.Configuration;
  29. using System.Linq;
  30. using System.ServiceModel.Configuration;
  31. using System.ServiceModel.Description;
  32. using System.ServiceModel.Discovery.Udp;
  33. namespace System.ServiceModel.Discovery.Configuration
  34. {
  35. public class UdpAnnouncementEndpointElement : AnnouncementEndpointElement
  36. {
  37. static ConfigurationPropertyCollection properties;
  38. static ConfigurationProperty max_announcement_delay, multicast_address, transport_settings;
  39. static UdpAnnouncementEndpointElement ()
  40. {
  41. max_announcement_delay = new ConfigurationProperty ("maxAnnouncementDelay", typeof (TimeSpan), "00:00:00.500", new TimeSpanConverter (), null, ConfigurationPropertyOptions.None);
  42. multicast_address = new ConfigurationProperty ("multicastAddress", typeof (Uri), "soap.udp://239.255.255.250:3702", new UriTypeConverter (), null, ConfigurationPropertyOptions.None);
  43. transport_settings = new ConfigurationProperty ("transportSettings", typeof (UdpTransportSettingsElement), null, null, null, ConfigurationPropertyOptions.None);
  44. properties = new ConfigurationPropertyCollection ();
  45. properties.Add (max_announcement_delay);
  46. properties.Add (multicast_address);
  47. properties.Add (transport_settings);
  48. }
  49. public UdpAnnouncementEndpointElement ()
  50. {
  51. }
  52. protected internal override Type EndpointType {
  53. get { return typeof (UdpAnnouncementEndpoint); }
  54. }
  55. [TypeConverter (typeof (TimeSpanConverter))]
  56. [ConfigurationPropertyAttribute("maxAnnouncementDelay", DefaultValue = "00:00:00.500")]
  57. public new TimeSpan MaxAnnouncementDelay {
  58. get { return (TimeSpan) base [max_announcement_delay]; }
  59. set { base [max_announcement_delay] = value; }
  60. }
  61. [ConfigurationPropertyAttribute("multicastAddress", DefaultValue = "soap.udp://239.255.255.250:3702")]
  62. public Uri MulticastAddress {
  63. get { return (Uri) base [multicast_address]; }
  64. set { base [multicast_address] = value; }
  65. }
  66. [ConfigurationPropertyAttribute("transportSettings")]
  67. public UdpTransportSettingsElement TransportSettings {
  68. get { return (UdpTransportSettingsElement) base [transport_settings]; }
  69. }
  70. protected override ConfigurationPropertyCollection Properties {
  71. get { return properties; }
  72. }
  73. protected internal override ServiceEndpoint CreateServiceEndpoint (ContractDescription contractDescription)
  74. {
  75. if (contractDescription == null)
  76. throw new ArgumentNullException ("contractDescription");
  77. DiscoveryVersion ver = null;
  78. switch (contractDescription.ContractType.Namespace) {
  79. case DiscoveryVersion.Namespace11:
  80. ver = DiscoveryVersion.WSDiscovery11;
  81. break;
  82. case DiscoveryVersion.NamespaceApril2005:
  83. ver = DiscoveryVersion.WSDiscoveryApril2005;
  84. break;
  85. case DiscoveryVersion.NamespaceCD1:
  86. ver = DiscoveryVersion.WSDiscoveryCD1;
  87. break;
  88. }
  89. var ret = new UdpAnnouncementEndpoint (ver, MulticastAddress);
  90. ret.MaxAnnouncementDelay = MaxAnnouncementDelay;
  91. TransportSettings.ApplyConfiguration (ret.TransportSettings);
  92. return ret;
  93. }
  94. protected internal override void InitializeFrom (ServiceEndpoint endpoint)
  95. {
  96. if (endpoint == null)
  97. throw new ArgumentNullException ("endpoint");
  98. var e = (UdpAnnouncementEndpoint) endpoint;
  99. MaxAnnouncementDelay = e.MaxAnnouncementDelay;
  100. MulticastAddress = e.MulticastAddress;
  101. TransportSettings.InitializeFrom (e.TransportSettings);
  102. }
  103. protected override void OnApplyConfiguration (ServiceEndpoint endpoint, ChannelEndpointElement serviceEndpointElement)
  104. {
  105. if (endpoint == null)
  106. throw new ArgumentNullException ("endpoint");
  107. var de = (AnnouncementEndpoint) endpoint;
  108. if (!de.DiscoveryVersion.Equals (DiscoveryVersion))
  109. throw new ArgumentException ("Argument AnnouncementEndpoint is initialized with different DiscoveryVersion");
  110. de.MaxAnnouncementDelay = MaxAnnouncementDelay;
  111. de.Address = serviceEndpointElement.CreateEndpointAddress (); // it depends on InternalVisibleTo(System.ServiceModel)
  112. var be = (UdpTransportBindingElement) de.Binding.CreateBindingElements ().First (b => b is UdpTransportBindingElement);
  113. TransportSettings.ApplyConfiguration (be.TransportSettings);
  114. }
  115. protected override void OnApplyConfiguration (ServiceEndpoint endpoint, ServiceEndpointElement serviceEndpointElement)
  116. {
  117. if (endpoint == null)
  118. throw new ArgumentNullException ("endpoint");
  119. var de = (AnnouncementEndpoint) endpoint;
  120. if (!de.DiscoveryVersion.Equals (DiscoveryVersion))
  121. throw new ArgumentException ("Argument AnnouncementEndpoint is initialized with different DiscoveryVersion");
  122. de.MaxAnnouncementDelay = MaxAnnouncementDelay;
  123. de.Address = serviceEndpointElement.CreateEndpointAddress (); // it depends on InternalVisibleTo(System.ServiceModel)
  124. var be = (UdpTransportBindingElement) de.Binding.CreateBindingElements ().First (b => b is UdpTransportBindingElement);
  125. TransportSettings.ApplyConfiguration (be.TransportSettings);
  126. }
  127. protected override void OnInitializeAndValidate (ChannelEndpointElement channelEndpointElement)
  128. {
  129. // It seems to do nothing.
  130. base.OnInitializeAndValidate (channelEndpointElement);
  131. }
  132. protected override void OnInitializeAndValidate (ServiceEndpointElement channelEndpointElement)
  133. {
  134. // It seems to do nothing.
  135. base.OnInitializeAndValidate (channelEndpointElement);
  136. }
  137. }
  138. }
  139. #endif