ServiceManager.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #region MIT License
  2. /**
  3. * ServiceManager.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2012 sta.blockhead
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #endregion
  28. using System;
  29. using System.Collections.Generic;
  30. namespace WebSocketSharp.Server
  31. {
  32. public class ServiceManager
  33. {
  34. #region Field
  35. private Dictionary<string, IServiceHost> _services;
  36. private bool _sweeped;
  37. #endregion
  38. #region Constructor
  39. public ServiceManager()
  40. {
  41. _services = new Dictionary<string, IServiceHost>();
  42. _sweeped = true;
  43. }
  44. #endregion
  45. #region Properties
  46. public int Count
  47. {
  48. get
  49. {
  50. return _services.Count;
  51. }
  52. }
  53. public bool Sweeped
  54. {
  55. get
  56. {
  57. return _sweeped;
  58. }
  59. set
  60. {
  61. if (_sweeped ^ value)
  62. {
  63. _sweeped = value;
  64. foreach (var svcHost in _services.Values)
  65. svcHost.Sweeped = value;
  66. }
  67. }
  68. }
  69. public IEnumerable<string> Path
  70. {
  71. get
  72. {
  73. return _services.Keys;
  74. }
  75. }
  76. public IEnumerable<IServiceHost> ServiceHost
  77. {
  78. get
  79. {
  80. return _services.Values;
  81. }
  82. }
  83. #endregion
  84. #region Public Methods
  85. public void Add(string absPath, IServiceHost svcHost)
  86. {
  87. _services.Add(Ext.UrlDecode(absPath), svcHost);
  88. }
  89. public void Broadcast(string data)
  90. {
  91. foreach (var svcHost in _services.Values)
  92. svcHost.Broadcast(data);
  93. }
  94. public void Stop()
  95. {
  96. foreach (var svcHost in _services.Values)
  97. svcHost.Stop();
  98. _services.Clear();
  99. }
  100. public bool TryGetServiceHost(string absPath, out IServiceHost svcHost)
  101. {
  102. return _services.TryGetValue(absPath, out svcHost);
  103. }
  104. #endregion
  105. }
  106. }