InventoryServiceConnector.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Servers.HttpServer;
  35. using OpenSim.Services.Interfaces;
  36. using OpenMetaverse;
  37. namespace OpenSim.Services.Connectors
  38. {
  39. public class InventoryServicesConnector : ISessionAuthInventoryService
  40. {
  41. private static readonly ILog m_log =
  42. LogManager.GetLogger(
  43. MethodBase.GetCurrentMethod().DeclaringType);
  44. private string m_ServerURI = String.Empty;
  45. private Dictionary<UUID, InventoryReceiptCallback> m_RequestingInventory = new Dictionary<UUID, InventoryReceiptCallback>();
  46. private Dictionary<UUID, DateTime> m_RequestTime = new Dictionary<UUID, DateTime>();
  47. public InventoryServicesConnector()
  48. {
  49. }
  50. public InventoryServicesConnector(string serverURI)
  51. {
  52. m_ServerURI = serverURI.TrimEnd('/');
  53. }
  54. public InventoryServicesConnector(IConfigSource source)
  55. {
  56. Initialise(source);
  57. }
  58. public virtual void Initialise(IConfigSource source)
  59. {
  60. IConfig inventoryConfig = source.Configs["InventoryService"];
  61. if (inventoryConfig == null)
  62. {
  63. m_log.Error("[INVENTORY CONNECTOR]: InventoryService missing from OpenSim.ini");
  64. throw new Exception("InventoryService missing from OpenSim.ini");
  65. }
  66. string serviceURI = inventoryConfig.GetString("InventoryServerURI",
  67. String.Empty);
  68. if (serviceURI == String.Empty)
  69. {
  70. m_log.Error("[INVENTORY CONNECTOR]: No Server URI named in section InventoryService");
  71. throw new Exception("Unable to proceed. Please make sure your ini files in config-include are updated according to .example's");
  72. }
  73. m_ServerURI = serviceURI.TrimEnd('/');
  74. }
  75. #region ISessionAuthInventoryService
  76. public string Host
  77. {
  78. get { return m_ServerURI; }
  79. }
  80. /// <summary>
  81. /// Caller must catch eventual Exceptions.
  82. /// </summary>
  83. /// <param name="userID"></param>
  84. /// <param name="sessionID"></param>
  85. /// <param name="callback"></param>
  86. public void GetUserInventory(string userIDStr, UUID sessionID, InventoryReceiptCallback callback)
  87. {
  88. UUID userID = UUID.Zero;
  89. if (UUID.TryParse(userIDStr, out userID))
  90. {
  91. lock (m_RequestingInventory)
  92. {
  93. // *HACK ALERT*
  94. // If an inventory request times out, it blocks any further requests from the
  95. // same user, even after a relog. This is bad, and makes me sad.
  96. // Really, we should detect a timeout and report a failure to the callback,
  97. // BUT in my testing i found that it's hard to detect a timeout.. sometimes,
  98. // a partial response is recieved, and sometimes a null response.
  99. // So, for now, add a timer of ten seconds (which is the request timeout).
  100. // This should basically have the same effect.
  101. lock (m_RequestTime)
  102. {
  103. if (m_RequestTime.ContainsKey(userID))
  104. {
  105. TimeSpan interval = DateTime.Now - m_RequestTime[userID];
  106. if (interval.TotalSeconds > 10)
  107. {
  108. m_RequestTime.Remove(userID);
  109. if (m_RequestingInventory.ContainsKey(userID))
  110. {
  111. m_RequestingInventory.Remove(userID);
  112. }
  113. }
  114. }
  115. if (!m_RequestingInventory.ContainsKey(userID))
  116. {
  117. m_RequestTime.Add(userID, DateTime.Now);
  118. m_RequestingInventory.Add(userID, callback);
  119. }
  120. else
  121. {
  122. m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetUserInventory - ignoring repeated request for user {0}", userID);
  123. return;
  124. }
  125. }
  126. }
  127. m_log.InfoFormat(
  128. "[INVENTORY CONNECTOR]: Requesting inventory from {0}/GetInventory/ for user {1}",
  129. m_ServerURI, userID);
  130. RestSessionObjectPosterResponse<Guid, InventoryCollection> requester
  131. = new RestSessionObjectPosterResponse<Guid, InventoryCollection>();
  132. requester.ResponseCallback = InventoryResponse;
  133. requester.BeginPostObject(m_ServerURI + "/GetInventory/", userID.Guid, sessionID.ToString(), userID.ToString());
  134. }
  135. }
  136. /// <summary>
  137. /// Gets the user folder for the given folder-type
  138. /// </summary>
  139. /// <param name="userID"></param>
  140. /// <param name="type"></param>
  141. /// <returns></returns>
  142. public Dictionary<AssetType, InventoryFolderBase> GetSystemFolders(string userID, UUID sessionID)
  143. {
  144. // !!! Not just yet.
  145. //try
  146. //{
  147. // List<InventoryFolderBase> folders = SynchronousRestSessionObjectPoster<Guid, List<InventoryFolderBase>>.BeginPostObject(
  148. // "POST", m_ServerURI + "/SystemFolders/", new Guid(userID), sessionID.ToString(), userID.ToString());
  149. // Dictionary<AssetType, InventoryFolderBase> dFolders = new Dictionary<AssetType, InventoryFolderBase>();
  150. // foreach (InventoryFolderBase f in folders)
  151. // dFolders[(AssetType)f.Type] = f;
  152. // return dFolders;
  153. //}
  154. //catch (Exception e)
  155. //{
  156. // m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetSystemFolders operation failed, {0} {1}",
  157. // e.Source, e.Message);
  158. //}
  159. return new Dictionary<AssetType, InventoryFolderBase>();
  160. }
  161. /// <summary>
  162. /// Gets everything (folders and items) inside a folder
  163. /// </summary>
  164. /// <param name="userId"></param>
  165. /// <param name="folderID"></param>
  166. /// <returns></returns>
  167. public InventoryCollection GetFolderContent(string userID, UUID folderID, UUID sessionID)
  168. {
  169. try
  170. {
  171. return SynchronousRestSessionObjectPoster<Guid, InventoryCollection>.BeginPostObject(
  172. "POST", m_ServerURI + "/GetFolderContent/", folderID.Guid, sessionID.ToString(), userID.ToString());
  173. }
  174. catch (Exception e)
  175. {
  176. m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetFolderForType operation failed, {0} {1}",
  177. e.Source, e.Message);
  178. }
  179. return null;
  180. }
  181. public bool AddFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  182. {
  183. try
  184. {
  185. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  186. "POST", m_ServerURI + "/NewFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  187. }
  188. catch (Exception e)
  189. {
  190. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory folder operation failed, {0} {1}",
  191. e.Source, e.Message);
  192. }
  193. return false;
  194. }
  195. public bool UpdateFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  196. {
  197. try
  198. {
  199. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  200. "POST", m_ServerURI + "/UpdateFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  201. }
  202. catch (Exception e)
  203. {
  204. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update inventory folder operation failed, {0} {1}",
  205. e.Source, e.Message);
  206. }
  207. return false;
  208. }
  209. public bool MoveFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  210. {
  211. try
  212. {
  213. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  214. "POST", m_ServerURI + "/MoveFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  215. }
  216. catch (Exception e)
  217. {
  218. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed, {0} {1}",
  219. e.Source, e.Message);
  220. }
  221. return false;
  222. }
  223. public bool PurgeFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  224. {
  225. try
  226. {
  227. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  228. "POST", m_ServerURI + "/PurgeFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  229. }
  230. catch (Exception e)
  231. {
  232. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed, {0} {1}",
  233. e.Source, e.Message);
  234. }
  235. return false;
  236. }
  237. public bool AddItem(string userID, InventoryItemBase item, UUID sessionID)
  238. {
  239. try
  240. {
  241. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  242. "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString());
  243. }
  244. catch (Exception e)
  245. {
  246. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory item operation failed, {0} {1}",
  247. e.Source, e.Message);
  248. }
  249. return false;
  250. }
  251. public bool UpdateItem(string userID, InventoryItemBase item, UUID sessionID)
  252. {
  253. try
  254. {
  255. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  256. "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString());
  257. }
  258. catch (Exception e)
  259. {
  260. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update new inventory item operation failed, {0} {1}",
  261. e.Source, e.Message);
  262. }
  263. return false;
  264. }
  265. public bool DeleteItem(string userID, InventoryItemBase item, UUID sessionID)
  266. {
  267. try
  268. {
  269. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  270. "POST", m_ServerURI + "/DeleteItem/", item, sessionID.ToString(), item.Owner.ToString());
  271. }
  272. catch (Exception e)
  273. {
  274. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Delete inventory item operation failed, {0} {1}",
  275. e.Source, e.Message);
  276. }
  277. return false;
  278. }
  279. public InventoryItemBase QueryItem(string userID, InventoryItemBase item, UUID sessionID)
  280. {
  281. try
  282. {
  283. return SynchronousRestSessionObjectPoster<InventoryItemBase, InventoryItemBase>.BeginPostObject(
  284. "POST", m_ServerURI + "/QueryItem/", item, sessionID.ToString(), item.Owner.ToString());
  285. }
  286. catch (Exception e)
  287. {
  288. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory item operation failed, {0} {1}",
  289. e.Source, e.Message);
  290. }
  291. return null;
  292. }
  293. public InventoryFolderBase QueryFolder(string userID, InventoryFolderBase item, UUID sessionID)
  294. {
  295. try
  296. {
  297. return SynchronousRestSessionObjectPoster<InventoryFolderBase, InventoryFolderBase>.BeginPostObject(
  298. "POST", m_ServerURI + "/QueryFolder/", item, sessionID.ToString(), item.Owner.ToString());
  299. }
  300. catch (Exception e)
  301. {
  302. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory item operation failed, {0} {1}",
  303. e.Source, e.Message);
  304. }
  305. return null;
  306. }
  307. #endregion
  308. /// <summary>
  309. /// Callback used by the inventory server GetInventory request
  310. /// </summary>
  311. /// <param name="userID"></param>
  312. private void InventoryResponse(InventoryCollection response)
  313. {
  314. UUID userID = response.UserID;
  315. InventoryReceiptCallback callback = null;
  316. lock (m_RequestingInventory)
  317. {
  318. if (m_RequestingInventory.ContainsKey(userID))
  319. {
  320. callback = m_RequestingInventory[userID];
  321. m_RequestingInventory.Remove(userID);
  322. lock (m_RequestTime)
  323. {
  324. if (m_RequestTime.ContainsKey(userID))
  325. {
  326. m_RequestTime.Remove(userID);
  327. }
  328. }
  329. }
  330. else
  331. {
  332. m_log.WarnFormat(
  333. "[INVENTORY CONNECTOR]: " +
  334. "Received inventory response for {0} for which we do not have a record of requesting!",
  335. userID);
  336. return;
  337. }
  338. }
  339. m_log.InfoFormat("[INVENTORY CONNECTOR]: " +
  340. "Received inventory response for user {0} containing {1} folders and {2} items",
  341. userID, response.Folders.Count, response.Items.Count);
  342. InventoryFolderImpl rootFolder = null;
  343. ICollection<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  344. ICollection<InventoryItemBase> items = new List<InventoryItemBase>();
  345. foreach (InventoryFolderBase folder in response.Folders)
  346. {
  347. if (folder.ParentID == UUID.Zero)
  348. {
  349. rootFolder = new InventoryFolderImpl(folder);
  350. folders.Add(rootFolder);
  351. break;
  352. }
  353. }
  354. if (rootFolder != null)
  355. {
  356. foreach (InventoryFolderBase folder in response.Folders)
  357. {
  358. if (folder.ID != rootFolder.ID)
  359. {
  360. folders.Add(new InventoryFolderImpl(folder));
  361. }
  362. }
  363. foreach (InventoryItemBase item in response.Items)
  364. {
  365. items.Add(item);
  366. }
  367. }
  368. else
  369. {
  370. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Did not get back an inventory containing a root folder for user {0}", userID);
  371. }
  372. callback(folders, items);
  373. }
  374. }
  375. }