GetAssetsModule.cs 19 KB

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