AsyncCommandManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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;
  29. using System.Collections.Generic;
  30. using System.Threading;
  31. using libsecondlife;
  32. using Axiom.Math;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Modules;
  35. using OpenSim.Region.Environment.Scenes;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.ScriptEngine.Common.ScriptEngineBase.AsyncCommandPlugins;
  38. using Timer=OpenSim.Region.ScriptEngine.Common.ScriptEngineBase.AsyncCommandPlugins.Timer;
  39. namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase
  40. {
  41. /// <summary>
  42. /// Handles LSL commands that takes long time and returns an event, for example timers, HTTP requests, etc.
  43. /// </summary>
  44. public class AsyncCommandManager : iScriptEngineFunctionModule
  45. {
  46. private static Thread cmdHandlerThread;
  47. private static int cmdHandlerThreadCycleSleepms;
  48. public ScriptEngine m_ScriptEngine;
  49. public AsyncCommandPlugins.Timer m_Timer;
  50. public AsyncCommandPlugins.HttpRequest m_HttpRequest;
  51. public AsyncCommandPlugins.Listener m_Listener;
  52. public AsyncCommandPlugins.SensorRepeat m_SensorRepeat;
  53. public AsyncCommandPlugins.XmlRequest m_XmlRequest;
  54. public AsyncCommandManager(ScriptEngine _ScriptEngine)
  55. {
  56. m_ScriptEngine = _ScriptEngine;
  57. ReadConfig();
  58. // Create instances of all plugins
  59. m_Timer = new Timer(this);
  60. m_HttpRequest = new HttpRequest(this);
  61. m_Listener = new Listener(this);
  62. m_SensorRepeat = new SensorRepeat(this);
  63. m_XmlRequest = new XmlRequest(this);
  64. StartThread();
  65. }
  66. private void StartThread()
  67. {
  68. if (cmdHandlerThread == null)
  69. {
  70. // Start the thread that will be doing the work
  71. cmdHandlerThread = new Thread(CmdHandlerThreadLoop);
  72. cmdHandlerThread.Name = "AsyncLSLCmdHandlerThread";
  73. cmdHandlerThread.Priority = ThreadPriority.BelowNormal;
  74. cmdHandlerThread.IsBackground = true;
  75. cmdHandlerThread.Start();
  76. OpenSim.Framework.ThreadTracker.Add(cmdHandlerThread);
  77. }
  78. }
  79. public void ReadConfig()
  80. {
  81. cmdHandlerThreadCycleSleepms = m_ScriptEngine.ScriptConfigSource.GetInt("AsyncLLCommandLoopms", 100);
  82. }
  83. ~AsyncCommandManager()
  84. {
  85. // Shut down thread
  86. try
  87. {
  88. if (cmdHandlerThread != null)
  89. {
  90. if (cmdHandlerThread.IsAlive == true)
  91. {
  92. cmdHandlerThread.Abort();
  93. //cmdHandlerThread.Join();
  94. }
  95. }
  96. }
  97. catch
  98. {
  99. }
  100. }
  101. private static void CmdHandlerThreadLoop()
  102. {
  103. while (true)
  104. {
  105. try
  106. {
  107. while (true)
  108. {
  109. Thread.Sleep(cmdHandlerThreadCycleSleepms);
  110. //lock (ScriptEngine.ScriptEngines)
  111. //{
  112. foreach (ScriptEngine se in new ArrayList(ScriptEngine.ScriptEngines))
  113. {
  114. se.m_ASYNCLSLCommandManager.DoOneCmdHandlerPass();
  115. }
  116. //}
  117. // Sleep before next cycle
  118. //Thread.Sleep(cmdHandlerThreadCycleSleepms);
  119. }
  120. }
  121. catch
  122. {
  123. }
  124. }
  125. }
  126. internal void DoOneCmdHandlerPass()
  127. {
  128. // Check timers
  129. m_Timer.CheckTimerEvents();
  130. // Check HttpRequests
  131. m_HttpRequest.CheckHttpRequests();
  132. // Check XMLRPCRequests
  133. m_XmlRequest.CheckXMLRPCRequests();
  134. // Check Listeners
  135. m_Listener.CheckListeners();
  136. // Check Sensors
  137. m_SensorRepeat.CheckSenseRepeaterEvents();
  138. }
  139. /// <summary>
  140. /// Remove a specific script (and all its pending commands)
  141. /// </summary>
  142. /// <param name="localID"></param>
  143. /// <param name="itemID"></param>
  144. public void RemoveScript(uint localID, LLUUID itemID)
  145. {
  146. // Remove a specific script
  147. // Remove from: Timers
  148. m_Timer.UnSetTimerEvents(localID, itemID);
  149. // Remove from: HttpRequest
  150. IHttpRequests iHttpReq =
  151. m_ScriptEngine.World.RequestModuleInterface<IHttpRequests>();
  152. iHttpReq.StopHttpRequest(localID, itemID);
  153. IWorldComm comms = m_ScriptEngine.World.RequestModuleInterface<IWorldComm>();
  154. comms.DeleteListener(itemID);
  155. IXMLRPC xmlrpc = m_ScriptEngine.World.RequestModuleInterface<IXMLRPC>();
  156. xmlrpc.DeleteChannels(itemID);
  157. xmlrpc.CancelSRDRequests(itemID);
  158. // Remove Sensors
  159. m_SensorRepeat.UnSetSenseRepeaterEvents(localID, itemID);
  160. }
  161. #region Check llRemoteData channels
  162. #endregion
  163. #region Check llListeners
  164. #endregion
  165. /// <summary>
  166. /// If set to true then threads and stuff should try to make a graceful exit
  167. /// </summary>
  168. public bool PleaseShutdown
  169. {
  170. get { return _PleaseShutdown; }
  171. set { _PleaseShutdown = value; }
  172. }
  173. private bool _PleaseShutdown = false;
  174. }
  175. }