InventoryServiceConnector.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. public bool AddFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  137. {
  138. try
  139. {
  140. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  141. "POST", m_ServerURI + "/NewFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  142. }
  143. catch (Exception e)
  144. {
  145. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory folder operation failed, {0} {1}",
  146. e.Source, e.Message);
  147. }
  148. return false;
  149. }
  150. public bool UpdateFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  151. {
  152. try
  153. {
  154. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  155. "POST", m_ServerURI + "/UpdateFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  156. }
  157. catch (Exception e)
  158. {
  159. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update inventory folder operation failed, {0} {1}",
  160. e.Source, e.Message);
  161. }
  162. return false;
  163. }
  164. public bool MoveFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  165. {
  166. try
  167. {
  168. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  169. "POST", m_ServerURI + "/MoveFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  170. }
  171. catch (Exception e)
  172. {
  173. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed, {0} {1}",
  174. e.Source, e.Message);
  175. }
  176. return false;
  177. }
  178. public bool PurgeFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  179. {
  180. try
  181. {
  182. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  183. "POST", m_ServerURI + "/PurgeFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  184. }
  185. catch (Exception e)
  186. {
  187. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed, {0} {1}",
  188. e.Source, e.Message);
  189. }
  190. return false;
  191. }
  192. public bool AddItem(string userID, InventoryItemBase item, UUID sessionID)
  193. {
  194. try
  195. {
  196. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  197. "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString());
  198. }
  199. catch (Exception e)
  200. {
  201. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory item operation failed, {0} {1}",
  202. e.Source, e.Message);
  203. }
  204. return false;
  205. }
  206. public bool UpdateItem(string userID, InventoryItemBase item, UUID sessionID)
  207. {
  208. try
  209. {
  210. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  211. "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString());
  212. }
  213. catch (Exception e)
  214. {
  215. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update new inventory item operation failed, {0} {1}",
  216. e.Source, e.Message);
  217. }
  218. return false;
  219. }
  220. public bool DeleteItem(string userID, InventoryItemBase item, UUID sessionID)
  221. {
  222. try
  223. {
  224. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  225. "POST", m_ServerURI + "/DeleteItem/", item, sessionID.ToString(), item.Owner.ToString());
  226. }
  227. catch (Exception e)
  228. {
  229. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Delete inventory item operation failed, {0} {1}",
  230. e.Source, e.Message);
  231. }
  232. return false;
  233. }
  234. public InventoryItemBase QueryItem(string userID, InventoryItemBase item, UUID sessionID)
  235. {
  236. try
  237. {
  238. return SynchronousRestSessionObjectPoster<InventoryItemBase, InventoryItemBase>.BeginPostObject(
  239. "POST", m_ServerURI + "/QueryItem/", item, sessionID.ToString(), item.Owner.ToString());
  240. }
  241. catch (Exception e)
  242. {
  243. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory item operation failed, {0} {1}",
  244. e.Source, e.Message);
  245. }
  246. return null;
  247. }
  248. public InventoryFolderBase QueryFolder(string userID, InventoryFolderBase item, UUID sessionID)
  249. {
  250. try
  251. {
  252. return SynchronousRestSessionObjectPoster<InventoryFolderBase, InventoryFolderBase>.BeginPostObject(
  253. "POST", m_ServerURI + "/QueryFolder/", item, sessionID.ToString(), item.Owner.ToString());
  254. }
  255. catch (Exception e)
  256. {
  257. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory item operation failed, {0} {1}",
  258. e.Source, e.Message);
  259. }
  260. return null;
  261. }
  262. #endregion
  263. /// <summary>
  264. /// Callback used by the inventory server GetInventory request
  265. /// </summary>
  266. /// <param name="userID"></param>
  267. private void InventoryResponse(InventoryCollection response)
  268. {
  269. UUID userID = response.UserID;
  270. InventoryReceiptCallback callback = null;
  271. lock (m_RequestingInventory)
  272. {
  273. if (m_RequestingInventory.ContainsKey(userID))
  274. {
  275. callback = m_RequestingInventory[userID];
  276. m_RequestingInventory.Remove(userID);
  277. lock (m_RequestTime)
  278. {
  279. if (m_RequestTime.ContainsKey(userID))
  280. {
  281. m_RequestTime.Remove(userID);
  282. }
  283. }
  284. }
  285. else
  286. {
  287. m_log.WarnFormat(
  288. "[INVENTORY CONNECTOR]: " +
  289. "Received inventory response for {0} for which we do not have a record of requesting!",
  290. userID);
  291. return;
  292. }
  293. }
  294. m_log.InfoFormat("[INVENTORY CONNECTOR]: " +
  295. "Received inventory response for user {0} containing {1} folders and {2} items",
  296. userID, response.Folders.Count, response.Items.Count);
  297. InventoryFolderImpl rootFolder = null;
  298. ICollection<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  299. ICollection<InventoryItemBase> items = new List<InventoryItemBase>();
  300. foreach (InventoryFolderBase folder in response.Folders)
  301. {
  302. if (folder.ParentID == UUID.Zero)
  303. {
  304. rootFolder = new InventoryFolderImpl(folder);
  305. folders.Add(rootFolder);
  306. break;
  307. }
  308. }
  309. if (rootFolder != null)
  310. {
  311. foreach (InventoryFolderBase folder in response.Folders)
  312. {
  313. if (folder.ID != rootFolder.ID)
  314. {
  315. folders.Add(new InventoryFolderImpl(folder));
  316. }
  317. }
  318. foreach (InventoryItemBase item in response.Items)
  319. {
  320. items.Add(item);
  321. }
  322. }
  323. else
  324. {
  325. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Did not get back an inventory containing a root folder for user {0}", userID);
  326. }
  327. callback(folders, items);
  328. }
  329. }
  330. }