Merge pull request #4540 from kumpera/android-changes-part1
[mono.git] / mcs / class / System.ServiceModel.Discovery / System.ServiceModel.Discovery.Configuration / UdpAnnouncementEndpointElement.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 using System;
26 using System.ComponentModel;
27 using System.Configuration;
28 using System.Linq;
29 using System.ServiceModel.Configuration;
30 using System.ServiceModel.Description;
31 using System.ServiceModel.Discovery.Udp;
32
33 namespace System.ServiceModel.Discovery.Configuration
34 {
35         public class UdpAnnouncementEndpointElement : AnnouncementEndpointElement
36         {
37                 static ConfigurationPropertyCollection properties;
38                 static ConfigurationProperty max_announcement_delay, multicast_address, transport_settings;
39                 
40                 static UdpAnnouncementEndpointElement ()
41                 {
42                         max_announcement_delay = new ConfigurationProperty ("maxAnnouncementDelay", 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 (max_announcement_delay);
47                         properties.Add (multicast_address);
48                         properties.Add (transport_settings);
49                 }
50                 
51                 public UdpAnnouncementEndpointElement ()
52                 {
53                 }
54                 
55                 protected internal override Type EndpointType {
56                         get { return typeof (UdpAnnouncementEndpoint); }
57                 }
58                 
59                 [TypeConverter (typeof (TimeSpanConverter))]
60                 [ConfigurationPropertyAttribute("maxAnnouncementDelay", DefaultValue = "00:00:00.500")]
61                 public new TimeSpan MaxAnnouncementDelay {
62                         get { return (TimeSpan) base [max_announcement_delay]; }
63                         set { base [max_announcement_delay] = value; }
64                 }
65                 
66                 [ConfigurationPropertyAttribute("multicastAddress", DefaultValue = "soap.udp://239.255.255.250:3702")]
67                 public Uri MulticastAddress {
68                         get { return (Uri) base [multicast_address]; }
69                         set { base [multicast_address] = value; }
70                 }
71                 
72                 [ConfigurationPropertyAttribute("transportSettings")]
73                 public UdpTransportSettingsElement TransportSettings {
74                         get { return (UdpTransportSettingsElement) base [transport_settings]; }
75                 }
76                 
77                 protected override ConfigurationPropertyCollection Properties {
78                         get { return properties; }
79                 }
80
81                 protected internal override ServiceEndpoint CreateServiceEndpoint (ContractDescription contractDescription)
82                 {
83                         if (contractDescription == null)
84                                 throw new ArgumentNullException ("contractDescription");
85                         DiscoveryVersion ver = null;
86                         switch (contractDescription.ContractType.Namespace) {
87                         case DiscoveryVersion.Namespace11:
88                                 ver = DiscoveryVersion.WSDiscovery11;
89                                 break;
90                         case DiscoveryVersion.NamespaceApril2005:
91                                 ver = DiscoveryVersion.WSDiscoveryApril2005;
92                                 break;
93                         case DiscoveryVersion.NamespaceCD1:
94                                 ver = DiscoveryVersion.WSDiscoveryCD1;
95                                 break;
96                         }
97                         var ret = new UdpAnnouncementEndpoint (ver, MulticastAddress);
98                         ret.MaxAnnouncementDelay = MaxAnnouncementDelay;
99                         TransportSettings.ApplyConfiguration (ret.TransportSettings);
100                         return ret;
101                 }
102
103                 protected internal override void InitializeFrom (ServiceEndpoint endpoint)
104                 {
105                         if (endpoint == null)
106                                 throw new ArgumentNullException ("endpoint");
107                         var e = (UdpAnnouncementEndpoint) endpoint;
108                         MaxAnnouncementDelay = e.MaxAnnouncementDelay;
109                         MulticastAddress = e.MulticastAddress;
110                         TransportSettings.InitializeFrom (e.TransportSettings);
111                 }
112
113                 protected override void OnApplyConfiguration (ServiceEndpoint endpoint, ChannelEndpointElement serviceEndpointElement)
114                 {
115                         if (endpoint == null)
116                                 throw new ArgumentNullException ("endpoint");
117                         var de = (AnnouncementEndpoint) endpoint;
118                         if (!de.DiscoveryVersion.Equals (DiscoveryVersion))
119                                 throw new ArgumentException ("Argument AnnouncementEndpoint is initialized with different DiscoveryVersion");
120                         de.MaxAnnouncementDelay = MaxAnnouncementDelay;
121                         de.Address = serviceEndpointElement.CreateEndpointAddress (); // it depends on InternalVisibleTo(System.ServiceModel)
122                         var be = (UdpTransportBindingElement) de.Binding.CreateBindingElements ().First (b => b is UdpTransportBindingElement);
123                         TransportSettings.ApplyConfiguration (be.TransportSettings);
124                 }
125
126                 protected override void OnApplyConfiguration (ServiceEndpoint endpoint, ServiceEndpointElement serviceEndpointElement)
127                 {
128                         if (endpoint == null)
129                                 throw new ArgumentNullException ("endpoint");
130                         var de = (AnnouncementEndpoint) endpoint;
131                         if (!de.DiscoveryVersion.Equals (DiscoveryVersion))
132                                 throw new ArgumentException ("Argument AnnouncementEndpoint is initialized with different DiscoveryVersion");
133                         de.MaxAnnouncementDelay = MaxAnnouncementDelay;
134                         de.Address = serviceEndpointElement.CreateEndpointAddress (); // it depends on InternalVisibleTo(System.ServiceModel)
135                         var be = (UdpTransportBindingElement) de.Binding.CreateBindingElements ().First (b => b is UdpTransportBindingElement);
136                         TransportSettings.ApplyConfiguration (be.TransportSettings);
137                 }
138
139                 protected override void OnInitializeAndValidate (ChannelEndpointElement channelEndpointElement)
140                 {
141                         // It seems to do nothing.
142                         base.OnInitializeAndValidate (channelEndpointElement);
143                 }
144
145                 protected override void OnInitializeAndValidate (ServiceEndpointElement serviceEndpointElement)
146                 {
147                         // It seems to do nothing.
148                         base.OnInitializeAndValidate (serviceEndpointElement);
149                 }
150         }
151 }
152