InventoryCache.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.Generic;
  29. using System.Reflection;
  30. using OpenSim.Framework;
  31. using OpenSim.Framework.Client;
  32. using OpenSim.Region.Framework.Scenes;
  33. using OpenMetaverse;
  34. using Nini.Config;
  35. using log4net;
  36. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
  37. {
  38. public class InventoryCache
  39. {
  40. private static readonly ILog m_log =
  41. LogManager.GetLogger(
  42. MethodBase.GetCurrentMethod().DeclaringType);
  43. protected BaseInventoryConnector m_Connector;
  44. protected List<Scene> m_Scenes;
  45. // The cache proper
  46. protected Dictionary<UUID, Dictionary<AssetType, InventoryFolderBase>> m_InventoryCache;
  47. public virtual void Init(IConfigSource source, BaseInventoryConnector connector)
  48. {
  49. m_Scenes = new List<Scene>();
  50. m_InventoryCache = new Dictionary<UUID, Dictionary<AssetType, InventoryFolderBase>>();
  51. m_Connector = connector;
  52. }
  53. public virtual void AddRegion(Scene scene)
  54. {
  55. m_Scenes.Add(scene);
  56. scene.EventManager.OnMakeRootAgent += OnMakeRootAgent;
  57. scene.EventManager.OnClientClosed += OnClientClosed;
  58. }
  59. public virtual void RemoveRegion(Scene scene)
  60. {
  61. if ((m_Scenes != null) && m_Scenes.Contains(scene))
  62. {
  63. m_Scenes.Remove(scene);
  64. }
  65. }
  66. void OnMakeRootAgent(ScenePresence presence)
  67. {
  68. // Get system folders
  69. // First check if they're here already
  70. lock (m_InventoryCache)
  71. {
  72. if (m_InventoryCache.ContainsKey(presence.UUID))
  73. {
  74. m_log.DebugFormat("[INVENTORY CACHE]: OnMakeRootAgent, system folders for {0} {1} already in cache", presence.Firstname, presence.Lastname);
  75. return;
  76. }
  77. }
  78. // If not, go get them and place them in the cache
  79. Dictionary<AssetType, InventoryFolderBase> folders = m_Connector.GetSystemFolders(presence.UUID);
  80. m_log.DebugFormat("[INVENTORY CACHE]: OnMakeRootAgent in {0}, fetched system folders for {1} {2}: count {3}",
  81. presence.Scene.RegionInfo.RegionName, presence.Firstname, presence.Lastname, folders.Count);
  82. if (folders.Count > 0)
  83. lock (m_InventoryCache)
  84. m_InventoryCache.Add(presence.UUID, folders);
  85. }
  86. void OnClientClosed(UUID clientID, Scene scene)
  87. {
  88. if (m_InventoryCache.ContainsKey(clientID)) // if it's still in cache
  89. {
  90. ScenePresence sp = null;
  91. foreach (Scene s in m_Scenes)
  92. {
  93. s.TryGetAvatar(clientID, out sp);
  94. if ((sp != null) && !sp.IsChildAgent && (s != scene))
  95. {
  96. m_log.DebugFormat("[INVENTORY CACHE]: OnClientClosed in {0}, but user {1} still in sim. Keeping system folders in cache",
  97. scene.RegionInfo.RegionName, clientID);
  98. return;
  99. }
  100. }
  101. // Drop system folders
  102. lock (m_InventoryCache)
  103. if (m_InventoryCache.ContainsKey(clientID))
  104. {
  105. m_log.DebugFormat("[INVENTORY CACHE]: OnClientClosed in {0}, user {1} out of sim. Dropping system folders",
  106. scene.RegionInfo.RegionName, clientID);
  107. m_InventoryCache.Remove(clientID);
  108. }
  109. }
  110. }
  111. public InventoryFolderBase GetFolderForType(UUID userID, AssetType type)
  112. {
  113. Dictionary<AssetType, InventoryFolderBase> folders = null;
  114. lock (m_InventoryCache)
  115. {
  116. m_InventoryCache.TryGetValue(userID, out folders);
  117. }
  118. if ((folders != null) && folders.ContainsKey(type))
  119. {
  120. return folders[type];
  121. }
  122. return null;
  123. }
  124. }
  125. }