AsyncCommandManager.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 OpenSim.Framework;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.ScriptEngine.XEngine.AsyncCommandPlugins;
  35. using Timer=OpenSim.Region.ScriptEngine.XEngine.AsyncCommandPlugins.Timer;
  36. using Dataserver=OpenSim.Region.ScriptEngine.XEngine.AsyncCommandPlugins.Dataserver;
  37. namespace OpenSim.Region.ScriptEngine.XEngine
  38. {
  39. /// <summary>
  40. /// Handles LSL commands that takes long time and returns an event, for example timers, HTTP requests, etc.
  41. /// </summary>
  42. public class AsyncCommandManager
  43. {
  44. private static Thread cmdHandlerThread;
  45. private static int cmdHandlerThreadCycleSleepms;
  46. public XEngine m_ScriptEngine;
  47. public Dataserver m_Dataserver;
  48. public Timer m_Timer;
  49. public HttpRequest m_HttpRequest;
  50. public Listener m_Listener;
  51. public SensorRepeat m_SensorRepeat;
  52. public XmlRequest m_XmlRequest;
  53. public AsyncCommandManager(XEngine _ScriptEngine)
  54. {
  55. m_ScriptEngine = _ScriptEngine;
  56. ReadConfig();
  57. // Create instances of all plugins
  58. m_Dataserver = new Dataserver(this);
  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 static 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. 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. foreach (XEngine xe in XEngine.ScriptEngines)
  111. {
  112. xe.m_ASYNCLSLCommandManager.DoOneCmdHandlerPass();
  113. }
  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. // Check dataserver
  134. m_Dataserver.ExpireRequests();
  135. }
  136. /// <summary>
  137. /// Remove a specific script (and all its pending commands)
  138. /// </summary>
  139. /// <param name="localID"></param>
  140. /// <param name="itemID"></param>
  141. public void RemoveScript(uint localID, LLUUID itemID)
  142. {
  143. // Remove a specific script
  144. // Remove dataserver events
  145. m_Dataserver.RemoveEvents(localID, itemID);
  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. }
  160. public Object[] GetSerializationData(LLUUID itemID)
  161. {
  162. List<Object> data = new List<Object>();
  163. Object[] listeners=m_Listener.GetSerializationData(itemID);
  164. if(listeners.Length > 0)
  165. {
  166. data.Add("listener");
  167. data.Add(listeners.Length);
  168. data.AddRange(listeners);
  169. }
  170. Object[] timers=m_Timer.GetSerializationData(itemID);
  171. if(timers.Length > 0)
  172. {
  173. data.Add("timer");
  174. data.Add(timers.Length);
  175. data.AddRange(timers);
  176. }
  177. Object[] sensors=m_SensorRepeat.GetSerializationData(itemID);
  178. if(sensors.Length > 0)
  179. {
  180. data.Add("sensor");
  181. data.Add(sensors.Length);
  182. data.AddRange(sensors);
  183. }
  184. return data.ToArray();
  185. }
  186. public void CreateFromData(uint localID, LLUUID itemID, LLUUID hostID,
  187. Object[] data)
  188. {
  189. int idx=0;
  190. int len;
  191. while(idx < data.Length)
  192. {
  193. string type = data[idx].ToString();
  194. len = (int)data[idx+1];
  195. idx+=2;
  196. if(len > 0)
  197. {
  198. Object[] item = new Object[len];
  199. Array.Copy(data, idx, item, 0, len);
  200. idx+=len;
  201. switch(type)
  202. {
  203. case "listener":
  204. m_Listener.CreateFromData(localID, itemID, hostID,
  205. item);
  206. break;
  207. case "timer":
  208. m_Timer.CreateFromData(localID, itemID, hostID, item);
  209. break;
  210. case "sensor":
  211. m_SensorRepeat.CreateFromData(localID, itemID, hostID,
  212. item);
  213. break;
  214. }
  215. }
  216. }
  217. }
  218. #region Check llRemoteData channels
  219. #endregion
  220. #region Check llListeners
  221. #endregion
  222. /// <summary>
  223. /// If set to true then threads and stuff should try to make a graceful exit
  224. /// </summary>
  225. public bool PleaseShutdown
  226. {
  227. get { return _PleaseShutdown; }
  228. set { _PleaseShutdown = value; }
  229. }
  230. private bool _PleaseShutdown = false;
  231. }
  232. }