UdpAnnouncementEndpointElement.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. namespace System.ServiceModel.Discovery.Configuration
  33. {
  34. public class UdpAnnouncementEndpointElement : AnnouncementEndpointElement
  35. {
  36. static ConfigurationPropertyCollection properties;
  37. static ConfigurationProperty max_announcement_delay, multicast_address, transport_settings;
  38. static UdpAnnouncementEndpointElement ()
  39. {
  40. max_announcement_delay = new ConfigurationProperty ("maxAnnouncementDelay", typeof (TimeSpan), "00:00:00.500", new TimeSpanConverter (), null, ConfigurationPropertyOptions.None);
  41. multicast_address = new ConfigurationProperty ("multicastAddress", typeof (Uri), "soap.udp://239.255.255.250:3702", new UriTypeConverter (), null, ConfigurationPropertyOptions.None);
  42. transport_settings = new ConfigurationProperty ("transportSettings", typeof (UdpTransportSettingsElement), null, null, null, ConfigurationPropertyOptions.None);
  43. properties = new ConfigurationPropertyCollection ();
  44. properties.Add (max_announcement_delay);
  45. properties.Add (multicast_address);
  46. properties.Add (transport_settings);
  47. }
  48. public UdpAnnouncementEndpointElement ()
  49. {
  50. }
  51. protected override Type EndpointType {
  52. get { return typeof (UdpAnnouncementEndpoint); }
  53. }
  54. [TypeConverter (typeof (TimeSpanConverter))]
  55. [ConfigurationPropertyAttribute("maxAnnouncementDelay", DefaultValue = "00:00:00.500")]
  56. public TimeSpan MaxAnnouncementDelay {
  57. get { return (TimeSpan) base [max_announcement_delay]; }
  58. set { base [max_announcement_delay] = value; }
  59. }
  60. [ConfigurationPropertyAttribute("multicastAddress", DefaultValue = "soap.udp://239.255.255.250:3702")]
  61. public Uri MulticastAddress {
  62. get { return (Uri) base [multicast_address]; }
  63. set { base [multicast_address] = value; }
  64. }
  65. [ConfigurationPropertyAttribute("transportSettings")]
  66. public UdpTransportSettingsElement TransportSettings {
  67. get { return (UdpTransportSettingsElement) base [transport_settings]; }
  68. }
  69. protected override ConfigurationPropertyCollection Properties {
  70. get { return properties; }
  71. }
  72. protected override ServiceEndpoint CreateServiceEndpoint (ContractDescription contractDescription)
  73. {
  74. if (contractDescription == null)
  75. throw new ArgumentNullException ("contractDescription");
  76. DiscoveryVersion ver = null;
  77. switch (contractDescription.ContractType.Namespace) {
  78. case DiscoveryVersion.Namespace11:
  79. ver = DiscoveryVersion.WSDiscovery11;
  80. break;
  81. case DiscoveryVersion.NamespaceApril2005:
  82. ver = DiscoveryVersion.WSDiscoveryApril2005;
  83. break;
  84. case DiscoveryVersion.NamespaceCD1:
  85. ver = DiscoveryVersion.WSDiscoveryCD1;
  86. break;
  87. }
  88. var ret = new UdpAnnouncementEndpoint (ver, MulticastAddress);
  89. ret.MaxAnnouncementDelay = MaxAnnouncementDelay;
  90. TransportSettings.ApplyConfiguration (ret.TransportSettings);
  91. return ret;
  92. }
  93. protected override void InitializeFrom (ServiceEndpoint endpoint)
  94. {
  95. if (endpoint == null)
  96. throw new ArgumentNullException ("endpoint");
  97. var e = (UdpAnnouncementEndpoint) endpoint;
  98. MaxAnnouncementDelay = e.MaxAnnouncementDelay;
  99. MulticastAddress = e.MulticastAddress;
  100. TransportSettings.InitializeFrom (e.TransportSettings);
  101. }
  102. protected override void OnApplyConfiguration (ServiceEndpoint endpoint, ChannelEndpointElement serviceEndpointElement)
  103. {
  104. if (endpoint == null)
  105. throw new ArgumentNullException ("endpoint");
  106. var de = (AnnouncementEndpoint) endpoint;
  107. if (!de.DiscoveryVersion.Equals (DiscoveryVersion))
  108. throw new ArgumentException ("Argument AnnouncementEndpoint is initialized with different DiscoveryVersion");
  109. de.MaxAnnouncementDelay = MaxAnnouncementDelay;
  110. de.Address = serviceEndpointElement.CreateEndpointAddress (); // it depends on InternalVisibleTo(System.ServiceModel)
  111. var be = (UdpTransportBindingElement) de.Binding.CreateBindingElements ().First (b => b is UdpTransportBindingElement);
  112. TransportSettings.ApplyConfiguration (be.TransportSettings);
  113. }
  114. protected override void OnApplyConfiguration (ServiceEndpoint endpoint, ServiceEndpointElement serviceEndpointElement)
  115. {
  116. if (endpoint == null)
  117. throw new ArgumentNullException ("endpoint");
  118. var de = (AnnouncementEndpoint) endpoint;
  119. if (!de.DiscoveryVersion.Equals (DiscoveryVersion))
  120. throw new ArgumentException ("Argument AnnouncementEndpoint is initialized with different DiscoveryVersion");
  121. de.MaxAnnouncementDelay = MaxAnnouncementDelay;
  122. de.Address = serviceEndpointElement.CreateEndpointAddress (); // it depends on InternalVisibleTo(System.ServiceModel)
  123. var be = (UdpTransportBindingElement) de.Binding.CreateBindingElements ().First (b => b is UdpTransportBindingElement);
  124. TransportSettings.ApplyConfiguration (be.TransportSettings);
  125. }
  126. protected override void OnInitializeAndValidate (ChannelEndpointElement channelEndpointElement)
  127. {
  128. // It seems to do nothing.
  129. base.OnInitializeAndValidate (channelEndpointElement);
  130. }
  131. protected override void OnInitializeAndValidate (ServiceEndpointElement channelEndpointElement)
  132. {
  133. // It seems to do nothing.
  134. base.OnInitializeAndValidate (channelEndpointElement);
  135. }
  136. }
  137. }
  138. #endif