AsyncCommandManager.cs 12 KB

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