UdpDiscoveryEndpointElement.cs 6.7 KB

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