WebFetchInvDescModule.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 OpenMetaverse.StructuredData;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Monitoring;
  39. using OpenSim.Framework.Servers;
  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. 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. private Scene m_scene;
  65. private IInventoryService m_InventoryService;
  66. private ILibraryService m_LibraryService;
  67. private bool m_Enabled;
  68. private string m_fetchInventoryDescendents2Url;
  69. private string m_webFetchInventoryDescendentsUrl;
  70. private static WebFetchInvDescHandler m_webFetchHandler;
  71. private static Thread[] m_workerThreads = null;
  72. private static DoubleQueue<aPollRequest> m_queue =
  73. new DoubleQueue<aPollRequest>();
  74. #region ISharedRegionModule Members
  75. public void Initialise(IConfigSource source)
  76. {
  77. IConfig config = source.Configs["ClientStack.LindenCaps"];
  78. if (config == null)
  79. return;
  80. m_fetchInventoryDescendents2Url = config.GetString("Cap_FetchInventoryDescendents2", string.Empty);
  81. m_webFetchInventoryDescendentsUrl = config.GetString("Cap_WebFetchInventoryDescendents", string.Empty);
  82. if (m_fetchInventoryDescendents2Url != string.Empty || m_webFetchInventoryDescendentsUrl != string.Empty)
  83. {
  84. m_Enabled = true;
  85. }
  86. }
  87. public void AddRegion(Scene s)
  88. {
  89. if (!m_Enabled)
  90. return;
  91. m_scene = s;
  92. }
  93. public void RemoveRegion(Scene s)
  94. {
  95. if (!m_Enabled)
  96. return;
  97. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  98. foreach (Thread t in m_workerThreads)
  99. Watchdog.AbortThread(t.ManagedThreadId);
  100. m_scene = null;
  101. }
  102. public void RegionLoaded(Scene s)
  103. {
  104. if (!m_Enabled)
  105. return;
  106. m_InventoryService = m_scene.InventoryService;
  107. m_LibraryService = m_scene.LibraryService;
  108. // We'll reuse the same handler for all requests.
  109. m_webFetchHandler = new WebFetchInvDescHandler(m_InventoryService, m_LibraryService);
  110. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  111. if (m_workerThreads == null)
  112. {
  113. m_workerThreads = new Thread[2];
  114. for (uint i = 0; i < 2; i++)
  115. {
  116. m_workerThreads[i] = Watchdog.StartThread(DoInventoryRequests,
  117. String.Format("InventoryWorkerThread{0}", i),
  118. ThreadPriority.Normal,
  119. false,
  120. true,
  121. null,
  122. int.MaxValue);
  123. }
  124. }
  125. }
  126. public void PostInitialise()
  127. {
  128. }
  129. public void Close() { }
  130. public string Name { get { return "WebFetchInvDescModule"; } }
  131. public Type ReplaceableInterface
  132. {
  133. get { return null; }
  134. }
  135. #endregion
  136. private class PollServiceInventoryEventArgs : PollServiceEventArgs
  137. {
  138. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  139. private Dictionary<UUID, Hashtable> responses =
  140. new Dictionary<UUID, Hashtable>();
  141. private Scene m_scene;
  142. public PollServiceInventoryEventArgs(Scene scene, string url, UUID pId) :
  143. base(null, url, null, null, null, pId, int.MaxValue)
  144. {
  145. m_scene = scene;
  146. HasEvents = (x, y) => { lock (responses) return responses.ContainsKey(x); };
  147. GetEvents = (x, y) =>
  148. {
  149. lock (responses)
  150. {
  151. try
  152. {
  153. return responses[x];
  154. }
  155. finally
  156. {
  157. responses.Remove(x);
  158. }
  159. }
  160. };
  161. Request = (x, y) =>
  162. {
  163. ScenePresence sp = m_scene.GetScenePresence(Id);
  164. if (sp == null)
  165. {
  166. m_log.ErrorFormat("[INVENTORY]: Unable to find ScenePresence for {0}", Id);
  167. return;
  168. }
  169. aPollRequest reqinfo = new aPollRequest();
  170. reqinfo.thepoll = this;
  171. reqinfo.reqID = x;
  172. reqinfo.request = y;
  173. reqinfo.presence = sp;
  174. reqinfo.folders = new List<UUID>();
  175. // Decode the request here
  176. string request = y["body"].ToString();
  177. request = request.Replace("<string>00000000-0000-0000-0000-000000000000</string>", "<uuid>00000000-0000-0000-0000-000000000000</uuid>");
  178. request = request.Replace("<key>fetch_folders</key><integer>0</integer>", "<key>fetch_folders</key><boolean>0</boolean>");
  179. request = request.Replace("<key>fetch_folders</key><integer>1</integer>", "<key>fetch_folders</key><boolean>1</boolean>");
  180. Hashtable hash = new Hashtable();
  181. try
  182. {
  183. hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  184. }
  185. catch (LLSD.LLSDParseException e)
  186. {
  187. m_log.ErrorFormat("[INVENTORY]: Fetch error: {0}{1}" + e.Message, e.StackTrace);
  188. m_log.Error("Request: " + request);
  189. return;
  190. }
  191. catch (System.Xml.XmlException)
  192. {
  193. m_log.ErrorFormat("[INVENTORY]: XML Format error");
  194. }
  195. ArrayList foldersrequested = (ArrayList)hash["folders"];
  196. bool highPriority = false;
  197. for (int i = 0; i < foldersrequested.Count; i++)
  198. {
  199. Hashtable inventoryhash = (Hashtable)foldersrequested[i];
  200. string folder = inventoryhash["folder_id"].ToString();
  201. UUID folderID;
  202. if (UUID.TryParse(folder, out folderID))
  203. {
  204. if (!reqinfo.folders.Contains(folderID))
  205. {
  206. //TODO: Port COF handling from Avination
  207. reqinfo.folders.Add(folderID);
  208. }
  209. }
  210. }
  211. if (highPriority)
  212. m_queue.EnqueueHigh(reqinfo);
  213. else
  214. m_queue.EnqueueLow(reqinfo);
  215. };
  216. NoEvents = (x, y) =>
  217. {
  218. /*
  219. lock (requests)
  220. {
  221. Hashtable request = requests.Find(id => id["RequestID"].ToString() == x.ToString());
  222. requests.Remove(request);
  223. }
  224. */
  225. Hashtable response = new Hashtable();
  226. response["int_response_code"] = 500;
  227. response["str_response_string"] = "Script timeout";
  228. response["content_type"] = "text/plain";
  229. response["keepalive"] = false;
  230. response["reusecontext"] = false;
  231. return response;
  232. };
  233. }
  234. public void Process(aPollRequest requestinfo)
  235. {
  236. UUID requestID = requestinfo.reqID;
  237. Hashtable response = new Hashtable();
  238. response["int_response_code"] = 200;
  239. response["content_type"] = "text/plain";
  240. response["keepalive"] = false;
  241. response["reusecontext"] = false;
  242. response["str_response_string"] = m_webFetchHandler.FetchInventoryDescendentsRequest(
  243. requestinfo.request["body"].ToString(), String.Empty, String.Empty, null, null);
  244. lock (responses)
  245. responses[requestID] = response;
  246. }
  247. }
  248. private void RegisterCaps(UUID agentID, Caps caps)
  249. {
  250. RegisterFetchDescendentsCap(agentID, caps, "FetchInventoryDescendents2", m_fetchInventoryDescendents2Url);
  251. }
  252. private void RegisterFetchDescendentsCap(UUID agentID, Caps caps, string capName, string url)
  253. {
  254. string capUrl;
  255. // disable the cap clause
  256. if (url == "")
  257. {
  258. return;
  259. }
  260. // handled by the simulator
  261. else if (url == "localhost")
  262. {
  263. capUrl = "/CAPS/" + UUID.Random() + "/";
  264. // Register this as a poll service
  265. PollServiceInventoryEventArgs args = new PollServiceInventoryEventArgs(m_scene, capUrl, agentID);
  266. args.Type = PollServiceEventArgs.EventType.Inventory;
  267. caps.RegisterPollHandler(capName, args);
  268. }
  269. // external handler
  270. else
  271. {
  272. capUrl = url;
  273. IExternalCapsModule handler = m_scene.RequestModuleInterface<IExternalCapsModule>();
  274. if (handler != null)
  275. handler.RegisterExternalUserCapsHandler(agentID,caps,capName,capUrl);
  276. else
  277. caps.RegisterHandler(capName, capUrl);
  278. }
  279. // m_log.DebugFormat(
  280. // "[FETCH INVENTORY DESCENDENTS2 MODULE]: Registered capability {0} at {1} in region {2} for {3}",
  281. // capName, capUrl, m_scene.RegionInfo.RegionName, agentID);
  282. }
  283. // private void DeregisterCaps(UUID agentID, Caps caps)
  284. // {
  285. // string capUrl;
  286. //
  287. // if (m_capsDict.TryGetValue(agentID, out capUrl))
  288. // {
  289. // MainServer.Instance.RemoveHTTPHandler("", capUrl);
  290. // m_capsDict.Remove(agentID);
  291. // }
  292. // }
  293. private void DoInventoryRequests()
  294. {
  295. while (true)
  296. {
  297. Watchdog.UpdateThread();
  298. aPollRequest poolreq = m_queue.Dequeue();
  299. if (poolreq != null && poolreq.thepoll != null)
  300. poolreq.thepoll.Process(poolreq);
  301. }
  302. }
  303. }
  304. }