WebFetchInvDescModule.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 Nini.Config;
  34. using Mono.Addins;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Framework.Capabilities;
  42. using OpenSim.Services.Interfaces;
  43. using Caps = OpenSim.Framework.Capabilities.Caps;
  44. using OpenSim.Capabilities.Handlers;
  45. using OpenSim.Framework.Monitoring;
  46. using OpenMetaverse.StructuredData;
  47. namespace OpenSim.Region.ClientStack.Linden
  48. {
  49. /// <summary>
  50. /// This module implements both WebFetchInventoryDescendents and FetchInventoryDescendents2 capabilities.
  51. /// </summary>
  52. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "WebFetchInvDescModule")]
  53. public class WebFetchInvDescModule : INonSharedRegionModule
  54. {
  55. class aPollRequest
  56. {
  57. public PollServiceInventoryEventArgs thepoll;
  58. public UUID reqID;
  59. public Hashtable request;
  60. public ScenePresence presence;
  61. public List<UUID> folders;
  62. }
  63. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  64. /// <summary>
  65. /// Control whether requests will be processed asynchronously.
  66. /// </summary>
  67. /// <remarks>
  68. /// Defaults to true. Can currently not be changed once a region has been added to the module.
  69. /// </remarks>
  70. public bool ProcessQueuedRequestsAsync { get; private set; }
  71. /// <summary>
  72. /// Number of inventory requests processed by this module.
  73. /// </summary>
  74. /// <remarks>
  75. /// It's the PollServiceRequestManager that actually sends completed requests back to the requester.
  76. /// </remarks>
  77. public static int ProcessedRequestsCount { get; set; }
  78. private static Stat s_queuedRequestsStat;
  79. private static Stat s_processedRequestsStat;
  80. public Scene Scene { get; private set; }
  81. private IInventoryService m_InventoryService;
  82. private ILibraryService m_LibraryService;
  83. private bool m_Enabled;
  84. private string m_fetchInventoryDescendents2Url;
  85. // private string m_webFetchInventoryDescendentsUrl;
  86. private static FetchInvDescHandler m_webFetchHandler;
  87. private static Thread[] m_workerThreads = null;
  88. private static OpenSim.Framework.BlockingQueue<aPollRequest> m_queue =
  89. new OpenSim.Framework.BlockingQueue<aPollRequest>();
  90. private static int m_NumberScenes = 0;
  91. #region ISharedRegionModule Members
  92. public WebFetchInvDescModule() : this(true) {}
  93. public WebFetchInvDescModule(bool processQueuedResultsAsync)
  94. {
  95. ProcessQueuedRequestsAsync = processQueuedResultsAsync;
  96. }
  97. public void Initialise(IConfigSource source)
  98. {
  99. IConfig config = source.Configs["ClientStack.LindenCaps"];
  100. if (config == null)
  101. return;
  102. m_fetchInventoryDescendents2Url = config.GetString("Cap_FetchInventoryDescendents2", string.Empty);
  103. // m_webFetchInventoryDescendentsUrl = config.GetString("Cap_WebFetchInventoryDescendents", string.Empty);
  104. // if (m_fetchInventoryDescendents2Url != string.Empty || m_webFetchInventoryDescendentsUrl != string.Empty)
  105. if (m_fetchInventoryDescendents2Url != string.Empty)
  106. {
  107. m_Enabled = true;
  108. }
  109. }
  110. public void AddRegion(Scene s)
  111. {
  112. if (!m_Enabled)
  113. return;
  114. Scene = s;
  115. }
  116. public void RemoveRegion(Scene s)
  117. {
  118. if (!m_Enabled)
  119. return;
  120. Scene.EventManager.OnRegisterCaps -= RegisterCaps;
  121. StatsManager.DeregisterStat(s_processedRequestsStat);
  122. StatsManager.DeregisterStat(s_queuedRequestsStat);
  123. m_NumberScenes--;
  124. Scene = null;
  125. }
  126. public void RegionLoaded(Scene s)
  127. {
  128. if (!m_Enabled)
  129. return;
  130. if (s_processedRequestsStat == null)
  131. s_processedRequestsStat =
  132. new Stat(
  133. "ProcessedFetchInventoryRequests",
  134. "Number of processed fetch inventory requests",
  135. "These have not necessarily yet been dispatched back to the requester.",
  136. "",
  137. "inventory",
  138. "httpfetch",
  139. StatType.Pull,
  140. MeasuresOfInterest.AverageChangeOverTime,
  141. stat => { stat.Value = ProcessedRequestsCount; },
  142. StatVerbosity.Debug);
  143. if (s_queuedRequestsStat == null)
  144. s_queuedRequestsStat =
  145. new Stat(
  146. "QueuedFetchInventoryRequests",
  147. "Number of fetch inventory requests queued for processing",
  148. "",
  149. "",
  150. "inventory",
  151. "httpfetch",
  152. StatType.Pull,
  153. MeasuresOfInterest.AverageChangeOverTime,
  154. stat => { stat.Value = m_queue.Count(); },
  155. StatVerbosity.Debug);
  156. StatsManager.RegisterStat(s_processedRequestsStat);
  157. StatsManager.RegisterStat(s_queuedRequestsStat);
  158. m_InventoryService = Scene.InventoryService;
  159. m_LibraryService = Scene.LibraryService;
  160. // We'll reuse the same handler for all requests.
  161. m_webFetchHandler = new FetchInvDescHandler(m_InventoryService, m_LibraryService, Scene);
  162. Scene.EventManager.OnRegisterCaps += RegisterCaps;
  163. m_NumberScenes++;
  164. int nworkers = 2; // was 2
  165. if (ProcessQueuedRequestsAsync && m_workerThreads == null)
  166. {
  167. m_workerThreads = new Thread[nworkers];
  168. for (uint i = 0; i < nworkers; i++)
  169. {
  170. m_workerThreads[i] = WorkManager.StartThread(DoInventoryRequests,
  171. String.Format("InventoryWorkerThread{0}", i),
  172. ThreadPriority.Normal,
  173. true,
  174. true,
  175. null,
  176. int.MaxValue);
  177. }
  178. }
  179. }
  180. public void PostInitialise()
  181. {
  182. }
  183. public void Close()
  184. {
  185. if (!m_Enabled)
  186. return;
  187. if (ProcessQueuedRequestsAsync)
  188. {
  189. if (m_NumberScenes <= 0 && m_workerThreads != null)
  190. {
  191. m_log.DebugFormat("[WebFetchInvDescModule] Closing");
  192. foreach (Thread t in m_workerThreads)
  193. Watchdog.AbortThread(t.ManagedThreadId);
  194. m_workerThreads = null;
  195. }
  196. }
  197. }
  198. public string Name { get { return "WebFetchInvDescModule"; } }
  199. public Type ReplaceableInterface
  200. {
  201. get { return null; }
  202. }
  203. #endregion
  204. private class PollServiceInventoryEventArgs : PollServiceEventArgs
  205. {
  206. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  207. private Dictionary<UUID, Hashtable> responses =
  208. new Dictionary<UUID, Hashtable>();
  209. private WebFetchInvDescModule m_module;
  210. public PollServiceInventoryEventArgs(WebFetchInvDescModule module, string url, UUID pId) :
  211. base(null, url, null, null, null, pId, int.MaxValue)
  212. {
  213. m_module = module;
  214. HasEvents = (x, y) => { lock (responses) return responses.ContainsKey(x); };
  215. GetEvents = (x, y) =>
  216. {
  217. lock (responses)
  218. {
  219. try
  220. {
  221. return responses[x];
  222. }
  223. finally
  224. {
  225. responses.Remove(x);
  226. }
  227. }
  228. };
  229. Request = (x, y) =>
  230. {
  231. ScenePresence sp = m_module.Scene.GetScenePresence(Id);
  232. aPollRequest reqinfo = new aPollRequest();
  233. reqinfo.thepoll = this;
  234. reqinfo.reqID = x;
  235. reqinfo.request = y;
  236. reqinfo.presence = sp;
  237. reqinfo.folders = new List<UUID>();
  238. // Decode the request here
  239. string request = y["body"].ToString();
  240. request = request.Replace("<string>00000000-0000-0000-0000-000000000000</string>", "<uuid>00000000-0000-0000-0000-000000000000</uuid>");
  241. request = request.Replace("<key>fetch_folders</key><integer>0</integer>", "<key>fetch_folders</key><boolean>0</boolean>");
  242. request = request.Replace("<key>fetch_folders</key><integer>1</integer>", "<key>fetch_folders</key><boolean>1</boolean>");
  243. Hashtable hash = new Hashtable();
  244. try
  245. {
  246. hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  247. }
  248. catch (LLSD.LLSDParseException e)
  249. {
  250. m_log.ErrorFormat("[INVENTORY]: Fetch error: {0}{1}" + e.Message, e.StackTrace);
  251. m_log.Error("Request: " + request);
  252. return;
  253. }
  254. catch (System.Xml.XmlException)
  255. {
  256. m_log.ErrorFormat("[INVENTORY]: XML Format error");
  257. }
  258. ArrayList foldersrequested = (ArrayList)hash["folders"];
  259. bool highPriority = false;
  260. for (int i = 0; i < foldersrequested.Count; i++)
  261. {
  262. Hashtable inventoryhash = (Hashtable)foldersrequested[i];
  263. string folder = inventoryhash["folder_id"].ToString();
  264. UUID folderID;
  265. if (UUID.TryParse(folder, out folderID))
  266. {
  267. if (!reqinfo.folders.Contains(folderID))
  268. {
  269. if (sp.COF != UUID.Zero && sp.COF == folderID)
  270. highPriority = true;
  271. reqinfo.folders.Add(folderID);
  272. }
  273. }
  274. }
  275. if (highPriority)
  276. m_queue.PriorityEnqueue(reqinfo);
  277. else
  278. m_queue.Enqueue(reqinfo);
  279. };
  280. NoEvents = (x, y) =>
  281. {
  282. /*
  283. lock (requests)
  284. {
  285. Hashtable request = requests.Find(id => id["RequestID"].ToString() == x.ToString());
  286. requests.Remove(request);
  287. }
  288. */
  289. Hashtable response = new Hashtable();
  290. response["int_response_code"] = 500;
  291. response["str_response_string"] = "Script timeout";
  292. response["content_type"] = "text/plain";
  293. response["keepalive"] = false;
  294. response["reusecontext"] = false;
  295. return response;
  296. };
  297. }
  298. public void Process(aPollRequest requestinfo)
  299. {
  300. if(m_module == null || m_module.Scene == null || m_module.Scene.ShuttingDown)
  301. return;
  302. UUID requestID = requestinfo.reqID;
  303. Hashtable response = new Hashtable();
  304. response["int_response_code"] = 200;
  305. response["content_type"] = "text/plain";
  306. response["keepalive"] = false;
  307. response["reusecontext"] = false;
  308. response["str_response_string"] = m_webFetchHandler.FetchInventoryDescendentsRequest(
  309. requestinfo.request["body"].ToString(), String.Empty, String.Empty, null, null);
  310. lock (responses)
  311. {
  312. if (responses.ContainsKey(requestID))
  313. m_log.WarnFormat("[FETCH INVENTORY DESCENDENTS2 MODULE]: Caught in the act of loosing responses! Please report this on mantis #7054");
  314. responses[requestID] = response;
  315. }
  316. requestinfo.folders.Clear();
  317. requestinfo.request.Clear();
  318. WebFetchInvDescModule.ProcessedRequestsCount++;
  319. }
  320. }
  321. private void RegisterCaps(UUID agentID, Caps caps)
  322. {
  323. RegisterFetchDescendentsCap(agentID, caps, "FetchInventoryDescendents2", m_fetchInventoryDescendents2Url);
  324. }
  325. private void RegisterFetchDescendentsCap(UUID agentID, Caps caps, string capName, string url)
  326. {
  327. string capUrl;
  328. // disable the cap clause
  329. if (url == "")
  330. {
  331. return;
  332. }
  333. // handled by the simulator
  334. else if (url == "localhost")
  335. {
  336. capUrl = "/CAPS/" + UUID.Random() + "/";
  337. // Register this as a poll service
  338. PollServiceInventoryEventArgs args = new PollServiceInventoryEventArgs(this, capUrl, agentID);
  339. args.Type = PollServiceEventArgs.EventType.Inventory;
  340. caps.RegisterPollHandler(capName, args);
  341. }
  342. // external handler
  343. else
  344. {
  345. capUrl = url;
  346. IExternalCapsModule handler = Scene.RequestModuleInterface<IExternalCapsModule>();
  347. if (handler != null)
  348. handler.RegisterExternalUserCapsHandler(agentID,caps,capName,capUrl);
  349. else
  350. caps.RegisterHandler(capName, capUrl);
  351. }
  352. // m_log.DebugFormat(
  353. // "[FETCH INVENTORY DESCENDENTS2 MODULE]: Registered capability {0} at {1} in region {2} for {3}",
  354. // capName, capUrl, m_scene.RegionInfo.RegionName, agentID);
  355. }
  356. // private void DeregisterCaps(UUID agentID, Caps caps)
  357. // {
  358. // string capUrl;
  359. //
  360. // if (m_capsDict.TryGetValue(agentID, out capUrl))
  361. // {
  362. // MainServer.Instance.RemoveHTTPHandler("", capUrl);
  363. // m_capsDict.Remove(agentID);
  364. // }
  365. // }
  366. private static void DoInventoryRequests()
  367. {
  368. while (true)
  369. {
  370. aPollRequest poolreq = m_queue.Dequeue(4500);
  371. Watchdog.UpdateThread();
  372. if (poolreq != null && poolreq.thepoll != null)
  373. {
  374. try
  375. {
  376. poolreq.thepoll.Process(poolreq);
  377. }
  378. catch (Exception e)
  379. {
  380. m_log.ErrorFormat(
  381. "[INVENTORY]: Failed to process queued inventory request {0} for {1}. Exception {2}",
  382. poolreq.reqID, poolreq.presence != null ? poolreq.presence.Name : "unknown", e);
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }