2 // PeerCustomResolverBindingElement.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2009 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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
;
38 namespace System
.ServiceModel
.Channels
40 public class PeerCustomResolverBindingElement
: PeerResolverBindingElement
42 public PeerCustomResolverBindingElement ()
44 settings
= new PeerCustomResolverSettings ();
47 private PeerCustomResolverBindingElement (
48 PeerCustomResolverBindingElement other
)
51 ReferralPolicy
= other
.ReferralPolicy
;
52 settings
= other
.settings
.Clone ();
55 public PeerCustomResolverBindingElement (BindingContext context
, PeerCustomResolverSettings settings
)
58 this.context
= context
;
61 public PeerCustomResolverBindingElement (PeerCustomResolverSettings settings
)
63 this.settings
= settings
;
66 BindingContext context
;
67 PeerCustomResolverSettings settings
;
70 public override PeerReferralPolicy ReferralPolicy { get; set; }
72 public override IChannelFactory
<TChannel
> BuildChannelFactory
<TChannel
> (
73 BindingContext context
)
75 var cf
= context
.BuildInnerChannelFactory
<TChannel
> ();
76 var pcf
= cf
as PeerChannelFactory
<TChannel
>;
78 pcf
.Resolver
= CreatePeerResolver ();
82 public override IChannelListener
<TChannel
>
83 BuildChannelListener
<TChannel
> (
84 BindingContext context
)
86 var cl
= context
.BuildInnerChannelListener
<TChannel
> ();
87 var pcl
= cl
as PeerChannelListener
<TChannel
>;
89 pcl
.Resolver
= CreatePeerResolver ();
93 public override BindingElement
Clone ()
95 return new PeerCustomResolverBindingElement (this);
98 public override PeerResolver
CreatePeerResolver ()
100 if (settings
!= null && settings
.Resolver
!= null)
101 return settings
.Resolver
;
103 var se
= new ServiceEndpoint (ContractDescription
.GetContract (typeof (IPeerResolverContract
)), settings
.Binding
, settings
.Address
);
104 return new PeerCustomResolver (se
);
108 public override T GetProperty
<T
> (BindingContext context
)
110 throw new NotImplementedException ();
114 internal interface ICustomPeerResolverClient
: IPeerResolverContract
, IClientChannel
118 internal class PeerCustomResolver
: PeerResolver
120 Guid client_id
= Guid
.NewGuid ();
121 ICustomPeerResolverClient client
;
122 string preserved_mesh_id
;
124 public PeerCustomResolver (ServiceEndpoint endpoint
)
126 if (endpoint
== null)
127 throw new ArgumentNullException ("endpoint");
128 client
= new ChannelFactory
<ICustomPeerResolverClient
> (endpoint
).CreateChannel ();
131 public override bool CanShareReferrals
{
132 get { return false; }
135 public override object Register (string meshId
,
136 PeerNodeAddress nodeAddress
, TimeSpan timeout
)
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
));
145 client
.OperationTimeout
= timeout
;
146 preserved_mesh_id
= meshId
;
147 return client
.Register (new RegisterInfo (client_id
, meshId
, nodeAddress
)).RegistrationId
;
150 public override ReadOnlyCollection
<PeerNodeAddress
> Resolve (
151 string meshId
, int maxAddresses
, TimeSpan timeout
)
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
));
160 client
.OperationTimeout
= timeout
;
161 return new ReadOnlyCollection
<PeerNodeAddress
> (client
.Resolve (new ResolveInfo (client_id
, meshId
, maxAddresses
)).Addresses
?? new PeerNodeAddress
[0]);
164 public override void Unregister (object registrationId
,
167 if (timeout
<= TimeSpan
.Zero
)
168 throw new ArgumentException (String
.Format ("Timeout value must be positive value. It was {0}", timeout
));
170 client
.OperationTimeout
= timeout
;
171 preserved_mesh_id
= null;
172 client
.Unregister (new UnregisterInfo (preserved_mesh_id
, (Guid
) registrationId
));
175 public override void Update (object registrationId
,
176 PeerNodeAddress updatedNodeAddress
, TimeSpan timeout
)
178 if (timeout
<= TimeSpan
.Zero
)
179 throw new ArgumentException (String
.Format ("Timeout value must be positive value. It was {0}", timeout
));
181 client
.OperationTimeout
= timeout
;
182 client
.Update (new UpdateInfo ((Guid
) registrationId
, client_id
, preserved_mesh_id
, updatedNodeAddress
));