Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.ServiceModel.Discovery / System.ServiceModel.Discovery.VersionCD1 / DiscoveryTargetClientCD1.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.Linq;
30 using System.ServiceModel;
31 using System.ServiceModel.Channels;
32 using System.ServiceModel.Description;
33 using System.ServiceModel.Dispatcher;
34 using System.Threading;
35
36 namespace System.ServiceModel.Discovery.VersionCD1
37 {
38         internal class DiscoveryTargetClientCD1 : DuplexClientBase<IDiscoveryTargetContractCD1>, DiscoveryClient.IDiscoveryCommon
39         {
40                 public DiscoveryTargetClientCD1 (ServiceEndpoint endpoint)
41                         : this (new DiscoveryTargetCallbackCD1 (), endpoint)
42                 {
43                 }
44
45                 DiscoveryTargetClientCD1 (DiscoveryTargetCallbackCD1 instance, ServiceEndpoint endpoint)
46                         : base (instance, endpoint)
47                 {
48                         instance.ReplyFindCompleted += delegate (MessageContractsCD1.FindResponse response) {
49                                 find_completed = delegate { return response; };
50                                 reply_find_handle.Set ();
51                         };
52                         instance.ReplyResolveCompleted += delegate (MessageContractsCD1.ResolveResponse response) {
53                                 resolve_completed = delegate { return response; };
54                                 reply_resolve_handle.Set ();
55                         };
56                 }
57
58                 // Find
59
60                 Func<FindCriteria,FindResponse> find_delegate;
61                 Func<MessageContractsCD1.FindResponse> find_completed;
62                 ManualResetEvent reply_find_handle = new ManualResetEvent (false);
63
64                 public IAsyncResult BeginFind (FindCriteria criteria, AsyncCallback callback, object state)
65                 {
66                         if (find_delegate == null)
67                                 find_delegate = new Func<FindCriteria,FindResponse> (Find);
68                         return find_delegate.BeginInvoke (criteria, callback, state);
69                 }
70                 
71                 public FindResponse EndFind (IAsyncResult result)
72                 {
73                         return find_delegate.EndInvoke (result);
74                 }
75                 
76                 FindResponse Find (FindCriteria criteria)
77                 {
78                         var req = new MessageContractsCD1.FindRequest () { Body = new FindCriteriaCD1 (criteria) };
79                         Channel.BeginFind (req, delegate (IAsyncResult result) {
80                                 Channel.EndFind (result);
81                         }, null);
82                         
83                         if (!reply_find_handle.WaitOne (InnerChannel.OperationTimeout))
84                                 throw new EndpointNotFoundException ("The discovery client could not receive Find operation response within the operation timeout.");
85                         try {
86                                 var ir = find_completed ();
87                                 var ret = new FindResponse ();
88                                 foreach (var fr in ir.Body)
89                                         ret.Endpoints.Add (fr.ToEndpointDiscoveryMetadata ());
90                                 return ret;
91                         } finally {
92                                 find_completed = null;
93                         }
94                 }
95
96                 // Resolve
97
98                 Func<ResolveCriteria,ResolveResponse> resolve_delegate;
99                 Func<MessageContractsCD1.ResolveResponse> resolve_completed;
100                 ManualResetEvent reply_resolve_handle = new ManualResetEvent (false);
101
102                 public IAsyncResult BeginResolve (ResolveCriteria criteria, AsyncCallback callback, object state)
103                 {
104                         if (resolve_delegate == null)
105                                 resolve_delegate = new Func<ResolveCriteria,ResolveResponse> (Resolve);
106                         return resolve_delegate.BeginInvoke (criteria, callback, state);
107                 }
108                 
109                 public ResolveResponse EndResolve (IAsyncResult result)
110                 {
111                         return resolve_delegate.EndInvoke (result);
112                 }
113                 
114                 public ResolveResponse Resolve (ResolveCriteria criteria)
115                 {
116                         var req = new MessageContractsCD1.ResolveRequest () { Body = new ResolveCriteriaCD1 (criteria) };
117                         Channel.BeginResolve (req, delegate (IAsyncResult result) {
118                                 Channel.EndResolve (result);
119                         }, null);
120
121                         if (!reply_resolve_handle.WaitOne (InnerChannel.OperationTimeout))
122                                 throw new TimeoutException ();
123                         try {
124                                 var ir = resolve_completed ();
125                                 var metadata = ir.Body.ToEndpointDiscoveryMetadata ();
126                                 var sequence = ir.MessageSequence.ToDiscoveryMessageSequence ();
127                                 return new ResolveResponse (metadata, sequence);
128                         } finally {
129                                 resolve_completed = null;
130                         }
131                 }
132         
133                 internal class DiscoveryTargetCallbackCD1 : IDiscoveryTargetCallbackContractCD1
134                 {
135                         public event Action<MessageContractsCD1.FindResponse> ReplyFindCompleted;
136                         public event Action<MessageContractsCD1.ResolveResponse> ReplyResolveCompleted;
137
138                         public void ReplyFind (MessageContractsCD1.FindResponse message)
139                         {
140                                 if (ReplyFindCompleted != null)
141                                         ReplyFindCompleted (message);
142                         }
143
144                         public void ReplyResolve (MessageContractsCD1.ResolveResponse message)
145                         {
146                                 if (ReplyResolveCompleted != null)
147                                         ReplyResolveCompleted (message);
148                         }
149                 }
150         }
151 }