HGAssetServiceConnector.cs 8.4 KB

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