JsonStoreCommands.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) Contributors
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using Mono.Addins;
  28. using System;
  29. using System.Reflection;
  30. using System.Threading;
  31. using System.Text;
  32. using System.Net;
  33. using System.Net.Sockets;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using System.Collections.Generic;
  42. using System.Text.RegularExpressions;
  43. namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
  44. {
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "JsonStoreCommandsModule")]
  46. public class JsonStoreCommandsModule : INonSharedRegionModule
  47. {
  48. private static readonly ILog m_log =
  49. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private IConfig m_config = null;
  51. private bool m_enabled = false;
  52. private Scene m_scene = null;
  53. //private IJsonStoreModule m_store;
  54. private JsonStoreModule m_store;
  55. #region Region Module interface
  56. // -----------------------------------------------------------------
  57. /// <summary>
  58. /// Name of this shared module is it's class name
  59. /// </summary>
  60. // -----------------------------------------------------------------
  61. public string Name
  62. {
  63. get { return this.GetType().Name; }
  64. }
  65. // -----------------------------------------------------------------
  66. /// <summary>
  67. /// Initialise this shared module
  68. /// </summary>
  69. /// <param name="scene">this region is getting initialised</param>
  70. /// <param name="source">nini config, we are not using this</param>
  71. // -----------------------------------------------------------------
  72. public void Initialise(IConfigSource config)
  73. {
  74. try
  75. {
  76. if ((m_config = config.Configs["JsonStore"]) == null)
  77. {
  78. // There is no configuration, the module is disabled
  79. // m_log.InfoFormat("[JsonStore] no configuration info");
  80. return;
  81. }
  82. m_enabled = m_config.GetBoolean("Enabled", m_enabled);
  83. }
  84. catch (Exception e)
  85. {
  86. m_log.Error("[JsonStore]: initialization error: {0}", e);
  87. return;
  88. }
  89. if (m_enabled)
  90. m_log.DebugFormat("[JsonStore]: module is enabled");
  91. }
  92. // -----------------------------------------------------------------
  93. /// <summary>
  94. /// everything is loaded, perform post load configuration
  95. /// </summary>
  96. // -----------------------------------------------------------------
  97. public void PostInitialise()
  98. {
  99. }
  100. // -----------------------------------------------------------------
  101. /// <summary>
  102. /// Nothing to do on close
  103. /// </summary>
  104. // -----------------------------------------------------------------
  105. public void Close()
  106. {
  107. }
  108. // -----------------------------------------------------------------
  109. /// <summary>
  110. /// </summary>
  111. // -----------------------------------------------------------------
  112. public void AddRegion(Scene scene)
  113. {
  114. if (m_enabled)
  115. {
  116. m_scene = scene;
  117. }
  118. }
  119. // -----------------------------------------------------------------
  120. /// <summary>
  121. /// </summary>
  122. // -----------------------------------------------------------------
  123. public void RemoveRegion(Scene scene)
  124. {
  125. // need to remove all references to the scene in the subscription
  126. // list to enable full garbage collection of the scene object
  127. }
  128. // -----------------------------------------------------------------
  129. /// <summary>
  130. /// Called when all modules have been added for a region. This is
  131. /// where we hook up events
  132. /// </summary>
  133. // -----------------------------------------------------------------
  134. public void RegionLoaded(Scene scene)
  135. {
  136. if (m_enabled)
  137. {
  138. m_scene = scene;
  139. m_store = (JsonStoreModule) m_scene.RequestModuleInterface<IJsonStoreModule>();
  140. if (m_store == null)
  141. {
  142. m_log.ErrorFormat("[JsonStoreCommands]: JsonModule interface not defined");
  143. m_enabled = false;
  144. return;
  145. }
  146. scene.AddCommand("JsonStore", this, "jsonstore stats", "jsonstore stats",
  147. "Display statistics about the state of the JsonStore module", "",
  148. CmdStats);
  149. }
  150. }
  151. /// -----------------------------------------------------------------
  152. /// <summary>
  153. /// </summary>
  154. // -----------------------------------------------------------------
  155. public Type ReplaceableInterface
  156. {
  157. get { return null; }
  158. }
  159. #endregion
  160. #region Commands
  161. private void CmdStats(string module, string[] cmd)
  162. {
  163. if (MainConsole.Instance.ConsoleScene != m_scene && MainConsole.Instance.ConsoleScene != null)
  164. return;
  165. JsonStoreStats stats = m_store.GetStoreStats();
  166. MainConsole.Instance.OutputFormat("{0}\t{1}",m_scene.RegionInfo.RegionName,stats.StoreCount);
  167. }
  168. #endregion
  169. }
  170. }