1
0

GetAssetsModule.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 Mono.Addins;
  34. using OpenSim.Framework.Monitoring;
  35. using log4net;
  36. using Nini.Config;
  37. using OpenMetaverse;
  38. using OpenSim.Capabilities.Handlers;
  39. using OpenSim.Framework;
  40. using OpenSim.Framework.Servers;
  41. using OpenSim.Framework.Servers.HttpServer;
  42. using OpenSim.Region.Framework.Interfaces;
  43. using OpenSim.Region.Framework.Scenes;
  44. using OpenSim.Services.Interfaces;
  45. using Caps = OpenSim.Framework.Capabilities.Caps;
  46. namespace OpenSim.Region.ClientStack.Linden
  47. {
  48. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GetAssetsModule")]
  49. public class GetAssetsModule : INonSharedRegionModule
  50. {
  51. // private static readonly ILog m_log =
  52. // LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. private Scene m_scene;
  54. private bool m_Enabled;
  55. private string m_GetTextureURL;
  56. private string m_GetMeshURL;
  57. private string m_GetMesh2URL;
  58. private string m_GetAssetURL;
  59. class APollRequest
  60. {
  61. public PollServiceAssetEventArgs thepoll;
  62. public UUID reqID;
  63. public OSHttpRequest request;
  64. }
  65. public class APollResponse
  66. {
  67. public OSHttpResponse osresponse;
  68. }
  69. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  70. private static IAssetService m_assetService = null;
  71. private static GetAssetsHandler m_getAssetHandler;
  72. private static ObjectJobEngine m_workerpool = null;
  73. private static int m_NumberScenes = 0;
  74. private static object m_loadLock = new object();
  75. protected IUserManagement m_UserManagement = null;
  76. #region Region Module interfaceBase Members
  77. public Type ReplaceableInterface
  78. {
  79. get { return null; }
  80. }
  81. public void Initialise(IConfigSource source)
  82. {
  83. IConfig config = source.Configs["ClientStack.LindenCaps"];
  84. if (config == null)
  85. return;
  86. m_GetTextureURL = config.GetString("Cap_GetTexture", string.Empty);
  87. if (m_GetTextureURL != string.Empty)
  88. m_Enabled = true;
  89. m_GetMeshURL = config.GetString("Cap_GetMesh", string.Empty);
  90. if (m_GetMeshURL != string.Empty)
  91. m_Enabled = true;
  92. m_GetMesh2URL = config.GetString("Cap_GetMesh2", string.Empty);
  93. if (m_GetMesh2URL != string.Empty)
  94. m_Enabled = true;
  95. m_GetAssetURL = config.GetString("Cap_GetAsset", string.Empty);
  96. if (m_GetAssetURL != string.Empty)
  97. m_Enabled = true;
  98. }
  99. public void AddRegion(Scene pScene)
  100. {
  101. if (!m_Enabled)
  102. return;
  103. m_scene = pScene;
  104. }
  105. public void RemoveRegion(Scene s)
  106. {
  107. if (!m_Enabled)
  108. return;
  109. s.EventManager.OnRegisterCaps -= RegisterCaps;
  110. m_NumberScenes--;
  111. m_scene = null;
  112. }
  113. public void RegionLoaded(Scene s)
  114. {
  115. if (!m_Enabled)
  116. return;
  117. lock(m_loadLock)
  118. {
  119. if (m_assetService == null)
  120. {
  121. m_assetService = s.RequestModuleInterface<IAssetService>();
  122. // We'll reuse the same handler for all requests.
  123. m_getAssetHandler = new GetAssetsHandler(m_assetService);
  124. }
  125. if (m_assetService == null)
  126. {
  127. m_Enabled = false;
  128. return;
  129. }
  130. if(m_UserManagement == null)
  131. m_UserManagement = s.RequestModuleInterface<IUserManagement>();
  132. s.EventManager.OnRegisterCaps += RegisterCaps;
  133. m_NumberScenes++;
  134. if (m_workerpool == null)
  135. m_workerpool = new ObjectJobEngine(DoAssetRequests, "GetCapsAssetWorker", 1000, 3);
  136. }
  137. }
  138. public void Close()
  139. {
  140. if(m_NumberScenes <= 0 && m_workerpool != null)
  141. {
  142. m_workerpool.Dispose();
  143. m_workerpool = null;
  144. }
  145. }
  146. public string Name { get { return "GetAssetsModule"; } }
  147. #endregion
  148. private static void DoAssetRequests(object o)
  149. {
  150. if (m_NumberScenes <= 0)
  151. return;
  152. APollRequest poolreq = o as APollRequest;
  153. if (poolreq != null && !poolreq.reqID.IsZero())
  154. poolreq.thepoll.Process(poolreq);
  155. }
  156. private class PollServiceAssetEventArgs : PollServiceEventArgs
  157. {
  158. //private List<Hashtable> requests = new List<Hashtable>();
  159. private List<OSHttpRequest> requests = new List<OSHttpRequest>();
  160. private Dictionary<UUID, APollResponse> responses =new Dictionary<UUID, APollResponse>();
  161. private HashSet<UUID> dropedResponses = new HashSet<UUID>();
  162. private Scene m_scene;
  163. private string m_hgassets = null;
  164. public PollServiceAssetEventArgs(string uri, UUID pId, Scene scene, string HGAssetSVC) :
  165. base(null, uri, null, null, null, null, pId, int.MaxValue)
  166. {
  167. m_scene = scene;
  168. m_hgassets = HGAssetSVC;
  169. HasEvents = (requestID, agentID) =>
  170. {
  171. lock (responses)
  172. {
  173. return responses.ContainsKey(requestID);
  174. }
  175. };
  176. Drop = (requestID, y) =>
  177. {
  178. lock (responses)
  179. {
  180. responses.Remove(requestID);
  181. lock(dropedResponses)
  182. dropedResponses.Add(requestID);
  183. }
  184. };
  185. GetEvents = (requestID, y) =>
  186. {
  187. lock (responses)
  188. {
  189. try
  190. {
  191. OSHttpResponse response = responses[requestID].osresponse;
  192. if (response.Priority < 0)
  193. response.Priority = 0;
  194. Hashtable lixo = new Hashtable(1);
  195. lixo["h"] = response;
  196. return lixo;
  197. }
  198. finally
  199. {
  200. responses.Remove(requestID);
  201. }
  202. }
  203. };
  204. // x is request id, y is request data hashtable
  205. Request = (requestID, request) =>
  206. {
  207. APollRequest reqinfo = new APollRequest();
  208. reqinfo.thepoll = this;
  209. reqinfo.reqID = requestID;
  210. reqinfo.request = request;
  211. m_workerpool.Enqueue(reqinfo);
  212. return null;
  213. };
  214. // this should never happen except possible on shutdown
  215. NoEvents = (x, y) =>
  216. {
  217. /*
  218. lock (requests)
  219. {
  220. Hashtable request = requests.Find(id => id["RequestID"].ToString() == x.ToString());
  221. requests.Remove(request);
  222. }
  223. */
  224. Hashtable response = new Hashtable();
  225. response["int_response_code"] = 500;
  226. response["str_response_string"] = "timeout";
  227. response["content_type"] = "text/plain";
  228. response["keepalive"] = false;
  229. return response;
  230. };
  231. }
  232. public void Process(APollRequest requestinfo)
  233. {
  234. UUID requestID = requestinfo.reqID;
  235. if(m_scene.ShuttingDown)
  236. return;
  237. lock(responses)
  238. {
  239. lock(dropedResponses)
  240. {
  241. if(dropedResponses.Contains(requestID))
  242. {
  243. dropedResponses.Remove(requestID);
  244. return;
  245. }
  246. }
  247. /* can't do this with current viewers; HG problem
  248. // If the avatar is gone, don't bother to get the texture
  249. if(m_scene.GetScenePresence(Id) == null)
  250. {
  251. curresponse = new Hashtable();
  252. curresponse["int_response_code"] = 500;
  253. curresponse["str_response_string"] = "timeout";
  254. curresponse["content_type"] = "text/plain";
  255. curresponse["keepalive"] = false;
  256. responses[requestID] = new APollResponse() { bytes = 0, response = curresponse };
  257. return;
  258. }
  259. */
  260. }
  261. OSHttpResponse response = new OSHttpResponse(requestinfo.request);
  262. m_getAssetHandler.Handle(requestinfo.request, response, m_hgassets);
  263. lock(responses)
  264. {
  265. lock(dropedResponses)
  266. {
  267. if(dropedResponses.Contains(requestID))
  268. {
  269. dropedResponses.Remove(requestID);
  270. return;
  271. }
  272. }
  273. APollResponse preq= new APollResponse()
  274. {
  275. osresponse = response
  276. };
  277. responses[requestID] = preq;
  278. }
  279. }
  280. }
  281. public void RegisterCaps(UUID agentID, Caps caps)
  282. {
  283. /*
  284. string hostName = m_scene.RegionInfo.ExternalHostName;
  285. uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port;
  286. string protocol = "http";
  287. if (MainServer.Instance.UseSSL)
  288. {
  289. hostName = MainServer.Instance.SSLCommonName;
  290. port = MainServer.Instance.SSLPort;
  291. protocol = "https";
  292. }
  293. string baseURL = String.Format("{0}://{1}:{2}", protocol, hostName, port);
  294. */
  295. string hgassets = null;
  296. if(m_UserManagement != null)
  297. hgassets = m_UserManagement.GetUserServerURL(agentID, "AssetServerURI");
  298. IExternalCapsModule handler = m_scene.RequestModuleInterface<IExternalCapsModule>();
  299. if (m_GetTextureURL.Equals("localhost"))
  300. {
  301. string capUrl = "/" + UUID.Random();
  302. if (handler != null)
  303. handler.RegisterExternalUserCapsHandler(agentID, caps, "GetTexture", capUrl);
  304. else
  305. caps.RegisterPollHandler("GetTexture", new PollServiceAssetEventArgs(capUrl, agentID, m_scene, hgassets));
  306. }
  307. else
  308. {
  309. caps.RegisterHandler("GetTexture", m_GetTextureURL);
  310. }
  311. //GetMesh
  312. if (m_GetMeshURL.Equals("localhost"))
  313. {
  314. string capUrl = "/" + UUID.Random();
  315. if (handler != null)
  316. handler.RegisterExternalUserCapsHandler(agentID, caps, "GetMesh", capUrl);
  317. else
  318. caps.RegisterPollHandler("GetMesh", new PollServiceAssetEventArgs(capUrl, agentID, m_scene, hgassets));
  319. }
  320. else if (!string.IsNullOrEmpty(m_GetMeshURL))
  321. caps.RegisterHandler("GetMesh", m_GetMeshURL);
  322. //GetMesh2
  323. if (m_GetMesh2URL.Equals("localhost"))
  324. {
  325. string capUrl = "/" + UUID.Random();
  326. if (handler != null)
  327. handler.RegisterExternalUserCapsHandler(agentID, caps, "GetMesh2", capUrl);
  328. else
  329. caps.RegisterPollHandler("GetMesh2", new PollServiceAssetEventArgs(capUrl, agentID, m_scene, hgassets));
  330. }
  331. else if (!string.IsNullOrEmpty(m_GetMesh2URL))
  332. caps.RegisterHandler("GetMesh2", m_GetMesh2URL);
  333. //ViewerAsset
  334. if (m_GetAssetURL.Equals("localhost"))
  335. {
  336. string capUrl = "/" + UUID.Random();
  337. if (handler != null)
  338. handler.RegisterExternalUserCapsHandler(agentID, caps, "ViewerAsset", capUrl);
  339. else
  340. caps.RegisterPollHandler("ViewerAsset", new PollServiceAssetEventArgs(capUrl, agentID, m_scene, hgassets));
  341. }
  342. else if (!string.IsNullOrEmpty(m_GetAssetURL))
  343. caps.RegisterHandler("ViewerAsset", m_GetAssetURL);
  344. }
  345. }
  346. }