2
0

CustomPeerResolverService.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // CustomPeerResolverService.cs
  3. //
  4. // Author:
  5. // Marcos Cobena ([email protected])
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
  9. //
  10. // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Collections.Generic;
  32. using System.IO;
  33. using System.Linq;
  34. using System.Net.Sockets;
  35. using System.Transactions;
  36. using System.Timers;
  37. namespace System.ServiceModel.PeerResolvers
  38. {
  39. [MonoTODO ("Implement cleanup and refresh")]
  40. [ServiceBehavior (ConcurrencyMode = ConcurrencyMode.Multiple,
  41. InstanceContextMode = InstanceContextMode.Single,
  42. UseSynchronizationContext = false)]
  43. public class CustomPeerResolverService : IPeerResolverContract
  44. {
  45. static ServiceHost localhost;
  46. static void SetupCustomPeerResolverServiceHost ()
  47. {
  48. // launch peer resolver service locally only when it does not seem to be running ...
  49. var t = new TcpListener (8931);
  50. try {
  51. t.Start ();
  52. t.Stop ();
  53. } catch {
  54. return;
  55. }
  56. Console.WriteLine ("WARNING: it is running peer resolver service locally. This means, the node registration is valid only within this application domain...");
  57. var host = new ServiceHost (new LocalPeerResolverService (TextWriter.Null));
  58. host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().InstanceContextMode = InstanceContextMode.Single;
  59. host.AddServiceEndpoint (typeof (ICustomPeerResolverContract), new BasicHttpBinding (), "http://localhost:8931");
  60. localhost = host;
  61. host.Open ();
  62. }
  63. ICustomPeerResolverClient client;
  64. bool control_shape, opened;
  65. TimeSpan refresh_interval, cleanup_interval;
  66. public CustomPeerResolverService ()
  67. {
  68. client = ChannelFactory<ICustomPeerResolverClient>.CreateChannel (new BasicHttpBinding (), new EndpointAddress ("http://localhost:8931"));
  69. refresh_interval = new TimeSpan (0, 10, 0);
  70. cleanup_interval = new TimeSpan (0, 1, 0);
  71. }
  72. public TimeSpan CleanupInterval {
  73. get { return cleanup_interval; }
  74. set {
  75. if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
  76. throw new ArgumentOutOfRangeException (
  77. "The interval is either zero or greater than max value.");
  78. if (opened)
  79. throw new InvalidOperationException ("The interval must be set before it is opened");
  80. cleanup_interval = value;
  81. }
  82. }
  83. public bool ControlShape {
  84. get { return control_shape; }
  85. set {
  86. if (opened)
  87. throw new InvalidOperationException ("The interval must be set before it is opened");
  88. control_shape = value;
  89. }
  90. }
  91. public TimeSpan RefreshInterval {
  92. get { return refresh_interval; }
  93. set {
  94. if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
  95. throw new ArgumentOutOfRangeException (
  96. "The interval is either zero or greater than max value.");
  97. if (opened)
  98. throw new InvalidOperationException ("The interval must be set before it is opened");
  99. refresh_interval = value;
  100. }
  101. }
  102. [MonoTODO ("Do we have to unregister nodes here?")]
  103. public virtual void Close ()
  104. {
  105. if (! opened)
  106. throw new InvalidOperationException ("The service has never been opened or it was closed by a previous call to this method.");
  107. client.Close ();
  108. opened = false;
  109. if (localhost != null) {
  110. localhost.Close ();
  111. localhost = null;
  112. }
  113. }
  114. public virtual ServiceSettingsResponseInfo GetServiceSettings ()
  115. {
  116. if (! opened)
  117. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  118. return client.GetServiceSettings ();
  119. }
  120. public virtual void Open ()
  121. {
  122. if (localhost == null)
  123. SetupCustomPeerResolverServiceHost ();
  124. if ((CleanupInterval == TimeSpan.Zero) || (RefreshInterval == TimeSpan.Zero))
  125. throw new ArgumentException ("Cleanup interval or refresh interval are set to a time span interval of zero.");
  126. if (opened)
  127. throw new InvalidOperationException ("The service has been started by a previous call to this method.");
  128. opened = true;
  129. client.Open ();
  130. client.SetCustomServiceSettings (new PeerServiceSettingsInfo () { ControlMeshShape = control_shape, RefreshInterval = refresh_interval, CleanupInterval = cleanup_interval });
  131. }
  132. public virtual RefreshResponseInfo Refresh (RefreshInfo refreshInfo)
  133. {
  134. if (refreshInfo == null)
  135. throw new ArgumentException ("Refresh info cannot be null.");
  136. if (! opened)
  137. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  138. return client.Refresh (refreshInfo);
  139. }
  140. public virtual RegisterResponseInfo Register (RegisterInfo registerInfo)
  141. {
  142. if (registerInfo == null)
  143. throw new ArgumentException ("Register info cannot be null.");
  144. if (! opened)
  145. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  146. return client.Register (registerInfo);
  147. }
  148. public virtual RegisterResponseInfo Register (Guid clientId, string meshId, PeerNodeAddress address)
  149. {
  150. return Register (new RegisterInfo (clientId, meshId, address));
  151. }
  152. public virtual ResolveResponseInfo Resolve (ResolveInfo resolveInfo)
  153. {
  154. if (resolveInfo == null)
  155. throw new ArgumentException ("Resolve info cannot be null.");
  156. if (! opened)
  157. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  158. return client.Resolve (resolveInfo);
  159. }
  160. public virtual void Unregister (UnregisterInfo unregisterInfo)
  161. {
  162. if (unregisterInfo == null)
  163. throw new ArgumentException ("Unregister info cannot be null.");
  164. if (! opened)
  165. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  166. client.Unregister (unregisterInfo);
  167. }
  168. public virtual RegisterResponseInfo Update (UpdateInfo updateInfo)
  169. {
  170. if (updateInfo == null)
  171. throw new ArgumentException ("Update info cannot be null.");
  172. if (! opened)
  173. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  174. return client.Update (updateInfo);
  175. }
  176. }
  177. }