SnapshotStore.cs 12 KB

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