1
0

WebFetchInvDescModule.cs 13 KB

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