DiscoveryTargetClientApril2005.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Author: Atsushi Enomoto <[email protected]>
  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. namespace System.ServiceModel.Discovery.VersionApril2005
  36. {
  37. internal class DiscoveryTargetClientApril2005 : DuplexClientBase<IDiscoveryTargetContractApril2005>, DiscoveryClient.IDiscoveryCommon
  38. {
  39. public DiscoveryTargetClientApril2005 (ServiceEndpoint endpoint)
  40. : this (new DiscoveryTargetCallbackApril2005 (), endpoint)
  41. {
  42. }
  43. DiscoveryTargetClientApril2005 (DiscoveryTargetCallbackApril2005 instance, ServiceEndpoint endpoint)
  44. : base (instance, endpoint)
  45. {
  46. instance.ReplyFindCompleted += delegate (MessageContractsApril2005.FindResponse response) {
  47. find_completed = delegate { return response; };
  48. reply_find_handle.Set ();
  49. };
  50. instance.ReplyResolveCompleted += delegate (MessageContractsApril2005.ResolveResponse response) {
  51. resolve_completed = delegate { return response; };
  52. reply_resolve_handle.Set ();
  53. };
  54. }
  55. // Find
  56. Func<FindCriteria,FindResponse> find_delegate;
  57. Func<MessageContractsApril2005.FindResponse> find_completed;
  58. ManualResetEvent reply_find_handle = new ManualResetEvent (false);
  59. public IAsyncResult BeginFind (FindCriteria criteria, AsyncCallback callback, object state)
  60. {
  61. if (find_delegate == null)
  62. find_delegate = new Func<FindCriteria,FindResponse> (Find);
  63. return find_delegate.BeginInvoke (criteria, callback, state);
  64. }
  65. public FindResponse EndFind (IAsyncResult result)
  66. {
  67. return find_delegate.EndInvoke (result);
  68. }
  69. FindResponse Find (FindCriteria criteria)
  70. {
  71. var req = new MessageContractsApril2005.FindRequest () { Body = new FindCriteriaApril2005 (criteria) };
  72. Channel.BeginFind (req, delegate (IAsyncResult result) {
  73. Channel.EndFind (result);
  74. }, null);
  75. if (!reply_find_handle.WaitOne (InnerChannel.OperationTimeout))
  76. throw new TimeoutException ();
  77. try {
  78. var ir = find_completed ();
  79. var ret = new FindResponse ();
  80. foreach (var fr in ir.Body)
  81. ret.Endpoints.Add (fr.ToEndpointDiscoveryMetadata ());
  82. return ret;
  83. } finally {
  84. find_completed = null;
  85. }
  86. }
  87. // Resolve
  88. Func<ResolveCriteria,ResolveResponse> resolve_delegate;
  89. Func<MessageContractsApril2005.ResolveResponse> resolve_completed;
  90. ManualResetEvent reply_resolve_handle = new ManualResetEvent (false);
  91. public IAsyncResult BeginResolve (ResolveCriteria criteria, AsyncCallback callback, object state)
  92. {
  93. if (resolve_delegate == null)
  94. resolve_delegate = new Func<ResolveCriteria,ResolveResponse> (Resolve);
  95. return resolve_delegate.BeginInvoke (criteria, callback, state);
  96. }
  97. public ResolveResponse EndResolve (IAsyncResult result)
  98. {
  99. return resolve_delegate.EndInvoke (result);
  100. }
  101. public ResolveResponse Resolve (ResolveCriteria criteria)
  102. {
  103. var req = new MessageContractsApril2005.ResolveRequest () { Body = new ResolveCriteriaApril2005 (criteria) };
  104. Channel.BeginResolve (req, delegate (IAsyncResult result) {
  105. Channel.EndResolve (result);
  106. }, null);
  107. if (!reply_resolve_handle.WaitOne (InnerChannel.OperationTimeout))
  108. throw new TimeoutException ();
  109. try {
  110. var ir = resolve_completed ();
  111. var metadata = ir.Body.ToEndpointDiscoveryMetadata ();
  112. var sequence = ir.MessageSequence.ToDiscoveryMessageSequence ();
  113. return new ResolveResponse (metadata, sequence);
  114. } finally {
  115. resolve_completed = null;
  116. }
  117. }
  118. internal class DiscoveryTargetCallbackApril2005 : IDiscoveryTargetCallbackContractApril2005
  119. {
  120. public event Action<MessageContractsApril2005.FindResponse> ReplyFindCompleted;
  121. public event Action<MessageContractsApril2005.ResolveResponse> ReplyResolveCompleted;
  122. public void ReplyFind (MessageContractsApril2005.FindResponse message)
  123. {
  124. if (ReplyFindCompleted != null)
  125. ReplyFindCompleted (message);
  126. }
  127. public void ReplyResolve (MessageContractsApril2005.ResolveResponse message)
  128. {
  129. if (ReplyResolveCompleted != null)
  130. ReplyResolveCompleted (message);
  131. }
  132. }
  133. }
  134. }