CustomPeerResolverService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // CustomPeerResolverService.cs
  3. //
  4. // Author:
  5. // Marcos Cobena ([email protected])
  6. //
  7. // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
  8. //
  9. using System.Collections.Generic;
  10. using System.Transactions;
  11. namespace System.ServiceModel.PeerResolvers
  12. {
  13. // FIXME: TransactionTimeout must be null by-default.
  14. [ServiceBehavior (AutomaticSessionShutdown = true, ConcurrencyMode = ConcurrencyMode.Multiple,
  15. InstanceContextMode = InstanceContextMode.Single, ReleaseServiceInstanceOnTransactionComplete = true,
  16. TransactionIsolationLevel = IsolationLevel.Unspecified, /*TransactionTimeout = null, */
  17. UseSynchronizationContext = false, ValidateMustUnderstand = true)]
  18. public class CustomPeerResolverService : IPeerResolverContract
  19. {
  20. TimeSpan cleanup_interval;
  21. bool control_shape;
  22. bool opened;
  23. // Maybe it's worth to change List<T> for a better distributed and faster collection.
  24. List<Node> mesh = new List<Node> ();
  25. object mesh_lock = new object ();
  26. TimeSpan refresh_interval;
  27. public CustomPeerResolverService ()
  28. {
  29. cleanup_interval = new TimeSpan (0, 1, 0);
  30. control_shape = false;
  31. opened = false;
  32. refresh_interval = new TimeSpan (0, 10, 0);
  33. }
  34. [MonoTODO ("To check for InvalidOperationException")]
  35. public TimeSpan CleanupInterval {
  36. get { return cleanup_interval; }
  37. set {
  38. if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
  39. throw new ArgumentOutOfRangeException (
  40. "The interval is either zero or greater than max value.");
  41. cleanup_interval = value;
  42. }
  43. }
  44. public bool ControlShape {
  45. get { return control_shape; }
  46. set { control_shape = value; }
  47. }
  48. [MonoTODO ("To check for InvalidOperationException")]
  49. public TimeSpan RefreshInterval {
  50. get { return refresh_interval; }
  51. set {
  52. if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
  53. throw new ArgumentOutOfRangeException (
  54. "The interval is either zero or greater than max value.");
  55. refresh_interval = value;
  56. }
  57. }
  58. [MonoTODO]
  59. public virtual void Close ()
  60. {
  61. if (! opened)
  62. throw new InvalidOperationException ("The service has never been opened or it was closed by a previous call to this method.");
  63. }
  64. [MonoTODO]
  65. public virtual ServiceSettingsResponseInfo GetServiceSettings ()
  66. {
  67. if (! opened)
  68. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  69. // return new ServiceSettingsResponseInfo ();
  70. throw new NotImplementedException ();
  71. }
  72. [MonoTODO]
  73. public virtual void Open ()
  74. {
  75. if ((cleanup_interval == TimeSpan.Zero) || (refresh_interval == TimeSpan.Zero))
  76. throw new ArgumentException ("Cleanup interval or refresh interval are set to a time span interval of zero.");
  77. if (opened)
  78. throw new InvalidOperationException ("The service has been started by a previous call to this method.");
  79. opened = true;
  80. }
  81. [MonoTODO]
  82. public virtual RefreshResponseInfo Refresh (RefreshInfo refreshInfo)
  83. {
  84. if (refreshInfo == null)
  85. throw new ArgumentException ("Refresh info cannot be null.");
  86. if (! opened)
  87. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  88. // return new RefreshResponseInfo ();
  89. throw new NotImplementedException ();
  90. }
  91. public virtual RegisterResponseInfo Register (RegisterInfo registerInfo)
  92. {
  93. if (registerInfo == null)
  94. throw new ArgumentException ("Register info cannot be null.");
  95. if (! opened)
  96. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  97. return Register (registerInfo.ClientId, registerInfo.MeshId, registerInfo.NodeAddress);
  98. }
  99. [MonoTODO]
  100. public virtual RegisterResponseInfo Register (Guid clientId,
  101. string meshId,
  102. PeerNodeAddress address)
  103. {
  104. Node n = new Node ();
  105. RegisterResponseInfo rri = new RegisterResponseInfo ();
  106. if (ControlShape) {
  107. // FIXME: To update mesh node here.
  108. lock (mesh_lock)
  109. {
  110. mesh.Add (n);
  111. // Console.WriteLine ("{0}, {1}, {2}", clientId, meshId, address);
  112. }
  113. }
  114. return rri;
  115. }
  116. [MonoTODO]
  117. public virtual ResolveResponseInfo Resolve (ResolveInfo resolveInfo)
  118. {
  119. ResolveResponseInfo rri = new ResolveResponseInfo ();
  120. if (resolveInfo == null)
  121. throw new ArgumentException ("Resolve info cannot be null.");
  122. if (! opened)
  123. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  124. if (ControlShape)
  125. {
  126. // FIXME: To resolve address here.
  127. }
  128. return rri;
  129. }
  130. [MonoTODO]
  131. public virtual void Unregister (UnregisterInfo unregisterInfo)
  132. {
  133. if (unregisterInfo == null)
  134. throw new ArgumentException ("Unregister info cannot be null.");
  135. if (! opened)
  136. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  137. if (ControlShape)
  138. {
  139. // FIXME: To remove node from mesh here.
  140. }
  141. }
  142. [MonoTODO]
  143. public virtual RegisterResponseInfo Update (UpdateInfo updateInfo)
  144. {
  145. if (updateInfo == null)
  146. throw new ArgumentException ("Update info cannot be null.");
  147. if (! opened)
  148. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  149. // return new RegisterResponseInfo ();
  150. throw new NotImplementedException ();
  151. }
  152. }
  153. internal class Node
  154. {
  155. }
  156. }