2 // Author: Atsushi Enomoto <atsushi@ximian.com>
4 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
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:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
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.
27 using System.ComponentModel;
28 using System.Configuration;
30 using System.ServiceModel.Configuration;
31 using System.ServiceModel.Description;
32 using System.ServiceModel.Discovery.Udp;
34 namespace System.ServiceModel.Discovery.Configuration
36 public class UdpDiscoveryEndpointElement : DiscoveryEndpointElement
38 static ConfigurationPropertyCollection properties;
39 static ConfigurationProperty discovery_mode, max_response_delay, multicast_address, transport_settings;
41 static UdpDiscoveryEndpointElement ()
43 discovery_mode = new ConfigurationProperty ("discoveryMode", typeof (ServiceDiscoveryMode), ServiceDiscoveryMode.Adhoc, null, null, ConfigurationPropertyOptions.None);
44 max_response_delay = new ConfigurationProperty ("maxResponseDelay", typeof (TimeSpan), "00:00:00.500", new TimeSpanConverter (), null, ConfigurationPropertyOptions.None);
45 multicast_address = new ConfigurationProperty ("multicastAddress", typeof (Uri), "soap.udp://239.255.255.250:3702", new UriTypeConverter (), null, ConfigurationPropertyOptions.None);
46 transport_settings = new ConfigurationProperty ("transportSettings", typeof (UdpTransportSettingsElement), null, null, null, ConfigurationPropertyOptions.None);
47 properties = new ConfigurationPropertyCollection ();
48 properties.Add (discovery_mode);
49 properties.Add (max_response_delay);
50 properties.Add (multicast_address);
51 properties.Add (transport_settings);
54 public UdpDiscoveryEndpointElement ()
58 [ConfigurationProperty ("discoveryMode", DefaultValue = ServiceDiscoveryMode.Adhoc)]
59 public new ServiceDiscoveryMode DiscoveryMode {
60 get { return (ServiceDiscoveryMode) base [discovery_mode]; }
61 set { base [discovery_mode] = value; }
64 protected override Type EndpointType {
65 get { return typeof (UdpDiscoveryEndpoint); }
68 [TypeConverter (typeof (TimeSpanConverter))]
69 [ConfigurationProperty ("maxResponseDelay", DefaultValue = "00:00:00.500")]
70 public new TimeSpan MaxResponseDelay {
71 get { return (TimeSpan) base [max_response_delay]; }
72 set { base [max_response_delay] = value; }
75 [ConfigurationProperty ("multicastAddress", DefaultValue = "soap.udp://239.255.255.250:3702")]
76 public Uri MulticastAddress {
77 get { return (Uri) base [multicast_address]; }
78 set { base [multicast_address] = value; }
81 [ConfigurationProperty ("transportSettings")]
82 public UdpTransportSettingsElement TransportSettings {
83 get { return (UdpTransportSettingsElement) base [transport_settings]; }
86 protected override ConfigurationPropertyCollection Properties {
87 get { return properties; }
90 protected override ServiceEndpoint CreateServiceEndpoint (ContractDescription contractDescription)
92 if (contractDescription == null)
93 throw new ArgumentNullException ("contractDescription");
94 DiscoveryVersion ver = null;
95 switch (contractDescription.ContractType.Namespace) {
96 case DiscoveryVersion.Namespace11:
97 ver = DiscoveryVersion.WSDiscovery11;
99 case DiscoveryVersion.NamespaceApril2005:
100 ver = DiscoveryVersion.WSDiscoveryApril2005;
102 case DiscoveryVersion.NamespaceCD1:
103 ver = DiscoveryVersion.WSDiscoveryCD1;
106 var ret = new UdpDiscoveryEndpoint (ver, MulticastAddress);
107 ret.MaxResponseDelay = MaxResponseDelay;
108 TransportSettings.ApplyConfiguration (ret.TransportSettings);
112 protected override void InitializeFrom (ServiceEndpoint endpoint)
114 if (endpoint == null)
115 throw new ArgumentNullException ("endpoint");
116 var e = (UdpDiscoveryEndpoint) endpoint;
117 MaxResponseDelay = e.MaxResponseDelay;
118 MulticastAddress = e.MulticastAddress;
119 TransportSettings.InitializeFrom (e.TransportSettings);
122 protected override void OnApplyConfiguration (ServiceEndpoint endpoint, ChannelEndpointElement serviceEndpointElement)
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);
135 protected override void OnApplyConfiguration (ServiceEndpoint endpoint, ServiceEndpointElement serviceEndpointElement)
137 if (endpoint == null)
138 throw new ArgumentNullException ("endpoint");
139 var de = (DiscoveryEndpoint) endpoint;
140 if (!de.DiscoveryVersion.Equals (DiscoveryVersion))
141 throw new ArgumentException ("Argument AnnouncementEndpoint is initialized with different DiscoveryVersion");
142 de.MaxResponseDelay = MaxResponseDelay;
143 de.Address = serviceEndpointElement.CreateEndpointAddress (); // it depends on InternalVisibleTo(System.ServiceModel)
144 var be = (UdpTransportBindingElement) de.Binding.CreateBindingElements ().First (b => b is UdpTransportBindingElement);
145 TransportSettings.ApplyConfiguration (be.TransportSettings);
148 protected override void OnInitializeAndValidate (ChannelEndpointElement channelEndpointElement)
150 // It seems to do nothing.
151 base.OnInitializeAndValidate (channelEndpointElement);
154 protected override void OnInitializeAndValidate (ServiceEndpointElement channelEndpointElement)
156 // It seems to do nothing.
157 base.OnInitializeAndValidate (channelEndpointElement);