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