OGS1InventoryService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.Net;
  30. using System.Reflection;
  31. using libsecondlife;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Communications.Cache;
  36. using OpenSim.Framework.Servers;
  37. namespace OpenSim.Region.Communications.OGS1
  38. {
  39. public class OGS1InventoryService : IInventoryServices
  40. {
  41. private static readonly ILog m_log
  42. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private string _inventoryServerUrl;
  44. private Dictionary<LLUUID, InventoryRequest> m_RequestingInventory = new Dictionary<LLUUID, InventoryRequest>();
  45. public OGS1InventoryService(string inventoryServerUrl)
  46. {
  47. _inventoryServerUrl = inventoryServerUrl;
  48. }
  49. #region IInventoryServices Members
  50. /// <summary>
  51. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  52. /// </summary>
  53. /// <param name="userID"></param>
  54. /// <param name="callback"></param>
  55. public void RequestInventoryForUser(LLUUID userID, InventoryReceiptCallback callback)
  56. {
  57. if (!m_RequestingInventory.ContainsKey(userID))
  58. {
  59. InventoryRequest request = new InventoryRequest(userID, callback);
  60. m_RequestingInventory.Add(userID, request);
  61. try
  62. {
  63. m_log.InfoFormat(
  64. "[OGS1 INVENTORY SERVICE]: Requesting inventory from {0}/GetInventory/ for user {1}",
  65. _inventoryServerUrl, userID);
  66. RestObjectPosterResponse<InventoryCollection> requester
  67. = new RestObjectPosterResponse<InventoryCollection>();
  68. requester.ResponseCallback = InventoryResponse;
  69. requester.BeginPostObject<Guid>(_inventoryServerUrl + "/GetInventory/", userID.UUID);
  70. }
  71. catch (WebException e)
  72. {
  73. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Request inventory operation failed, {0} {1}",
  74. e.Source, e.Message);
  75. }
  76. }
  77. else
  78. {
  79. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: RequestInventoryForUser() - could you not find user profile for {0}", userID);
  80. }
  81. }
  82. /// <summary>
  83. /// Callback used by the inventory server GetInventory request
  84. /// </summary>
  85. /// <param name="userID"></param>
  86. private void InventoryResponse(InventoryCollection response)
  87. {
  88. LLUUID userID = response.UserID;
  89. if (m_RequestingInventory.ContainsKey(userID))
  90. {
  91. m_log.InfoFormat("[OGS1 INVENTORY SERVICE]: " +
  92. "Received inventory response for user {0} containing {1} folders and {2} items",
  93. userID, response.Folders.Count, response.Items.Count);
  94. InventoryFolderImpl rootFolder = null;
  95. InventoryRequest request = m_RequestingInventory[userID];
  96. ICollection<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  97. ICollection<InventoryItemBase> items = new List<InventoryItemBase>();
  98. foreach (InventoryFolderBase folder in response.Folders)
  99. {
  100. if (folder.ParentID == LLUUID.Zero)
  101. {
  102. rootFolder = new InventoryFolderImpl(folder);
  103. folders.Add(rootFolder);
  104. break;
  105. }
  106. }
  107. if (rootFolder != null)
  108. {
  109. foreach (InventoryFolderBase folder in response.Folders)
  110. {
  111. if (folder.ID != rootFolder.ID)
  112. {
  113. folders.Add(new InventoryFolderImpl(folder));
  114. }
  115. }
  116. foreach (InventoryItemBase item in response.Items)
  117. {
  118. items.Add(item);
  119. }
  120. }
  121. else
  122. {
  123. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Did not get back an inventory containing a root folder for user {0}", userID);
  124. }
  125. request.Callback(userID, folders, items);
  126. m_RequestingInventory.Remove(userID);
  127. }
  128. else
  129. {
  130. m_log.WarnFormat(
  131. "[OGS1 INVENTORY SERVICE]: " +
  132. "Received inventory response for {0} for which we do not have a record of requesting!",
  133. userID);
  134. }
  135. }
  136. /// <summary>
  137. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  138. /// </summary>
  139. /// <param name="userID"></param>
  140. /// <param name="folder"></param>
  141. public void AddNewInventoryFolder(LLUUID userID, InventoryFolderBase folder)
  142. {
  143. try
  144. {
  145. SynchronousRestObjectPoster.BeginPostObject<InventoryFolderBase, bool>(
  146. "POST", _inventoryServerUrl + "/NewFolder/", folder);
  147. }
  148. catch (WebException e)
  149. {
  150. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Add new inventory folder operation failed, {0} {1}",
  151. e.Source, e.Message);
  152. }
  153. }
  154. /// <summary>
  155. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  156. /// </summary>
  157. /// <param name="userID"></param>
  158. /// <param name="folder"></param>
  159. public void MoveInventoryFolder(LLUUID userID, InventoryFolderBase folder)
  160. {
  161. try
  162. {
  163. SynchronousRestObjectPoster.BeginPostObject<InventoryFolderBase, bool>(
  164. "POST", _inventoryServerUrl + "/MoveFolder/", folder);
  165. }
  166. catch (WebException e)
  167. {
  168. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Move inventory folder operation failed, {0} {1}",
  169. e.Source, e.Message);
  170. }
  171. }
  172. /// <summary>
  173. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  174. /// </summary>
  175. /// <param name="userID"></param>
  176. /// <param name="folder"></param>
  177. /// <returns></returns>
  178. public void PurgeInventoryFolder(LLUUID userID, InventoryFolderBase folder)
  179. {
  180. try
  181. {
  182. SynchronousRestObjectPoster.BeginPostObject<InventoryFolderBase, bool>(
  183. "POST", _inventoryServerUrl + "/PurgeFolder/", folder);
  184. }
  185. catch (WebException e)
  186. {
  187. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Move inventory folder operation failed, {0} {1}",
  188. e.Source, e.Message);
  189. }
  190. }
  191. /// <summary>
  192. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  193. /// </summary>
  194. /// <param name="userID"></param>
  195. /// <param name="folder"></param>
  196. public void AddNewInventoryItem(LLUUID userID, InventoryItemBase item)
  197. {
  198. try
  199. {
  200. SynchronousRestObjectPoster.BeginPostObject<InventoryItemBase, bool>(
  201. "POST", _inventoryServerUrl + "/NewItem/", item);
  202. }
  203. catch (WebException e)
  204. {
  205. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Add new inventory item operation failed, {0} {1}",
  206. e.Source, e.Message);
  207. }
  208. }
  209. // TODO: this is a temporary workaround, the UpdateInventoryItem method need to be implemented
  210. public void UpdateInventoryItem(LLUUID userID, InventoryItemBase item)
  211. {
  212. try
  213. {
  214. SynchronousRestObjectPoster.BeginPostObject<InventoryItemBase, bool>(
  215. "POST", _inventoryServerUrl + "/NewItem/", item);
  216. }
  217. catch (WebException e)
  218. {
  219. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Update new inventory item operation failed, {0} {1}",
  220. e.Source, e.Message);
  221. }
  222. }
  223. /// <summary>
  224. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  225. /// </summary>
  226. /// <param name="userID"></param>
  227. /// <param name="folder"></param>
  228. public void DeleteInventoryItem(LLUUID userID, InventoryItemBase item)
  229. {
  230. try
  231. {
  232. SynchronousRestObjectPoster.BeginPostObject<InventoryItemBase, bool>(
  233. "POST", _inventoryServerUrl + "/DeleteItem/", item);
  234. }
  235. catch (WebException e)
  236. {
  237. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Delete inventory item operation failed, {0} {1}",
  238. e.Source, e.Message);
  239. }
  240. }
  241. public bool HasInventoryForUser(LLUUID userID)
  242. {
  243. return false;
  244. }
  245. public InventoryFolderBase RequestRootFolder(LLUUID userID)
  246. {
  247. return null;
  248. }
  249. public bool CreateNewUserInventory(LLUUID user)
  250. {
  251. return false;
  252. }
  253. // See IInventoryServices
  254. public List<InventoryFolderBase> GetInventorySkeleton(LLUUID userId)
  255. {
  256. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: The GetInventorySkeleton() method here should never be called!");
  257. return new List<InventoryFolderBase>();
  258. }
  259. #endregion
  260. /// <summary>
  261. /// Caches a pending inventory request that has yet to be satisfied by the inventory service
  262. /// </summary>
  263. public class InventoryRequest
  264. {
  265. public LLUUID UserID;
  266. public InventoryReceiptCallback Callback;
  267. public InventoryRequest(LLUUID userId, InventoryReceiptCallback callback)
  268. {
  269. UserID = userId;
  270. Callback = callback;
  271. }
  272. }
  273. }
  274. }