WebFetchInvDescModule.cs 15 KB

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