AsyncCommandManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 OpenSimulator 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.Framework.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 = Watchdog.StartThread(CmdHandlerThreadLoop, "AsyncLSLCmdHandlerThread", ThreadPriority.Normal, true);
  123. }
  124. }
  125. private void ReadConfig()
  126. {
  127. // cmdHandlerThreadCycleSleepms = m_ScriptEngine.Config.GetInt("AsyncLLCommandLoopms", 100);
  128. // TODO: Make this sane again
  129. cmdHandlerThreadCycleSleepms = 100;
  130. }
  131. ~AsyncCommandManager()
  132. {
  133. // Shut down thread
  134. // try
  135. // {
  136. // if (cmdHandlerThread != null)
  137. // {
  138. // if (cmdHandlerThread.IsAlive == true)
  139. // {
  140. // cmdHandlerThread.Abort();
  141. // //cmdHandlerThread.Join();
  142. // }
  143. // }
  144. // }
  145. // catch
  146. // {
  147. // }
  148. }
  149. /// <summary>
  150. /// Main loop for the manager thread
  151. /// </summary>
  152. private static void CmdHandlerThreadLoop()
  153. {
  154. while (true)
  155. {
  156. try
  157. {
  158. while (true)
  159. {
  160. Thread.Sleep(cmdHandlerThreadCycleSleepms);
  161. DoOneCmdHandlerPass();
  162. Watchdog.UpdateThread();
  163. }
  164. }
  165. catch
  166. {
  167. }
  168. }
  169. }
  170. private static void DoOneCmdHandlerPass()
  171. {
  172. // Check HttpRequests
  173. m_HttpRequest[m_ScriptEngines[0]].CheckHttpRequests();
  174. // Check XMLRPCRequests
  175. m_XmlRequest[m_ScriptEngines[0]].CheckXMLRPCRequests();
  176. foreach (IScriptEngine s in m_ScriptEngines)
  177. {
  178. // Check Listeners
  179. m_Listener[s].CheckListeners();
  180. // Check timers
  181. m_Timer[s].CheckTimerEvents();
  182. // Check Sensors
  183. m_SensorRepeat[s].CheckSenseRepeaterEvents();
  184. // Check dataserver
  185. m_Dataserver[s].ExpireRequests();
  186. }
  187. }
  188. /// <summary>
  189. /// Remove a specific script (and all its pending commands)
  190. /// </summary>
  191. /// <param name="localID"></param>
  192. /// <param name="itemID"></param>
  193. public static void RemoveScript(IScriptEngine engine, uint localID, UUID itemID)
  194. {
  195. // Remove a specific script
  196. // Remove dataserver events
  197. m_Dataserver[engine].RemoveEvents(localID, itemID);
  198. // Remove from: Timers
  199. m_Timer[engine].UnSetTimerEvents(localID, itemID);
  200. // Remove from: HttpRequest
  201. IHttpRequestModule iHttpReq =
  202. engine.World.RequestModuleInterface<IHttpRequestModule>();
  203. iHttpReq.StopHttpRequest(localID, itemID);
  204. IWorldComm comms = engine.World.RequestModuleInterface<IWorldComm>();
  205. if (comms != null)
  206. comms.DeleteListener(itemID);
  207. IXMLRPC xmlrpc = engine.World.RequestModuleInterface<IXMLRPC>();
  208. xmlrpc.DeleteChannels(itemID);
  209. xmlrpc.CancelSRDRequests(itemID);
  210. // Remove Sensors
  211. m_SensorRepeat[engine].UnSetSenseRepeaterEvents(localID, itemID);
  212. }
  213. public static Object[] GetSerializationData(IScriptEngine engine, UUID itemID)
  214. {
  215. List<Object> data = new List<Object>();
  216. Object[] listeners=m_Listener[engine].GetSerializationData(itemID);
  217. if (listeners.Length > 0)
  218. {
  219. data.Add("listener");
  220. data.Add(listeners.Length);
  221. data.AddRange(listeners);
  222. }
  223. Object[] timers=m_Timer[engine].GetSerializationData(itemID);
  224. if (timers.Length > 0)
  225. {
  226. data.Add("timer");
  227. data.Add(timers.Length);
  228. data.AddRange(timers);
  229. }
  230. Object[] sensors=m_SensorRepeat[engine].GetSerializationData(itemID);
  231. if (sensors.Length > 0)
  232. {
  233. data.Add("sensor");
  234. data.Add(sensors.Length);
  235. data.AddRange(sensors);
  236. }
  237. return data.ToArray();
  238. }
  239. public static void CreateFromData(IScriptEngine engine, uint localID,
  240. UUID itemID, UUID hostID, Object[] data)
  241. {
  242. int idx = 0;
  243. int len;
  244. while (idx < data.Length)
  245. {
  246. string type = data[idx].ToString();
  247. len = (int)data[idx+1];
  248. idx+=2;
  249. if (len > 0)
  250. {
  251. Object[] item = new Object[len];
  252. Array.Copy(data, idx, item, 0, len);
  253. idx+=len;
  254. switch (type)
  255. {
  256. case "listener":
  257. m_Listener[engine].CreateFromData(localID, itemID,
  258. hostID, item);
  259. break;
  260. case "timer":
  261. m_Timer[engine].CreateFromData(localID, itemID,
  262. hostID, item);
  263. break;
  264. case "sensor":
  265. m_SensorRepeat[engine].CreateFromData(localID,
  266. itemID, hostID, item);
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. #region Check llRemoteData channels
  273. #endregion
  274. #region Check llListeners
  275. #endregion
  276. /// <summary>
  277. /// If set to true then threads and stuff should try to make a graceful exit
  278. /// </summary>
  279. public bool PleaseShutdown
  280. {
  281. get { return _PleaseShutdown; }
  282. set { _PleaseShutdown = value; }
  283. }
  284. private bool _PleaseShutdown = false;
  285. }
  286. }