GetAssetsModule.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 Thread[] m_workerThreads = null;
  73. private static int m_NumberScenes = 0;
  74. private static BlockingCollection<APollRequest> m_queue = new BlockingCollection<APollRequest>();
  75. private static object m_loadLock = new object();
  76. protected IUserManagement m_UserManagement = null;
  77. #region Region Module interfaceBase Members
  78. public Type ReplaceableInterface
  79. {
  80. get { return null; }
  81. }
  82. public void Initialise(IConfigSource source)
  83. {
  84. IConfig config = source.Configs["ClientStack.LindenCaps"];
  85. if (config == null)
  86. return;
  87. m_GetTextureURL = config.GetString("Cap_GetTexture", string.Empty);
  88. if (m_GetTextureURL != string.Empty)
  89. m_Enabled = true;
  90. m_GetMeshURL = config.GetString("Cap_GetMesh", string.Empty);
  91. if (m_GetMeshURL != string.Empty)
  92. m_Enabled = true;
  93. m_GetMesh2URL = config.GetString("Cap_GetMesh2", string.Empty);
  94. if (m_GetMesh2URL != string.Empty)
  95. m_Enabled = true;
  96. m_GetAssetURL = config.GetString("Cap_GetAsset", string.Empty);
  97. if (m_GetAssetURL != string.Empty)
  98. m_Enabled = true;
  99. }
  100. public void AddRegion(Scene pScene)
  101. {
  102. if (!m_Enabled)
  103. return;
  104. m_scene = pScene;
  105. }
  106. public void RemoveRegion(Scene s)
  107. {
  108. if (!m_Enabled)
  109. return;
  110. s.EventManager.OnRegisterCaps -= RegisterCaps;
  111. m_NumberScenes--;
  112. m_scene = null;
  113. }
  114. public void RegionLoaded(Scene s)
  115. {
  116. if (!m_Enabled)
  117. return;
  118. lock(m_loadLock)
  119. {
  120. if (m_assetService == null)
  121. {
  122. m_assetService = s.RequestModuleInterface<IAssetService>();
  123. // We'll reuse the same handler for all requests.
  124. m_getAssetHandler = new GetAssetsHandler(m_assetService);
  125. }
  126. if (m_assetService == null)
  127. {
  128. m_Enabled = false;
  129. return;
  130. }
  131. if(m_UserManagement == null)
  132. m_UserManagement = s.RequestModuleInterface<IUserManagement>();
  133. s.EventManager.OnRegisterCaps += RegisterCaps;
  134. m_NumberScenes++;
  135. if (m_workerThreads == null)
  136. {
  137. m_workerThreads = new Thread[3];
  138. for (uint i = 0; i < 3; i++)
  139. {
  140. m_workerThreads[i] = WorkManager.StartThread(DoAssetRequests,
  141. String.Format("GetCapsAssetWorker{0}", i),
  142. ThreadPriority.Normal,
  143. true,
  144. false,
  145. null,
  146. int.MaxValue);
  147. }
  148. }
  149. }
  150. }
  151. public void Close()
  152. {
  153. if(m_NumberScenes <= 0 && m_workerThreads != null)
  154. {
  155. m_log.DebugFormat("[GetAssetsModule] Closing");
  156. foreach (Thread t in m_workerThreads)
  157. Watchdog.AbortThread(t.ManagedThreadId);
  158. // This will fail on region shutdown. Its harmless.
  159. // Prevent red ink.
  160. try
  161. {
  162. m_queue.Dispose();
  163. }
  164. catch {}
  165. }
  166. }
  167. public string Name { get { return "GetAssetsModule"; } }
  168. #endregion
  169. private static void DoAssetRequests()
  170. {
  171. try
  172. {
  173. while (m_NumberScenes > 0)
  174. {
  175. APollRequest poolreq;
  176. if (m_queue.TryTake(out poolreq, 4500))
  177. {
  178. if (m_NumberScenes <= 0)
  179. break;
  180. Watchdog.UpdateThread();
  181. if (poolreq.reqID != UUID.Zero)
  182. poolreq.thepoll.Process(poolreq);
  183. poolreq = null;
  184. }
  185. Watchdog.UpdateThread();
  186. }
  187. }
  188. catch (ThreadAbortException)
  189. {
  190. Thread.ResetAbort();
  191. }
  192. }
  193. private class PollServiceAssetEventArgs : PollServiceEventArgs
  194. {
  195. //private List<Hashtable> requests = new List<Hashtable>();
  196. private List<OSHttpRequest> requests = new List<OSHttpRequest>();
  197. private Dictionary<UUID, APollResponse> responses =new Dictionary<UUID, APollResponse>();
  198. private HashSet<UUID> dropedResponses = new HashSet<UUID>();
  199. private Scene m_scene;
  200. private string m_hgassets = null;
  201. public PollServiceAssetEventArgs(string uri, UUID pId, Scene scene, string HGAssetSVC) :
  202. base(null, uri, null, null, null, null, pId, int.MaxValue)
  203. {
  204. m_scene = scene;
  205. m_hgassets = HGAssetSVC;
  206. HasEvents = (requestID, agentID) =>
  207. {
  208. lock (responses)
  209. {
  210. APollResponse response;
  211. if (responses.TryGetValue(requestID, out response))
  212. {
  213. ScenePresence sp = m_scene.GetScenePresence(pId);
  214. if (sp == null || sp.IsDeleted)
  215. return true;
  216. OSHttpResponse resp = response.osresponse;
  217. if(Util.GetTimeStamp() - resp.RequestTS > (resp.RawBufferLen > 2000000 ? 10 : 5))
  218. return sp.CapCanSendAsset(2, resp.RawBufferLen);
  219. if (resp.Priority > 0)
  220. return sp.CapCanSendAsset(resp.Priority, resp.RawBufferLen);
  221. return sp.CapCanSendAsset(2, resp.RawBufferLen);
  222. }
  223. return false;
  224. }
  225. };
  226. Drop = (requestID, y) =>
  227. {
  228. lock (responses)
  229. {
  230. responses.Remove(requestID);
  231. lock(dropedResponses)
  232. dropedResponses.Add(requestID);
  233. }
  234. };
  235. GetEvents = (requestID, y) =>
  236. {
  237. lock (responses)
  238. {
  239. try
  240. {
  241. OSHttpResponse response = responses[requestID].osresponse;
  242. if (response.Priority < 0)
  243. response.Priority = 0;
  244. Hashtable lixo = new Hashtable(1);
  245. lixo["h"] = response;
  246. return lixo;
  247. }
  248. finally
  249. {
  250. responses.Remove(requestID);
  251. }
  252. }
  253. };
  254. // x is request id, y is request data hashtable
  255. Request = (requestID, request) =>
  256. {
  257. APollRequest reqinfo = new APollRequest();
  258. reqinfo.thepoll = this;
  259. reqinfo.reqID = requestID;
  260. reqinfo.request = request;
  261. m_queue.Add(reqinfo);
  262. return null;
  263. };
  264. // this should never happen except possible on shutdown
  265. NoEvents = (x, y) =>
  266. {
  267. /*
  268. lock (requests)
  269. {
  270. Hashtable request = requests.Find(id => id["RequestID"].ToString() == x.ToString());
  271. requests.Remove(request);
  272. }
  273. */
  274. Hashtable response = new Hashtable();
  275. response["int_response_code"] = 500;
  276. response["str_response_string"] = "timeout";
  277. response["content_type"] = "text/plain";
  278. response["keepalive"] = false;
  279. return response;
  280. };
  281. }
  282. public void Process(APollRequest requestinfo)
  283. {
  284. UUID requestID = requestinfo.reqID;
  285. if(m_scene.ShuttingDown)
  286. return;
  287. lock(responses)
  288. {
  289. lock(dropedResponses)
  290. {
  291. if(dropedResponses.Contains(requestID))
  292. {
  293. dropedResponses.Remove(requestID);
  294. return;
  295. }
  296. }
  297. /* can't do this with current viewers; HG problem
  298. // If the avatar is gone, don't bother to get the texture
  299. if(m_scene.GetScenePresence(Id) == null)
  300. {
  301. curresponse = new Hashtable();
  302. curresponse["int_response_code"] = 500;
  303. curresponse["str_response_string"] = "timeout";
  304. curresponse["content_type"] = "text/plain";
  305. curresponse["keepalive"] = false;
  306. responses[requestID] = new APollResponse() { bytes = 0, response = curresponse };
  307. return;
  308. }
  309. */
  310. }
  311. OSHttpResponse response = new OSHttpResponse(requestinfo.request);
  312. m_getAssetHandler.Handle(requestinfo.request, response, m_hgassets);
  313. lock(responses)
  314. {
  315. lock(dropedResponses)
  316. {
  317. if(dropedResponses.Contains(requestID))
  318. {
  319. dropedResponses.Remove(requestID);
  320. return;
  321. }
  322. }
  323. APollResponse preq= new APollResponse()
  324. {
  325. osresponse = response
  326. };
  327. responses[requestID] = preq;
  328. }
  329. }
  330. }
  331. public void RegisterCaps(UUID agentID, Caps caps)
  332. {
  333. string hostName = m_scene.RegionInfo.ExternalHostName;
  334. uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port;
  335. string protocol = "http";
  336. if (MainServer.Instance.UseSSL)
  337. {
  338. hostName = MainServer.Instance.SSLCommonName;
  339. port = MainServer.Instance.SSLPort;
  340. protocol = "https";
  341. }
  342. string hgassets = null;
  343. if(m_UserManagement != null)
  344. hgassets = m_UserManagement.GetUserServerURL(agentID, "AssetServerURI");
  345. IExternalCapsModule handler = m_scene.RequestModuleInterface<IExternalCapsModule>();
  346. string baseURL = String.Format("{0}://{1}:{2}", protocol, hostName, port);
  347. if (m_GetTextureURL == "localhost")
  348. {
  349. string capUrl = "/" + UUID.Random();
  350. // Register this as a poll service
  351. PollServiceAssetEventArgs args = new PollServiceAssetEventArgs(capUrl, agentID, m_scene, hgassets);
  352. if (handler != null)
  353. handler.RegisterExternalUserCapsHandler(agentID, caps, "GetTexture", capUrl);
  354. else
  355. caps.RegisterPollHandler("GetTexture", args);
  356. }
  357. else
  358. {
  359. caps.RegisterHandler("GetTexture", m_GetTextureURL);
  360. }
  361. //GetMesh
  362. if (m_GetMeshURL == "localhost")
  363. {
  364. string capUrl = "/" + UUID.Random();
  365. PollServiceAssetEventArgs args = new PollServiceAssetEventArgs(capUrl, agentID, m_scene, hgassets);
  366. if (handler != null)
  367. handler.RegisterExternalUserCapsHandler(agentID, caps, "GetMesh", capUrl);
  368. else
  369. caps.RegisterPollHandler("GetMesh", args);
  370. }
  371. else if (m_GetMeshURL != string.Empty)
  372. caps.RegisterHandler("GetMesh", m_GetMeshURL);
  373. //GetMesh2
  374. if (m_GetMesh2URL == "localhost")
  375. {
  376. string capUrl = "/" + UUID.Random();
  377. PollServiceAssetEventArgs args = new PollServiceAssetEventArgs(capUrl, agentID, m_scene, hgassets);
  378. if (handler != null)
  379. handler.RegisterExternalUserCapsHandler(agentID, caps, "GetMesh2", capUrl);
  380. else
  381. caps.RegisterPollHandler("GetMesh2", args);
  382. }
  383. else if (m_GetMesh2URL != string.Empty)
  384. caps.RegisterHandler("GetMesh2", m_GetMesh2URL);
  385. //ViewerAsset
  386. if (m_GetAssetURL == "localhost")
  387. {
  388. string capUrl = "/" + UUID.Random();
  389. PollServiceAssetEventArgs args = new PollServiceAssetEventArgs(capUrl, agentID, m_scene, hgassets);
  390. if (handler != null)
  391. handler.RegisterExternalUserCapsHandler(agentID, caps, "ViewerAsset", capUrl);
  392. else
  393. caps.RegisterPollHandler("ViewerAsset", args);
  394. }
  395. else if (m_GetAssetURL != string.Empty)
  396. caps.RegisterHandler("ViewerAsset", m_GetAssetURL);
  397. }
  398. }
  399. }