[corlib] Improve CancellationTokenSource test
[mono.git] / mcs / class / System.ServiceModel.Discovery / System.ServiceModel.Discovery.VersionApril2005 / DiscoveryTargetClientApril2005.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.VersionApril2005
37 {
38         internal class DiscoveryTargetClientApril2005 : DuplexClientBase<IDiscoveryTargetContractApril2005>, DiscoveryClient.IDiscoveryCommon
39         {
40                 public DiscoveryTargetClientApril2005 (ServiceEndpoint endpoint)
41                         : this (new DiscoveryTargetCallbackApril2005 (), endpoint)
42                 {
43                 }
44
45                 DiscoveryTargetClientApril2005 (DiscoveryTargetCallbackApril2005 instance, ServiceEndpoint endpoint)
46                         : base (instance, endpoint)
47                 {
48                         instance.ReplyFindCompleted += delegate (MessageContractsApril2005.FindResponse response) {
49                                 find_completed = delegate { return response; };
50                                 reply_find_handle.Set ();
51                         };
52                         instance.ReplyResolveCompleted += delegate (MessageContractsApril2005.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<MessageContractsApril2005.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 MessageContractsApril2005.FindRequest () { Body = new FindCriteriaApril2005 (criteria) };
79                         Channel.BeginFind (req, delegate (IAsyncResult result) {
80                                 Channel.EndFind (result);
81                         }, null);
82                         
83                         var timeout = InnerChannel.OperationTimeout < criteria.Duration ? InnerChannel.OperationTimeout : criteria.Duration;
84                         if (!reply_find_handle.WaitOne (timeout))
85                                 throw new EndpointNotFoundException ("The discovery client could not receive Find operation response within the operation timeout.");
86                         try {
87                                 var ir = find_completed ();
88                                 var ret = new FindResponse ();
89                                 foreach (var fr in ir.Body)
90                                         ret.Endpoints.Add (fr.ToEndpointDiscoveryMetadata ());
91                                 return ret;
92                         } finally {
93                                 find_completed = null;
94                         }
95                 }
96
97                 // Resolve
98
99                 Func<ResolveCriteria,ResolveResponse> resolve_delegate;
100                 Func<MessageContractsApril2005.ResolveResponse> resolve_completed;
101                 ManualResetEvent reply_resolve_handle = new ManualResetEvent (false);
102
103                 public IAsyncResult BeginResolve (ResolveCriteria criteria, AsyncCallback callback, object state)
104                 {
105                         if (resolve_delegate == null)
106                                 resolve_delegate = new Func<ResolveCriteria,ResolveResponse> (Resolve);
107                         return resolve_delegate.BeginInvoke (criteria, callback, state);
108                 }
109                 
110                 public ResolveResponse EndResolve (IAsyncResult result)
111                 {
112                         return resolve_delegate.EndInvoke (result);
113                 }
114                 
115                 public ResolveResponse Resolve (ResolveCriteria criteria)
116                 {
117                         var req = new MessageContractsApril2005.ResolveRequest () { Body = new ResolveCriteriaApril2005 (criteria) };
118                         Channel.BeginResolve (req, delegate (IAsyncResult result) {
119                                 Channel.EndResolve (result);
120                         }, null);
121
122                         var timeout = InnerChannel.OperationTimeout < criteria.Duration ? InnerChannel.OperationTimeout : criteria.Duration;
123                         if (!reply_find_handle.WaitOne (timeout))
124                                 throw new TimeoutException ();
125                         try {
126                                 var ir = resolve_completed ();
127                                 var metadata = ir.Body.ToEndpointDiscoveryMetadata ();
128                                 var sequence = ir.MessageSequence.ToDiscoveryMessageSequence ();
129                                 return new ResolveResponse (metadata, sequence);
130                         } finally {
131                                 resolve_completed = null;
132                         }
133                 }
134         
135                 internal class DiscoveryTargetCallbackApril2005 : IDiscoveryTargetCallbackContractApril2005
136                 {
137                         public event Action<MessageContractsApril2005.FindResponse> ReplyFindCompleted;
138                         public event Action<MessageContractsApril2005.ResolveResponse> ReplyResolveCompleted;
139
140                         public void ReplyFind (MessageContractsApril2005.FindResponse message)
141                         {
142                                 if (ReplyFindCompleted != null)
143                                         ReplyFindCompleted (message);
144                         }
145
146                         public void ReplyResolve (MessageContractsApril2005.ResolveResponse message)
147                         {
148                                 if (ReplyResolveCompleted != null)
149                                         ReplyResolveCompleted (message);
150                         }
151                 }
152         }
153 }