HGAssetServiceConnector.cs 7.4 KB

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