1
0

HGAssetServiceConnector.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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, bool dummy)
  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 (id.Equals(Util.UUIDZeroString))
  115. return false;
  116. if (Util.ParseForeignAssetID(id, out url, out assetID) > 0)
  117. {
  118. IAssetService connector = GetConnector(url);
  119. return connector.Get(assetID, sender, handler);
  120. }
  121. return false;
  122. }
  123. public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
  124. {
  125. return;
  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. var url2assets = new Dictionary<string, List<AssetAndIndex>>();
  140. for (int i = 0; i < ids.Length; i++)
  141. {
  142. string url = string.Empty;
  143. string assetID = string.Empty;
  144. if (Util.ParseForeignAssetID(ids[i], out url, out assetID) > 0)
  145. {
  146. if (!url2assets.ContainsKey(url))
  147. url2assets.Add(url, new List<AssetAndIndex>());
  148. url2assets[url].Add(new AssetAndIndex(UUID.Parse(assetID), i));
  149. }
  150. }
  151. // Query each of the servers in turn
  152. bool[] exist = new bool[ids.Length];
  153. foreach (string url in url2assets.Keys)
  154. {
  155. AssetServicesConnector connector = GetConnector(url);
  156. lock (connector.ConnectorLock)
  157. {
  158. List<AssetAndIndex> curAssets = url2assets[url];
  159. string[] assetIDs = curAssets.ConvertAll(a => a.assetID.ToString()).ToArray();
  160. bool[] curExist = connector.AssetsExist(assetIDs);
  161. int i = 0;
  162. foreach (AssetAndIndex ai in curAssets)
  163. {
  164. exist[ai.index] = curExist[i];
  165. ++i;
  166. }
  167. }
  168. }
  169. return exist;
  170. }
  171. public string Store(AssetBase asset)
  172. {
  173. string url = string.Empty;
  174. string assetID = string.Empty;
  175. if (Util.ParseForeignAssetID(asset.ID, out url, out assetID) > 0)
  176. {
  177. AssetServicesConnector connector = GetConnector(url);
  178. // Restore the assetID to a simple UUID
  179. asset.ID = assetID;
  180. lock ((connector.ConnectorLock))
  181. return connector.Store(asset);
  182. }
  183. return string.Empty;
  184. }
  185. public bool UpdateContent(string id, byte[] data)
  186. {
  187. return false;
  188. }
  189. public bool Delete(string id)
  190. {
  191. return false;
  192. }
  193. }
  194. }