UserProfileCacheService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.Collections.Generic;
  28. using System.Reflection;
  29. using log4net;
  30. using OpenMetaverse;
  31. using OpenSim.Services.Interfaces;
  32. namespace OpenSim.Framework.Communications.Cache
  33. {
  34. /// <summary>
  35. /// Holds user profile information and retrieves it from backend services.
  36. /// </summary>
  37. public class UserProfileCacheService
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. /// <value>
  41. /// Standard format for names.
  42. /// </value>
  43. public const string NAME_FORMAT = "{0} {1}";
  44. /// <summary>
  45. /// The comms manager holds references to services (user, grid, inventory, etc.)
  46. /// </summary>
  47. private readonly CommunicationsManager m_commsManager;
  48. /// <summary>
  49. /// User profiles indexed by UUID
  50. /// </summary>
  51. private readonly Dictionary<UUID, CachedUserInfo> m_userProfilesById
  52. = new Dictionary<UUID, CachedUserInfo>();
  53. /// <summary>
  54. /// User profiles indexed by name
  55. /// </summary>
  56. private readonly Dictionary<string, CachedUserInfo> m_userProfilesByName
  57. = new Dictionary<string, CachedUserInfo>();
  58. /// <summary>
  59. /// The root library folder.
  60. /// </summary>
  61. public readonly InventoryFolderImpl LibraryRoot;
  62. private IInventoryService m_InventoryService;
  63. /// <summary>
  64. /// Constructor
  65. /// </summary>
  66. /// <param name="commsManager"></param>
  67. /// <param name="libraryRootFolder"></param>
  68. public UserProfileCacheService(CommunicationsManager commsManager, LibraryRootFolder libraryRootFolder)
  69. {
  70. m_commsManager = commsManager;
  71. LibraryRoot = libraryRootFolder;
  72. }
  73. public void SetInventoryService(IInventoryService invService)
  74. {
  75. m_InventoryService = invService;
  76. }
  77. /// <summary>
  78. /// A new user has moved into a region in this instance so retrieve their profile from the user service.
  79. /// </summary>
  80. ///
  81. /// It isn't strictly necessary to make this call since user data can be lazily requested later on. However,
  82. /// it might be helpful in order to avoid an initial response delay later on
  83. ///
  84. /// <param name="userID"></param>
  85. public void AddNewUser(UUID userID)
  86. {
  87. if (userID == UUID.Zero)
  88. return;
  89. //m_log.DebugFormat("[USER CACHE]: Adding user profile for {0}", userID);
  90. GetUserDetails(userID);
  91. }
  92. /// <summary>
  93. /// Remove this user's profile cache.
  94. /// </summary>
  95. /// <param name="userID"></param>
  96. /// <returns>true if the user was successfully removed, false otherwise</returns>
  97. public bool RemoveUser(UUID userId)
  98. {
  99. if (!RemoveFromCaches(userId))
  100. {
  101. m_log.WarnFormat(
  102. "[USER CACHE]: Tried to remove the profile of user {0}, but this was not in the scene", userId);
  103. return false;
  104. }
  105. return true;
  106. }
  107. /// <summary>
  108. /// Get details of the given user.
  109. /// </summary>
  110. /// If the user isn't in cache then the user is requested from the profile service.
  111. /// <param name="userID"></param>
  112. /// <returns>null if no user details are found</returns>
  113. public CachedUserInfo GetUserDetails(string fname, string lname)
  114. {
  115. lock (m_userProfilesByName)
  116. {
  117. CachedUserInfo userInfo;
  118. if (m_userProfilesByName.TryGetValue(string.Format(NAME_FORMAT, fname, lname), out userInfo))
  119. {
  120. return userInfo;
  121. }
  122. else
  123. {
  124. UserProfileData userProfile = m_commsManager.UserService.GetUserProfile(fname, lname);
  125. if (userProfile != null)
  126. {
  127. if ((userProfile.UserAssetURI == null || userProfile.UserAssetURI == "") && m_commsManager.NetworkServersInfo != null)
  128. userProfile.UserAssetURI = m_commsManager.NetworkServersInfo.AssetURL;
  129. if ((userProfile.UserInventoryURI == null || userProfile.UserInventoryURI == "") && m_commsManager.NetworkServersInfo != null)
  130. userProfile.UserInventoryURI = m_commsManager.NetworkServersInfo.InventoryURL;
  131. return AddToCaches(userProfile);
  132. }
  133. else
  134. return null;
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// Get details of the given user.
  140. /// </summary>
  141. /// If the user isn't in cache then the user is requested from the profile service.
  142. /// <param name="userID"></param>
  143. /// <returns>null if no user details are found</returns>
  144. public CachedUserInfo GetUserDetails(UUID userID)
  145. {
  146. if (userID == UUID.Zero)
  147. return null;
  148. lock (m_userProfilesById)
  149. {
  150. if (m_userProfilesById.ContainsKey(userID))
  151. {
  152. return m_userProfilesById[userID];
  153. }
  154. else
  155. {
  156. UserProfileData userProfile = m_commsManager.UserService.GetUserProfile(userID);
  157. if (userProfile != null)
  158. {
  159. if ((userProfile.UserAssetURI == null || userProfile.UserAssetURI == "") && m_commsManager.NetworkServersInfo != null)
  160. userProfile.UserAssetURI = m_commsManager.NetworkServersInfo.AssetURL;
  161. if ((userProfile.UserInventoryURI == null || userProfile.UserInventoryURI == "") && m_commsManager.NetworkServersInfo != null)
  162. userProfile.UserInventoryURI = m_commsManager.NetworkServersInfo.InventoryURL;
  163. return AddToCaches(userProfile);
  164. }
  165. else
  166. return null;
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// Update an existing profile
  172. /// </summary>
  173. /// <param name="userProfile"></param>
  174. /// <returns>true if a user profile was found to update, false otherwise</returns>
  175. // Commented out for now. The implementation needs to be improved by protecting against race conditions,
  176. // probably by making sure that the update doesn't use the UserCacheInfo.UserProfile directly (possibly via
  177. // returning a read only class from the cache).
  178. // public bool StoreProfile(UserProfileData userProfile)
  179. // {
  180. // lock (m_userProfilesById)
  181. // {
  182. // CachedUserInfo userInfo = GetUserDetails(userProfile.ID);
  183. //
  184. // if (userInfo != null)
  185. // {
  186. // userInfo.m_userProfile = userProfile;
  187. // m_commsManager.UserService.UpdateUserProfile(userProfile);
  188. //
  189. // return true;
  190. // }
  191. // }
  192. //
  193. // return false;
  194. // }
  195. /// <summary>
  196. /// Populate caches with the given user profile
  197. /// </summary>
  198. /// <param name="userProfile"></param>
  199. protected CachedUserInfo AddToCaches(UserProfileData userProfile)
  200. {
  201. CachedUserInfo createdUserInfo = new CachedUserInfo(m_InventoryService, userProfile);
  202. lock (m_userProfilesById)
  203. {
  204. m_userProfilesById[createdUserInfo.UserProfile.ID] = createdUserInfo;
  205. lock (m_userProfilesByName)
  206. {
  207. m_userProfilesByName[createdUserInfo.UserProfile.Name] = createdUserInfo;
  208. }
  209. }
  210. return createdUserInfo;
  211. }
  212. /// <summary>
  213. /// Remove profile belong to the given uuid from the caches
  214. /// </summary>
  215. /// <param name="userUuid"></param>
  216. /// <returns>true if there was a profile to remove, false otherwise</returns>
  217. protected bool RemoveFromCaches(UUID userId)
  218. {
  219. lock (m_userProfilesById)
  220. {
  221. if (m_userProfilesById.ContainsKey(userId))
  222. {
  223. CachedUserInfo userInfo = m_userProfilesById[userId];
  224. m_userProfilesById.Remove(userId);
  225. lock (m_userProfilesByName)
  226. {
  227. m_userProfilesByName.Remove(userInfo.UserProfile.Name);
  228. }
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. /// <summary>
  235. /// Preloads User data into the region cache. Modules may use this service to add non-standard clients
  236. /// </summary>
  237. /// <param name="userData"></param>
  238. public void PreloadUserCache(UserProfileData userData)
  239. {
  240. AddToCaches(userData);
  241. }
  242. }
  243. }