WebFetchInvDescModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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.IO;
  32. using System.Reflection;
  33. using System.Text;
  34. using System.Threading;
  35. using log4net;
  36. using Nini.Config;
  37. using Mono.Addins;
  38. using OpenMetaverse;
  39. using OpenSim.Framework;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes;
  43. using OpenSim.Framework.Capabilities;
  44. using OpenSim.Services.Interfaces;
  45. using Caps = OpenSim.Framework.Capabilities.Caps;
  46. using OpenSim.Capabilities.Handlers;
  47. using OpenSim.Framework.Monitoring;
  48. using OpenMetaverse.StructuredData;
  49. namespace OpenSim.Region.ClientStack.Linden
  50. {
  51. /// <summary>
  52. /// This module implements both WebFetchInventoryDescendents and FetchInventoryDescendents2 capabilities.
  53. /// </summary>
  54. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "WebFetchInvDescModule")]
  55. public class WebFetchInvDescModule : INonSharedRegionModule
  56. {
  57. class APollRequest
  58. {
  59. public PollServiceInventoryEventArgs thepoll;
  60. public UUID reqID;
  61. public OSHttpRequest request;
  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 ExpiringKey<UUID> m_badRequests;
  85. private string m_fetchInventoryDescendents2Url;
  86. // private string m_webFetchInventoryDescendentsUrl;
  87. private static FetchInvDescHandler m_webFetchHandler;
  88. private static ObjectJobEngine m_workerpool = null;
  89. private static int m_NumberScenes = 0;
  90. #region ISharedRegionModule Members
  91. public WebFetchInvDescModule() : this(true) {}
  92. public WebFetchInvDescModule(bool processQueuedResultsAsync)
  93. {
  94. ProcessQueuedRequestsAsync = processQueuedResultsAsync;
  95. }
  96. public void Initialise(IConfigSource source)
  97. {
  98. IConfig config = source.Configs["ClientStack.LindenCaps"];
  99. if (config == null)
  100. return;
  101. m_fetchInventoryDescendents2Url = config.GetString("Cap_FetchInventoryDescendents2", string.Empty);
  102. // m_webFetchInventoryDescendentsUrl = config.GetString("Cap_WebFetchInventoryDescendents", string.Empty);
  103. // if (m_fetchInventoryDescendents2Url != string.Empty || m_webFetchInventoryDescendentsUrl != string.Empty)
  104. if (m_fetchInventoryDescendents2Url != string.Empty)
  105. {
  106. m_Enabled = true;
  107. }
  108. }
  109. public void AddRegion(Scene s)
  110. {
  111. if (!m_Enabled)
  112. return;
  113. Scene = s;
  114. }
  115. public void RemoveRegion(Scene s)
  116. {
  117. if (!m_Enabled)
  118. return;
  119. m_NumberScenes--;
  120. Scene.EventManager.OnRegisterCaps -= RegisterCaps;
  121. StatsManager.DeregisterStat(s_processedRequestsStat);
  122. StatsManager.DeregisterStat(s_queuedRequestsStat);
  123. Scene = null;
  124. }
  125. public void RegionLoaded(Scene s)
  126. {
  127. if (!m_Enabled)
  128. return;
  129. if (s_processedRequestsStat == null)
  130. s_processedRequestsStat =
  131. new Stat(
  132. "ProcessedFetchInventoryRequests",
  133. "Number of processed fetch inventory requests",
  134. "These have not necessarily yet been dispatched back to the requester.",
  135. "",
  136. "inventory",
  137. "httpfetch",
  138. StatType.Pull,
  139. MeasuresOfInterest.AverageChangeOverTime,
  140. stat => { stat.Value = ProcessedRequestsCount; },
  141. StatVerbosity.Debug);
  142. if (s_queuedRequestsStat == null)
  143. s_queuedRequestsStat =
  144. new Stat(
  145. "QueuedFetchInventoryRequests",
  146. "Number of fetch inventory requests queued for processing",
  147. "",
  148. "",
  149. "inventory",
  150. "httpfetch",
  151. StatType.Pull,
  152. MeasuresOfInterest.AverageChangeOverTime,
  153. stat => { stat.Value = m_workerpool.Count; },
  154. StatVerbosity.Debug);
  155. StatsManager.RegisterStat(s_processedRequestsStat);
  156. StatsManager.RegisterStat(s_queuedRequestsStat);
  157. m_InventoryService = Scene.InventoryService;
  158. m_LibraryService = Scene.LibraryService;
  159. // We'll reuse the same handler for all requests.
  160. m_webFetchHandler = new FetchInvDescHandler(m_InventoryService, m_LibraryService, Scene);
  161. Scene.EventManager.OnRegisterCaps += RegisterCaps;
  162. if(m_badRequests == null)
  163. m_badRequests = new ExpiringKey<UUID>(30000);
  164. m_NumberScenes++;
  165. if (ProcessQueuedRequestsAsync && m_workerpool == null)
  166. m_workerpool = new ObjectJobEngine(DoInventoryRequests, "InventoryWorker",2000,2);
  167. }
  168. public void PostInitialise()
  169. {
  170. }
  171. public void Close()
  172. {
  173. if (!m_Enabled)
  174. return;
  175. if (ProcessQueuedRequestsAsync)
  176. {
  177. if (m_NumberScenes <= 0 && m_workerpool != null)
  178. {
  179. m_workerpool.Dispose();
  180. m_workerpool = null;
  181. m_badRequests.Dispose();
  182. m_badRequests = null;
  183. }
  184. }
  185. // m_queue.Dispose();
  186. }
  187. public string Name { get { return "WebFetchInvDescModule"; } }
  188. public Type ReplaceableInterface
  189. {
  190. get { return null; }
  191. }
  192. #endregion
  193. private class PollServiceInventoryEventArgs : PollServiceEventArgs
  194. {
  195. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  196. private Dictionary<UUID, Hashtable> responses = new Dictionary<UUID, Hashtable>();
  197. private HashSet<UUID> dropedResponses = new HashSet<UUID>();
  198. private WebFetchInvDescModule m_module;
  199. public PollServiceInventoryEventArgs(WebFetchInvDescModule module, string url, UUID pId) :
  200. base(null, url, null, null, null, null, pId, int.MaxValue)
  201. {
  202. m_module = module;
  203. HasEvents = (requestID, y) =>
  204. {
  205. lock (responses)
  206. return responses.ContainsKey(requestID);
  207. };
  208. Drop = (requestID, y) =>
  209. {
  210. lock (responses)
  211. {
  212. responses.Remove(requestID);
  213. lock(dropedResponses)
  214. dropedResponses.Add(requestID);
  215. }
  216. };
  217. GetEvents = (requestID, y) =>
  218. {
  219. lock (responses)
  220. {
  221. try
  222. {
  223. return responses[requestID];
  224. }
  225. finally
  226. {
  227. responses.Remove(requestID);
  228. }
  229. }
  230. };
  231. Request = (requestID, request) =>
  232. {
  233. APollRequest reqinfo = new APollRequest();
  234. reqinfo.thepoll = this;
  235. reqinfo.reqID = requestID;
  236. reqinfo.request = request;
  237. m_workerpool.Enqueue(reqinfo);
  238. return null;
  239. };
  240. NoEvents = (x, y) =>
  241. {
  242. Hashtable response = new Hashtable();
  243. response["int_response_code"] = 500;
  244. response["str_response_string"] = "Script timeout";
  245. response["content_type"] = "text/plain";
  246. response["keepalive"] = false;
  247. return response;
  248. };
  249. }
  250. public void Process(APollRequest requestinfo)
  251. {
  252. if(m_module == null || m_module.Scene == null || m_module.Scene.ShuttingDown)
  253. return;
  254. UUID requestID = requestinfo.reqID;
  255. lock(responses)
  256. {
  257. lock(dropedResponses)
  258. {
  259. if(dropedResponses.Contains(requestID))
  260. {
  261. dropedResponses.Remove(requestID);
  262. return;
  263. }
  264. }
  265. }
  266. OSHttpResponse osresponse = new OSHttpResponse(requestinfo.request);
  267. m_webFetchHandler.FetchInventoryDescendentsRequest(requestinfo.request, osresponse, m_module.m_badRequests);
  268. requestinfo.request.InputStream.Dispose();
  269. lock (responses)
  270. {
  271. lock(dropedResponses)
  272. {
  273. if(dropedResponses.Contains(requestID))
  274. {
  275. dropedResponses.Remove(requestID);
  276. ProcessedRequestsCount++;
  277. return;
  278. }
  279. }
  280. Hashtable response = new Hashtable();
  281. response["h"] = osresponse;
  282. responses[requestID] = response;
  283. }
  284. ProcessedRequestsCount++;
  285. }
  286. }
  287. private void RegisterCaps(UUID agentID, Caps caps)
  288. {
  289. RegisterFetchDescendentsCap(agentID, caps, "FetchInventoryDescendents2", m_fetchInventoryDescendents2Url);
  290. }
  291. private void RegisterFetchDescendentsCap(UUID agentID, Caps caps, string capName, string url)
  292. {
  293. string capUrl;
  294. // disable the cap clause
  295. if (url == "")
  296. {
  297. return;
  298. }
  299. // handled by the simulator
  300. else if (url == "localhost")
  301. {
  302. capUrl = "/" + UUID.Random();
  303. // Register this as a poll service
  304. PollServiceInventoryEventArgs args = new PollServiceInventoryEventArgs(this, capUrl, agentID);
  305. //args.Type = PollServiceEventArgs.EventType.Inventory;
  306. caps.RegisterPollHandler(capName, args);
  307. }
  308. // external handler
  309. else
  310. {
  311. capUrl = url;
  312. IExternalCapsModule handler = Scene.RequestModuleInterface<IExternalCapsModule>();
  313. if (handler != null)
  314. handler.RegisterExternalUserCapsHandler(agentID,caps,capName,capUrl);
  315. else
  316. caps.RegisterHandler(capName, capUrl);
  317. }
  318. }
  319. private static void DoInventoryRequests(object o)
  320. {
  321. if(m_NumberScenes <= 0)
  322. return;
  323. APollRequest poolreq = o as APollRequest;
  324. if (poolreq != null && poolreq.thepoll != null)
  325. poolreq.thepoll.Process(poolreq);
  326. }
  327. }
  328. }