LSLLongCmdHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.Generic;
  29. using System.Threading;
  30. using libsecondlife;
  31. using OpenSim.Region.Environment.Interfaces;
  32. using OpenSim.Region.Environment.Modules;
  33. namespace OpenSim.Grid.ScriptEngine.DotNetEngine
  34. {
  35. /// <summary>
  36. /// Handles LSL commands that takes long time and returns an event, for example timers, HTTP requests, etc.
  37. /// </summary>
  38. internal class LSLLongCmdHandler
  39. {
  40. private Thread cmdHandlerThread;
  41. private int cmdHandlerThreadCycleSleepms = 100;
  42. private ScriptEngine m_ScriptEngine;
  43. public LSLLongCmdHandler(ScriptEngine _ScriptEngine)
  44. {
  45. m_ScriptEngine = _ScriptEngine;
  46. // Start the thread that will be doing the work
  47. cmdHandlerThread = new Thread(CmdHandlerThreadLoop);
  48. cmdHandlerThread.Name = "CmdHandlerThread";
  49. cmdHandlerThread.Priority = ThreadPriority.BelowNormal;
  50. cmdHandlerThread.IsBackground = true;
  51. cmdHandlerThread.Start();
  52. }
  53. ~LSLLongCmdHandler()
  54. {
  55. // Shut down thread
  56. try
  57. {
  58. if (cmdHandlerThread != null)
  59. {
  60. if (cmdHandlerThread.IsAlive == true)
  61. {
  62. cmdHandlerThread.Abort();
  63. cmdHandlerThread.Join();
  64. }
  65. }
  66. }
  67. catch
  68. {
  69. }
  70. }
  71. private void CmdHandlerThreadLoop()
  72. {
  73. while (true)
  74. {
  75. // Check timers
  76. CheckTimerEvents();
  77. Thread.Sleep(25);
  78. // Check HttpRequests
  79. CheckHttpRequests();
  80. Thread.Sleep(25);
  81. // Check XMLRPCRequests
  82. CheckXMLRPCRequests();
  83. Thread.Sleep(25);
  84. // Check Listeners
  85. CheckListeners();
  86. Thread.Sleep(25);
  87. // Sleep before next cycle
  88. //Thread.Sleep(cmdHandlerThreadCycleSleepms);
  89. }
  90. }
  91. /// <summary>
  92. /// Remove a specific script (and all its pending commands)
  93. /// </summary>
  94. /// <param name="m_localID"></param>
  95. /// <param name="m_itemID"></param>
  96. public void RemoveScript(uint localID, LLUUID itemID)
  97. {
  98. // Remove a specific script
  99. // Remove from: Timers
  100. UnSetTimerEvents(localID, itemID);
  101. // Remove from: HttpRequest
  102. StopHttpRequest(localID, itemID);
  103. }
  104. #region TIMER
  105. //
  106. // TIMER
  107. //
  108. private class TimerClass
  109. {
  110. public uint localID;
  111. public LLUUID itemID;
  112. public double interval;
  113. public DateTime next;
  114. }
  115. private List<TimerClass> Timers = new List<TimerClass>();
  116. private object TimerListLock = new object();
  117. public void SetTimerEvent(uint m_localID, LLUUID m_itemID, double sec)
  118. {
  119. Console.WriteLine("SetTimerEvent");
  120. // Always remove first, in case this is a re-set
  121. UnSetTimerEvents(m_localID, m_itemID);
  122. if (sec == 0) // Disabling timer
  123. return;
  124. // Add to timer
  125. TimerClass ts = new TimerClass();
  126. ts.localID = m_localID;
  127. ts.itemID = m_itemID;
  128. ts.interval = sec;
  129. ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  130. lock (TimerListLock)
  131. {
  132. Timers.Add(ts);
  133. }
  134. }
  135. public void UnSetTimerEvents(uint m_localID, LLUUID m_itemID)
  136. {
  137. // Remove from timer
  138. lock (TimerListLock)
  139. {
  140. List<TimerClass> NewTimers = new List<TimerClass>();
  141. foreach (TimerClass ts in Timers)
  142. {
  143. if (ts.localID != m_localID && ts.itemID != m_itemID)
  144. {
  145. NewTimers.Add(ts);
  146. }
  147. }
  148. Timers.Clear();
  149. Timers = NewTimers;
  150. }
  151. }
  152. public void CheckTimerEvents()
  153. {
  154. // Nothing to do here?
  155. if (Timers.Count == 0)
  156. return;
  157. lock (TimerListLock)
  158. {
  159. // Go through all timers
  160. foreach (TimerClass ts in Timers)
  161. {
  162. // Time has passed?
  163. if (ts.next.ToUniversalTime() < DateTime.Now.ToUniversalTime())
  164. {
  165. // Add it to queue
  166. m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(ts.localID, ts.itemID, "timer",
  167. new object[] {});
  168. // set next interval
  169. ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  170. }
  171. }
  172. }
  173. }
  174. #endregion
  175. #region HTTP REQUEST
  176. //
  177. // HTTP REAQUEST
  178. //
  179. private class HttpClass
  180. {
  181. public uint localID;
  182. public LLUUID itemID;
  183. public string url;
  184. public List<string> parameters;
  185. public string body;
  186. public DateTime next;
  187. public string response_request_id;
  188. public int response_status;
  189. public List<string> response_metadata;
  190. public string response_body;
  191. public void SendRequest()
  192. {
  193. // TODO: SEND REQUEST!!!
  194. }
  195. public void Stop()
  196. {
  197. // TODO: Cancel any ongoing request
  198. }
  199. public bool CheckResponse()
  200. {
  201. // TODO: Check if we got a response yet, return true if so -- false if not
  202. return true;
  203. // TODO: If we got a response, set the following then return true
  204. //response_request_id
  205. //response_status
  206. //response_metadata
  207. //response_body
  208. }
  209. }
  210. private List<HttpClass> HttpRequests = new List<HttpClass>();
  211. private object HttpListLock = new object();
  212. public void StartHttpRequest(uint localID, LLUUID itemID, string url, List<string> parameters, string body)
  213. {
  214. Console.WriteLine("StartHttpRequest");
  215. HttpClass htc = new HttpClass();
  216. htc.localID = localID;
  217. htc.itemID = itemID;
  218. htc.url = url;
  219. htc.parameters = parameters;
  220. htc.body = body;
  221. lock (HttpListLock)
  222. {
  223. //ADD REQUEST
  224. HttpRequests.Add(htc);
  225. }
  226. }
  227. public void StopHttpRequest(uint m_localID, LLUUID m_itemID)
  228. {
  229. // Remove from list
  230. lock (HttpListLock)
  231. {
  232. List<HttpClass> NewHttpList = new List<HttpClass>();
  233. foreach (HttpClass ts in HttpRequests)
  234. {
  235. if (ts.localID != m_localID && ts.itemID != m_itemID)
  236. {
  237. // Keeping this one
  238. NewHttpList.Add(ts);
  239. }
  240. else
  241. {
  242. // Shutting this one down
  243. ts.Stop();
  244. }
  245. }
  246. HttpRequests.Clear();
  247. HttpRequests = NewHttpList;
  248. }
  249. }
  250. public void CheckHttpRequests()
  251. {
  252. // Nothing to do here?
  253. if (HttpRequests.Count == 0)
  254. return;
  255. lock (HttpListLock)
  256. {
  257. foreach (HttpClass ts in HttpRequests)
  258. {
  259. if (ts.CheckResponse() == true)
  260. {
  261. // Add it to event queue
  262. //key request_id, integer status, list metadata, string body
  263. object[] resobj =
  264. new object[]
  265. {ts.response_request_id, ts.response_status, ts.response_metadata, ts.response_body};
  266. m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(ts.localID, ts.itemID, "http_response",
  267. resobj);
  268. // Now stop it
  269. StopHttpRequest(ts.localID, ts.itemID);
  270. }
  271. }
  272. }
  273. }
  274. #endregion
  275. public void CheckXMLRPCRequests()
  276. {
  277. IXMLRPC xmlrpc = m_ScriptEngine.World.RequestModuleInterface<IXMLRPC>();
  278. while (xmlrpc.hasRequests())
  279. {
  280. RPCRequestInfo rInfo = xmlrpc.GetNextRequest();
  281. Console.WriteLine("PICKED REQUEST");
  282. //Deliver data to prim's remote_data handler
  283. object[] resobj = new object[]
  284. {
  285. 2, rInfo.GetChannelKey().ToString(), rInfo.GetMessageID().ToString(), "", rInfo.GetIntValue(),
  286. rInfo.GetStrVal()
  287. };
  288. m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(
  289. rInfo.GetLocalID(), rInfo.GetItemID(), "remote_data", resobj
  290. );
  291. }
  292. }
  293. public void CheckListeners()
  294. {
  295. IWorldComm comms = m_ScriptEngine.World.RequestModuleInterface<IWorldComm>();
  296. while (comms.HasMessages())
  297. {
  298. ListenerInfo lInfo = comms.GetNextMessage();
  299. Console.WriteLine("PICKED LISTENER");
  300. //Deliver data to prim's listen handler
  301. object[] resobj = new object[]
  302. {
  303. lInfo.GetChannel(), lInfo.GetName(), lInfo.GetID().ToString(), lInfo.GetMessage()
  304. };
  305. m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(
  306. lInfo.GetLocalID(), lInfo.GetItemID(), "listen", resobj
  307. );
  308. }
  309. }
  310. }
  311. }