ongoing DiscoveryClient work. May need further contract fixes.
[mono.git] / mcs / class / System.ServiceModel.Discovery / System.ServiceModel.Discovery / DiscoveryClient.cs
1 //
2 // Author: Atsushi Enomoto <atsushi@ximian.com>
3 //
4 // Copyright (C) 2009,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.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;
33
34 namespace System.ServiceModel.Discovery
35 {
36         public sealed class DiscoveryClient : ICommunicationObject, IDisposable
37         {
38                 internal interface IDiscoveryCommon
39                 {
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);
44                 }
45                 
46                 public DiscoveryClient ()
47                         : this (String.Empty)
48                 {
49                 }
50
51                 public DiscoveryClient (DiscoveryEndpoint discoveryEndpoint)
52                 {
53                         if (discoveryEndpoint == null)
54                                 throw new ArgumentNullException ("discoveryEndpoint");
55
56                         client = Activator.CreateInstance (discoveryEndpoint.DiscoveryVersion.DiscoveryProxyClientType, new object [] {discoveryEndpoint});
57                 }
58
59                 public DiscoveryClient (string endpointConfigurationName)
60                 {
61                         throw new NotImplementedException ();
62                 }
63
64                 // FIXME: make it dynamic (dmcs crashes for now)
65                 object client;
66
67                 public ChannelFactory ChannelFactory {
68                         get { return (ChannelFactory) client.GetType ().GetProperty ("ChannelFactory").GetValue (client, null); }
69                 }
70
71                 public ClientCredentials ClientCredentials {
72                         get { return ChannelFactory.Credentials; }
73                 }
74
75                 public ServiceEndpoint Endpoint {
76                         get { return ChannelFactory.Endpoint; }
77                 }
78
79                 public IClientChannel InnerChannel {
80                         get { return (IClientChannel) client.GetType ().GetProperty ("InnerChannel").GetValue (client, null); }
81                 }
82
83                 CommunicationState State {
84                         get { return ((ICommunicationObject) this).State; }
85                 }
86
87                 CommunicationState ICommunicationObject.State {
88                         get { return ((ICommunicationObject) client).State; }
89                 }
90
91                 public event EventHandler<FindCompletedEventArgs> FindCompleted;
92                 public event EventHandler<FindProgressChangedEventArgs> FindProgressChanged;
93                 public event EventHandler<AnnouncementEventArgs> ProxyAvailable;
94                 public event EventHandler<ResolveCompletedEventArgs> ResolveCompleted;
95
96                 event EventHandler ICommunicationObject.Closed {
97                         add { InnerChannel.Closed += value; }
98                         remove { InnerChannel.Closed -= value; }
99                 }
100                 event EventHandler ICommunicationObject.Closing {
101                         add { InnerChannel.Closing += value; }
102                         remove { InnerChannel.Closing -= value; }
103                 }
104                 event EventHandler ICommunicationObject.Faulted {
105                         add { InnerChannel.Faulted += value; }
106                         remove { InnerChannel.Faulted -= value; }
107                 }
108                 event EventHandler ICommunicationObject.Opened {
109                         add { InnerChannel.Opened += value; }
110                         remove { InnerChannel.Opened -= value; }
111                 }
112                 event EventHandler ICommunicationObject.Opening {
113                         add { InnerChannel.Opening += value; }
114                         remove { InnerChannel.Opening -= value; }
115                 }
116
117                 public void Open ()
118                 {
119                         ((ICommunicationObject) this).Open ();
120                 }
121
122                 public void Close ()
123                 {
124                         ((ICommunicationObject) this).Close ();
125                 }
126
127                 bool cancelled;
128
129                 public void CancelAsync (object userState)
130                 {
131                         throw new NotImplementedException ();
132                 }
133
134                 // find
135
136                 public FindResponse Find (FindCriteria criteria)
137                 {
138                         return EndFind (BeginFind (criteria, null, null));
139                 }
140
141                 public void FindAsync (FindCriteria criteria)
142                 {
143                         FindAsync (criteria, null);
144                 }
145
146                 public void FindAsync (FindCriteria criteria, object userState)
147                 {
148                         AsyncCallback cb = delegate (IAsyncResult result) {
149                                 FindResponse ret = null;
150                                 Exception error = null;
151                                 try {
152                                         ret = EndFind (result);
153                                 } catch (Exception ex) {
154                                         error = ex;
155                                 }
156                                 OnFindCompleted (new FindCompletedEventArgs (ret, error, cancelled, result.AsyncState));
157                         };
158                         cancelled = false;
159                         BeginFind (criteria, cb, userState);
160                 }
161
162                 void OnFindCompleted (FindCompletedEventArgs args)
163                 {
164                         if (FindCompleted != null)
165                                 FindCompleted (this, args);
166                 }
167
168                 IAsyncResult BeginFind (FindCriteria criteria, AsyncCallback callback, object state)
169                 {
170                         return ((IDiscoveryCommon) client).BeginFind (criteria, callback, state);
171                 }
172                 
173                 FindResponse EndFind (IAsyncResult result)
174                 {
175                         return ((IDiscoveryCommon) client).EndFind (result);
176                 }
177
178                 // resolve
179
180                 public ResolveResponse Resolve (ResolveCriteria criteria)
181                 {
182                         return EndResolve (BeginResolve (criteria, null, null));
183                 }
184
185                 public void ResolveAsync (ResolveCriteria criteria)
186                 {
187                         ResolveAsync (criteria, null);
188                 }
189
190                 public void ResolveAsync (ResolveCriteria criteria, object userState)
191                 {
192                         AsyncCallback cb = delegate (IAsyncResult result) {
193                                 ResolveResponse ret = null;
194                                 Exception error = null;
195                                 try {
196                                         ret = EndResolve (result);
197                                 } catch (Exception ex) {
198                                         error = ex;
199                                 }
200                                 OnResolveCompleted (new ResolveCompletedEventArgs (ret, error, cancelled, result.AsyncState));
201                         };
202                         cancelled = false;
203                         BeginResolve (criteria, cb, userState);
204                 }
205
206                 void OnResolveCompleted (ResolveCompletedEventArgs args)
207                 {
208                         if (ResolveCompleted != null)
209                                 ResolveCompleted (this, args);
210                 }
211
212                 IAsyncResult BeginResolve (ResolveCriteria criteria, AsyncCallback callback, object state)
213                 {
214                         return ((IDiscoveryCommon) client).BeginResolve (criteria, callback, state);
215                 }
216                 
217                 ResolveResponse EndResolve (IAsyncResult result)
218                 {
219                         return ((IDiscoveryCommon) client).EndResolve (result);
220                 }
221
222                 // explicit interface impl.
223
224                 void ICommunicationObject.Open ()
225                 {
226                         InnerChannel.Open ();
227                 }
228
229                 void ICommunicationObject.Open (TimeSpan timeout)
230                 {
231                         InnerChannel.Open (timeout);
232                 }
233
234                 void ICommunicationObject.Close ()
235                 {
236                         InnerChannel.Close ();
237                 }
238
239                 void ICommunicationObject.Close (TimeSpan timeout)
240                 {
241                         InnerChannel.Close (timeout);
242                 }
243
244                 IAsyncResult ICommunicationObject.BeginOpen (AsyncCallback callback, object state)
245                 {
246                         return InnerChannel.BeginOpen (callback, state);
247                 }
248
249                 IAsyncResult ICommunicationObject.BeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
250                 {
251                         return InnerChannel.BeginOpen (timeout, callback, state);
252                 }
253
254                 IAsyncResult ICommunicationObject.BeginClose (AsyncCallback callback, object state)
255                 {
256                         return InnerChannel.BeginClose (callback, state);
257                 }
258
259                 IAsyncResult ICommunicationObject.BeginClose (TimeSpan timeout, AsyncCallback callback, object state)
260                 {
261                         return InnerChannel.BeginClose (timeout, callback, state);
262                 }
263
264                 void ICommunicationObject.EndOpen (IAsyncResult result)
265                 {
266                         InnerChannel.EndOpen (result);
267                 }
268
269                 void ICommunicationObject.EndClose (IAsyncResult result)
270                 {
271                         InnerChannel.EndClose (result);
272                 }
273
274                 void ICommunicationObject.Abort ()
275                 {
276                         InnerChannel.Abort ();
277                 }
278
279                 void IDisposable.Dispose ()
280                 {
281                         InnerChannel.Dispose ();
282                 }
283         }
284 }