UdpDiscoveryEndpointElement.cs 6.8 KB

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