HGAssetServiceConnector.cs 8.8 KB

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