AssetServiceConnector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 delegate void AssetRetrievedEx(AssetBase asset);
  48. // Keeps track of concurrent requests for the same asset, so that it's only loaded once.
  49. // Maps: Asset ID -> Handlers which will be called when the asset has been loaded
  50. private Dictionary<string, AssetRetrievedEx> m_AssetHandlers = new Dictionary<string, AssetRetrievedEx>();
  51. public AssetServicesConnector()
  52. {
  53. }
  54. public AssetServicesConnector(string serverURI)
  55. {
  56. m_ServerURI = serverURI.TrimEnd('/');
  57. }
  58. public AssetServicesConnector(IConfigSource source)
  59. {
  60. Initialise(source);
  61. }
  62. public virtual void Initialise(IConfigSource source)
  63. {
  64. IConfig assetConfig = source.Configs["AssetService"];
  65. if (assetConfig == null)
  66. {
  67. m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
  68. throw new Exception("Asset connector init error");
  69. }
  70. string serviceURI = assetConfig.GetString("AssetServerURI",
  71. String.Empty);
  72. if (serviceURI == String.Empty)
  73. {
  74. m_log.Error("[ASSET CONNECTOR]: No Server URI named in section AssetService");
  75. throw new Exception("Asset connector init error");
  76. }
  77. m_ServerURI = serviceURI;
  78. MainConsole.Instance.Commands.AddCommand("asset", false, "dump asset",
  79. "dump asset <id> <file>",
  80. "dump one cached asset", HandleDumpAsset);
  81. }
  82. protected void SetCache(IImprovedAssetCache cache)
  83. {
  84. m_Cache = cache;
  85. }
  86. public AssetBase Get(string id)
  87. {
  88. string uri = m_ServerURI + "/assets/" + id;
  89. AssetBase asset = null;
  90. if (m_Cache != null)
  91. asset = m_Cache.Get(id);
  92. if (asset == null)
  93. {
  94. asset = SynchronousRestObjectRequester.
  95. MakeRequest<int, AssetBase>("GET", uri, 0);
  96. if (m_Cache != null)
  97. m_Cache.Cache(asset);
  98. }
  99. return asset;
  100. }
  101. public AssetBase GetCached(string id)
  102. {
  103. if (m_Cache != null)
  104. return m_Cache.Get(id);
  105. return null;
  106. }
  107. public AssetMetadata GetMetadata(string id)
  108. {
  109. if (m_Cache != null)
  110. {
  111. AssetBase fullAsset = m_Cache.Get(id);
  112. if (fullAsset != null)
  113. return fullAsset.Metadata;
  114. }
  115. string uri = m_ServerURI + "/assets/" + id + "/metadata";
  116. AssetMetadata asset = SynchronousRestObjectRequester.
  117. MakeRequest<int, AssetMetadata>("GET", uri, 0);
  118. return asset;
  119. }
  120. public byte[] GetData(string id)
  121. {
  122. if (m_Cache != null)
  123. {
  124. AssetBase fullAsset = m_Cache.Get(id);
  125. if (fullAsset != null)
  126. return fullAsset.Data;
  127. }
  128. RestClient rc = new RestClient(m_ServerURI);
  129. rc.AddResourcePath("assets");
  130. rc.AddResourcePath(id);
  131. rc.AddResourcePath("data");
  132. rc.RequestMethod = "GET";
  133. Stream s = rc.Request();
  134. if (s == null)
  135. return null;
  136. if (s.Length > 0)
  137. {
  138. byte[] ret = new byte[s.Length];
  139. s.Read(ret, 0, (int)s.Length);
  140. return ret;
  141. }
  142. return null;
  143. }
  144. public bool Get(string id, Object sender, AssetRetrieved handler)
  145. {
  146. string uri = m_ServerURI + "/assets/" + id;
  147. AssetBase asset = null;
  148. if (m_Cache != null)
  149. asset = m_Cache.Get(id);
  150. if (asset == null)
  151. {
  152. lock (m_AssetHandlers)
  153. {
  154. AssetRetrievedEx handlerEx = new AssetRetrievedEx(delegate(AssetBase _asset) { handler(id, sender, _asset); });
  155. AssetRetrievedEx handlers;
  156. if (m_AssetHandlers.TryGetValue(id, out handlers))
  157. {
  158. // Someone else is already loading this asset. It will notify our handler when done.
  159. handlers += handlerEx;
  160. return true;
  161. }
  162. // Load the asset ourselves
  163. handlers += handlerEx;
  164. m_AssetHandlers.Add(id, handlers);
  165. }
  166. bool success = false;
  167. try
  168. {
  169. AsynchronousRestObjectRequester.MakeRequest<int, AssetBase>("GET", uri, 0,
  170. delegate(AssetBase a)
  171. {
  172. if (m_Cache != null)
  173. m_Cache.Cache(a);
  174. AssetRetrievedEx handlers;
  175. lock (m_AssetHandlers)
  176. {
  177. handlers = m_AssetHandlers[id];
  178. m_AssetHandlers.Remove(id);
  179. }
  180. handlers.Invoke(a);
  181. });
  182. success = true;
  183. }
  184. finally
  185. {
  186. if (!success)
  187. {
  188. lock (m_AssetHandlers)
  189. {
  190. m_AssetHandlers.Remove(id);
  191. }
  192. }
  193. }
  194. }
  195. else
  196. {
  197. handler(id, sender, asset);
  198. }
  199. return true;
  200. }
  201. public string Store(AssetBase asset)
  202. {
  203. if (asset.Temporary || asset.Local)
  204. {
  205. if (m_Cache != null)
  206. m_Cache.Cache(asset);
  207. return asset.ID;
  208. }
  209. string uri = m_ServerURI + "/assets/";
  210. string newID = string.Empty;
  211. try
  212. {
  213. newID = SynchronousRestObjectRequester.
  214. MakeRequest<AssetBase, string>("POST", uri, asset);
  215. }
  216. catch (Exception e)
  217. {
  218. m_log.WarnFormat("[ASSET CONNECTOR]: Unable to send asset {0} to asset server. Reason: {1}", asset.ID, e.Message);
  219. }
  220. if (newID != String.Empty)
  221. {
  222. // Placing this here, so that this work with old asset servers that don't send any reply back
  223. // SynchronousRestObjectRequester returns somethins that is not an empty string
  224. if (newID != null)
  225. asset.ID = newID;
  226. if (m_Cache != null)
  227. m_Cache.Cache(asset);
  228. }
  229. return newID;
  230. }
  231. public bool UpdateContent(string id, byte[] data)
  232. {
  233. AssetBase asset = null;
  234. if (m_Cache != null)
  235. asset = m_Cache.Get(id);
  236. if (asset == null)
  237. {
  238. AssetMetadata metadata = GetMetadata(id);
  239. if (metadata == null)
  240. return false;
  241. asset = new AssetBase(metadata.FullID, metadata.Name, metadata.Type, UUID.Zero.ToString());
  242. asset.Metadata = metadata;
  243. }
  244. asset.Data = data;
  245. string uri = m_ServerURI + "/assets/" + id;
  246. if (SynchronousRestObjectRequester.
  247. MakeRequest<AssetBase, bool>("POST", uri, asset))
  248. {
  249. if (m_Cache != null)
  250. m_Cache.Cache(asset);
  251. return true;
  252. }
  253. return false;
  254. }
  255. public bool Delete(string id)
  256. {
  257. string uri = m_ServerURI + "/assets/" + id;
  258. if (SynchronousRestObjectRequester.
  259. MakeRequest<int, bool>("DELETE", uri, 0))
  260. {
  261. if (m_Cache != null)
  262. m_Cache.Expire(id);
  263. return true;
  264. }
  265. return false;
  266. }
  267. private void HandleDumpAsset(string module, string[] args)
  268. {
  269. if (args.Length != 4)
  270. {
  271. MainConsole.Instance.Output("Syntax: dump asset <id> <file>");
  272. return;
  273. }
  274. UUID assetID;
  275. if (!UUID.TryParse(args[2], out assetID))
  276. {
  277. MainConsole.Instance.Output("Invalid asset ID");
  278. return;
  279. }
  280. if (m_Cache == null)
  281. {
  282. MainConsole.Instance.Output("Instance uses no cache");
  283. return;
  284. }
  285. AssetBase asset = m_Cache.Get(assetID.ToString());
  286. if (asset == null)
  287. {
  288. MainConsole.Instance.Output("Asset not found in cache");
  289. return;
  290. }
  291. string fileName = args[3];
  292. FileStream fs = File.Create(fileName);
  293. fs.Write(asset.Data, 0, asset.Data.Length);
  294. fs.Close();
  295. }
  296. }
  297. }