AsyncCommandManager.cs 7.1 KB

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