UserProfileCacheService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 OpenSim 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.Threading;
  30. using libsecondlife;
  31. using OpenSim.Framework.Console;
  32. namespace OpenSim.Framework.Communications.Cache
  33. {
  34. public class UserProfileCacheService
  35. {
  36. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  37. // Fields
  38. private readonly CommunicationsManager m_parent;
  39. private readonly Dictionary<LLUUID, CachedUserInfo> m_userProfiles = new Dictionary<LLUUID, CachedUserInfo>();
  40. public LibraryRootFolder libraryRoot = new LibraryRootFolder();
  41. // Methods
  42. public UserProfileCacheService(CommunicationsManager parent)
  43. {
  44. m_parent = parent;
  45. }
  46. /// <summary>
  47. /// A new user has moved into a region in this instance
  48. /// so get info from servers
  49. /// </summary>
  50. /// <param name="userID"></param>
  51. public void AddNewUser(LLUUID userID)
  52. {
  53. // Potential fix - Multithreading issue.
  54. lock (m_userProfiles)
  55. {
  56. if (!m_userProfiles.ContainsKey(userID))
  57. {
  58. CachedUserInfo userInfo = new CachedUserInfo(m_parent);
  59. userInfo.UserProfile = m_parent.UserService.GetUserProfile(userID);
  60. if (userInfo.UserProfile != null)
  61. {
  62. // The inventory will be populated when the user actually enters the scene
  63. m_userProfiles.Add(userID, userInfo);
  64. }
  65. else
  66. {
  67. m_log.ErrorFormat("[USER CACHE]: User profile for user {0} not found", userID);
  68. }
  69. }
  70. }
  71. }
  72. public void UpdateUserInventory(LLUUID userID)
  73. {
  74. CachedUserInfo userInfo = GetUserDetails(userID);
  75. if (userInfo != null)
  76. {
  77. RequestInventoryForUser(userID, userInfo);
  78. }
  79. }
  80. public CachedUserInfo GetUserDetails(LLUUID userID)
  81. {
  82. if (m_userProfiles.ContainsKey(userID))
  83. return m_userProfiles[userID];
  84. else
  85. return null;
  86. }
  87. public void HandleCreateInventoryFolder(IClientAPI remoteClient, LLUUID folderID, ushort folderType,
  88. string folderName, LLUUID parentID)
  89. {
  90. CachedUserInfo userProfile;
  91. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  92. {
  93. if (userProfile.RootFolder != null)
  94. {
  95. if (userProfile.RootFolder.folderID == parentID)
  96. {
  97. InventoryFolderImpl createdFolder =
  98. userProfile.RootFolder.CreateNewSubFolder(folderID, folderName, folderType);
  99. if (createdFolder != null)
  100. {
  101. InventoryFolderBase createdBaseFolder = new InventoryFolderBase();
  102. createdBaseFolder.agentID = createdFolder.agentID;
  103. createdBaseFolder.folderID = createdFolder.folderID;
  104. createdBaseFolder.name = createdFolder.name;
  105. createdBaseFolder.parentID = createdFolder.parentID;
  106. createdBaseFolder.type = createdFolder.type;
  107. createdBaseFolder.version = createdFolder.version;
  108. m_parent.InventoryService.AddNewInventoryFolder(remoteClient.AgentId, createdBaseFolder);
  109. }
  110. }
  111. else
  112. {
  113. InventoryFolderImpl folder = userProfile.RootFolder.HasSubFolder(parentID);
  114. if (folder != null)
  115. {
  116. folder.CreateNewSubFolder(folderID, folderName, folderType);
  117. }
  118. }
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// Handle a client request to update the inventory folder
  124. ///
  125. /// FIXME: We call add new inventory folder because in the data layer, we happen to use an SQL REPLACE
  126. /// so this will work to rename an existing folder. Needless to say, to rely on this is very confusing,
  127. /// and needs to be changed.
  128. /// </summary>
  129. /// <param name="remoteClient"></param>
  130. /// <param name="folderID"></param>
  131. /// <param name="type"></param>
  132. /// <param name="name"></param>
  133. /// <param name="parentID"></param>
  134. public void HandleUpdateInventoryFolder(IClientAPI remoteClient, LLUUID folderID, ushort type, string name,
  135. LLUUID parentID)
  136. {
  137. CachedUserInfo userProfile;
  138. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  139. {
  140. if (userProfile.RootFolder != null)
  141. {
  142. InventoryFolderBase baseFolder = new InventoryFolderBase();
  143. baseFolder.agentID = remoteClient.AgentId;
  144. baseFolder.folderID = folderID;
  145. baseFolder.name = name;
  146. baseFolder.parentID = parentID;
  147. baseFolder.type = (short) type;
  148. baseFolder.version = userProfile.RootFolder.version;
  149. m_parent.InventoryService.AddNewInventoryFolder(remoteClient.AgentId, baseFolder);
  150. }
  151. }
  152. }
  153. public void HandleMoveInventoryFolder(IClientAPI remoteClient, LLUUID folderID, LLUUID parentID)
  154. {
  155. CachedUserInfo userProfile;
  156. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  157. {
  158. if (userProfile.RootFolder != null)
  159. {
  160. InventoryFolderBase baseFolder = new InventoryFolderBase();
  161. baseFolder.agentID = remoteClient.AgentId;
  162. baseFolder.folderID = folderID;
  163. baseFolder.parentID = parentID;
  164. m_parent.InventoryService.MoveInventoryFolder(remoteClient.AgentId, baseFolder);
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// Tell the client about the various child items and folders contained in the requested folder.
  170. /// </summary>
  171. /// <param name="remoteClient"></param>
  172. /// <param name="folderID"></param>
  173. /// <param name="ownerID"></param>
  174. /// <param name="fetchFolders"></param>
  175. /// <param name="fetchItems"></param>
  176. /// <param name="sortOrder"></param>
  177. public void HandleFetchInventoryDescendents(IClientAPI remoteClient, LLUUID folderID, LLUUID ownerID,
  178. bool fetchFolders, bool fetchItems, int sortOrder)
  179. {
  180. // XXX We're not handling sortOrder yet!
  181. InventoryFolderImpl fold = null;
  182. if (folderID == libraryRoot.folderID)
  183. {
  184. remoteClient.SendInventoryFolderDetails(
  185. libraryRoot.agentID, libraryRoot.folderID, libraryRoot.RequestListOfItems(),
  186. libraryRoot.RequestListOfFolders(), fetchFolders, fetchItems);
  187. return;
  188. }
  189. if ((fold = libraryRoot.HasSubFolder(folderID)) != null)
  190. {
  191. remoteClient.SendInventoryFolderDetails(
  192. libraryRoot.agentID, folderID, fold.RequestListOfItems(),
  193. fold.RequestListOfFolders(), fetchFolders, fetchItems);
  194. return;
  195. }
  196. CachedUserInfo userProfile;
  197. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  198. {
  199. // XXX: When a client crosses into a scene, their entire inventory is fetched
  200. // asynchronously. However, if the client is logging on and does not have a cached root
  201. // folder, then the root folder request usually comes in *before* the async completes, leading to
  202. // inventory failure.
  203. //
  204. // This is a crude way of dealing with that by retrying the lookup.
  205. if (userProfile.RootFolder == null)
  206. {
  207. int attempts = 5;
  208. while (attempts-- > 0)
  209. {
  210. Thread.Sleep(3000);
  211. if (userProfile.RootFolder != null)
  212. {
  213. break;
  214. }
  215. }
  216. }
  217. if (userProfile.RootFolder != null)
  218. {
  219. if (userProfile.RootFolder.folderID == folderID)
  220. {
  221. // m_log.DebugFormat(
  222. // "[AGENT INVENTORY]: Found root folder {0} for client {1}",
  223. // folderID, remoteClient.AgentId);
  224. remoteClient.SendInventoryFolderDetails(
  225. remoteClient.AgentId, folderID, userProfile.RootFolder.RequestListOfItems(),
  226. userProfile.RootFolder.RequestListOfFolders(),
  227. fetchFolders, fetchItems);
  228. return;
  229. }
  230. else
  231. {
  232. if ((fold = userProfile.RootFolder.HasSubFolder(folderID)) != null)
  233. {
  234. // m_log.DebugFormat(
  235. // "[AGENT INVENTORY]: Found folder {0} for client {1}",
  236. // folderID, remoteClient.AgentId);
  237. remoteClient.SendInventoryFolderDetails(
  238. remoteClient.AgentId, folderID, fold.RequestListOfItems(),
  239. fold.RequestListOfFolders(), fetchFolders, fetchItems);
  240. return;
  241. }
  242. }
  243. }
  244. else
  245. {
  246. m_log.ErrorFormat("[INVENTORY CACHE]: Could not find root folder for user {0}", remoteClient.Name);
  247. return;
  248. }
  249. }
  250. else
  251. {
  252. m_log.ErrorFormat("[INVENTORY CACHE]: Could not find user profile for {0} for folder {1}",
  253. remoteClient.Name, folderID);
  254. return;
  255. }
  256. // If we've reached this point then we couldn't find the folder, even though the client thinks
  257. // it exists
  258. m_log.ErrorFormat("[INVENTORY CACHE]: Could not find folder {0} for user {1}",
  259. folderID, remoteClient.Name);
  260. }
  261. public List<InventoryItemBase> HandleFetchInventoryDescendentsCAPS(LLUUID agentID, LLUUID folderID, LLUUID ownerID,
  262. bool fetchFolders, bool fetchItems, int sortOrder)
  263. {
  264. // XXX We're not handling sortOrder yet!
  265. // with CAPS we are only return items in the folders at the moment
  266. // need to find the format that sub folder details are sent in
  267. // if (fetchItems)
  268. // {
  269. InventoryFolderImpl fold = null;
  270. if (folderID == libraryRoot.folderID)
  271. {
  272. return libraryRoot.RequestListOfItems();
  273. }
  274. if ((fold = libraryRoot.HasSubFolder(folderID)) != null)
  275. {
  276. return fold.RequestListOfItems();
  277. }
  278. CachedUserInfo userProfile;
  279. if (m_userProfiles.TryGetValue(agentID, out userProfile))
  280. {
  281. if (userProfile.RootFolder != null)
  282. {
  283. if (userProfile.RootFolder.folderID == folderID)
  284. {
  285. return userProfile.RootFolder.RequestListOfItems();
  286. }
  287. else
  288. {
  289. if ((fold = userProfile.RootFolder.HasSubFolder(folderID)) != null)
  290. {
  291. return fold.RequestListOfItems();
  292. }
  293. }
  294. }
  295. else
  296. {
  297. m_log.ErrorFormat("[INVENTORY CACHE]: Could not find root folder for user {0}", agentID.ToString());
  298. return new List<InventoryItemBase>(); ;
  299. }
  300. }
  301. else
  302. {
  303. m_log.ErrorFormat("[INVENTORY CACHE]: " +
  304. "Could not find user profile for {0} for folder {1}",
  305. agentID.ToString(), folderID);
  306. return new List<InventoryItemBase>();
  307. }
  308. // If we've reached this point then we couldn't find the folder, even though the client thinks
  309. // it exists
  310. m_log.ErrorFormat("[INVENTORY CACHE]: " +
  311. "Could not find folder {0} for user {1}",
  312. folderID, agentID.ToString());
  313. // }
  314. return new List<InventoryItemBase>();
  315. }
  316. public void HandlePurgeInventoryDescendents(IClientAPI remoteClient, LLUUID folderID)
  317. {
  318. // m_log.InfoFormat("[INVENTORYCACHE]: Purging folder {0} for {1} uuid {2}",
  319. // folderID, remoteClient.Name, remoteClient.AgentId);
  320. CachedUserInfo userProfile;
  321. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  322. {
  323. if (userProfile.RootFolder != null)
  324. {
  325. InventoryFolderImpl subFolder = userProfile.RootFolder.HasSubFolder(folderID);
  326. if (subFolder != null)
  327. {
  328. List<InventoryItemBase> items = subFolder.RequestListOfItems();
  329. foreach (InventoryItemBase item in items)
  330. {
  331. userProfile.DeleteItem(remoteClient.AgentId, item);
  332. }
  333. }
  334. }
  335. }
  336. }
  337. public void HandleFetchInventory(IClientAPI remoteClient, LLUUID itemID, LLUUID ownerID)
  338. {
  339. if (ownerID == libraryRoot.agentID)
  340. {
  341. //Console.WriteLine("request info for library item");
  342. return;
  343. }
  344. CachedUserInfo userProfile;
  345. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  346. {
  347. if (userProfile.RootFolder != null)
  348. {
  349. InventoryItemBase item = userProfile.RootFolder.HasItem(itemID);
  350. if (item != null)
  351. {
  352. remoteClient.SendInventoryItemDetails(ownerID, item);
  353. }
  354. }
  355. }
  356. }
  357. private void RequestInventoryForUser(LLUUID userID, CachedUserInfo userInfo)
  358. {
  359. m_parent.InventoryService.RequestInventoryForUser(userID, userInfo.FolderReceive, userInfo.ItemReceive);
  360. }
  361. }
  362. }