2
0

LocalPeerResolverService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // LocalPeerResolverService.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if true
  29. using System;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Runtime.Serialization;
  34. using System.ServiceModel;
  35. using System.ServiceModel.PeerResolvers;
  36. namespace System.ServiceModel.PeerResolvers
  37. {
  38. // This implementation of peer resolver should open up node
  39. // registration to some extent, say, valid within the same machine.
  40. //
  41. // A correct implementation should be using something like zeroconf
  42. // to register the local machine as a peer node.
  43. class LocalPeerResolverService : ICustomPeerResolverContract
  44. {
  45. public LocalPeerResolverService (TextWriter log)
  46. {
  47. this.log = log ?? TextWriter.Null;
  48. }
  49. TextWriter log;
  50. Dictionary<string,Mesh> mesh_map = new Dictionary<string,Mesh> ();
  51. // CustomPeerResolverService delegation
  52. // Open(), Close(), ControlShape, RefreshInterval, CleanupInterval
  53. public bool ControlShape { get; set; }
  54. public TimeSpan RefreshInterval { get; set; }
  55. public TimeSpan CleanupInterval { get; set; }
  56. // (internal) ICustomPeerResolverContract implementation
  57. public PeerServiceSettingsInfo GetCustomServiceSettings ()
  58. {
  59. log.WriteLine ("REQUEST: GetCustomServiceSettings");
  60. return new PeerServiceSettingsInfo () {
  61. ControlMeshShape = this.ControlShape,
  62. RefreshInterval = this.RefreshInterval,
  63. CleanupInterval = this.CleanupInterval };
  64. }
  65. public void SetCustomServiceSettings (PeerServiceSettingsInfo info)
  66. {
  67. log.WriteLine ("REQUEST: SetCustomServiceSettings(ControlMeshShape:{0}, RefreshInterval:{1}, CleanupInterval:{2}", info.ControlMeshShape, info.RefreshInterval, info.CleanupInterval);
  68. ControlShape = info.ControlMeshShape;
  69. RefreshInterval = info.RefreshInterval;
  70. CleanupInterval = info.CleanupInterval;
  71. }
  72. // IPeerResolverContract implementation
  73. public ServiceSettingsResponseInfo GetServiceSettings ()
  74. {
  75. return new ServiceSettingsResponseInfo () { ControlMeshShape = this.ControlShape };
  76. }
  77. public RefreshResponseInfo Refresh (RefreshInfo refreshInfo)
  78. {
  79. var r = refreshInfo;
  80. log.WriteLine ("REQUEST: Refresh (Mesh: {0}, Registraion: {1})", r.MeshId, r.RegistrationId);
  81. var mesh = GetExistingMesh (r.MeshId);
  82. var node = mesh.FirstOrDefault (n => n.RegistrationId == r.RegistrationId);
  83. if (node == null)
  84. return new RefreshResponseInfo () { Result = RefreshResult.RegistrationNotFound };
  85. node.Refresh ();
  86. return new RefreshResponseInfo () { Result = RefreshResult.Success, RegistrationLifetime = RefreshInterval - (DateTime.Now - node.LastRefreshTime) };
  87. }
  88. public RegisterResponseInfo Register (RegisterInfo registerInfo)
  89. {
  90. var r = registerInfo;
  91. log.WriteLine ("REQUEST: Register (Mesh: {0}, Client: {1}, NodeAddress: endpoint {2})", r.MeshId, r.ClientId, r.NodeAddress.EndpointAddress);
  92. Mesh mesh;
  93. if (!mesh_map.TryGetValue (r.MeshId, out mesh)) {
  94. mesh = new Mesh (r.MeshId);
  95. mesh_map.Add (r.MeshId, mesh);
  96. }
  97. var node = RegisterNode (mesh, r.ClientId, r.NodeAddress);
  98. return new RegisterResponseInfo () { RegistrationId = node.RegistrationId };
  99. }
  100. public ResolveResponseInfo Resolve (ResolveInfo resolveInfo)
  101. {
  102. var r = resolveInfo;
  103. log.WriteLine ("REQUEST: Resolve (Mesh: {0}, Client: {1}, MaxAddresses: {2})", r.MeshId, r.ClientId, r.MaxAddresses);
  104. Mesh mesh;
  105. var rr = new ResolveResponseInfo ();
  106. if (!mesh_map.TryGetValue (r.MeshId, out mesh))
  107. return rr;
  108. // FIXME: find out how to use the argument ClientId.
  109. // So far, it is used to filter out the registered node from the same ClientId.
  110. foreach (var node in mesh.TakeWhile (n => n.ClientId != r.ClientId)) {
  111. rr.Addresses.Add (node.Address);
  112. if (rr.Addresses.Count == r.MaxAddresses)
  113. break;
  114. }
  115. return rr;
  116. }
  117. public void Unregister (UnregisterInfo unregisterInfo)
  118. {
  119. var u = unregisterInfo;
  120. log.WriteLine ("REQUEST: Unregister (Mesh: {0}, Registration: {1})", u.MeshId, u.RegistrationId);
  121. Mesh mesh = GetExistingMesh (u.MeshId);
  122. lock (mesh) {
  123. var node = mesh.GetRegisteredNode (u.RegistrationId);
  124. mesh.Remove (node);
  125. }
  126. }
  127. public RegisterResponseInfo Update (UpdateInfo updateInfo)
  128. {
  129. var u = updateInfo;
  130. log.WriteLine ("REQUEST: Update (Mesh: {0}, Registration: {1}, NodeAddress:)", u.MeshId, u.RegistrationId, u.NodeAddress);
  131. var mesh = GetExistingMesh (u.MeshId);
  132. var node = mesh.GetRegisteredNode (u.RegistrationId);
  133. node.Update (u.NodeAddress);
  134. return new RegisterResponseInfo () { RegistrationId = node.RegistrationId };
  135. }
  136. Mesh GetExistingMesh (string meshId)
  137. {
  138. Mesh mesh;
  139. if (!mesh_map.TryGetValue (meshId, out mesh))
  140. throw new InvalidOperationException (String.Format ("Specified mesh {0} does not exist", meshId));
  141. return mesh;
  142. }
  143. Node RegisterNode (Mesh mesh, Guid clientId, PeerNodeAddress addr)
  144. {
  145. lock (mesh) {
  146. var node = new Node () { ClientId = clientId, Address = addr };
  147. mesh.Add (node);
  148. node.LastRefreshTime = DateTime.Now;
  149. return node;
  150. }
  151. }
  152. }
  153. class Mesh : List<Node>
  154. {
  155. public Mesh (string id)
  156. {
  157. Id = id;
  158. }
  159. public string Id { get; private set; }
  160. public Node GetRegisteredNode (Guid registrationId)
  161. {
  162. var node = this.FirstOrDefault (n => n.RegistrationId == registrationId);
  163. if (node == null)
  164. throw new InvalidOperationException (String.Format ("Node with registration Id {0} does not exist in the specified mesh {0}", registrationId, Id));
  165. return node;
  166. }
  167. }
  168. class Node
  169. {
  170. public Node ()
  171. {
  172. RegistrationId = Guid.NewGuid ();
  173. }
  174. public Guid RegistrationId { get; private set; }
  175. public Guid ClientId { get; set; }
  176. public PeerNodeAddress Address { get; set; }
  177. public DateTime LastRefreshTime { get; set; }
  178. public void Refresh ()
  179. {
  180. LastRefreshTime = DateTime.Now;
  181. }
  182. public void Update (PeerNodeAddress addr)
  183. {
  184. Address = addr;
  185. LastRefreshTime = DateTime.Now;
  186. }
  187. }
  188. }
  189. #endif