CustomPeerResolverService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #if false
  40. [MonoTODO ("Implement cleanup and refresh")]
  41. // FIXME: TransactionTimeout must be null by-default.
  42. [ServiceBehavior (AutomaticSessionShutdown = true, ConcurrencyMode = ConcurrencyMode.Multiple,
  43. InstanceContextMode = InstanceContextMode.Single, ReleaseServiceInstanceOnTransactionComplete = true,
  44. TransactionIsolationLevel = IsolationLevel.Unspecified, /*TransactionTimeout = null, */
  45. UseSynchronizationContext = false, ValidateMustUnderstand = true)]
  46. public class CustomPeerResolverService : IPeerResolverContract
  47. {
  48. bool control_shape;
  49. bool opened;
  50. // Maybe it's worth to change List<T> for a better distributed and faster collection.
  51. List<Node> mesh = new List<Node> ();
  52. object mesh_lock = new object ();
  53. Timer refresh_timer, cleanup_timer;
  54. DateTime last_refresh_time = DateTime.Now;
  55. public CustomPeerResolverService ()
  56. {
  57. refresh_timer = new Timer () { AutoReset = true };
  58. RefreshInterval = new TimeSpan (0, 10, 0);
  59. refresh_timer.Elapsed += delegate {
  60. // FIXME: implement
  61. };
  62. cleanup_timer = new Timer () { AutoReset = true };
  63. CleanupInterval = new TimeSpan (0, 1, 0);
  64. cleanup_timer.Elapsed += delegate {
  65. // FIXME: implement
  66. };
  67. control_shape = false;
  68. opened = false;
  69. }
  70. public TimeSpan CleanupInterval {
  71. get { return TimeSpan.FromMilliseconds ((int) cleanup_timer.Interval); }
  72. set {
  73. if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
  74. throw new ArgumentOutOfRangeException (
  75. "The interval is either zero or greater than max value.");
  76. if (opened)
  77. throw new InvalidOperationException ("The interval must be set before it is opened");
  78. cleanup_timer.Interval = value.TotalMilliseconds;
  79. }
  80. }
  81. public bool ControlShape {
  82. get { return control_shape; }
  83. set { control_shape = value; }
  84. }
  85. public TimeSpan RefreshInterval {
  86. get { return TimeSpan.FromMilliseconds ((int) refresh_timer.Interval); }
  87. set {
  88. if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
  89. throw new ArgumentOutOfRangeException (
  90. "The interval is either zero or greater than max value.");
  91. if (opened)
  92. throw new InvalidOperationException ("The interval must be set before it is opened");
  93. refresh_timer.Interval = value.TotalMilliseconds;
  94. }
  95. }
  96. [MonoTODO ("Do we have to unregister nodes here?")]
  97. public virtual void Close ()
  98. {
  99. if (! opened)
  100. throw new InvalidOperationException ("The service has never been opened or it was closed by a previous call to this method.");
  101. refresh_timer.Stop ();
  102. cleanup_timer.Stop ();
  103. }
  104. [MonoTODO]
  105. public virtual ServiceSettingsResponseInfo GetServiceSettings ()
  106. {
  107. if (! opened)
  108. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  109. // return new ServiceSettingsResponseInfo ();
  110. throw new NotImplementedException ();
  111. }
  112. public virtual void Open ()
  113. {
  114. if ((CleanupInterval == TimeSpan.Zero) || (RefreshInterval == TimeSpan.Zero))
  115. throw new ArgumentException ("Cleanup interval or refresh interval are set to a time span interval of zero.");
  116. if (opened)
  117. throw new InvalidOperationException ("The service has been started by a previous call to this method.");
  118. opened = true;
  119. refresh_timer.Start ();
  120. cleanup_timer.Start ();
  121. }
  122. [MonoTODO]
  123. public virtual RefreshResponseInfo Refresh (RefreshInfo refreshInfo)
  124. {
  125. if (refreshInfo == null)
  126. throw new ArgumentException ("Refresh info cannot be null.");
  127. if (! opened)
  128. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  129. var node = mesh.FirstOrDefault (n => n.MeshId == refreshInfo.MeshId && n.RegistrationId.Equals (refreshInfo.RegistrationId));
  130. if (node == null)
  131. return new RefreshResponseInfo (TimeSpan.Zero, RefreshResult.RegistrationNotFound);
  132. // FIXME: implement actual refresh.
  133. last_refresh_time = DateTime.Now;
  134. return new RefreshResponseInfo (RefreshInterval - (DateTime.Now - last_refresh_time), RefreshResult.Success);
  135. }
  136. public virtual RegisterResponseInfo Register (RegisterInfo registerInfo)
  137. {
  138. if (registerInfo == null)
  139. throw new ArgumentException ("Register info cannot be null.");
  140. if (! opened)
  141. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  142. return Register (registerInfo.ClientId, registerInfo.MeshId, registerInfo.NodeAddress);
  143. }
  144. public virtual RegisterResponseInfo Register (Guid clientId, string meshId, PeerNodeAddress address)
  145. {
  146. Node n = new Node () { RegistrationId = Guid.NewGuid (), MeshId = meshId, ClientId = clientId, NodeAddress = address };
  147. RegisterResponseInfo rri = new RegisterResponseInfo ();
  148. rri.RegistrationId = n.RegistrationId;
  149. lock (mesh_lock)
  150. mesh.Add (n);
  151. return rri;
  152. }
  153. public virtual ResolveResponseInfo Resolve (ResolveInfo resolveInfo)
  154. {
  155. ResolveResponseInfo rri = new ResolveResponseInfo ();
  156. if (resolveInfo == null)
  157. throw new ArgumentException ("Resolve info cannot be null.");
  158. if (! opened)
  159. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  160. foreach (var node in mesh)
  161. if (node.MeshId == resolveInfo.MeshId &&
  162. node.ClientId == resolveInfo.ClientId)
  163. rri.Addresses.Add (node.NodeAddress);
  164. return rri;
  165. }
  166. public virtual void Unregister (UnregisterInfo unregisterInfo)
  167. {
  168. if (unregisterInfo == null)
  169. throw new ArgumentException ("Unregister info cannot be null.");
  170. if (! opened)
  171. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  172. lock (mesh_lock)
  173. foreach (var node in mesh)
  174. if (node.MeshId == unregisterInfo.MeshId &&
  175. node.RegistrationId == unregisterInfo.RegistrationId) {
  176. mesh.Remove (node);
  177. break;
  178. }
  179. }
  180. [MonoTODO]
  181. public virtual RegisterResponseInfo Update (UpdateInfo updateInfo)
  182. {
  183. if (updateInfo == null)
  184. throw new ArgumentException ("Update info cannot be null.");
  185. if (! opened)
  186. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  187. // return new RegisterResponseInfo ();
  188. throw new NotImplementedException ();
  189. }
  190. }
  191. internal class Node
  192. {
  193. public Guid ClientId { get; set; }
  194. public string MeshId { get; set; }
  195. public Guid RegistrationId { get; set; }
  196. public PeerNodeAddress NodeAddress { get; set; }
  197. }
  198. #else
  199. [MonoTODO ("Implement cleanup and refresh")]
  200. // FIXME: TransactionTimeout must be null by-default.
  201. [ServiceBehavior (AutomaticSessionShutdown = true, ConcurrencyMode = ConcurrencyMode.Multiple,
  202. InstanceContextMode = InstanceContextMode.Single, ReleaseServiceInstanceOnTransactionComplete = true,
  203. TransactionIsolationLevel = IsolationLevel.Unspecified, /*TransactionTimeout = null, */
  204. UseSynchronizationContext = false, ValidateMustUnderstand = true)]
  205. public class CustomPeerResolverService : IPeerResolverContract
  206. {
  207. static ServiceHost localhost;
  208. static void SetupCustomPeerResolverServiceHost ()
  209. {
  210. // launch peer resolver service locally only when it does not seem to be running ...
  211. var t = new TcpListener (8931);
  212. try {
  213. t.Start ();
  214. t.Stop ();
  215. } catch {
  216. return;
  217. }
  218. Console.WriteLine ("WARNING: it is running peer resolver service locally. This means, the node registration is valid only within this application domain...");
  219. var host = new ServiceHost (new LocalPeerResolverService (TextWriter.Null));
  220. host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().InstanceContextMode = InstanceContextMode.Single;
  221. host.AddServiceEndpoint (typeof (ICustomPeerResolverContract), new BasicHttpBinding (), "http://localhost:8931");
  222. localhost = host;
  223. host.Open ();
  224. }
  225. ICustomPeerResolverClient client;
  226. bool control_shape, opened;
  227. TimeSpan refresh_interval, cleanup_interval;
  228. public CustomPeerResolverService ()
  229. {
  230. client = ChannelFactory<ICustomPeerResolverClient>.CreateChannel (new BasicHttpBinding (), new EndpointAddress ("http://localhost:8931"));
  231. refresh_interval = new TimeSpan (0, 10, 0);
  232. cleanup_interval = new TimeSpan (0, 1, 0);
  233. }
  234. public TimeSpan CleanupInterval {
  235. get { return cleanup_interval; }
  236. set {
  237. if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
  238. throw new ArgumentOutOfRangeException (
  239. "The interval is either zero or greater than max value.");
  240. if (opened)
  241. throw new InvalidOperationException ("The interval must be set before it is opened");
  242. cleanup_interval = value;
  243. }
  244. }
  245. public bool ControlShape {
  246. get { return control_shape; }
  247. set {
  248. if (opened)
  249. throw new InvalidOperationException ("The interval must be set before it is opened");
  250. control_shape = value;
  251. }
  252. }
  253. public TimeSpan RefreshInterval {
  254. get { return refresh_interval; }
  255. set {
  256. if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
  257. throw new ArgumentOutOfRangeException (
  258. "The interval is either zero or greater than max value.");
  259. if (opened)
  260. throw new InvalidOperationException ("The interval must be set before it is opened");
  261. refresh_interval = value;
  262. }
  263. }
  264. [MonoTODO ("Do we have to unregister nodes here?")]
  265. public virtual void Close ()
  266. {
  267. if (! opened)
  268. throw new InvalidOperationException ("The service has never been opened or it was closed by a previous call to this method.");
  269. client.Close ();
  270. opened = false;
  271. if (localhost != null) {
  272. localhost.Close ();
  273. localhost = null;
  274. }
  275. }
  276. public virtual ServiceSettingsResponseInfo GetServiceSettings ()
  277. {
  278. if (! opened)
  279. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  280. return client.GetServiceSettings ();
  281. }
  282. public virtual void Open ()
  283. {
  284. if (localhost == null)
  285. SetupCustomPeerResolverServiceHost ();
  286. if ((CleanupInterval == TimeSpan.Zero) || (RefreshInterval == TimeSpan.Zero))
  287. throw new ArgumentException ("Cleanup interval or refresh interval are set to a time span interval of zero.");
  288. if (opened)
  289. throw new InvalidOperationException ("The service has been started by a previous call to this method.");
  290. opened = true;
  291. client.Open ();
  292. client.SetCustomServiceSettings (new PeerServiceSettingsInfo () { ControlMeshShape = control_shape, RefreshInterval = refresh_interval, CleanupInterval = cleanup_interval });
  293. }
  294. public virtual RefreshResponseInfo Refresh (RefreshInfo refreshInfo)
  295. {
  296. if (refreshInfo == null)
  297. throw new ArgumentException ("Refresh info cannot be null.");
  298. if (! opened)
  299. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  300. return client.Refresh (refreshInfo);
  301. }
  302. public virtual RegisterResponseInfo Register (RegisterInfo registerInfo)
  303. {
  304. if (registerInfo == null)
  305. throw new ArgumentException ("Register info cannot be null.");
  306. if (! opened)
  307. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  308. return client.Register (registerInfo);
  309. }
  310. public virtual RegisterResponseInfo Register (Guid clientId, string meshId, PeerNodeAddress address)
  311. {
  312. return Register (new RegisterInfo (clientId, meshId, address));
  313. }
  314. public virtual ResolveResponseInfo Resolve (ResolveInfo resolveInfo)
  315. {
  316. if (resolveInfo == null)
  317. throw new ArgumentException ("Resolve info cannot be null.");
  318. if (! opened)
  319. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  320. return client.Resolve (resolveInfo);
  321. }
  322. public virtual void Unregister (UnregisterInfo unregisterInfo)
  323. {
  324. if (unregisterInfo == null)
  325. throw new ArgumentException ("Unregister info cannot be null.");
  326. if (! opened)
  327. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  328. client.Unregister (unregisterInfo);
  329. }
  330. public virtual RegisterResponseInfo Update (UpdateInfo updateInfo)
  331. {
  332. if (updateInfo == null)
  333. throw new ArgumentException ("Update info cannot be null.");
  334. if (! opened)
  335. throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
  336. return client.Update (updateInfo);
  337. }
  338. }
  339. #endif
  340. }