AsyncCommandManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.Interfaces;
  35. using OpenSim.Region.ScriptEngine.Shared;
  36. using OpenSim.Region.ScriptEngine.Shared.Api.Plugins;
  37. using Timer=OpenSim.Region.ScriptEngine.Shared.Api.Plugins.Timer;
  38. namespace OpenSim.Region.ScriptEngine.Shared.Api
  39. {
  40. /// <summary>
  41. /// Handles LSL commands that takes long time and returns an event, for example timers, HTTP requests, etc.
  42. /// </summary>
  43. public class AsyncCommandManager
  44. {
  45. private static Thread cmdHandlerThread;
  46. private static int cmdHandlerThreadCycleSleepms;
  47. private static List<AsyncCommandManager> m_Managers = new List<AsyncCommandManager>();
  48. public IScriptEngine m_ScriptEngine;
  49. private Dataserver m_Dataserver;
  50. private Timer m_Timer;
  51. private HttpRequest m_HttpRequest;
  52. private Listener m_Listener;
  53. private SensorRepeat m_SensorRepeat;
  54. private XmlRequest m_XmlRequest;
  55. public Dataserver DataserverPlugin
  56. {
  57. get { return m_Dataserver; }
  58. }
  59. public Timer TimerPlugin
  60. {
  61. get { return m_Timer; }
  62. }
  63. public HttpRequest HttpRequestPlugin
  64. {
  65. get { return m_HttpRequest; }
  66. }
  67. public Listener ListenerPlugin
  68. {
  69. get { return m_Listener; }
  70. }
  71. public SensorRepeat SensorRepeatPlugin
  72. {
  73. get { return m_SensorRepeat; }
  74. }
  75. public XmlRequest XmlRequestPlugin
  76. {
  77. get { return m_XmlRequest; }
  78. }
  79. public AsyncCommandManager[] Managers
  80. {
  81. get { return m_Managers.ToArray(); }
  82. }
  83. public AsyncCommandManager(IScriptEngine _ScriptEngine)
  84. {
  85. m_ScriptEngine = _ScriptEngine;
  86. if (!m_Managers.Contains(this))
  87. m_Managers.Add(this);
  88. ReadConfig();
  89. // Create instances of all plugins
  90. m_Dataserver = new Dataserver(this);
  91. m_Timer = new Timer(this);
  92. m_HttpRequest = new HttpRequest(this);
  93. m_Listener = new Listener(this);
  94. m_SensorRepeat = new SensorRepeat(this);
  95. m_XmlRequest = new XmlRequest(this);
  96. StartThread();
  97. }
  98. private static void StartThread()
  99. {
  100. if (cmdHandlerThread == null)
  101. {
  102. // Start the thread that will be doing the work
  103. cmdHandlerThread = new Thread(CmdHandlerThreadLoop);
  104. cmdHandlerThread.Name = "AsyncLSLCmdHandlerThread";
  105. cmdHandlerThread.Priority = ThreadPriority.BelowNormal;
  106. cmdHandlerThread.IsBackground = true;
  107. cmdHandlerThread.Start();
  108. ThreadTracker.Add(cmdHandlerThread);
  109. }
  110. }
  111. public void ReadConfig()
  112. {
  113. cmdHandlerThreadCycleSleepms = m_ScriptEngine.Config.GetInt("AsyncLLCommandLoopms", 100);
  114. }
  115. ~AsyncCommandManager()
  116. {
  117. // Shut down thread
  118. try
  119. {
  120. if (cmdHandlerThread != null)
  121. {
  122. if (cmdHandlerThread.IsAlive == true)
  123. {
  124. cmdHandlerThread.Abort();
  125. //cmdHandlerThread.Join();
  126. }
  127. }
  128. }
  129. catch
  130. {
  131. }
  132. }
  133. private static void CmdHandlerThreadLoop()
  134. {
  135. while (true)
  136. {
  137. try
  138. {
  139. while (true)
  140. {
  141. Thread.Sleep(cmdHandlerThreadCycleSleepms);
  142. foreach (AsyncCommandManager m in m_Managers)
  143. {
  144. m.DoOneCmdHandlerPass();
  145. }
  146. }
  147. }
  148. catch
  149. {
  150. }
  151. }
  152. }
  153. public void DoOneCmdHandlerPass()
  154. {
  155. // Check timers
  156. m_Timer.CheckTimerEvents();
  157. // Check HttpRequests
  158. m_HttpRequest.CheckHttpRequests();
  159. // Check XMLRPCRequests
  160. m_XmlRequest.CheckXMLRPCRequests();
  161. // Check Listeners
  162. m_Listener.CheckListeners();
  163. // Check Sensors
  164. m_SensorRepeat.CheckSenseRepeaterEvents();
  165. // Check dataserver
  166. m_Dataserver.ExpireRequests();
  167. }
  168. /// <summary>
  169. /// Remove a specific script (and all its pending commands)
  170. /// </summary>
  171. /// <param name="localID"></param>
  172. /// <param name="itemID"></param>
  173. public void RemoveScript(uint localID, LLUUID itemID)
  174. {
  175. // Remove a specific script
  176. // Remove dataserver events
  177. m_Dataserver.RemoveEvents(localID, itemID);
  178. // Remove from: Timers
  179. m_Timer.UnSetTimerEvents(localID, itemID);
  180. // Remove from: HttpRequest
  181. IHttpRequests iHttpReq =
  182. m_ScriptEngine.World.RequestModuleInterface<IHttpRequests>();
  183. iHttpReq.StopHttpRequest(localID, itemID);
  184. IWorldComm comms = m_ScriptEngine.World.RequestModuleInterface<IWorldComm>();
  185. comms.DeleteListener(itemID);
  186. IXMLRPC xmlrpc = m_ScriptEngine.World.RequestModuleInterface<IXMLRPC>();
  187. xmlrpc.DeleteChannels(itemID);
  188. xmlrpc.CancelSRDRequests(itemID);
  189. // Remove Sensors
  190. m_SensorRepeat.UnSetSenseRepeaterEvents(localID, itemID);
  191. }
  192. public Object[] GetSerializationData(LLUUID itemID)
  193. {
  194. List<Object> data = new List<Object>();
  195. Object[] listeners=m_Listener.GetSerializationData(itemID);
  196. if (listeners.Length > 0)
  197. {
  198. data.Add("listener");
  199. data.Add(listeners.Length);
  200. data.AddRange(listeners);
  201. }
  202. Object[] timers=m_Timer.GetSerializationData(itemID);
  203. if (timers.Length > 0)
  204. {
  205. data.Add("timer");
  206. data.Add(timers.Length);
  207. data.AddRange(timers);
  208. }
  209. Object[] sensors=m_SensorRepeat.GetSerializationData(itemID);
  210. if (sensors.Length > 0)
  211. {
  212. data.Add("sensor");
  213. data.Add(sensors.Length);
  214. data.AddRange(sensors);
  215. }
  216. return data.ToArray();
  217. }
  218. public void CreateFromData(uint localID, LLUUID itemID, LLUUID hostID,
  219. Object[] data)
  220. {
  221. int idx = 0;
  222. int len;
  223. while (idx < data.Length)
  224. {
  225. string type = data[idx].ToString();
  226. len = (int)data[idx+1];
  227. idx+=2;
  228. if (len > 0)
  229. {
  230. Object[] item = new Object[len];
  231. Array.Copy(data, idx, item, 0, len);
  232. idx+=len;
  233. switch (type)
  234. {
  235. case "listener":
  236. m_Listener.CreateFromData(localID, itemID, hostID,
  237. item);
  238. break;
  239. case "timer":
  240. m_Timer.CreateFromData(localID, itemID, hostID, item);
  241. break;
  242. case "sensor":
  243. m_SensorRepeat.CreateFromData(localID, itemID, hostID,
  244. item);
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. #region Check llRemoteData channels
  251. #endregion
  252. #region Check llListeners
  253. #endregion
  254. /// <summary>
  255. /// If set to true then threads and stuff should try to make a graceful exit
  256. /// </summary>
  257. public bool PleaseShutdown
  258. {
  259. get { return _PleaseShutdown; }
  260. set { _PleaseShutdown = value; }
  261. }
  262. private bool _PleaseShutdown = false;
  263. }
  264. }