HGInventoryBroker.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 Nini.Config;
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Reflection;
  32. using OpenSim.Framework;
  33. using OpenSim.Server.Base;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Services.Interfaces;
  37. using OpenSim.Services.Connectors;
  38. using OpenSim.Services.Connectors.SimianGrid;
  39. using OpenMetaverse;
  40. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
  41. {
  42. public class HGInventoryBroker : ISharedRegionModule, IInventoryService
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(
  46. MethodBase.GetCurrentMethod().DeclaringType);
  47. private static bool m_Enabled = false;
  48. private static IInventoryService m_LocalGridInventoryService;
  49. private Dictionary<string, IInventoryService> m_connectors = new Dictionary<string, IInventoryService>();
  50. // A cache of userIDs --> ServiceURLs, for HGBroker only
  51. protected Dictionary<UUID, string> m_InventoryURLs = new Dictionary<UUID,string>();
  52. private List<Scene> m_Scenes = new List<Scene>();
  53. public Type ReplaceableInterface
  54. {
  55. get { return null; }
  56. }
  57. public string Name
  58. {
  59. get { return "HGInventoryBroker"; }
  60. }
  61. public void Initialise(IConfigSource source)
  62. {
  63. IConfig moduleConfig = source.Configs["Modules"];
  64. if (moduleConfig != null)
  65. {
  66. string name = moduleConfig.GetString("InventoryServices", "");
  67. if (name == Name)
  68. {
  69. IConfig inventoryConfig = source.Configs["InventoryService"];
  70. if (inventoryConfig == null)
  71. {
  72. m_log.Error("[HG INVENTORY CONNECTOR]: InventoryService missing from OpenSim.ini");
  73. return;
  74. }
  75. string localDll = inventoryConfig.GetString("LocalGridInventoryService",
  76. String.Empty);
  77. //string HGDll = inventoryConfig.GetString("HypergridInventoryService",
  78. // String.Empty);
  79. if (localDll == String.Empty)
  80. {
  81. m_log.Error("[HG INVENTORY CONNECTOR]: No LocalGridInventoryService named in section InventoryService");
  82. //return;
  83. throw new Exception("Unable to proceed. Please make sure your ini files in config-include are updated according to .example's");
  84. }
  85. Object[] args = new Object[] { source };
  86. m_LocalGridInventoryService =
  87. ServerUtils.LoadPlugin<IInventoryService>(localDll,
  88. args);
  89. if (m_LocalGridInventoryService == null)
  90. {
  91. m_log.Error("[HG INVENTORY CONNECTOR]: Can't load local inventory service");
  92. return;
  93. }
  94. m_Enabled = true;
  95. m_log.InfoFormat("[HG INVENTORY CONNECTOR]: HG inventory broker enabled with inner connector of type {0}", m_LocalGridInventoryService.GetType());
  96. }
  97. }
  98. }
  99. public void PostInitialise()
  100. {
  101. }
  102. public void Close()
  103. {
  104. }
  105. public void AddRegion(Scene scene)
  106. {
  107. if (!m_Enabled)
  108. return;
  109. m_Scenes.Add(scene);
  110. scene.RegisterModuleInterface<IInventoryService>(this);
  111. scene.EventManager.OnClientClosed += OnClientClosed;
  112. }
  113. public void RemoveRegion(Scene scene)
  114. {
  115. if (!m_Enabled)
  116. return;
  117. m_Scenes.Remove(scene);
  118. }
  119. public void RegionLoaded(Scene scene)
  120. {
  121. if (!m_Enabled)
  122. return;
  123. m_log.InfoFormat("[HG INVENTORY CONNECTOR]: Enabled HG inventory for region {0}", scene.RegionInfo.RegionName);
  124. }
  125. #region URL Cache
  126. void OnClientClosed(UUID clientID, Scene scene)
  127. {
  128. if (m_InventoryURLs.ContainsKey(clientID)) // if it's in cache
  129. {
  130. ScenePresence sp = null;
  131. foreach (Scene s in m_Scenes)
  132. {
  133. s.TryGetScenePresence(clientID, out sp);
  134. if ((sp != null) && !sp.IsChildAgent && (s != scene))
  135. {
  136. m_log.DebugFormat("[INVENTORY CACHE]: OnClientClosed in {0}, but user {1} still in sim. Keeping inventoryURL in cache",
  137. scene.RegionInfo.RegionName, clientID);
  138. return;
  139. }
  140. }
  141. DropInventoryServiceURL(clientID);
  142. }
  143. }
  144. /// <summary>
  145. /// Gets the user's inventory URL from its serviceURLs, if the user is foreign,
  146. /// and sticks it in the cache
  147. /// </summary>
  148. /// <param name="userID"></param>
  149. private void CacheInventoryServiceURL(UUID userID)
  150. {
  151. if (m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, userID) == null)
  152. {
  153. // The user does not have a local account; let's cache its service URL
  154. string inventoryURL = string.Empty;
  155. ScenePresence sp = null;
  156. foreach (Scene scene in m_Scenes)
  157. {
  158. scene.TryGetScenePresence(userID, out sp);
  159. if (sp != null)
  160. {
  161. AgentCircuitData aCircuit = scene.AuthenticateHandler.GetAgentCircuitData(sp.ControllingClient.CircuitCode);
  162. if (aCircuit.ServiceURLs.ContainsKey("InventoryServerURI"))
  163. {
  164. inventoryURL = aCircuit.ServiceURLs["InventoryServerURI"].ToString();
  165. if (inventoryURL != null && inventoryURL != string.Empty)
  166. {
  167. inventoryURL = inventoryURL.Trim(new char[] { '/' });
  168. m_InventoryURLs.Add(userID, inventoryURL);
  169. m_log.DebugFormat("[HG INVENTORY CONNECTOR]: Added {0} to the cache of inventory URLs", inventoryURL);
  170. return;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. private void DropInventoryServiceURL(UUID userID)
  178. {
  179. lock (m_InventoryURLs)
  180. if (m_InventoryURLs.ContainsKey(userID))
  181. {
  182. string url = m_InventoryURLs[userID];
  183. m_InventoryURLs.Remove(userID);
  184. m_log.DebugFormat("[HG INVENTORY CONNECTOR]: Removed {0} from the cache of inventory URLs", url);
  185. }
  186. }
  187. public string GetInventoryServiceURL(UUID userID)
  188. {
  189. if (m_InventoryURLs.ContainsKey(userID))
  190. return m_InventoryURLs[userID];
  191. CacheInventoryServiceURL(userID);
  192. if (m_InventoryURLs.ContainsKey(userID))
  193. return m_InventoryURLs[userID];
  194. return null; //it means that the methods should forward to local grid's inventory
  195. }
  196. #endregion
  197. #region IInventoryService
  198. public bool CreateUserInventory(UUID userID)
  199. {
  200. return m_LocalGridInventoryService.CreateUserInventory(userID);
  201. }
  202. public List<InventoryFolderBase> GetInventorySkeleton(UUID userId)
  203. {
  204. return m_LocalGridInventoryService.GetInventorySkeleton(userId);
  205. }
  206. public InventoryCollection GetUserInventory(UUID userID)
  207. {
  208. return null;
  209. }
  210. public void GetUserInventory(UUID userID, InventoryReceiptCallback callback)
  211. {
  212. }
  213. public InventoryFolderBase GetRootFolder(UUID userID)
  214. {
  215. //m_log.DebugFormat("[HG INVENTORY CONNECTOR]: GetRootFolder for {0}", userID);
  216. string invURL = GetInventoryServiceURL(userID);
  217. if (invURL == null) // not there, forward to local inventory connector to resolve
  218. return m_LocalGridInventoryService.GetRootFolder(userID);
  219. IInventoryService connector = GetConnector(invURL);
  220. return connector.GetRootFolder(userID);
  221. }
  222. public InventoryFolderBase GetFolderForType(UUID userID, AssetType type)
  223. {
  224. //m_log.DebugFormat("[HG INVENTORY CONNECTOR]: GetFolderForType {0} type {1}", userID, type);
  225. string invURL = GetInventoryServiceURL(userID);
  226. if (invURL == null) // not there, forward to local inventory connector to resolve
  227. return m_LocalGridInventoryService.GetFolderForType(userID, type);
  228. IInventoryService connector = GetConnector(invURL);
  229. return connector.GetFolderForType(userID, type);
  230. }
  231. public InventoryCollection GetFolderContent(UUID userID, UUID folderID)
  232. {
  233. //m_log.Debug("[HG INVENTORY CONNECTOR]: GetFolderContent " + folderID);
  234. string invURL = GetInventoryServiceURL(userID);
  235. if (invURL == null) // not there, forward to local inventory connector to resolve
  236. return m_LocalGridInventoryService.GetFolderContent(userID, folderID);
  237. IInventoryService connector = GetConnector(invURL);
  238. return connector.GetFolderContent(userID, folderID);
  239. }
  240. public List<InventoryItemBase> GetFolderItems(UUID userID, UUID folderID)
  241. {
  242. //m_log.Debug("[HG INVENTORY CONNECTOR]: GetFolderItems " + folderID);
  243. string invURL = GetInventoryServiceURL(userID);
  244. if (invURL == null) // not there, forward to local inventory connector to resolve
  245. return m_LocalGridInventoryService.GetFolderItems(userID, folderID);
  246. IInventoryService connector = GetConnector(invURL);
  247. return connector.GetFolderItems(userID, folderID);
  248. }
  249. public bool AddFolder(InventoryFolderBase folder)
  250. {
  251. if (folder == null)
  252. return false;
  253. //m_log.Debug("[HG INVENTORY CONNECTOR]: AddFolder " + folder.ID);
  254. string invURL = GetInventoryServiceURL(folder.Owner);
  255. if (invURL == null) // not there, forward to local inventory connector to resolve
  256. return m_LocalGridInventoryService.AddFolder(folder);
  257. IInventoryService connector = GetConnector(invURL);
  258. return connector.AddFolder(folder);
  259. }
  260. public bool UpdateFolder(InventoryFolderBase folder)
  261. {
  262. if (folder == null)
  263. return false;
  264. //m_log.Debug("[HG INVENTORY CONNECTOR]: UpdateFolder " + folder.ID);
  265. string invURL = GetInventoryServiceURL(folder.Owner);
  266. if (invURL == null) // not there, forward to local inventory connector to resolve
  267. return m_LocalGridInventoryService.UpdateFolder(folder);
  268. IInventoryService connector = GetConnector(invURL);
  269. return connector.UpdateFolder(folder);
  270. }
  271. public bool DeleteFolders(UUID ownerID, List<UUID> folderIDs)
  272. {
  273. if (folderIDs == null)
  274. return false;
  275. if (folderIDs.Count == 0)
  276. return false;
  277. //m_log.Debug("[HG INVENTORY CONNECTOR]: DeleteFolders for " + ownerID);
  278. string invURL = GetInventoryServiceURL(ownerID);
  279. if (invURL == null) // not there, forward to local inventory connector to resolve
  280. return m_LocalGridInventoryService.DeleteFolders(ownerID, folderIDs);
  281. IInventoryService connector = GetConnector(invURL);
  282. return connector.DeleteFolders(ownerID, folderIDs);
  283. }
  284. public bool MoveFolder(InventoryFolderBase folder)
  285. {
  286. if (folder == null)
  287. return false;
  288. //m_log.Debug("[HG INVENTORY CONNECTOR]: MoveFolder for " + folder.Owner);
  289. string invURL = GetInventoryServiceURL(folder.Owner);
  290. if (invURL == null) // not there, forward to local inventory connector to resolve
  291. return m_LocalGridInventoryService.MoveFolder(folder);
  292. IInventoryService connector = GetConnector(invURL);
  293. return connector.MoveFolder(folder);
  294. }
  295. public bool PurgeFolder(InventoryFolderBase folder)
  296. {
  297. if (folder == null)
  298. return false;
  299. //m_log.Debug("[HG INVENTORY CONNECTOR]: PurgeFolder for " + folder.Owner);
  300. string invURL = GetInventoryServiceURL(folder.Owner);
  301. if (invURL == null) // not there, forward to local inventory connector to resolve
  302. return m_LocalGridInventoryService.PurgeFolder(folder);
  303. IInventoryService connector = GetConnector(invURL);
  304. return connector.PurgeFolder(folder);
  305. }
  306. public bool AddItem(InventoryItemBase item)
  307. {
  308. if (item == null)
  309. return false;
  310. //m_log.Debug("[HG INVENTORY CONNECTOR]: AddItem " + item.ID);
  311. string invURL = GetInventoryServiceURL(item.Owner);
  312. if (invURL == null) // not there, forward to local inventory connector to resolve
  313. return m_LocalGridInventoryService.AddItem(item);
  314. IInventoryService connector = GetConnector(invURL);
  315. return connector.AddItem(item);
  316. }
  317. public bool UpdateItem(InventoryItemBase item)
  318. {
  319. if (item == null)
  320. return false;
  321. //m_log.Debug("[HG INVENTORY CONNECTOR]: UpdateItem " + item.ID);
  322. string invURL = GetInventoryServiceURL(item.Owner);
  323. if (invURL == null) // not there, forward to local inventory connector to resolve
  324. return m_LocalGridInventoryService.UpdateItem(item);
  325. IInventoryService connector = GetConnector(invURL);
  326. return connector.UpdateItem(item);
  327. }
  328. public bool MoveItems(UUID ownerID, List<InventoryItemBase> items)
  329. {
  330. if (items == null)
  331. return false;
  332. if (items.Count == 0)
  333. return true;
  334. //m_log.Debug("[HG INVENTORY CONNECTOR]: MoveItems for " + ownerID);
  335. string invURL = GetInventoryServiceURL(ownerID);
  336. if (invURL == null) // not there, forward to local inventory connector to resolve
  337. return m_LocalGridInventoryService.MoveItems(ownerID, items);
  338. IInventoryService connector = GetConnector(invURL);
  339. return connector.MoveItems(ownerID, items);
  340. }
  341. public bool DeleteItems(UUID ownerID, List<UUID> itemIDs)
  342. {
  343. //m_log.DebugFormat("[HG INVENTORY CONNECTOR]: Delete {0} items for user {1}", itemIDs.Count, ownerID);
  344. if (itemIDs == null)
  345. return false;
  346. if (itemIDs.Count == 0)
  347. return true;
  348. string invURL = GetInventoryServiceURL(ownerID);
  349. if (invURL == null) // not there, forward to local inventory connector to resolve
  350. return m_LocalGridInventoryService.DeleteItems(ownerID, itemIDs);
  351. IInventoryService connector = GetConnector(invURL);
  352. return connector.DeleteItems(ownerID, itemIDs);
  353. }
  354. public InventoryItemBase GetItem(InventoryItemBase item)
  355. {
  356. if (item == null)
  357. return null;
  358. //m_log.Debug("[HG INVENTORY CONNECTOR]: GetItem " + item.ID);
  359. string invURL = GetInventoryServiceURL(item.Owner);
  360. if (invURL == null) // not there, forward to local inventory connector to resolve
  361. return m_LocalGridInventoryService.GetItem(item);
  362. IInventoryService connector = GetConnector(invURL);
  363. return connector.GetItem(item);
  364. }
  365. public InventoryFolderBase GetFolder(InventoryFolderBase folder)
  366. {
  367. if (folder == null)
  368. return null;
  369. //m_log.Debug("[HG INVENTORY CONNECTOR]: GetFolder " + folder.ID);
  370. string invURL = GetInventoryServiceURL(folder.Owner);
  371. if (invURL == null) // not there, forward to local inventory connector to resolve
  372. return m_LocalGridInventoryService.GetFolder(folder);
  373. IInventoryService connector = GetConnector(invURL);
  374. return connector.GetFolder(folder);
  375. }
  376. public bool HasInventoryForUser(UUID userID)
  377. {
  378. return false;
  379. }
  380. public List<InventoryItemBase> GetActiveGestures(UUID userId)
  381. {
  382. return new List<InventoryItemBase>();
  383. }
  384. public int GetAssetPermissions(UUID userID, UUID assetID)
  385. {
  386. //m_log.Debug("[HG INVENTORY CONNECTOR]: GetAssetPermissions " + assetID);
  387. string invURL = GetInventoryServiceURL(userID);
  388. if (invURL == null) // not there, forward to local inventory connector to resolve
  389. return m_LocalGridInventoryService.GetAssetPermissions(userID, assetID);
  390. IInventoryService connector = GetConnector(invURL);
  391. return connector.GetAssetPermissions(userID, assetID);
  392. }
  393. #endregion
  394. private IInventoryService GetConnector(string url)
  395. {
  396. IInventoryService connector = null;
  397. lock (m_connectors)
  398. {
  399. if (m_connectors.ContainsKey(url))
  400. {
  401. connector = m_connectors[url];
  402. }
  403. else
  404. {
  405. // Still not as flexible as I would like this to be,
  406. // but good enough for now
  407. string connectorType = new HeloServicesConnector(url).Helo();
  408. m_log.DebugFormat("[HG INVENTORY SERVICE]: HELO returned {0}", connectorType);
  409. if (connectorType == "opensim-simian")
  410. connector = new SimianInventoryServiceConnector(url);
  411. else
  412. connector = new RemoteXInventoryServicesConnector(url);
  413. m_connectors.Add(url, connector);
  414. }
  415. }
  416. return connector;
  417. }
  418. }
  419. }