2009-12-04 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / PeerCustomResolverBindingElement.cs
1 //
2 // PeerCustomResolverBindingElement.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2009 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.Net.Security;
32 using System.ServiceModel;
33 using System.ServiceModel.Description;
34 using System.ServiceModel.PeerResolvers;
35 using System.Text;
36 using System.Xml;
37
38 namespace System.ServiceModel.Channels
39 {
40         public class PeerCustomResolverBindingElement : PeerResolverBindingElement
41         {
42                 public PeerCustomResolverBindingElement ()
43                 {
44                         settings = new PeerCustomResolverSettings ();
45                 }
46
47                 private PeerCustomResolverBindingElement (
48                         PeerCustomResolverBindingElement other)
49                         : base (other)
50                 {
51                         ReferralPolicy = other.ReferralPolicy;
52                         settings = other.settings.Clone ();
53                 }
54
55                 public PeerCustomResolverBindingElement (BindingContext context, PeerCustomResolverSettings settings)
56                         : this (settings)
57                 {
58                         this.context = context;
59                 }
60
61                 public PeerCustomResolverBindingElement (PeerCustomResolverSettings settings)
62                 {
63                         this.settings = settings;
64                 }
65
66                 BindingContext context;
67                 PeerCustomResolverSettings settings;
68
69                 [MonoTODO]
70                 public override PeerReferralPolicy ReferralPolicy { get; set; }
71
72                 public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
73                         BindingContext context)
74                 {
75                         var cf = context.BuildInnerChannelFactory<TChannel> ();
76                         var pcf = cf as PeerChannelFactory<TChannel>;
77                         if (pcf != null)
78                                 pcf.Resolver = CreatePeerResolver ();
79                         return cf;
80                 }
81
82                 public override IChannelListener<TChannel>
83                         BuildChannelListener<TChannel> (
84                         BindingContext context)
85                 {
86                         var cl = context.BuildInnerChannelListener<TChannel> ();
87                         var pcl = cl as PeerChannelListener<TChannel>;
88                         if (pcl != null)
89                                 pcl.Resolver = CreatePeerResolver ();
90                         return cl;
91                 }
92
93                 public override BindingElement Clone ()
94                 {
95                         return new PeerCustomResolverBindingElement (this);
96                 }
97
98                 public override PeerResolver CreatePeerResolver ()
99                 {
100                         if (settings != null && settings.Resolver != null)
101                                 return settings.Resolver;
102
103                         var se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IPeerResolverContract)), settings.Binding, settings.Address);
104                         return new PeerCustomResolver (se);
105                 }
106
107                 [MonoTODO]
108                 public override T GetProperty<T> (BindingContext context)
109                 {
110                         throw new NotImplementedException ();
111                 }
112         }
113
114         internal interface ICustomPeerResolverClient : IPeerResolverContract, IClientChannel
115         {
116         }
117
118         internal class PeerCustomResolver : PeerResolver
119         {
120                 Guid client_id = Guid.NewGuid ();
121                 ICustomPeerResolverClient client;
122                 string preserved_mesh_id;
123
124                 public PeerCustomResolver (ServiceEndpoint endpoint)
125                 {
126                         if (endpoint == null)
127                                 throw new ArgumentNullException ("endpoint");
128                         client = new ChannelFactory<ICustomPeerResolverClient> (endpoint).CreateChannel ();
129                 }
130
131                 public override bool CanShareReferrals {
132                         get { return false; }
133                 }
134
135                 public override object Register (string meshId,
136                         PeerNodeAddress nodeAddress, TimeSpan timeout)
137                 {
138                         if (String.IsNullOrEmpty (meshId))
139                                 throw new ArgumentNullException ("meshId");
140                         if (nodeAddress == null)
141                                 throw new ArgumentNullException ("nodeAddress");
142                         if (timeout <= TimeSpan.Zero)
143                                 throw new ArgumentException (String.Format ("Timeout value must be positive value. It was {0}", timeout));
144
145                         client.OperationTimeout = timeout;
146                         preserved_mesh_id = meshId;
147                         return client.Register (new RegisterInfo (client_id, meshId, nodeAddress)).RegistrationId;
148                 }
149
150                 public override ReadOnlyCollection<PeerNodeAddress> Resolve (
151                         string meshId, int maxAddresses, TimeSpan timeout)
152                 {
153                         if (String.IsNullOrEmpty (meshId))
154                                 throw new ArgumentNullException ("meshId");
155                         if (maxAddresses <= 0)
156                                 throw new ArgumentOutOfRangeException ("maxAddresses must be positive integer");
157                         if (timeout <= TimeSpan.Zero)
158                                 throw new ArgumentException (String.Format ("Timeout value must be positive value. It was {0}", timeout));
159
160                         client.OperationTimeout = timeout;
161                         return new ReadOnlyCollection<PeerNodeAddress> (client.Resolve (new ResolveInfo (client_id, meshId, maxAddresses)).Addresses ?? new PeerNodeAddress [0]);
162                 }
163
164                 public override void Unregister (object registrationId,
165                         TimeSpan timeout)
166                 {
167                         if (timeout <= TimeSpan.Zero)
168                                 throw new ArgumentException (String.Format ("Timeout value must be positive value. It was {0}", timeout));
169
170                         client.OperationTimeout = timeout;
171                         preserved_mesh_id = null;
172                         client.Unregister (new UnregisterInfo (preserved_mesh_id, (Guid) registrationId));
173                 }
174
175                 public override void Update (object registrationId,
176                         PeerNodeAddress updatedNodeAddress, TimeSpan timeout)
177                 {
178                         if (timeout <= TimeSpan.Zero)
179                                 throw new ArgumentException (String.Format ("Timeout value must be positive value. It was {0}", timeout));
180
181                         client.OperationTimeout = timeout;
182                         client.Update (new UpdateInfo ((Guid) registrationId, client_id, preserved_mesh_id, updatedNodeAddress));
183                 }
184         }
185 }