FetchLibDescModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 log4net;
  32. using Nini.Config;
  33. using Mono.Addins;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. using Caps = OpenSim.Framework.Capabilities.Caps;
  41. using OpenSim.Capabilities.Handlers;
  42. using OpenSim.Framework.Monitoring;
  43. using OpenMetaverse.StructuredData;
  44. namespace OpenSim.Region.ClientStack.Linden
  45. {
  46. /// <summary>
  47. /// This module implements both WebFetchInventoryDescendents and FetchInventoryDescendents2 capabilities.
  48. /// </summary>
  49. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "FetchLibDescModule")]
  50. public class FetchLibDescModule : INonSharedRegionModule
  51. {
  52. class APollRequest
  53. {
  54. public PollServiceInventoryEventArgs thepoll;
  55. public UUID reqID;
  56. public OSHttpRequest request;
  57. }
  58. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  59. /// <summary>
  60. /// Control whether requests will be processed asynchronously.
  61. /// </summary>
  62. /// <remarks>
  63. /// Defaults to true. Can currently not be changed once a region has been added to the module.
  64. /// </remarks>
  65. public bool ProcessQueuedRequestsAsync { get; private set; }
  66. /// <summary>
  67. /// Number of inventory requests processed by this module.
  68. /// </summary>
  69. /// <remarks>
  70. /// It's the PollServiceRequestManager that actually sends completed requests back to the requester.
  71. /// </remarks>
  72. public static int ProcessedRequestsCount { get; set; }
  73. public Scene Scene { get; private set; }
  74. private ILibraryService m_LibraryService;
  75. private bool m_Enabled;
  76. private ExpiringKey<UUID> m_badRequests;
  77. private string m_fetchLibDescendents2Url;
  78. private static FetchLibDescHandler m_FetchHandler;
  79. private static ObjectJobEngine m_workerpool = null;
  80. #region ISharedRegionModule Members
  81. public FetchLibDescModule() : this(true) {}
  82. public FetchLibDescModule(bool processQueuedResultsAsync)
  83. {
  84. ProcessQueuedRequestsAsync = processQueuedResultsAsync;
  85. }
  86. public void Initialise(IConfigSource source)
  87. {
  88. IConfig config = source.Configs["ClientStack.LindenCaps"];
  89. if (config == null)
  90. return;
  91. m_fetchLibDescendents2Url = config.GetString("Cap_FetchLibDescendents2", string.Empty);
  92. m_Enabled = m_fetchLibDescendents2Url.Length > 0;
  93. }
  94. public void AddRegion(Scene s)
  95. {
  96. if (!m_Enabled)
  97. return;
  98. Scene = s;
  99. }
  100. public void RemoveRegion(Scene s)
  101. {
  102. if (!m_Enabled)
  103. return;
  104. Scene.EventManager.OnRegisterCaps -= RegisterCaps;
  105. Scene = null;
  106. }
  107. public void RegionLoaded(Scene s)
  108. {
  109. if (!m_Enabled)
  110. return;
  111. m_LibraryService = Scene.LibraryService;
  112. // We'll reuse the same handler for all requests.
  113. m_FetchHandler = new FetchLibDescHandler(m_LibraryService, Scene);
  114. Scene.EventManager.OnRegisterCaps += RegisterCaps;
  115. if(m_badRequests == null)
  116. m_badRequests = new ExpiringKey<UUID>(30000);
  117. if (ProcessQueuedRequestsAsync && m_workerpool == null)
  118. m_workerpool = new ObjectJobEngine(DoInventoryRequests, "LibInventoryWorker", 2000, 2);
  119. }
  120. public void PostInitialise()
  121. {
  122. }
  123. public void Close()
  124. {
  125. if (!m_Enabled)
  126. return;
  127. if (ProcessQueuedRequestsAsync)
  128. {
  129. if (m_workerpool != null)
  130. {
  131. m_workerpool.Dispose();
  132. m_workerpool = null;
  133. m_badRequests.Dispose();
  134. m_badRequests = null;
  135. }
  136. }
  137. //m_queue.Dispose();
  138. }
  139. public string Name { get { return "FetchLibDescModule"; } }
  140. public Type ReplaceableInterface
  141. {
  142. get { return null; }
  143. }
  144. #endregion
  145. private class PollServiceInventoryEventArgs : PollServiceEventArgs
  146. {
  147. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  148. private Dictionary<UUID, Hashtable> responses = new Dictionary<UUID, Hashtable>();
  149. private HashSet<UUID> dropedResponses = new HashSet<UUID>();
  150. private FetchLibDescModule m_module;
  151. public PollServiceInventoryEventArgs(FetchLibDescModule module, string url, UUID pId) :
  152. base(null, url, null, null, null, null, pId, int.MaxValue)
  153. {
  154. m_module = module;
  155. HasEvents = (requestID, y) =>
  156. {
  157. lock (responses)
  158. return responses.ContainsKey(requestID);
  159. };
  160. Drop = (requestID, y) =>
  161. {
  162. lock (responses)
  163. {
  164. responses.Remove(requestID);
  165. lock(dropedResponses)
  166. dropedResponses.Add(requestID);
  167. }
  168. };
  169. GetEvents = (requestID, y) =>
  170. {
  171. lock (responses)
  172. {
  173. try
  174. {
  175. return responses[requestID];
  176. }
  177. finally
  178. {
  179. responses.Remove(requestID);
  180. }
  181. }
  182. };
  183. Request = (requestID, request) =>
  184. {
  185. APollRequest reqinfo = new APollRequest();
  186. reqinfo.thepoll = this;
  187. reqinfo.reqID = requestID;
  188. reqinfo.request = request;
  189. m_workerpool.Enqueue(reqinfo);
  190. return null;
  191. };
  192. NoEvents = (x, y) =>
  193. {
  194. Hashtable response = new Hashtable();
  195. response["int_response_code"] = 500;
  196. response["str_response_string"] = "Script timeout";
  197. response["content_type"] = "text/plain";
  198. response["keepalive"] = false;
  199. return response;
  200. };
  201. }
  202. public void Process(APollRequest requestinfo)
  203. {
  204. if(m_module == null || m_module.Scene == null || m_module.Scene.ShuttingDown)
  205. return;
  206. UUID requestID = requestinfo.reqID;
  207. lock(responses)
  208. {
  209. lock(dropedResponses)
  210. {
  211. if(dropedResponses.Contains(requestID))
  212. {
  213. dropedResponses.Remove(requestID);
  214. return;
  215. }
  216. }
  217. }
  218. OSHttpResponse osresponse = new OSHttpResponse(requestinfo.request);
  219. m_FetchHandler.FetchRequest(requestinfo.request, osresponse, m_module.m_badRequests, requestinfo.thepoll.Id);
  220. requestinfo.request.InputStream.Dispose();
  221. lock (responses)
  222. {
  223. lock(dropedResponses)
  224. {
  225. if(dropedResponses.Contains(requestID))
  226. {
  227. dropedResponses.Remove(requestID);
  228. ProcessedRequestsCount++;
  229. return;
  230. }
  231. }
  232. Hashtable response = new Hashtable();
  233. response["h"] = osresponse;
  234. responses[requestID] = response;
  235. }
  236. ProcessedRequestsCount++;
  237. }
  238. }
  239. private void RegisterCaps(UUID agentID, Caps caps)
  240. {
  241. RegisterFetchLibDescendentsCap(agentID, caps, "FetchLibDescendents2", m_fetchLibDescendents2Url);
  242. }
  243. private void RegisterFetchLibDescendentsCap(UUID agentID, Caps caps, string capName, string url)
  244. {
  245. string capUrl;
  246. // handled by the simulator
  247. if (url == "localhost")
  248. {
  249. capUrl = "/" + UUID.Random();
  250. // Register this as a poll service
  251. PollServiceInventoryEventArgs args = new PollServiceInventoryEventArgs(this, capUrl, agentID);
  252. //args.Type = PollServiceEventArgs.EventType.Inventory;
  253. caps.RegisterPollHandler(capName, args);
  254. }
  255. // external handler
  256. else
  257. {
  258. capUrl = url;
  259. IExternalCapsModule handler = Scene.RequestModuleInterface<IExternalCapsModule>();
  260. if (handler != null)
  261. handler.RegisterExternalUserCapsHandler(agentID, caps, capName, capUrl);
  262. else
  263. caps.RegisterHandler(capName, capUrl);
  264. }
  265. }
  266. private static void DoInventoryRequests(object o)
  267. {
  268. APollRequest poolreq = o as APollRequest;
  269. if (poolreq != null && poolreq.thepoll != null)
  270. poolreq.thepoll.Process(poolreq);
  271. }
  272. }
  273. }