AsyncCommandManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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.Reflection;
  31. using System.Threading;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Monitoring;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.ScriptEngine.Interfaces;
  38. using OpenSim.Region.ScriptEngine.Shared;
  39. using OpenSim.Region.ScriptEngine.Shared.Api.Plugins;
  40. using Timer=OpenSim.Region.ScriptEngine.Shared.Api.Plugins.Timer;
  41. namespace OpenSim.Region.ScriptEngine.Shared.Api
  42. {
  43. /// <summary>
  44. /// Handles LSL commands that takes long time and returns an event, for example timers, HTTP requests, etc.
  45. /// </summary>
  46. public class AsyncCommandManager
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private static Thread cmdHandlerThread;
  50. private static int cmdHandlerThreadCycleSleepms;
  51. /// <summary>
  52. /// Lock for reading/writing static components of AsyncCommandManager.
  53. /// </summary>
  54. /// <remarks>
  55. /// This lock exists so that multiple threads from different engines and/or different copies of the same engine
  56. /// are prevented from running non-thread safe code (e.g. read/write of lists) concurrently.
  57. /// </remarks>
  58. private static object staticLock = new object();
  59. private static List<IScriptEngine> m_ScriptEngines =
  60. new List<IScriptEngine>();
  61. public IScriptEngine m_ScriptEngine;
  62. private static Dictionary<IScriptEngine, Dataserver> m_Dataserver =
  63. new Dictionary<IScriptEngine, Dataserver>();
  64. private static Dictionary<IScriptEngine, Timer> m_Timer =
  65. new Dictionary<IScriptEngine, Timer>();
  66. private static Dictionary<IScriptEngine, Listener> m_Listener =
  67. new Dictionary<IScriptEngine, Listener>();
  68. private static Dictionary<IScriptEngine, HttpRequest> m_HttpRequest =
  69. new Dictionary<IScriptEngine, HttpRequest>();
  70. private static Dictionary<IScriptEngine, SensorRepeat> m_SensorRepeat =
  71. new Dictionary<IScriptEngine, SensorRepeat>();
  72. private static Dictionary<IScriptEngine, XmlRequest> m_XmlRequest =
  73. new Dictionary<IScriptEngine, XmlRequest>();
  74. public Dataserver DataserverPlugin
  75. {
  76. get
  77. {
  78. lock (staticLock)
  79. return m_Dataserver[m_ScriptEngine];
  80. }
  81. }
  82. public Timer TimerPlugin
  83. {
  84. get
  85. {
  86. lock (staticLock)
  87. return m_Timer[m_ScriptEngine];
  88. }
  89. }
  90. public HttpRequest HttpRequestPlugin
  91. {
  92. get
  93. {
  94. lock (staticLock)
  95. return m_HttpRequest[m_ScriptEngine];
  96. }
  97. }
  98. public Listener ListenerPlugin
  99. {
  100. get
  101. {
  102. lock (staticLock)
  103. return m_Listener[m_ScriptEngine];
  104. }
  105. }
  106. public SensorRepeat SensorRepeatPlugin
  107. {
  108. get
  109. {
  110. lock (staticLock)
  111. return m_SensorRepeat[m_ScriptEngine];
  112. }
  113. }
  114. public XmlRequest XmlRequestPlugin
  115. {
  116. get
  117. {
  118. lock (staticLock)
  119. return m_XmlRequest[m_ScriptEngine];
  120. }
  121. }
  122. public IScriptEngine[] ScriptEngines
  123. {
  124. get
  125. {
  126. lock (staticLock)
  127. return m_ScriptEngines.ToArray();
  128. }
  129. }
  130. public AsyncCommandManager(IScriptEngine _ScriptEngine)
  131. {
  132. m_ScriptEngine = _ScriptEngine;
  133. // If there is more than one scene in the simulator or multiple script engines are used on the same region
  134. // then more than one thread could arrive at this block of code simultaneously. However, it cannot be
  135. // executed concurrently both because concurrent list operations are not thread-safe and because of other
  136. // race conditions such as the later check of cmdHandlerThread == null.
  137. lock (staticLock)
  138. {
  139. if (m_ScriptEngines.Count == 0)
  140. ReadConfig();
  141. if (!m_ScriptEngines.Contains(m_ScriptEngine))
  142. m_ScriptEngines.Add(m_ScriptEngine);
  143. // Create instances of all plugins
  144. if (!m_Dataserver.ContainsKey(m_ScriptEngine))
  145. m_Dataserver[m_ScriptEngine] = new Dataserver(this);
  146. if (!m_Timer.ContainsKey(m_ScriptEngine))
  147. m_Timer[m_ScriptEngine] = new Timer(this);
  148. if (!m_HttpRequest.ContainsKey(m_ScriptEngine))
  149. m_HttpRequest[m_ScriptEngine] = new HttpRequest(this);
  150. if (!m_Listener.ContainsKey(m_ScriptEngine))
  151. m_Listener[m_ScriptEngine] = new Listener(this);
  152. if (!m_SensorRepeat.ContainsKey(m_ScriptEngine))
  153. m_SensorRepeat[m_ScriptEngine] = new SensorRepeat(this);
  154. if (!m_XmlRequest.ContainsKey(m_ScriptEngine))
  155. m_XmlRequest[m_ScriptEngine] = new XmlRequest(this);
  156. StartThread();
  157. }
  158. }
  159. private static void StartThread()
  160. {
  161. if (cmdHandlerThread == null)
  162. {
  163. // Start the thread that will be doing the work
  164. cmdHandlerThread
  165. = Watchdog.StartThread(
  166. CmdHandlerThreadLoop, "AsyncLSLCmdHandlerThread", ThreadPriority.Normal, true, true);
  167. }
  168. }
  169. private void ReadConfig()
  170. {
  171. // cmdHandlerThreadCycleSleepms = m_ScriptEngine.Config.GetInt("AsyncLLCommandLoopms", 100);
  172. // TODO: Make this sane again
  173. cmdHandlerThreadCycleSleepms = 100;
  174. }
  175. ~AsyncCommandManager()
  176. {
  177. // Shut down thread
  178. // try
  179. // {
  180. // if (cmdHandlerThread != null)
  181. // {
  182. // if (cmdHandlerThread.IsAlive == true)
  183. // {
  184. // cmdHandlerThread.Abort();
  185. // //cmdHandlerThread.Join();
  186. // }
  187. // }
  188. // }
  189. // catch
  190. // {
  191. // }
  192. }
  193. /// <summary>
  194. /// Main loop for the manager thread
  195. /// </summary>
  196. private static void CmdHandlerThreadLoop()
  197. {
  198. while (true)
  199. {
  200. try
  201. {
  202. Thread.Sleep(cmdHandlerThreadCycleSleepms);
  203. DoOneCmdHandlerPass();
  204. Watchdog.UpdateThread();
  205. }
  206. catch (Exception e)
  207. {
  208. m_log.Error("[ASYNC COMMAND MANAGER]: Exception in command handler pass: ", e);
  209. }
  210. }
  211. }
  212. private static void DoOneCmdHandlerPass()
  213. {
  214. lock (staticLock)
  215. {
  216. // Check HttpRequests
  217. m_HttpRequest[m_ScriptEngines[0]].CheckHttpRequests();
  218. // Check XMLRPCRequests
  219. m_XmlRequest[m_ScriptEngines[0]].CheckXMLRPCRequests();
  220. foreach (IScriptEngine s in m_ScriptEngines)
  221. {
  222. // Check Listeners
  223. m_Listener[s].CheckListeners();
  224. // Check timers
  225. m_Timer[s].CheckTimerEvents();
  226. // Check Sensors
  227. m_SensorRepeat[s].CheckSenseRepeaterEvents();
  228. // Check dataserver
  229. m_Dataserver[s].ExpireRequests();
  230. }
  231. }
  232. }
  233. /// <summary>
  234. /// Remove a specific script (and all its pending commands)
  235. /// </summary>
  236. /// <param name="localID"></param>
  237. /// <param name="itemID"></param>
  238. public static void RemoveScript(IScriptEngine engine, uint localID, UUID itemID)
  239. {
  240. // m_log.DebugFormat("[ASYNC COMMAND MANAGER]: Removing facilities for script {0}", itemID);
  241. lock (staticLock)
  242. {
  243. // Remove dataserver events
  244. m_Dataserver[engine].RemoveEvents(localID, itemID);
  245. // Remove from: Timers
  246. m_Timer[engine].UnSetTimerEvents(localID, itemID);
  247. // Remove from: HttpRequest
  248. IHttpRequestModule iHttpReq = engine.World.RequestModuleInterface<IHttpRequestModule>();
  249. if (iHttpReq != null)
  250. iHttpReq.StopHttpRequestsForScript(itemID);
  251. IWorldComm comms = engine.World.RequestModuleInterface<IWorldComm>();
  252. if (comms != null)
  253. comms.DeleteListener(itemID);
  254. IXMLRPC xmlrpc = engine.World.RequestModuleInterface<IXMLRPC>();
  255. if (xmlrpc != null)
  256. {
  257. xmlrpc.DeleteChannels(itemID);
  258. xmlrpc.CancelSRDRequests(itemID);
  259. }
  260. // Remove Sensors
  261. m_SensorRepeat[engine].UnSetSenseRepeaterEvents(localID, itemID);
  262. }
  263. }
  264. /// <summary>
  265. /// Get the sensor repeat plugin for this script engine.
  266. /// </summary>
  267. /// <param name="engine"></param>
  268. /// <returns></returns>
  269. public static SensorRepeat GetSensorRepeatPlugin(IScriptEngine engine)
  270. {
  271. lock (staticLock)
  272. {
  273. if (m_SensorRepeat.ContainsKey(engine))
  274. return m_SensorRepeat[engine];
  275. else
  276. return null;
  277. }
  278. }
  279. /// <summary>
  280. /// Get the dataserver plugin for this script engine.
  281. /// </summary>
  282. /// <param name="engine"></param>
  283. /// <returns></returns>
  284. public static Dataserver GetDataserverPlugin(IScriptEngine engine)
  285. {
  286. lock (staticLock)
  287. {
  288. if (m_Dataserver.ContainsKey(engine))
  289. return m_Dataserver[engine];
  290. else
  291. return null;
  292. }
  293. }
  294. /// <summary>
  295. /// Get the timer plugin for this script engine.
  296. /// </summary>
  297. /// <param name="engine"></param>
  298. /// <returns></returns>
  299. public static Timer GetTimerPlugin(IScriptEngine engine)
  300. {
  301. lock (staticLock)
  302. {
  303. if (m_Timer.ContainsKey(engine))
  304. return m_Timer[engine];
  305. else
  306. return null;
  307. }
  308. }
  309. /// <summary>
  310. /// Get the listener plugin for this script engine.
  311. /// </summary>
  312. /// <param name="engine"></param>
  313. /// <returns></returns>
  314. public static Listener GetListenerPlugin(IScriptEngine engine)
  315. {
  316. lock (staticLock)
  317. {
  318. if (m_Listener.ContainsKey(engine))
  319. return m_Listener[engine];
  320. else
  321. return null;
  322. }
  323. }
  324. public static Object[] GetSerializationData(IScriptEngine engine, UUID itemID)
  325. {
  326. List<Object> data = new List<Object>();
  327. lock (staticLock)
  328. {
  329. Object[] listeners = m_Listener[engine].GetSerializationData(itemID);
  330. if (listeners.Length > 0)
  331. {
  332. data.Add("listener");
  333. data.Add(listeners.Length);
  334. data.AddRange(listeners);
  335. }
  336. Object[] timers=m_Timer[engine].GetSerializationData(itemID);
  337. if (timers.Length > 0)
  338. {
  339. data.Add("timer");
  340. data.Add(timers.Length);
  341. data.AddRange(timers);
  342. }
  343. Object[] sensors = m_SensorRepeat[engine].GetSerializationData(itemID);
  344. if (sensors.Length > 0)
  345. {
  346. data.Add("sensor");
  347. data.Add(sensors.Length);
  348. data.AddRange(sensors);
  349. }
  350. }
  351. return data.ToArray();
  352. }
  353. public static void CreateFromData(IScriptEngine engine, uint localID,
  354. UUID itemID, UUID hostID, Object[] data)
  355. {
  356. int idx = 0;
  357. int len;
  358. while (idx < data.Length)
  359. {
  360. string type = data[idx].ToString();
  361. len = (int)data[idx+1];
  362. idx+=2;
  363. if (len > 0)
  364. {
  365. Object[] item = new Object[len];
  366. Array.Copy(data, idx, item, 0, len);
  367. idx+=len;
  368. lock (staticLock)
  369. {
  370. switch (type)
  371. {
  372. case "listener":
  373. m_Listener[engine].CreateFromData(localID, itemID,
  374. hostID, item);
  375. break;
  376. case "timer":
  377. m_Timer[engine].CreateFromData(localID, itemID,
  378. hostID, item);
  379. break;
  380. case "sensor":
  381. m_SensorRepeat[engine].CreateFromData(localID,
  382. itemID, hostID, item);
  383. break;
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. }