ScriptEngine.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  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 System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using log4net;
  31. using Nini.Config;
  32. using OpenSim.Region.Environment.Interfaces;
  33. using OpenSim.Region.Environment.Scenes;
  34. namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase
  35. {
  36. /// <summary>
  37. /// This is the root object for ScriptEngine. Objects access each other trough this class.
  38. /// </summary>
  39. ///
  40. [Serializable]
  41. public abstract class ScriptEngine : IRegionModule, ScriptServerInterfaces.ScriptEngine, iScriptEngineFunctionModule
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. public static List<ScriptEngine> ScriptEngines = new List<ScriptEngine>();
  45. public Scene World;
  46. public EventManager m_EventManager; // Handles and queues incoming events from OpenSim
  47. public EventQueueManager m_EventQueueManager; // Executes events, handles script threads
  48. public ScriptManager m_ScriptManager; // Load, unload and execute scripts
  49. public AppDomainManager m_AppDomainManager; // Handles loading/unloading of scripts into AppDomains
  50. public AsyncCommandManager m_ASYNCLSLCommandManager; // Asyncronous LSL commands (commands that returns with an event)
  51. public static MaintenanceThread m_MaintenanceThread; // Thread that does different kinds of maintenance, for example refreshing config and killing scripts that has been running too long
  52. public IConfigSource ConfigSource;
  53. public IConfig ScriptConfigSource;
  54. public abstract string ScriptEngineName { get; }
  55. /// <summary>
  56. /// How many seconds between re-reading config-file. 0 = never. ScriptEngine will try to adjust to new config changes.
  57. /// </summary>
  58. public int RefreshConfigFileSeconds {
  59. get { return (int)(RefreshConfigFilens / 10000000); }
  60. set { RefreshConfigFilens = value * 10000000; }
  61. }
  62. public long RefreshConfigFilens;
  63. public ScriptManager GetScriptManager()
  64. {
  65. return _GetScriptManager();
  66. }
  67. public abstract ScriptManager _GetScriptManager();
  68. public ILog Log
  69. {
  70. get { return m_log; }
  71. }
  72. public ScriptEngine()
  73. {
  74. Common.mySE = this; // For logging, just need any instance, doesn't matter
  75. lock (ScriptEngines)
  76. {
  77. ScriptEngines.Add(this); // Keep a list of ScriptEngines for shared threads to process all instances
  78. }
  79. }
  80. public void InitializeEngine(Scene Sceneworld, IConfigSource config, bool HookUpToServer, ScriptManager newScriptManager)
  81. {
  82. World = Sceneworld;
  83. ConfigSource = config;
  84. m_log.Info("[" + ScriptEngineName + "]: ScriptEngine initializing");
  85. // Make sure we have config
  86. if (ConfigSource.Configs[ScriptEngineName] == null)
  87. ConfigSource.AddConfig(ScriptEngineName);
  88. ScriptConfigSource = ConfigSource.Configs[ScriptEngineName];
  89. //m_log.Info("[" + ScriptEngineName + "]: InitializeEngine");
  90. // Create all objects we'll be using
  91. m_EventQueueManager = new EventQueueManager(this);
  92. m_EventManager = new EventManager(this, HookUpToServer);
  93. // We need to start it
  94. newScriptManager.Start();
  95. m_ScriptManager = newScriptManager;
  96. m_AppDomainManager = new AppDomainManager(this);
  97. m_ASYNCLSLCommandManager = new AsyncCommandManager(this);
  98. if (m_MaintenanceThread == null)
  99. m_MaintenanceThread = new MaintenanceThread();
  100. m_log.Info("[" + ScriptEngineName + "]: Reading configuration from config section \"" + ScriptEngineName + "\"");
  101. ReadConfig();
  102. // Should we iterate the region for scripts that needs starting?
  103. // Or can we assume we are loaded before anything else so we can use proper events?
  104. }
  105. public void Shutdown()
  106. {
  107. // We are shutting down
  108. lock (ScriptEngines)
  109. {
  110. ScriptEngines.Remove(this);
  111. }
  112. }
  113. ScriptServerInterfaces.RemoteEvents ScriptServerInterfaces.ScriptEngine.EventManager()
  114. {
  115. return this.m_EventManager;
  116. }
  117. public void ReadConfig()
  118. {
  119. #if DEBUG
  120. //m_log.Debug("[" + ScriptEngineName + "]: Refreshing configuration for all modules");
  121. #endif
  122. RefreshConfigFileSeconds = ScriptConfigSource.GetInt("RefreshConfig", 30);
  123. // Create a new object (probably not necessary?)
  124. // ScriptConfigSource = ConfigSource.Configs[ScriptEngineName];
  125. if (m_EventQueueManager != null) m_EventQueueManager.ReadConfig();
  126. if (m_EventManager != null) m_EventManager.ReadConfig();
  127. if (m_ScriptManager != null) m_ScriptManager.ReadConfig();
  128. if (m_AppDomainManager != null) m_AppDomainManager.ReadConfig();
  129. if (m_ASYNCLSLCommandManager != null) m_ASYNCLSLCommandManager.ReadConfig();
  130. if (m_MaintenanceThread != null) m_MaintenanceThread.ReadConfig();
  131. }
  132. #region IRegionModule
  133. public abstract void Initialise(Scene scene, IConfigSource config);
  134. public void PostInitialise()
  135. {
  136. }
  137. public void Close()
  138. {
  139. }
  140. public string Name
  141. {
  142. get { return "Common." + ScriptEngineName; }
  143. }
  144. public bool IsSharedModule
  145. {
  146. get { return false; }
  147. }
  148. #endregion
  149. }
  150. }