2 // Author: Atsushi Enomoto <atsushi@ximian.com>
4 // Copyright (C) 2009,2010 Novell, Inc (http://www.novell.com)
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:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
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.
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
28 using System.ComponentModel;
29 using System.ServiceModel;
30 using System.ServiceModel.Channels;
31 using System.ServiceModel.Description;
32 using System.ServiceModel.Dispatcher;
34 namespace System.ServiceModel.Discovery
36 public sealed class DiscoveryClient : ICommunicationObject, IDisposable
38 internal interface IDiscoveryCommon
40 IAsyncResult BeginFind (FindCriteria criteria, AsyncCallback callback, object state);
41 FindResponse EndFind (IAsyncResult result);
42 IAsyncResult BeginResolve (ResolveCriteria criteria, AsyncCallback callback, object state);
43 ResolveResponse EndResolve (IAsyncResult result);
46 public DiscoveryClient ()
51 public DiscoveryClient (DiscoveryEndpoint discoveryEndpoint)
53 if (discoveryEndpoint == null)
54 throw new ArgumentNullException ("discoveryEndpoint");
56 // create DiscoveryTargetClientXX for each version:
57 // Managed -> DiscoveryTargetClientType (request-reply)
58 // Adhoc -> DiscoveryProxyClientType (duplex)
59 if (discoveryEndpoint.DiscoveryMode == ServiceDiscoveryMode.Managed)
60 client = Activator.CreateInstance (discoveryEndpoint.DiscoveryVersion.DiscoveryProxyClientType, new object [] {discoveryEndpoint});
62 client = Activator.CreateInstance (discoveryEndpoint.DiscoveryVersion.DiscoveryTargetClientType, new object [] {discoveryEndpoint});
65 public DiscoveryClient (string endpointConfigurationName)
67 throw new NotImplementedException ();
70 // FIXME: make it dynamic (dmcs crashes for now)
73 public ChannelFactory ChannelFactory {
74 get { return (ChannelFactory) client.GetType ().GetProperty ("ChannelFactory").GetValue (client, null); }
77 public ClientCredentials ClientCredentials {
78 get { return ChannelFactory.Credentials; }
81 public ServiceEndpoint Endpoint {
82 get { return ChannelFactory.Endpoint; }
85 public IClientChannel InnerChannel {
86 get { return (IClientChannel) client.GetType ().GetProperty ("InnerChannel").GetValue (client, null); }
89 CommunicationState State {
90 get { return ((ICommunicationObject) this).State; }
93 CommunicationState ICommunicationObject.State {
94 get { return ((ICommunicationObject) client).State; }
97 public event EventHandler<FindCompletedEventArgs> FindCompleted;
98 public event EventHandler<FindProgressChangedEventArgs> FindProgressChanged;
99 public event EventHandler<AnnouncementEventArgs> ProxyAvailable;
100 public event EventHandler<ResolveCompletedEventArgs> ResolveCompleted;
102 event EventHandler ICommunicationObject.Closed {
103 add { InnerChannel.Closed += value; }
104 remove { InnerChannel.Closed -= value; }
106 event EventHandler ICommunicationObject.Closing {
107 add { InnerChannel.Closing += value; }
108 remove { InnerChannel.Closing -= value; }
110 event EventHandler ICommunicationObject.Faulted {
111 add { InnerChannel.Faulted += value; }
112 remove { InnerChannel.Faulted -= value; }
114 event EventHandler ICommunicationObject.Opened {
115 add { InnerChannel.Opened += value; }
116 remove { InnerChannel.Opened -= value; }
118 event EventHandler ICommunicationObject.Opening {
119 add { InnerChannel.Opening += value; }
120 remove { InnerChannel.Opening -= value; }
125 ((ICommunicationObject) this).Open ();
130 ((ICommunicationObject) this).Close ();
135 public void CancelAsync (object userState)
137 throw new NotImplementedException ();
142 public FindResponse Find (FindCriteria criteria)
144 return EndFind (BeginFind (criteria, null, null));
147 public void FindAsync (FindCriteria criteria)
149 FindAsync (criteria, null);
152 public void FindAsync (FindCriteria criteria, object userState)
154 AsyncCallback cb = delegate (IAsyncResult result) {
155 FindResponse ret = null;
156 Exception error = null;
158 ret = EndFind (result);
159 } catch (Exception ex) {
162 OnFindCompleted (new FindCompletedEventArgs (ret, error, cancelled, result.AsyncState));
165 BeginFind (criteria, cb, userState);
168 void OnFindCompleted (FindCompletedEventArgs args)
170 if (FindCompleted != null)
171 FindCompleted (this, args);
174 IAsyncResult BeginFind (FindCriteria criteria, AsyncCallback callback, object state)
176 return ((IDiscoveryCommon) client).BeginFind (criteria, callback, state);
179 FindResponse EndFind (IAsyncResult result)
181 return ((IDiscoveryCommon) client).EndFind (result);
186 public ResolveResponse Resolve (ResolveCriteria criteria)
188 return EndResolve (BeginResolve (criteria, null, null));
191 public void ResolveAsync (ResolveCriteria criteria)
193 ResolveAsync (criteria, null);
196 public void ResolveAsync (ResolveCriteria criteria, object userState)
198 AsyncCallback cb = delegate (IAsyncResult result) {
199 ResolveResponse ret = null;
200 Exception error = null;
202 ret = EndResolve (result);
203 } catch (Exception ex) {
206 OnResolveCompleted (new ResolveCompletedEventArgs (ret, error, cancelled, result.AsyncState));
209 BeginResolve (criteria, cb, userState);
212 void OnResolveCompleted (ResolveCompletedEventArgs args)
214 if (ResolveCompleted != null)
215 ResolveCompleted (this, args);
218 IAsyncResult BeginResolve (ResolveCriteria criteria, AsyncCallback callback, object state)
220 return ((IDiscoveryCommon) client).BeginResolve (criteria, callback, state);
223 ResolveResponse EndResolve (IAsyncResult result)
225 return ((IDiscoveryCommon) client).EndResolve (result);
228 // explicit interface impl.
230 void ICommunicationObject.Open ()
232 InnerChannel.Open ();
235 void ICommunicationObject.Open (TimeSpan timeout)
237 InnerChannel.Open (timeout);
240 void ICommunicationObject.Close ()
242 InnerChannel.Close ();
245 void ICommunicationObject.Close (TimeSpan timeout)
247 InnerChannel.Close (timeout);
250 IAsyncResult ICommunicationObject.BeginOpen (AsyncCallback callback, object state)
252 return InnerChannel.BeginOpen (callback, state);
255 IAsyncResult ICommunicationObject.BeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
257 return InnerChannel.BeginOpen (timeout, callback, state);
260 IAsyncResult ICommunicationObject.BeginClose (AsyncCallback callback, object state)
262 return InnerChannel.BeginClose (callback, state);
265 IAsyncResult ICommunicationObject.BeginClose (TimeSpan timeout, AsyncCallback callback, object state)
267 return InnerChannel.BeginClose (timeout, callback, state);
270 void ICommunicationObject.EndOpen (IAsyncResult result)
272 InnerChannel.EndOpen (result);
275 void ICommunicationObject.EndClose (IAsyncResult result)
277 InnerChannel.EndClose (result);
280 void ICommunicationObject.Abort ()
282 InnerChannel.Abort ();
285 void IDisposable.Dispose ()
287 InnerChannel.Dispose ();