aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/DataSnapshot/SnapshotStore.cs
diff options
context:
space:
mode:
authorCharles Krinke2008-05-17 15:47:08 +0000
committerCharles Krinke2008-05-17 15:47:08 +0000
commitf6a4f8844f01fb756bbc26d65c252fc07ab8c2bf (patch)
treeda27e199a03ec4afd829a3ab9b06deab47cd02a3 /OpenSim/Region/DataSnapshot/SnapshotStore.cs
parentwhile this doesn't fix the initial no pants issue in grid (which still baffle... (diff)
downloadopensim-SC_OLD-f6a4f8844f01fb756bbc26d65c252fc07ab8c2bf.zip
opensim-SC_OLD-f6a4f8844f01fb756bbc26d65c252fc07ab8c2bf.tar.gz
opensim-SC_OLD-f6a4f8844f01fb756bbc26d65c252fc07ab8c2bf.tar.bz2
opensim-SC_OLD-f6a4f8844f01fb756bbc26d65c252fc07ab8c2bf.tar.xz
Thank you very much KMeisthax for DataSnapshot 1.1
to enhance search capability on OpenSim sims using external search engines such as Metaversink.com and others.
Diffstat (limited to 'OpenSim/Region/DataSnapshot/SnapshotStore.cs')
-rw-r--r--OpenSim/Region/DataSnapshot/SnapshotStore.cs316
1 files changed, 316 insertions, 0 deletions
diff --git a/OpenSim/Region/DataSnapshot/SnapshotStore.cs b/OpenSim/Region/DataSnapshot/SnapshotStore.cs
new file mode 100644
index 0000000..05d5640
--- /dev/null
+++ b/OpenSim/Region/DataSnapshot/SnapshotStore.cs
@@ -0,0 +1,316 @@
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 OpenSim 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*/
28
29using System;
30using System.Collections.Generic;
31using System.Text;
32using System.Xml;
33using System.IO;
34using OpenSim.Region.Environment.Scenes;
35using OpenSim.Region.DataSnapshot.Interfaces;
36using libsecondlife;
37
38namespace OpenSim.Region.DataSnapshot
39{
40 public class SnapshotStore
41 {
42 #region Class Members
43 private String m_directory = "unyuu"; //not an attempt at adding RM references to core SVN, honest
44 private Dictionary<Scene, bool> m_scenes = null;
45 private List<IDataSnapshotProvider> m_providers = null;
46 private log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
47 private Dictionary<String, String> m_gridinfo = null;
48 private bool m_cacheEnabled = true;
49 private string m_listener_port = "9000"; //TODO: Set default port over 9000
50 private string m_hostname = "127.0.0.1";
51 #endregion
52
53 public SnapshotStore(string directory, Dictionary<String, String> gridinfo, string port, string hostname) {
54 m_directory = directory;
55 m_scenes = new Dictionary<Scene, bool>();
56 m_providers = new List<IDataSnapshotProvider>();
57 m_gridinfo = gridinfo;
58 m_listener_port = port;
59 m_hostname = hostname;
60
61 if (Directory.Exists(m_directory))
62 {
63 m_log.Info("[DATASNAPSHOT]: Repsonse and fragment cache directory already exists.");
64 }
65 else
66 {
67 // Try to create the directory.
68 m_log.Info("[DATASNAPSHOT]: Creating directory " + m_directory);
69 try
70 {
71 Directory.CreateDirectory(m_directory);
72 }
73 catch (Exception e)
74 {
75 m_log.Error("[DATASNAPSHOT]: Failed to create directory " + m_directory, e);
76
77 //This isn't a horrible problem, just disable cacheing.
78 m_cacheEnabled = false;
79 m_log.Error("[DATASNAPSHOT]: Could not create directory, response cache has been disabled.");
80 }
81 }
82 }
83
84 public void ForceSceneStale(Scene scene) {
85 m_scenes[scene] = true;
86 }
87
88 #region Fragment storage
89 public XmlNode GetFragment(IDataSnapshotProvider provider, XmlDocument factory)
90 {
91 XmlNode data = null;
92
93 if (provider.Stale || !m_cacheEnabled)
94 {
95 data = provider.RequestSnapshotData(factory);
96
97 if (m_cacheEnabled)
98 {
99 String path = DataFileNameFragment(provider.GetParentScene, provider.Name);
100
101 using (XmlTextWriter snapXWriter = new XmlTextWriter(path, Encoding.Default))
102 {
103 snapXWriter.Formatting = Formatting.Indented;
104 snapXWriter.WriteStartDocument();
105 data.WriteTo(snapXWriter);
106 snapXWriter.WriteEndDocument();
107 }
108 }
109
110 //mark provider as not stale, parent scene as stale
111 provider.Stale = false;
112 m_scenes[provider.GetParentScene] = true;
113
114 m_log.Info("[DATASNAPSHOT]: Generated fragment response for provider type " + provider.Name);
115 }
116 else
117 {
118 String path = DataFileNameFragment(provider.GetParentScene, provider.Name);
119
120 XmlDocument fragDocument = new XmlDocument();
121 fragDocument.PreserveWhitespace = true;
122 fragDocument.Load(path);
123 foreach (XmlNode node in fragDocument)
124 {
125 data = factory.ImportNode(node, true);
126 }
127
128 m_log.Info("[DATASNAPSHOT]: Retrieved fragment response for provider type " + provider.Name);
129 }
130
131 return data;
132 }
133 #endregion
134
135 #region Response storage
136 public XmlNode GetScene(Scene scene, XmlDocument factory)
137 {
138 m_log.Debug("[DATASNAPSHOT]: Data requested for scene " + scene.RegionInfo.RegionName);
139
140 if (!m_scenes.ContainsKey(scene)) {
141 m_scenes.Add(scene, true); //stale by default
142 }
143
144 XmlNode regionElement = null;
145
146 if (!m_scenes[scene])
147 {
148 m_log.Info("[DATASNAPSHOT]: Attempting to retrieve snapshot from cache.");
149 //get snapshot from cache
150 String path = DataFileNameScene(scene);
151
152 XmlDocument fragDocument = new XmlDocument();
153 fragDocument.PreserveWhitespace = true;
154
155 fragDocument.Load(path);
156
157 foreach (XmlNode node in fragDocument)
158 {
159 regionElement = factory.ImportNode(node, true);
160 }
161
162 m_log.Info("[DATASNAPSHOT]: Obtained snapshot from cache for " + scene.RegionInfo.RegionName);
163 }
164 else
165 {
166 m_log.Info("[DATASNAPSHOT]: Attempting to generate snapshot.");
167 //make snapshot
168 regionElement = MakeRegionNode(scene, factory);
169
170 regionElement.AppendChild(GetGridSnapshotData(factory));
171 XmlNode regionData = factory.CreateNode(XmlNodeType.Element, "data", "");
172
173 foreach (IDataSnapshotProvider dataprovider in m_providers)
174 {
175 if (dataprovider.GetParentScene == scene)
176 {
177 regionData.AppendChild(GetFragment(dataprovider, factory));
178 }
179 }
180
181 regionElement.AppendChild(regionData);
182
183 factory.AppendChild(regionElement);
184
185 //save snapshot
186 String path = DataFileNameScene(scene);
187
188 using (XmlTextWriter snapXWriter = new XmlTextWriter(path, Encoding.Default))
189 {
190 snapXWriter.Formatting = Formatting.Indented;
191 snapXWriter.WriteStartDocument();
192 regionElement.WriteTo(snapXWriter);
193 snapXWriter.WriteEndDocument();
194 }
195
196 m_scenes[scene] = false;
197
198 m_log.Info("[DATASNAPSHOT]: Generated new snapshot for " + scene.RegionInfo.RegionName);
199 }
200
201 return regionElement;
202 }
203
204 #endregion
205
206 #region Helpers
207 private string DataFileNameFragment(Scene scene, String fragmentName)
208 {
209 return Path.Combine(m_directory, Path.ChangeExtension(scene.RegionInfo.RegionName + "_" + fragmentName, "xml"));
210 }
211
212 private string DataFileNameScene(Scene scene)
213 {
214 return Path.Combine(m_directory, Path.ChangeExtension(scene.RegionInfo.RegionName, "xml"));
215 //return (m_snapsDir + Path.DirectorySeparatorChar + scene.RegionInfo.RegionName + ".xml");
216 }
217
218 private XmlNode MakeRegionNode(Scene scene, XmlDocument basedoc)
219 {
220 XmlNode docElement = basedoc.CreateNode(XmlNodeType.Element, "region", "");
221
222 XmlAttribute attr = basedoc.CreateAttribute("category");
223 attr.Value = GetRegionCategory(scene);
224 docElement.Attributes.Append(attr);
225
226 attr = basedoc.CreateAttribute("entities");
227 attr.Value = scene.Entities.Count.ToString();
228 docElement.Attributes.Append(attr);
229
230 //attr = basedoc.CreateAttribute("parcels");
231 //attr.Value = scene.LandManager.landList.Count.ToString();
232 //docElement.Attributes.Append(attr);
233
234
235 XmlNode infoblock = basedoc.CreateNode(XmlNodeType.Element, "info", "");
236
237 XmlNode infopiece = basedoc.CreateNode(XmlNodeType.Element, "uuid", "");
238 infopiece.InnerText = scene.RegionInfo.RegionID.ToString();
239 infoblock.AppendChild(infopiece);
240
241 infopiece = basedoc.CreateNode(XmlNodeType.Element, "url", "");
242 infopiece.InnerText = "http://" + m_hostname + ":" + m_listener_port;
243 infoblock.AppendChild(infopiece);
244
245 infopiece = basedoc.CreateNode(XmlNodeType.Element, "name", "");
246 infopiece.InnerText = scene.RegionInfo.RegionName;
247 infoblock.AppendChild(infopiece);
248
249 docElement.AppendChild(infoblock);
250
251 m_log.Debug("[DATASNAPSHOT]: Generated region node");
252 return docElement;
253 }
254
255 private String GetRegionCategory(Scene scene)
256 {
257
258 //Boolean choice between:
259 // "PG" - Mormontown
260 // "Mature" - Sodom and Gomorrah
261 // (Depreciated) "Patriotic Nigra Testing Sandbox" - Abandon Hope All Ye Who Enter Here
262 if ((scene.RegionInfo.EstateSettings.simAccess & Simulator.SimAccess.Mature) == Simulator.SimAccess.Mature)
263 {
264 return "Mature";
265 }
266 else if ((scene.RegionInfo.EstateSettings.simAccess & Simulator.SimAccess.PG) == Simulator.SimAccess.PG)
267 {
268 return "PG";
269 }
270 else
271 {
272 return "Unknown";
273 }
274 }
275
276 private XmlNode GetGridSnapshotData(XmlDocument factory)
277 {
278 XmlNode griddata = factory.CreateNode(XmlNodeType.Element, "grid", "");
279
280 foreach (KeyValuePair<String, String> GridData in m_gridinfo)
281 {
282 //TODO: make it lowercase tag names for diva
283 XmlNode childnode = factory.CreateNode(XmlNodeType.Element, GridData.Key, "");
284 childnode.InnerText = GridData.Value;
285 griddata.AppendChild(childnode);
286 }
287
288 m_log.Debug("[DATASNAPSHOT]: Got grid snapshot data");
289
290 return griddata;
291 }
292 #endregion
293
294 #region Manage internal collections
295 public void AddScene(Scene newScene)
296 {
297 m_scenes.Add(newScene, true);
298 }
299
300 public void RemoveScene(Scene deadScene)
301 {
302 m_scenes.Remove(deadScene);
303 }
304
305 public void AddProvider(IDataSnapshotProvider newProvider)
306 {
307 m_providers.Add(newProvider);
308 }
309
310 public void RemoveProvider(IDataSnapshotProvider deadProvider)
311 {
312 m_providers.Remove(deadProvider);
313 }
314 #endregion
315 }
316}