1
0

AsyncCommandManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. = WorkManager.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. public static void StateChange(IScriptEngine engine, uint localID, UUID itemID)
  265. {
  266. // Remove a specific script
  267. // Remove dataserver events
  268. m_Dataserver[engine].RemoveEvents(localID, itemID);
  269. IWorldComm comms = engine.World.RequestModuleInterface<IWorldComm>();
  270. if (comms != null)
  271. comms.DeleteListener(itemID);
  272. IXMLRPC xmlrpc = engine.World.RequestModuleInterface<IXMLRPC>();
  273. if (xmlrpc != null)
  274. {
  275. xmlrpc.DeleteChannels(itemID);
  276. xmlrpc.CancelSRDRequests(itemID);
  277. }
  278. // Remove Sensors
  279. m_SensorRepeat[engine].UnSetSenseRepeaterEvents(localID, itemID);
  280. }
  281. /// <summary>
  282. /// Get the sensor repeat plugin for this script engine.
  283. /// </summary>
  284. /// <param name="engine"></param>
  285. /// <returns></returns>
  286. public static SensorRepeat GetSensorRepeatPlugin(IScriptEngine engine)
  287. {
  288. lock (staticLock)
  289. {
  290. if (m_SensorRepeat.ContainsKey(engine))
  291. return m_SensorRepeat[engine];
  292. else
  293. return null;
  294. }
  295. }
  296. /// <summary>
  297. /// Get the dataserver plugin for this script engine.
  298. /// </summary>
  299. /// <param name="engine"></param>
  300. /// <returns></returns>
  301. public static Dataserver GetDataserverPlugin(IScriptEngine engine)
  302. {
  303. lock (staticLock)
  304. {
  305. if (m_Dataserver.ContainsKey(engine))
  306. return m_Dataserver[engine];
  307. else
  308. return null;
  309. }
  310. }
  311. /// <summary>
  312. /// Get the timer plugin for this script engine.
  313. /// </summary>
  314. /// <param name="engine"></param>
  315. /// <returns></returns>
  316. public static Timer GetTimerPlugin(IScriptEngine engine)
  317. {
  318. lock (staticLock)
  319. {
  320. if (m_Timer.ContainsKey(engine))
  321. return m_Timer[engine];
  322. else
  323. return null;
  324. }
  325. }
  326. /// <summary>
  327. /// Get the listener plugin for this script engine.
  328. /// </summary>
  329. /// <param name="engine"></param>
  330. /// <returns></returns>
  331. public static Listener GetListenerPlugin(IScriptEngine engine)
  332. {
  333. lock (staticLock)
  334. {
  335. if (m_Listener.ContainsKey(engine))
  336. return m_Listener[engine];
  337. else
  338. return null;
  339. }
  340. }
  341. public static Object[] GetSerializationData(IScriptEngine engine, UUID itemID)
  342. {
  343. List<Object> data = new List<Object>();
  344. lock (staticLock)
  345. {
  346. Object[] listeners = m_Listener[engine].GetSerializationData(itemID);
  347. if (listeners.Length > 0)
  348. {
  349. data.Add("listener");
  350. data.Add(listeners.Length);
  351. data.AddRange(listeners);
  352. }
  353. Object[] timers=m_Timer[engine].GetSerializationData(itemID);
  354. if (timers.Length > 0)
  355. {
  356. data.Add("timer");
  357. data.Add(timers.Length);
  358. data.AddRange(timers);
  359. }
  360. Object[] sensors = m_SensorRepeat[engine].GetSerializationData(itemID);
  361. if (sensors.Length > 0)
  362. {
  363. data.Add("sensor");
  364. data.Add(sensors.Length);
  365. data.AddRange(sensors);
  366. }
  367. }
  368. return data.ToArray();
  369. }
  370. public static void CreateFromData(IScriptEngine engine, uint localID,
  371. UUID itemID, UUID hostID, Object[] data)
  372. {
  373. int idx = 0;
  374. int len;
  375. while (idx < data.Length)
  376. {
  377. string type = data[idx].ToString();
  378. len = (int)data[idx+1];
  379. idx+=2;
  380. if (len > 0)
  381. {
  382. Object[] item = new Object[len];
  383. Array.Copy(data, idx, item, 0, len);
  384. idx+=len;
  385. lock (staticLock)
  386. {
  387. switch (type)
  388. {
  389. case "listener":
  390. m_Listener[engine].CreateFromData(localID, itemID,
  391. hostID, item);
  392. break;
  393. case "timer":
  394. m_Timer[engine].CreateFromData(localID, itemID,
  395. hostID, item);
  396. break;
  397. case "sensor":
  398. m_SensorRepeat[engine].CreateFromData(localID,
  399. itemID, hostID, item);
  400. break;
  401. }
  402. }
  403. }
  404. }
  405. }
  406. }
  407. }