CustomPeerResolverService.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.Linq;
  33. using System.Transactions;
  34. using System.Timers;
  35. namespace System.ServiceModel.PeerResolvers
  36. {
  37. [MonoTODO ("Implement cleanup and refresh")]
  38. // FIXME: TransactionTimeout must be null by-default.
  39. [ServiceBehavior (AutomaticSessionShutdown = true, ConcurrencyMode = ConcurrencyMode.Multiple,
  40. InstanceContextMode = InstanceContextMode.Single, ReleaseServiceInstanceOnTransactionComplete = true,
  41. TransactionIsolationLevel = IsolationLevel.Unspecified, /*TransactionTimeout = null, */
  42. UseSynchronizationContext = false, ValidateMustUnderstand = true)]
  43. public class CustomPeerResolverService : IPeerResolverContract
  44. {
  45. bool control_shape;
  46. bool opened;
  47. // Maybe it's worth to change List<T> for a better distributed and faster collection.
  48. List<Node> mesh = new List<Node> ();
  49. object mesh_lock = new object ();
  50. Timer refresh_timer, cleanup_timer;
  51. DateTime last_refresh_time = DateTime.Now;
  52. public CustomPeerResolverService ()
  53. {
  54. refresh_timer = new Timer () { AutoReset = true };
  55. RefreshInterval = new TimeSpan (0, 10, 0);
  56. refresh_timer.Elapsed += delegate {
  57. // FIXME: implement
  58. };
  59. cleanup_timer = new Timer () { AutoReset = true };
  60. CleanupInterval = new TimeSpan (0, 1, 0);
  61. cleanup_timer.Elapsed += delegate {
  62. // FIXME: implement
  63. };
  64. control_shape = false;
  65. opened = false;
  66. }
  67. public TimeSpan CleanupInterval {
  68. get { return TimeSpan.FromMilliseconds ((int) cleanup_timer.Interval); }
  69. set {
  70. if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
  71. throw new ArgumentOutOfRangeException (
  72. "The interval is either zero or greater than max value.");
  73. if (opened)
  74. throw new InvalidOperationException ("The interval must be set before it is opened");
  75. cleanup_timer.Interval = value.TotalMilliseconds;
  76. }
  77. }
  78. public bool ControlShape {
  79. get { return control_shape; }
  80. set { control_shape = value; }
  81. }
  82. public TimeSpan RefreshInterval {
  83. get { return TimeSpan.FromMilliseconds ((int) refresh_timer.Interval); }
  84. set {
  85. if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
  86. throw new ArgumentOutOfRangeException (
  87. "The interval is either zero or greater than max value.");
  88. if (opened)
  89. throw new InvalidOperationException ("The interval must be set before it is opened");
  90. refresh_timer.Interval = value.TotalMilliseconds;
  91. }
  92. }
  93. [MonoTODO ("Do we have to unregister nodes here?")]
  94. public virtual void Close ()
  95. {
  96. if (! opened)
  97. throw new InvalidOperationException ("The service has never been opened or it was closed by a previous call to this method.");
  98. refresh_timer.Stop ();
  99. cleanup_timer.Stop ();
  100. }
  101. [MonoTODO]
  102. public virtual ServiceSettingsResponseInfo GetServiceSettings ()
  103. {
  104. if (! opened)
  105. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  106. // return new ServiceSettingsResponseInfo ();
  107. throw new NotImplementedException ();
  108. }
  109. public virtual void Open ()
  110. {
  111. if ((CleanupInterval == TimeSpan.Zero) || (RefreshInterval == TimeSpan.Zero))
  112. throw new ArgumentException ("Cleanup interval or refresh interval are set to a time span interval of zero.");
  113. if (opened)
  114. throw new InvalidOperationException ("The service has been started by a previous call to this method.");
  115. opened = true;
  116. refresh_timer.Start ();
  117. cleanup_timer.Start ();
  118. }
  119. [MonoTODO]
  120. public virtual RefreshResponseInfo Refresh (RefreshInfo refreshInfo)
  121. {
  122. if (refreshInfo == null)
  123. throw new ArgumentException ("Refresh info cannot be null.");
  124. if (! opened)
  125. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  126. var node = mesh.FirstOrDefault (n => n.MeshId == refreshInfo.MeshId && n.RegistrationId.Equals (refreshInfo.RegistrationId));
  127. if (node == null)
  128. return new RefreshResponseInfo (TimeSpan.Zero, RefreshResult.RegistrationNotFound);
  129. // FIXME: implement actual refresh.
  130. last_refresh_time = DateTime.Now;
  131. return new RefreshResponseInfo (RefreshInterval - (DateTime.Now - last_refresh_time), RefreshResult.Success);
  132. }
  133. public virtual RegisterResponseInfo Register (RegisterInfo registerInfo)
  134. {
  135. if (registerInfo == null)
  136. throw new ArgumentException ("Register info cannot be null.");
  137. if (! opened)
  138. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  139. return Register (registerInfo.ClientId, registerInfo.MeshId, registerInfo.NodeAddress);
  140. }
  141. public virtual RegisterResponseInfo Register (Guid clientId, string meshId, PeerNodeAddress address)
  142. {
  143. Node n = new Node () { RegistrationId = Guid.NewGuid (), MeshId = meshId, ClientId = clientId, NodeAddress = address };
  144. RegisterResponseInfo rri = new RegisterResponseInfo ();
  145. rri.RegistrationId = n.RegistrationId;
  146. lock (mesh_lock)
  147. mesh.Add (n);
  148. return rri;
  149. }
  150. public virtual ResolveResponseInfo Resolve (ResolveInfo resolveInfo)
  151. {
  152. ResolveResponseInfo rri = new ResolveResponseInfo ();
  153. if (resolveInfo == null)
  154. throw new ArgumentException ("Resolve info cannot be null.");
  155. if (! opened)
  156. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  157. foreach (var node in mesh)
  158. if (node.MeshId == resolveInfo.MeshId &&
  159. node.ClientId == resolveInfo.ClientId)
  160. rri.Addresses.Add (node.NodeAddress);
  161. return rri;
  162. }
  163. public virtual void Unregister (UnregisterInfo unregisterInfo)
  164. {
  165. if (unregisterInfo == null)
  166. throw new ArgumentException ("Unregister info cannot be null.");
  167. if (! opened)
  168. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  169. lock (mesh_lock)
  170. foreach (var node in mesh)
  171. if (node.MeshId == unregisterInfo.MeshId &&
  172. node.RegistrationId == unregisterInfo.RegistrationId) {
  173. mesh.Remove (node);
  174. break;
  175. }
  176. }
  177. [MonoTODO]
  178. public virtual RegisterResponseInfo Update (UpdateInfo updateInfo)
  179. {
  180. if (updateInfo == null)
  181. throw new ArgumentException ("Update info cannot be null.");
  182. if (! opened)
  183. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  184. // return new RegisterResponseInfo ();
  185. throw new NotImplementedException ();
  186. }
  187. }
  188. internal class Node
  189. {
  190. public Guid ClientId { get; set; }
  191. public string MeshId { get; set; }
  192. public Guid RegistrationId { get; set; }
  193. public PeerNodeAddress NodeAddress { get; set; }
  194. }
  195. }