diff options
Diffstat (limited to 'OpenSim/Region/DataSnapshot/DataSnapshotManager.cs')
-rw-r--r-- | OpenSim/Region/DataSnapshot/DataSnapshotManager.cs | 457 |
1 files changed, 0 insertions, 457 deletions
diff --git a/OpenSim/Region/DataSnapshot/DataSnapshotManager.cs b/OpenSim/Region/DataSnapshot/DataSnapshotManager.cs deleted file mode 100644 index 5e62f23..0000000 --- a/OpenSim/Region/DataSnapshot/DataSnapshotManager.cs +++ /dev/null | |||
@@ -1,457 +0,0 @@ | |||
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 | */ | ||
28 | |||
29 | using System; | ||
30 | using System.Collections.Generic; | ||
31 | using System.IO; | ||
32 | using System.Net; | ||
33 | using System.Reflection; | ||
34 | using System.Xml; | ||
35 | using log4net; | ||
36 | using Nini.Config; | ||
37 | using OpenMetaverse; | ||
38 | using Mono.Addins; | ||
39 | using OpenSim.Framework; | ||
40 | using OpenSim.Framework.Communications; | ||
41 | using OpenSim.Region.DataSnapshot.Interfaces; | ||
42 | using OpenSim.Region.Framework.Interfaces; | ||
43 | using OpenSim.Region.Framework.Scenes; | ||
44 | |||
45 | [assembly: Addin("DataSnapshot", "0.1")] | ||
46 | [assembly: AddinDependency("OpenSim", "0.5")] | ||
47 | |||
48 | namespace OpenSim.Region.DataSnapshot | ||
49 | { | ||
50 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "DataSnapshotManager")] | ||
51 | public class DataSnapshotManager : ISharedRegionModule, IDataSnapshot | ||
52 | { | ||
53 | #region Class members | ||
54 | //Information from config | ||
55 | private bool m_enabled = false; | ||
56 | private bool m_configLoaded = false; | ||
57 | private List<string> m_disabledModules = new List<string>(); | ||
58 | private Dictionary<string, string> m_gridinfo = new Dictionary<string, string>(); | ||
59 | private string m_snapsDir = "DataSnapshot"; | ||
60 | private string m_exposure_level = "minimum"; | ||
61 | |||
62 | //Lists of stuff we need | ||
63 | private List<Scene> m_scenes = new List<Scene>(); | ||
64 | private List<IDataSnapshotProvider> m_dataproviders = new List<IDataSnapshotProvider>(); | ||
65 | |||
66 | //Various internal objects | ||
67 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
68 | internal object m_syncInit = new object(); | ||
69 | |||
70 | //DataServices and networking | ||
71 | private string m_dataServices = "noservices"; | ||
72 | public string m_listener_port = ConfigSettings.DefaultRegionHttpPort.ToString(); | ||
73 | public string m_hostname = "127.0.0.1"; | ||
74 | private UUID m_Secret = UUID.Random(); | ||
75 | private bool m_servicesNotified = false; | ||
76 | |||
77 | //Update timers | ||
78 | private int m_period = 20; // in seconds | ||
79 | private int m_maxStales = 500; | ||
80 | private int m_stales = 0; | ||
81 | private int m_lastUpdate = 0; | ||
82 | |||
83 | //Program objects | ||
84 | private SnapshotStore m_snapStore = null; | ||
85 | |||
86 | #endregion | ||
87 | |||
88 | #region Properties | ||
89 | |||
90 | public string ExposureLevel | ||
91 | { | ||
92 | get { return m_exposure_level; } | ||
93 | } | ||
94 | |||
95 | public UUID Secret | ||
96 | { | ||
97 | get { return m_Secret; } | ||
98 | } | ||
99 | |||
100 | #endregion | ||
101 | |||
102 | #region Region Module interface | ||
103 | |||
104 | public void Initialise(IConfigSource config) | ||
105 | { | ||
106 | if (!m_configLoaded) | ||
107 | { | ||
108 | m_configLoaded = true; | ||
109 | //m_log.Debug("[DATASNAPSHOT]: Loading configuration"); | ||
110 | //Read from the config for options | ||
111 | lock (m_syncInit) | ||
112 | { | ||
113 | try | ||
114 | { | ||
115 | m_enabled = config.Configs["DataSnapshot"].GetBoolean("index_sims", m_enabled); | ||
116 | IConfig conf = config.Configs["GridService"]; | ||
117 | if (conf != null) | ||
118 | m_gridinfo.Add("gatekeeperURL", conf.GetString("Gatekeeper", String.Empty)); | ||
119 | |||
120 | m_gridinfo.Add( | ||
121 | "name", config.Configs["DataSnapshot"].GetString("gridname", "the lost continent of hippo")); | ||
122 | m_exposure_level = config.Configs["DataSnapshot"].GetString("data_exposure", m_exposure_level); | ||
123 | m_period = config.Configs["DataSnapshot"].GetInt("default_snapshot_period", m_period); | ||
124 | m_maxStales = config.Configs["DataSnapshot"].GetInt("max_changes_before_update", m_maxStales); | ||
125 | m_snapsDir = config.Configs["DataSnapshot"].GetString("snapshot_cache_directory", m_snapsDir); | ||
126 | m_dataServices = config.Configs["DataSnapshot"].GetString("data_services", m_dataServices); | ||
127 | m_listener_port = config.Configs["Network"].GetString("http_listener_port", m_listener_port); | ||
128 | |||
129 | String[] annoying_string_array = config.Configs["DataSnapshot"].GetString("disable_modules", "").Split(".".ToCharArray()); | ||
130 | foreach (String bloody_wanker in annoying_string_array) | ||
131 | { | ||
132 | m_disabledModules.Add(bloody_wanker); | ||
133 | } | ||
134 | m_lastUpdate = Environment.TickCount; | ||
135 | } | ||
136 | catch (Exception) | ||
137 | { | ||
138 | m_log.Warn("[DATASNAPSHOT]: Could not load configuration. DataSnapshot will be disabled."); | ||
139 | m_enabled = false; | ||
140 | return; | ||
141 | } | ||
142 | |||
143 | if (m_enabled) | ||
144 | m_snapStore = new SnapshotStore(m_snapsDir, m_gridinfo, m_listener_port, m_hostname); | ||
145 | } | ||
146 | |||
147 | } | ||
148 | |||
149 | } | ||
150 | |||
151 | public void AddRegion(Scene scene) | ||
152 | { | ||
153 | if (!m_enabled) | ||
154 | return; | ||
155 | |||
156 | m_log.DebugFormat("[DATASNAPSHOT]: Module added to Scene {0}.", scene.RegionInfo.RegionName); | ||
157 | |||
158 | m_snapStore.AddScene(scene); | ||
159 | m_scenes.Add(scene); | ||
160 | |||
161 | Assembly currentasm = Assembly.GetExecutingAssembly(); | ||
162 | |||
163 | foreach (Type pluginType in currentasm.GetTypes()) | ||
164 | { | ||
165 | if (pluginType.IsPublic) | ||
166 | { | ||
167 | if (!pluginType.IsAbstract) | ||
168 | { | ||
169 | if (pluginType.GetInterface("IDataSnapshotProvider") != null) | ||
170 | { | ||
171 | IDataSnapshotProvider module = (IDataSnapshotProvider)Activator.CreateInstance(pluginType); | ||
172 | module.Initialize(scene, this); | ||
173 | module.OnStale += MarkDataStale; | ||
174 | |||
175 | m_dataproviders.Add(module); | ||
176 | m_snapStore.AddProvider(module); | ||
177 | |||
178 | m_log.Debug("[DATASNAPSHOT]: Added new data provider type: " + pluginType.Name); | ||
179 | } | ||
180 | } | ||
181 | } | ||
182 | } | ||
183 | |||
184 | // Must be done here because on shared modules, PostInitialise() will run | ||
185 | // BEFORE any scenes are registered. There is no "all scenes have been loaded" | ||
186 | // kind of callback because scenes may be created dynamically, so we cannot | ||
187 | // have that info, ever. | ||
188 | if (!m_servicesNotified) | ||
189 | { | ||
190 | //Hand it the first scene, assuming that all scenes have the same BaseHTTPServer | ||
191 | new DataRequestHandler(m_scenes[0], this); | ||
192 | |||
193 | m_hostname = m_scenes[0].RegionInfo.ExternalHostName; | ||
194 | |||
195 | if (m_dataServices != "" && m_dataServices != "noservices") | ||
196 | NotifyDataServices(m_dataServices, "online"); | ||
197 | |||
198 | m_servicesNotified = true; | ||
199 | } | ||
200 | } | ||
201 | |||
202 | public void RemoveRegion(Scene scene) | ||
203 | { | ||
204 | if (!m_enabled) | ||
205 | return; | ||
206 | |||
207 | m_log.Info("[DATASNAPSHOT]: Region " + scene.RegionInfo.RegionName + " is being removed, removing from indexing"); | ||
208 | Scene restartedScene = SceneForUUID(scene.RegionInfo.RegionID); | ||
209 | |||
210 | m_scenes.Remove(restartedScene); | ||
211 | m_snapStore.RemoveScene(restartedScene); | ||
212 | |||
213 | //Getting around the fact that we can't remove objects from a collection we are enumerating over | ||
214 | List<IDataSnapshotProvider> providersToRemove = new List<IDataSnapshotProvider>(); | ||
215 | |||
216 | foreach (IDataSnapshotProvider provider in m_dataproviders) | ||
217 | { | ||
218 | if (provider.GetParentScene == restartedScene) | ||
219 | { | ||
220 | providersToRemove.Add(provider); | ||
221 | } | ||
222 | } | ||
223 | |||
224 | foreach (IDataSnapshotProvider provider in providersToRemove) | ||
225 | { | ||
226 | m_dataproviders.Remove(provider); | ||
227 | m_snapStore.RemoveProvider(provider); | ||
228 | } | ||
229 | |||
230 | m_snapStore.RemoveScene(restartedScene); | ||
231 | } | ||
232 | |||
233 | public void PostInitialise() | ||
234 | { | ||
235 | } | ||
236 | |||
237 | public void RegionLoaded(Scene scene) | ||
238 | { | ||
239 | if (!m_enabled) | ||
240 | return; | ||
241 | |||
242 | m_log.DebugFormat("[DATASNAPSHOT]: Marking scene {0} as stale.", scene.RegionInfo.RegionName); | ||
243 | m_snapStore.ForceSceneStale(scene); | ||
244 | } | ||
245 | |||
246 | public void Close() | ||
247 | { | ||
248 | if (!m_enabled) | ||
249 | return; | ||
250 | |||
251 | if (m_enabled && m_dataServices != "" && m_dataServices != "noservices") | ||
252 | NotifyDataServices(m_dataServices, "offline"); | ||
253 | } | ||
254 | |||
255 | |||
256 | public string Name | ||
257 | { | ||
258 | get { return "External Data Generator"; } | ||
259 | } | ||
260 | |||
261 | public Type ReplaceableInterface | ||
262 | { | ||
263 | get { return null; } | ||
264 | } | ||
265 | |||
266 | #endregion | ||
267 | |||
268 | #region Associated helper functions | ||
269 | |||
270 | public Scene SceneForName(string name) | ||
271 | { | ||
272 | foreach (Scene scene in m_scenes) | ||
273 | if (scene.RegionInfo.RegionName == name) | ||
274 | return scene; | ||
275 | |||
276 | return null; | ||
277 | } | ||
278 | |||
279 | public Scene SceneForUUID(UUID id) | ||
280 | { | ||
281 | foreach (Scene scene in m_scenes) | ||
282 | if (scene.RegionInfo.RegionID == id) | ||
283 | return scene; | ||
284 | |||
285 | return null; | ||
286 | } | ||
287 | |||
288 | #endregion | ||
289 | |||
290 | #region [Public] Snapshot storage functions | ||
291 | |||
292 | /** | ||
293 | * Reply to the http request | ||
294 | */ | ||
295 | public XmlDocument GetSnapshot(string regionName) | ||
296 | { | ||
297 | CheckStale(); | ||
298 | |||
299 | XmlDocument requestedSnap = new XmlDocument(); | ||
300 | requestedSnap.AppendChild(requestedSnap.CreateXmlDeclaration("1.0", null, null)); | ||
301 | requestedSnap.AppendChild(requestedSnap.CreateWhitespace("\r\n")); | ||
302 | |||
303 | XmlNode regiondata = requestedSnap.CreateNode(XmlNodeType.Element, "regiondata", ""); | ||
304 | try | ||
305 | { | ||
306 | if (regionName == null || regionName == "") | ||
307 | { | ||
308 | XmlNode timerblock = requestedSnap.CreateNode(XmlNodeType.Element, "expire", ""); | ||
309 | timerblock.InnerText = m_period.ToString(); | ||
310 | regiondata.AppendChild(timerblock); | ||
311 | |||
312 | regiondata.AppendChild(requestedSnap.CreateWhitespace("\r\n")); | ||
313 | foreach (Scene scene in m_scenes) | ||
314 | { | ||
315 | regiondata.AppendChild(m_snapStore.GetScene(scene, requestedSnap)); | ||
316 | } | ||
317 | } | ||
318 | else | ||
319 | { | ||
320 | Scene scene = SceneForName(regionName); | ||
321 | regiondata.AppendChild(m_snapStore.GetScene(scene, requestedSnap)); | ||
322 | } | ||
323 | requestedSnap.AppendChild(regiondata); | ||
324 | regiondata.AppendChild(requestedSnap.CreateWhitespace("\r\n")); | ||
325 | } | ||
326 | catch (XmlException e) | ||
327 | { | ||
328 | m_log.Warn("[DATASNAPSHOT]: XmlException while trying to load snapshot: " + e.ToString()); | ||
329 | requestedSnap = GetErrorMessage(regionName, e); | ||
330 | } | ||
331 | catch (Exception e) | ||
332 | { | ||
333 | m_log.Warn("[DATASNAPSHOT]: Caught unknown exception while trying to load snapshot: " + e.StackTrace); | ||
334 | requestedSnap = GetErrorMessage(regionName, e); | ||
335 | } | ||
336 | |||
337 | |||
338 | return requestedSnap; | ||
339 | } | ||
340 | |||
341 | private XmlDocument GetErrorMessage(string regionName, Exception e) | ||
342 | { | ||
343 | XmlDocument errorMessage = new XmlDocument(); | ||
344 | XmlNode error = errorMessage.CreateNode(XmlNodeType.Element, "error", ""); | ||
345 | XmlNode region = errorMessage.CreateNode(XmlNodeType.Element, "region", ""); | ||
346 | region.InnerText = regionName; | ||
347 | |||
348 | XmlNode exception = errorMessage.CreateNode(XmlNodeType.Element, "exception", ""); | ||
349 | exception.InnerText = e.ToString(); | ||
350 | |||
351 | error.AppendChild(region); | ||
352 | error.AppendChild(exception); | ||
353 | errorMessage.AppendChild(error); | ||
354 | |||
355 | return errorMessage; | ||
356 | } | ||
357 | |||
358 | #endregion | ||
359 | |||
360 | #region External data services | ||
361 | private void NotifyDataServices(string servicesStr, string serviceName) | ||
362 | { | ||
363 | Stream reply = null; | ||
364 | string delimStr = ";"; | ||
365 | char [] delimiter = delimStr.ToCharArray(); | ||
366 | |||
367 | string[] services = servicesStr.Split(delimiter); | ||
368 | |||
369 | for (int i = 0; i < services.Length; i++) | ||
370 | { | ||
371 | string url = services[i].Trim(); | ||
372 | RestClient cli = new RestClient(url); | ||
373 | cli.AddQueryParameter("service", serviceName); | ||
374 | cli.AddQueryParameter("host", m_hostname); | ||
375 | cli.AddQueryParameter("port", m_listener_port); | ||
376 | cli.AddQueryParameter("secret", m_Secret.ToString()); | ||
377 | cli.RequestMethod = "GET"; | ||
378 | try | ||
379 | { | ||
380 | reply = cli.Request(); | ||
381 | } | ||
382 | catch (WebException) | ||
383 | { | ||
384 | m_log.Warn("[DATASNAPSHOT]: Unable to notify " + url); | ||
385 | } | ||
386 | catch (Exception e) | ||
387 | { | ||
388 | m_log.Warn("[DATASNAPSHOT]: Ignoring unknown exception " + e.ToString()); | ||
389 | } | ||
390 | byte[] response = new byte[1024]; | ||
391 | // int n = 0; | ||
392 | try | ||
393 | { | ||
394 | // n = reply.Read(response, 0, 1024); | ||
395 | reply.Read(response, 0, 1024); | ||
396 | } | ||
397 | catch (Exception e) | ||
398 | { | ||
399 | m_log.WarnFormat("[DATASNAPSHOT]: Unable to decode reply from data service. Ignoring. {0}", e.StackTrace); | ||
400 | } | ||
401 | // This is not quite working, so... | ||
402 | // string responseStr = Util.UTF8.GetString(response); | ||
403 | m_log.Info("[DATASNAPSHOT]: data service " + url + " notified. Secret: " + m_Secret); | ||
404 | } | ||
405 | |||
406 | } | ||
407 | #endregion | ||
408 | |||
409 | #region Latency-based update functions | ||
410 | |||
411 | public void MarkDataStale(IDataSnapshotProvider provider) | ||
412 | { | ||
413 | //Behavior here: Wait m_period seconds, then update if there has not been a request in m_period seconds | ||
414 | //or m_maxStales has been exceeded | ||
415 | m_stales++; | ||
416 | } | ||
417 | |||
418 | private void CheckStale() | ||
419 | { | ||
420 | // Wrap check | ||
421 | if (Environment.TickCount < m_lastUpdate) | ||
422 | { | ||
423 | m_lastUpdate = Environment.TickCount; | ||
424 | } | ||
425 | |||
426 | if (m_stales >= m_maxStales) | ||
427 | { | ||
428 | if (Environment.TickCount - m_lastUpdate >= 20000) | ||
429 | { | ||
430 | m_stales = 0; | ||
431 | m_lastUpdate = Environment.TickCount; | ||
432 | MakeEverythingStale(); | ||
433 | } | ||
434 | } | ||
435 | else | ||
436 | { | ||
437 | if (m_lastUpdate + 1000 * m_period < Environment.TickCount) | ||
438 | { | ||
439 | m_stales = 0; | ||
440 | m_lastUpdate = Environment.TickCount; | ||
441 | MakeEverythingStale(); | ||
442 | } | ||
443 | } | ||
444 | } | ||
445 | |||
446 | public void MakeEverythingStale() | ||
447 | { | ||
448 | m_log.Debug("[DATASNAPSHOT]: Marking all scenes as stale."); | ||
449 | foreach (Scene scene in m_scenes) | ||
450 | { | ||
451 | m_snapStore.ForceSceneStale(scene); | ||
452 | } | ||
453 | } | ||
454 | #endregion | ||
455 | |||
456 | } | ||
457 | } | ||