SnapshotStore.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Text.RegularExpressions;
  33. using System.Xml;
  34. using log4net;
  35. using OpenSim.Region.DataSnapshot.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. namespace OpenSim.Region.DataSnapshot
  38. {
  39. public class SnapshotStore
  40. {
  41. #region Class Members
  42. private String m_directory = "unyuu"; //not an attempt at adding RM references to core SVN, honest
  43. private Dictionary<Scene, bool> m_scenes = null;
  44. private List<IDataSnapshotProvider> m_providers = null;
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private Dictionary<String, String> m_gridinfo = null;
  47. private bool m_cacheEnabled = true;
  48. private string m_listener_port = "9000"; //TODO: Set default port over 9000
  49. private string m_hostname = "127.0.0.1";
  50. #endregion
  51. public SnapshotStore(string directory, Dictionary<String, String> gridinfo, string port, string hostname) {
  52. m_directory = directory;
  53. m_scenes = new Dictionary<Scene, bool>();
  54. m_providers = new List<IDataSnapshotProvider>();
  55. m_gridinfo = gridinfo;
  56. m_listener_port = port;
  57. m_hostname = hostname;
  58. if (Directory.Exists(m_directory))
  59. {
  60. m_log.Info("[DATASNAPSHOT]: Response and fragment cache directory already exists.");
  61. }
  62. else
  63. {
  64. // Try to create the directory.
  65. m_log.Info("[DATASNAPSHOT]: Creating directory " + m_directory);
  66. try
  67. {
  68. Directory.CreateDirectory(m_directory);
  69. }
  70. catch (Exception e)
  71. {
  72. m_log.Error("[DATASNAPSHOT]: Failed to create directory " + m_directory, e);
  73. //This isn't a horrible problem, just disable cacheing.
  74. m_cacheEnabled = false;
  75. m_log.Error("[DATASNAPSHOT]: Could not create directory, response cache has been disabled.");
  76. }
  77. }
  78. }
  79. public void ForceSceneStale(Scene scene) {
  80. m_scenes[scene] = true;
  81. }
  82. #region Fragment storage
  83. public XmlNode GetFragment(IDataSnapshotProvider provider, XmlDocument factory)
  84. {
  85. XmlNode data = null;
  86. if (provider.Stale || !m_cacheEnabled)
  87. {
  88. data = provider.RequestSnapshotData(factory);
  89. if (m_cacheEnabled)
  90. {
  91. String path = DataFileNameFragment(provider.GetParentScene, provider.Name);
  92. try
  93. {
  94. using (XmlTextWriter snapXWriter = new XmlTextWriter(path, Encoding.Default))
  95. {
  96. snapXWriter.Formatting = Formatting.Indented;
  97. snapXWriter.WriteStartDocument();
  98. data.WriteTo(snapXWriter);
  99. snapXWriter.WriteEndDocument();
  100. }
  101. }
  102. catch (Exception e)
  103. {
  104. m_log.WarnFormat("[DATASNAPSHOT]: Exception on writing to file {0}: {1}", path, e.Message);
  105. }
  106. }
  107. //mark provider as not stale, parent scene as stale
  108. provider.Stale = false;
  109. m_scenes[provider.GetParentScene] = true;
  110. m_log.Info("[DATASNAPSHOT]: Generated fragment response for provider type " + provider.Name);
  111. }
  112. else
  113. {
  114. String path = DataFileNameFragment(provider.GetParentScene, provider.Name);
  115. XmlDocument fragDocument = new XmlDocument();
  116. fragDocument.PreserveWhitespace = true;
  117. fragDocument.Load(path);
  118. foreach (XmlNode node in fragDocument)
  119. {
  120. data = factory.ImportNode(node, true);
  121. }
  122. m_log.Info("[DATASNAPSHOT]: Retrieved fragment response for provider type " + provider.Name);
  123. }
  124. return data;
  125. }
  126. #endregion
  127. #region Response storage
  128. public XmlNode GetScene(Scene scene, XmlDocument factory)
  129. {
  130. m_log.Debug("[DATASNAPSHOT]: Data requested for scene " + scene.RegionInfo.RegionName);
  131. if (!m_scenes.ContainsKey(scene)) {
  132. m_scenes.Add(scene, true); //stale by default
  133. }
  134. XmlNode regionElement = null;
  135. if (!m_scenes[scene])
  136. {
  137. m_log.Info("[DATASNAPSHOT]: Attempting to retrieve snapshot from cache.");
  138. //get snapshot from cache
  139. String path = DataFileNameScene(scene);
  140. XmlDocument fragDocument = new XmlDocument();
  141. fragDocument.PreserveWhitespace = true;
  142. fragDocument.Load(path);
  143. foreach (XmlNode node in fragDocument)
  144. {
  145. regionElement = factory.ImportNode(node, true);
  146. }
  147. m_log.Info("[DATASNAPSHOT]: Obtained snapshot from cache for " + scene.RegionInfo.RegionName);
  148. }
  149. else
  150. {
  151. m_log.Info("[DATASNAPSHOT]: Attempting to generate snapshot.");
  152. //make snapshot
  153. regionElement = MakeRegionNode(scene, factory);
  154. regionElement.AppendChild(GetGridSnapshotData(factory));
  155. XmlNode regionData = factory.CreateNode(XmlNodeType.Element, "data", "");
  156. foreach (IDataSnapshotProvider dataprovider in m_providers)
  157. {
  158. if (dataprovider.GetParentScene == scene)
  159. {
  160. regionData.AppendChild(GetFragment(dataprovider, factory));
  161. }
  162. }
  163. regionElement.AppendChild(regionData);
  164. factory.AppendChild(regionElement);
  165. //save snapshot
  166. String path = DataFileNameScene(scene);
  167. try
  168. {
  169. using (XmlTextWriter snapXWriter = new XmlTextWriter(path, Encoding.Default))
  170. {
  171. snapXWriter.Formatting = Formatting.Indented;
  172. snapXWriter.WriteStartDocument();
  173. regionElement.WriteTo(snapXWriter);
  174. snapXWriter.WriteEndDocument();
  175. }
  176. }
  177. catch (Exception e)
  178. {
  179. m_log.WarnFormat("[DATASNAPSHOT]: Exception on writing to file {0}: {1}", path, e.Message);
  180. }
  181. m_scenes[scene] = false;
  182. m_log.Info("[DATASNAPSHOT]: Generated new snapshot for " + scene.RegionInfo.RegionName);
  183. }
  184. return regionElement;
  185. }
  186. #endregion
  187. #region Helpers
  188. private string DataFileNameFragment(Scene scene, String fragmentName)
  189. {
  190. return Path.Combine(m_directory, Path.ChangeExtension(Sanitize(scene.RegionInfo.RegionName + "_" + fragmentName), "xml"));
  191. }
  192. private string DataFileNameScene(Scene scene)
  193. {
  194. return Path.Combine(m_directory, Path.ChangeExtension(Sanitize(scene.RegionInfo.RegionName), "xml"));
  195. //return (m_snapsDir + Path.DirectorySeparatorChar + scene.RegionInfo.RegionName + ".xml");
  196. }
  197. private static string Sanitize(string name)
  198. {
  199. string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
  200. string invalidReStr = string.Format(@"[{0}]", invalidChars);
  201. string newname = Regex.Replace(name, invalidReStr, "_");
  202. return newname.Replace('.', '_');
  203. }
  204. private XmlNode MakeRegionNode(Scene scene, XmlDocument basedoc)
  205. {
  206. XmlNode docElement = basedoc.CreateNode(XmlNodeType.Element, "region", "");
  207. XmlAttribute attr = basedoc.CreateAttribute("category");
  208. attr.Value = GetRegionCategory(scene);
  209. docElement.Attributes.Append(attr);
  210. attr = basedoc.CreateAttribute("entities");
  211. attr.Value = scene.Entities.Count.ToString();
  212. docElement.Attributes.Append(attr);
  213. //attr = basedoc.CreateAttribute("parcels");
  214. //attr.Value = scene.LandManager.landList.Count.ToString();
  215. //docElement.Attributes.Append(attr);
  216. XmlNode infoblock = basedoc.CreateNode(XmlNodeType.Element, "info", "");
  217. XmlNode infopiece = basedoc.CreateNode(XmlNodeType.Element, "uuid", "");
  218. infopiece.InnerText = scene.RegionInfo.RegionID.ToString();
  219. infoblock.AppendChild(infopiece);
  220. infopiece = basedoc.CreateNode(XmlNodeType.Element, "url", "");
  221. infopiece.InnerText = "http://" + m_hostname + ":" + m_listener_port;
  222. infoblock.AppendChild(infopiece);
  223. infopiece = basedoc.CreateNode(XmlNodeType.Element, "name", "");
  224. infopiece.InnerText = scene.RegionInfo.RegionName;
  225. infoblock.AppendChild(infopiece);
  226. infopiece = basedoc.CreateNode(XmlNodeType.Element, "handle", "");
  227. infopiece.InnerText = scene.RegionInfo.RegionHandle.ToString();
  228. infoblock.AppendChild(infopiece);
  229. docElement.AppendChild(infoblock);
  230. m_log.Debug("[DATASNAPSHOT]: Generated region node");
  231. return docElement;
  232. }
  233. private String GetRegionCategory(Scene scene)
  234. {
  235. if (scene.RegionInfo.RegionSettings.Maturity == 0)
  236. return "PG";
  237. if (scene.RegionInfo.RegionSettings.Maturity == 1)
  238. return "Mature";
  239. if (scene.RegionInfo.RegionSettings.Maturity == 2)
  240. return "Adult";
  241. return "Unknown";
  242. }
  243. private XmlNode GetGridSnapshotData(XmlDocument factory)
  244. {
  245. XmlNode griddata = factory.CreateNode(XmlNodeType.Element, "grid", "");
  246. foreach (KeyValuePair<String, String> GridData in m_gridinfo)
  247. {
  248. //TODO: make it lowercase tag names for diva
  249. XmlNode childnode = factory.CreateNode(XmlNodeType.Element, GridData.Key, "");
  250. childnode.InnerText = GridData.Value;
  251. griddata.AppendChild(childnode);
  252. }
  253. m_log.Debug("[DATASNAPSHOT]: Got grid snapshot data");
  254. return griddata;
  255. }
  256. #endregion
  257. #region Manage internal collections
  258. public void AddScene(Scene newScene)
  259. {
  260. m_scenes.Add(newScene, true);
  261. }
  262. public void RemoveScene(Scene deadScene)
  263. {
  264. m_scenes.Remove(deadScene);
  265. }
  266. public void AddProvider(IDataSnapshotProvider newProvider)
  267. {
  268. m_providers.Add(newProvider);
  269. }
  270. public void RemoveProvider(IDataSnapshotProvider deadProvider)
  271. {
  272. m_providers.Remove(deadProvider);
  273. }
  274. #endregion
  275. }
  276. }