1
0

AssetServicesConnector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Communications;
  36. using OpenSim.Services.Interfaces;
  37. using OpenMetaverse;
  38. namespace OpenSim.Services.Connectors
  39. {
  40. public class AssetServicesConnector : IAssetService
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(
  44. MethodBase.GetCurrentMethod().DeclaringType);
  45. private string m_ServerURI = String.Empty;
  46. private IImprovedAssetCache m_Cache = null;
  47. private int m_maxAssetRequestConcurrency = 30;
  48. private delegate void AssetRetrievedEx(AssetBase asset);
  49. // Keeps track of concurrent requests for the same asset, so that it's only loaded once.
  50. // Maps: Asset ID -> Handlers which will be called when the asset has been loaded
  51. private Dictionary<string, AssetRetrievedEx> m_AssetHandlers = new Dictionary<string, AssetRetrievedEx>();
  52. public int MaxAssetRequestConcurrency
  53. {
  54. get { return m_maxAssetRequestConcurrency; }
  55. set { m_maxAssetRequestConcurrency = value; }
  56. }
  57. public AssetServicesConnector()
  58. {
  59. }
  60. public AssetServicesConnector(string serverURI)
  61. {
  62. m_ServerURI = serverURI.TrimEnd('/');
  63. }
  64. public AssetServicesConnector(IConfigSource source)
  65. {
  66. Initialise(source);
  67. }
  68. public virtual void Initialise(IConfigSource source)
  69. {
  70. IConfig netconfig = source.Configs["Network"];
  71. if (netconfig != null)
  72. m_maxAssetRequestConcurrency = netconfig.GetInt("MaxRequestConcurrency",m_maxAssetRequestConcurrency);
  73. IConfig assetConfig = source.Configs["AssetService"];
  74. if (assetConfig == null)
  75. {
  76. m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
  77. throw new Exception("Asset connector init error");
  78. }
  79. string serviceURI = assetConfig.GetString("AssetServerURI",
  80. String.Empty);
  81. if (serviceURI == String.Empty)
  82. {
  83. m_log.Error("[ASSET CONNECTOR]: No Server URI named in section AssetService");
  84. throw new Exception("Asset connector init error");
  85. }
  86. m_ServerURI = serviceURI;
  87. }
  88. protected void SetCache(IImprovedAssetCache cache)
  89. {
  90. m_Cache = cache;
  91. }
  92. public AssetBase Get(string id)
  93. {
  94. // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Synchronous get request for {0}", id);
  95. string uri = m_ServerURI + "/assets/" + id;
  96. AssetBase asset = null;
  97. if (m_Cache != null)
  98. asset = m_Cache.Get(id);
  99. if (asset == null)
  100. {
  101. asset = SynchronousRestObjectRequester.
  102. MakeRequest<int, AssetBase>("GET", uri, 0, m_maxAssetRequestConcurrency);
  103. if (m_Cache != null)
  104. m_Cache.Cache(asset);
  105. }
  106. return asset;
  107. }
  108. public AssetBase GetCached(string id)
  109. {
  110. // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Cache request for {0}", id);
  111. if (m_Cache != null)
  112. return m_Cache.Get(id);
  113. return null;
  114. }
  115. public AssetMetadata GetMetadata(string id)
  116. {
  117. if (m_Cache != null)
  118. {
  119. AssetBase fullAsset = m_Cache.Get(id);
  120. if (fullAsset != null)
  121. return fullAsset.Metadata;
  122. }
  123. string uri = m_ServerURI + "/assets/" + id + "/metadata";
  124. AssetMetadata asset = SynchronousRestObjectRequester.
  125. MakeRequest<int, AssetMetadata>("GET", uri, 0);
  126. return asset;
  127. }
  128. public byte[] GetData(string id)
  129. {
  130. if (m_Cache != null)
  131. {
  132. AssetBase fullAsset = m_Cache.Get(id);
  133. if (fullAsset != null)
  134. return fullAsset.Data;
  135. }
  136. RestClient rc = new RestClient(m_ServerURI);
  137. rc.AddResourcePath("assets");
  138. rc.AddResourcePath(id);
  139. rc.AddResourcePath("data");
  140. rc.RequestMethod = "GET";
  141. Stream s = rc.Request();
  142. if (s == null)
  143. return null;
  144. if (s.Length > 0)
  145. {
  146. byte[] ret = new byte[s.Length];
  147. s.Read(ret, 0, (int)s.Length);
  148. return ret;
  149. }
  150. return null;
  151. }
  152. public bool Get(string id, Object sender, AssetRetrieved handler)
  153. {
  154. // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Potentially asynchronous get request for {0}", id);
  155. string uri = m_ServerURI + "/assets/" + id;
  156. AssetBase asset = null;
  157. if (m_Cache != null)
  158. asset = m_Cache.Get(id);
  159. if (asset == null)
  160. {
  161. lock (m_AssetHandlers)
  162. {
  163. AssetRetrievedEx handlerEx = new AssetRetrievedEx(delegate(AssetBase _asset) { handler(id, sender, _asset); });
  164. AssetRetrievedEx handlers;
  165. if (m_AssetHandlers.TryGetValue(id, out handlers))
  166. {
  167. // Someone else is already loading this asset. It will notify our handler when done.
  168. handlers += handlerEx;
  169. return true;
  170. }
  171. // Load the asset ourselves
  172. handlers += handlerEx;
  173. m_AssetHandlers.Add(id, handlers);
  174. }
  175. bool success = false;
  176. try
  177. {
  178. AsynchronousRestObjectRequester.MakeRequest<int, AssetBase>("GET", uri, 0,
  179. delegate(AssetBase a)
  180. {
  181. if (m_Cache != null)
  182. m_Cache.Cache(a);
  183. AssetRetrievedEx handlers;
  184. lock (m_AssetHandlers)
  185. {
  186. handlers = m_AssetHandlers[id];
  187. m_AssetHandlers.Remove(id);
  188. }
  189. handlers.Invoke(a);
  190. }, m_maxAssetRequestConcurrency);
  191. success = true;
  192. }
  193. finally
  194. {
  195. if (!success)
  196. {
  197. lock (m_AssetHandlers)
  198. {
  199. m_AssetHandlers.Remove(id);
  200. }
  201. }
  202. }
  203. }
  204. else
  205. {
  206. handler(id, sender, asset);
  207. }
  208. return true;
  209. }
  210. public string Store(AssetBase asset)
  211. {
  212. if (asset.Local)
  213. {
  214. if (m_Cache != null)
  215. m_Cache.Cache(asset);
  216. return asset.ID;
  217. }
  218. string uri = m_ServerURI + "/assets/";
  219. string newID = string.Empty;
  220. try
  221. {
  222. newID = SynchronousRestObjectRequester.
  223. MakeRequest<AssetBase, string>("POST", uri, asset);
  224. }
  225. catch (Exception e)
  226. {
  227. m_log.WarnFormat("[ASSET CONNECTOR]: Unable to send asset {0} to asset server. Reason: {1}", asset.ID, e.Message);
  228. }
  229. if (newID != String.Empty)
  230. {
  231. // Placing this here, so that this work with old asset servers that don't send any reply back
  232. // SynchronousRestObjectRequester returns somethins that is not an empty string
  233. if (newID != null)
  234. asset.ID = newID;
  235. if (m_Cache != null)
  236. m_Cache.Cache(asset);
  237. }
  238. return newID;
  239. }
  240. public bool UpdateContent(string id, byte[] data)
  241. {
  242. AssetBase asset = null;
  243. if (m_Cache != null)
  244. asset = m_Cache.Get(id);
  245. if (asset == null)
  246. {
  247. AssetMetadata metadata = GetMetadata(id);
  248. if (metadata == null)
  249. return false;
  250. asset = new AssetBase(metadata.FullID, metadata.Name, metadata.Type, UUID.Zero.ToString());
  251. asset.Metadata = metadata;
  252. }
  253. asset.Data = data;
  254. string uri = m_ServerURI + "/assets/" + id;
  255. if (SynchronousRestObjectRequester.
  256. MakeRequest<AssetBase, bool>("POST", uri, asset))
  257. {
  258. if (m_Cache != null)
  259. m_Cache.Cache(asset);
  260. return true;
  261. }
  262. return false;
  263. }
  264. public bool Delete(string id)
  265. {
  266. string uri = m_ServerURI + "/assets/" + id;
  267. if (SynchronousRestObjectRequester.
  268. MakeRequest<int, bool>("DELETE", uri, 0))
  269. {
  270. if (m_Cache != null)
  271. m_Cache.Expire(id);
  272. return true;
  273. }
  274. return false;
  275. }
  276. }
  277. }