Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.ServiceModel.Discovery / System.ServiceModel.Discovery.Configuration / UdpDiscoveryEndpointElement.cs
1 //
2 // Author: Atsushi Enomoto <atsushi@ximian.com>
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
34 namespace System.ServiceModel.Discovery.Configuration
35 {
36         public class UdpDiscoveryEndpointElement : DiscoveryEndpointElement
37         {
38                 static ConfigurationPropertyCollection properties;
39                 static ConfigurationProperty discovery_mode, max_response_delay, multicast_address, transport_settings;
40                 
41                 static UdpDiscoveryEndpointElement ()
42                 {
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);
52                 }
53
54                 public UdpDiscoveryEndpointElement ()
55                 {
56                 }
57
58                 [ConfigurationProperty ("discoveryMode", DefaultValue = ServiceDiscoveryMode.Adhoc)]
59                 public new ServiceDiscoveryMode DiscoveryMode {
60                         get { return (ServiceDiscoveryMode) base [discovery_mode]; }
61                         set { base [discovery_mode] = value; }
62                 }
63
64                 protected override Type EndpointType {
65                         get { return typeof (UdpDiscoveryEndpoint); }
66                 }
67
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; }
73                 }
74
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; }
79                 }
80
81                 [ConfigurationProperty ("transportSettings")]
82                 public UdpTransportSettingsElement TransportSettings {
83                         get { return (UdpTransportSettingsElement) base [transport_settings]; }
84                 }
85                 
86                 protected override ConfigurationPropertyCollection Properties {
87                         get { return properties; }
88                 }
89                 
90                 protected override ServiceEndpoint CreateServiceEndpoint (ContractDescription contractDescription)
91                 {
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;
98                                 break;
99                         case DiscoveryVersion.NamespaceApril2005:
100                                 ver = DiscoveryVersion.WSDiscoveryApril2005;
101                                 break;
102                         case DiscoveryVersion.NamespaceCD1:
103                                 ver = DiscoveryVersion.WSDiscoveryCD1;
104                                 break;
105                         }
106                         var ret = new UdpDiscoveryEndpoint (ver, MulticastAddress);
107                         ret.MaxResponseDelay = MaxResponseDelay;
108                         TransportSettings.ApplyConfiguration (ret.TransportSettings);
109                         return ret;
110                 }
111
112                 protected override void InitializeFrom (ServiceEndpoint endpoint)
113                 {
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);
120                 }
121
122                 protected override void OnApplyConfiguration (ServiceEndpoint endpoint, ChannelEndpointElement 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
135                 protected override void OnApplyConfiguration (ServiceEndpoint endpoint, ServiceEndpointElement serviceEndpointElement)
136                 {
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);
146                 }
147
148                 protected override void OnInitializeAndValidate (ChannelEndpointElement channelEndpointElement)
149                 {
150                         // It seems to do nothing.
151                         base.OnInitializeAndValidate (channelEndpointElement);
152                 }
153
154                 protected override void OnInitializeAndValidate (ServiceEndpointElement channelEndpointElement)
155                 {
156                         // It seems to do nothing.
157                         base.OnInitializeAndValidate (channelEndpointElement);
158                 }
159         }
160 }
161
162 #endif