HGAssetServiceConnector.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.Collections.Specialized;
  32. using System.Reflection;
  33. using System.Web;
  34. using OpenSim.Framework;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Services.Connectors.Hypergrid;
  37. using OpenMetaverse;
  38. namespace OpenSim.Services.Connectors
  39. {
  40. public class HGAssetServiceConnector : IAssetService
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private ExpiringCacheOS<string, AssetServicesConnector> m_connectors = new ExpiringCacheOS<string, AssetServicesConnector>(120000);
  44. public HGAssetServiceConnector(IConfigSource source)
  45. {
  46. IConfig moduleConfig = source.Configs["Modules"];
  47. if (moduleConfig != null)
  48. {
  49. // string name = moduleConfig.GetString("AssetServices", "");
  50. IConfig assetConfig = source.Configs["AssetService"];
  51. if (assetConfig == null)
  52. {
  53. m_log.Error("[HG ASSET SERVICE]: AssetService missing from OpenSim.ini");
  54. return;
  55. }
  56. m_log.Info("[HG ASSET SERVICE]: HG asset service enabled");
  57. }
  58. }
  59. private AssetServicesConnector GetConnector(string url)
  60. {
  61. AssetServicesConnector connector = null;
  62. lock (m_connectors)
  63. {
  64. if (!m_connectors.TryGetValue(url, 120000, out connector))
  65. {
  66. // Still not as flexible as I would like this to be,
  67. // but good enough for now
  68. connector = new AssetServicesConnector(url);
  69. m_connectors.Add(url, connector);
  70. }
  71. }
  72. return connector;
  73. }
  74. public AssetBase Get(string id)
  75. {
  76. string url = string.Empty;
  77. string assetID = string.Empty;
  78. if (Util.ParseForeignAssetID(id, out url, out assetID) > 0)
  79. {
  80. IAssetService connector = GetConnector(url);
  81. return connector.Get(assetID);
  82. }
  83. return null;
  84. }
  85. public AssetBase Get(string id, string ForeignAssetService)
  86. {
  87. IAssetService connector = GetConnector(ForeignAssetService);
  88. return connector.Get(id);
  89. }
  90. public AssetBase GetCached(string id)
  91. {
  92. string url = string.Empty;
  93. string assetID = string.Empty;
  94. if (Util.ParseForeignAssetID(id, out url, out assetID) > 0)
  95. {
  96. IAssetService connector = GetConnector(url);
  97. return connector.GetCached(assetID);
  98. }
  99. return null;
  100. }
  101. public AssetMetadata GetMetadata(string id)
  102. {
  103. string url = string.Empty;
  104. string assetID = string.Empty;
  105. if (Util.ParseForeignAssetID(id, out url, out assetID) > 0)
  106. {
  107. IAssetService connector = GetConnector(url);
  108. return connector.GetMetadata(assetID);
  109. }
  110. return null;
  111. }
  112. public byte[] GetData(string id)
  113. {
  114. return null;
  115. }
  116. public bool Get(string id, object sender, AssetRetrieved handler)
  117. {
  118. string url = string.Empty;
  119. string assetID = string.Empty;
  120. if (Util.ParseForeignAssetID(id, out url, out assetID) > 0)
  121. {
  122. IAssetService connector = GetConnector(url);
  123. return connector.Get(assetID, sender, handler);
  124. }
  125. return false;
  126. }
  127. private struct AssetAndIndex
  128. {
  129. public UUID assetID;
  130. public int index;
  131. public AssetAndIndex(UUID assetID, int index)
  132. {
  133. this.assetID = assetID;
  134. this.index = index;
  135. }
  136. }
  137. public virtual bool[] AssetsExist(string[] ids)
  138. {
  139. // This method is a bit complicated because it works even if the assets belong to different
  140. // servers; that requires sending separate requests to each server.
  141. // Group the assets by the server they belong to
  142. var url2assets = new Dictionary<string, List<AssetAndIndex>>();
  143. for (int i = 0; i < ids.Length; i++)
  144. {
  145. string url = string.Empty;
  146. string assetID = string.Empty;
  147. if (Util.ParseForeignAssetID(ids[i], out url, out assetID) > 0)
  148. {
  149. if (!url2assets.ContainsKey(url))
  150. url2assets.Add(url, new List<AssetAndIndex>());
  151. url2assets[url].Add(new AssetAndIndex(UUID.Parse(assetID), i));
  152. }
  153. }
  154. // Query each of the servers in turn
  155. bool[] exist = new bool[ids.Length];
  156. foreach (string url in url2assets.Keys)
  157. {
  158. AssetServicesConnector connector = GetConnector(url);
  159. lock (connector.ConnectorLock)
  160. {
  161. List<AssetAndIndex> curAssets = url2assets[url];
  162. string[] assetIDs = curAssets.ConvertAll(a => a.assetID.ToString()).ToArray();
  163. bool[] curExist = connector.AssetsExist(assetIDs);
  164. int i = 0;
  165. foreach (AssetAndIndex ai in curAssets)
  166. {
  167. exist[ai.index] = curExist[i];
  168. ++i;
  169. }
  170. }
  171. }
  172. return exist;
  173. }
  174. public string Store(AssetBase asset)
  175. {
  176. string url = string.Empty;
  177. string assetID = string.Empty;
  178. if (Util.ParseForeignAssetID(asset.ID, out url, out assetID) > 0)
  179. {
  180. AssetServicesConnector connector = GetConnector(url);
  181. // Restore the assetID to a simple UUID
  182. asset.ID = assetID;
  183. lock ((connector.ConnectorLock))
  184. return connector.Store(asset);
  185. }
  186. return string.Empty;
  187. }
  188. public bool UpdateContent(string id, byte[] data)
  189. {
  190. return false;
  191. }
  192. public bool Delete(string id)
  193. {
  194. return false;
  195. }
  196. }
  197. }