AsyncCommandManager.cs 6.8 KB

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