opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 1 /*
2 * Copyright (c) Contributors
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 using Mono.Addins;
28  
29 using System;
30 using System.Reflection;
31 using System.Threading;
32 using System.Text;
33 using System.Net;
34 using System.Net.Sockets;
35 using log4net;
36 using Nini.Config;
37 using OpenMetaverse;
38 using OpenMetaverse.StructuredData;
39 using OpenSim.Framework;
40 using OpenSim.Region.Framework.Interfaces;
41 using OpenSim.Region.Framework.Scenes;
42 using System.Collections.Generic;
43 using System.Text.RegularExpressions;
44  
45 namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
46 {
47 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "JsonStoreModule")]
48  
49 public class JsonStoreModule : INonSharedRegionModule, IJsonStoreModule
50 {
51 private static readonly ILog m_log =
52 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
53  
54 private IConfig m_config = null;
55 private bool m_enabled = false;
56 private bool m_enableObjectStore = false;
57 private int m_maxStringSpace = Int32.MaxValue;
58  
59 private Scene m_scene = null;
60  
61 private Dictionary<UUID,JsonStore> m_JsonValueStore;
62  
63 private UUID m_sharedStore;
64  
65 #region Region Module interface
66  
67 // -----------------------------------------------------------------
68 /// <summary>
69 /// Name of this shared module is it's class name
70 /// </summary>
71 // -----------------------------------------------------------------
72 public string Name
73 {
74 get { return this.GetType().Name; }
75 }
76  
77 // -----------------------------------------------------------------
78 /// <summary>
79 /// Initialise this shared module
80 /// </summary>
81 /// <param name="scene">this region is getting initialised</param>
82 /// <param name="source">nini config, we are not using this</param>
83 // -----------------------------------------------------------------
84 public void Initialise(IConfigSource config)
85 {
86 try
87 {
88 if ((m_config = config.Configs["JsonStore"]) == null)
89 {
90 // There is no configuration, the module is disabled
91 // m_log.InfoFormat("[JsonStore] no configuration info");
92 return;
93 }
94  
95 m_enabled = m_config.GetBoolean("Enabled", m_enabled);
96 m_enableObjectStore = m_config.GetBoolean("EnableObjectStore", m_enableObjectStore);
97 m_maxStringSpace = m_config.GetInt("MaxStringSpace", m_maxStringSpace);
98 if (m_maxStringSpace == 0)
99 m_maxStringSpace = Int32.MaxValue;
100 }
101 catch (Exception e)
102 {
103 m_log.Error("[JsonStore]: initialization error: {0}", e);
104 return;
105 }
106  
107 if (m_enabled)
108 m_log.DebugFormat("[JsonStore]: module is enabled");
109 }
110  
111 // -----------------------------------------------------------------
112 /// <summary>
113 /// everything is loaded, perform post load configuration
114 /// </summary>
115 // -----------------------------------------------------------------
116 public void PostInitialise()
117 {
118 }
119  
120 // -----------------------------------------------------------------
121 /// <summary>
122 /// Nothing to do on close
123 /// </summary>
124 // -----------------------------------------------------------------
125 public void Close()
126 {
127 }
128  
129 // -----------------------------------------------------------------
130 /// <summary>
131 /// </summary>
132 // -----------------------------------------------------------------
133 public void AddRegion(Scene scene)
134 {
135 if (m_enabled)
136 {
137 m_scene = scene;
138 m_scene.RegisterModuleInterface<IJsonStoreModule>(this);
139  
140 m_sharedStore = UUID.Zero;
141 m_JsonValueStore = new Dictionary<UUID,JsonStore>();
142 m_JsonValueStore.Add(m_sharedStore,new JsonStore(""));
143  
144 scene.EventManager.OnObjectBeingRemovedFromScene += EventManagerOnObjectBeingRemovedFromScene;
145 }
146 }
147  
148 // -----------------------------------------------------------------
149 /// <summary>
150 /// </summary>
151 // -----------------------------------------------------------------
152 public void RemoveRegion(Scene scene)
153 {
154 scene.EventManager.OnObjectBeingRemovedFromScene -= EventManagerOnObjectBeingRemovedFromScene;
155  
156 // need to remove all references to the scene in the subscription
157 // list to enable full garbage collection of the scene object
158 }
159  
160 // -----------------------------------------------------------------
161 /// <summary>
162 /// Called when all modules have been added for a region. This is
163 /// where we hook up events
164 /// </summary>
165 // -----------------------------------------------------------------
166 public void RegionLoaded(Scene scene)
167 {
168 if (m_enabled)
169 {
170 }
171 }
172  
173 /// -----------------------------------------------------------------
174 /// <summary>
175 /// </summary>
176 // -----------------------------------------------------------------
177 public Type ReplaceableInterface
178 {
179 get { return null; }
180 }
181  
182 #endregion
183  
184 #region SceneEvents
185 // -----------------------------------------------------------------
186 /// <summary>
187 ///
188 /// </summary>
189 // -----------------------------------------------------------------
190 public void EventManagerOnObjectBeingRemovedFromScene(SceneObjectGroup obj)
191 {
192 obj.ForEachPart(delegate(SceneObjectPart sop) { DestroyStore(sop.UUID); } );
193 }
194  
195 #endregion
196  
197 #region ScriptInvocationInteface
198  
199  
200 // -----------------------------------------------------------------
201 /// <summary>
202 ///
203 /// </summary>
204 // -----------------------------------------------------------------
205 public JsonStoreStats GetStoreStats()
206 {
207 JsonStoreStats stats;
208  
209 lock (m_JsonValueStore)
210 {
211 stats.StoreCount = m_JsonValueStore.Count;
212 }
213  
214 return stats;
215 }
216  
217 // -----------------------------------------------------------------
218 /// <summary>
219 ///
220 /// </summary>
221 // -----------------------------------------------------------------
222 public bool AttachObjectStore(UUID objectID)
223 {
224 if (! m_enabled) return false;
225 if (! m_enableObjectStore) return false;
226  
227 SceneObjectPart sop = m_scene.GetSceneObjectPart(objectID);
228 if (sop == null)
229 {
230 m_log.ErrorFormat("[JsonStore] unable to attach to unknown object; {0}", objectID);
231 return false;
232 }
233  
234 lock (m_JsonValueStore)
235 {
236 if (m_JsonValueStore.ContainsKey(objectID))
237 return true;
238  
239 JsonStore map = new JsonObjectStore(m_scene,objectID);
240 m_JsonValueStore.Add(objectID,map);
241 }
242  
243 return true;
244 }
245  
246 // -----------------------------------------------------------------
247 /// <summary>
248 ///
249 /// </summary>
250 // -----------------------------------------------------------------
251 public bool CreateStore(string value, ref UUID result)
252 {
253 if (result == UUID.Zero)
254 result = UUID.Random();
255  
256 JsonStore map = null;
257  
258 if (! m_enabled) return false;
259  
260  
261 try
262 {
263 map = new JsonStore(value);
264 }
265 catch (Exception e)
266 {
267 m_log.ErrorFormat("[JsonStore]: Unable to initialize store from {0}", value);
268 return false;
269 }
270  
271 lock (m_JsonValueStore)
272 m_JsonValueStore.Add(result,map);
273  
274 return true;
275 }
276  
277 // -----------------------------------------------------------------
278 /// <summary>
279 ///
280 /// </summary>
281 // -----------------------------------------------------------------
282 public bool DestroyStore(UUID storeID)
283 {
284 if (! m_enabled) return false;
285  
286 lock (m_JsonValueStore)
287 return m_JsonValueStore.Remove(storeID);
288  
289 return true;
290 }
291  
292 // -----------------------------------------------------------------
293 /// <summary>
294 ///
295 /// </summary>
296 // -----------------------------------------------------------------
297 public bool TestStore(UUID storeID)
298 {
299 if (! m_enabled) return false;
300  
301 lock (m_JsonValueStore)
302 return m_JsonValueStore.ContainsKey(storeID);
303 }
304  
305 // -----------------------------------------------------------------
306 /// <summary>
307 ///
308 /// </summary>
309 // -----------------------------------------------------------------
310 public JsonStoreNodeType GetNodeType(UUID storeID, string path)
311 {
312 if (! m_enabled) return JsonStoreNodeType.Undefined;
313  
314 JsonStore map = null;
315 lock (m_JsonValueStore)
316 {
317 if (! m_JsonValueStore.TryGetValue(storeID,out map))
318 {
319 m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
320 return JsonStoreNodeType.Undefined;
321 }
322 }
323  
324 try
325 {
326 lock (map)
327 return map.GetNodeType(path);
328 }
329 catch (Exception e)
330 {
331 m_log.Error(string.Format("[JsonStore]: Path test failed for {0} in {1}", path, storeID), e);
332 }
333  
334 return JsonStoreNodeType.Undefined;
335 }
336  
337 // -----------------------------------------------------------------
338 /// <summary>
339 ///
340 /// </summary>
341 // -----------------------------------------------------------------
342 public JsonStoreValueType GetValueType(UUID storeID, string path)
343 {
344 if (! m_enabled) return JsonStoreValueType.Undefined;
345  
346 JsonStore map = null;
347 lock (m_JsonValueStore)
348 {
349 if (! m_JsonValueStore.TryGetValue(storeID,out map))
350 {
351 m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
352 return JsonStoreValueType.Undefined;
353 }
354 }
355  
356 try
357 {
358 lock (map)
359 return map.GetValueType(path);
360 }
361 catch (Exception e)
362 {
363 m_log.Error(string.Format("[JsonStore]: Path test failed for {0} in {1}", path, storeID), e);
364 }
365  
366 return JsonStoreValueType.Undefined;
367 }
368  
369 // -----------------------------------------------------------------
370 /// <summary>
371 ///
372 /// </summary>
373 // -----------------------------------------------------------------
374 public bool SetValue(UUID storeID, string path, string value, bool useJson)
375 {
376 if (! m_enabled) return false;
377  
378 JsonStore map = null;
379 lock (m_JsonValueStore)
380 {
381 if (! m_JsonValueStore.TryGetValue(storeID,out map))
382 {
383 m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
384 return false;
385 }
386 }
387  
388 try
389 {
390 lock (map)
391 {
392 if (map.StringSpace > m_maxStringSpace)
393 {
394 m_log.WarnFormat("[JsonStore] {0} exceeded string size; {1} bytes used of {2} limit",
395 storeID,map.StringSpace,m_maxStringSpace);
396 return false;
397 }
398  
399 return map.SetValue(path,value,useJson);
400 }
401 }
402 catch (Exception e)
403 {
404 m_log.Error(string.Format("[JsonStore]: Unable to assign {0} to {1} in {2}", value, path, storeID), e);
405 }
406  
407 return false;
408 }
409  
410 // -----------------------------------------------------------------
411 /// <summary>
412 ///
413 /// </summary>
414 // -----------------------------------------------------------------
415 public bool RemoveValue(UUID storeID, string path)
416 {
417 if (! m_enabled) return false;
418  
419 JsonStore map = null;
420 lock (m_JsonValueStore)
421 {
422 if (! m_JsonValueStore.TryGetValue(storeID,out map))
423 {
424 m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
425 return false;
426 }
427 }
428  
429 try
430 {
431 lock (map)
432 return map.RemoveValue(path);
433 }
434 catch (Exception e)
435 {
436 m_log.Error(string.Format("[JsonStore]: Unable to remove {0} in {1}", path, storeID), e);
437 }
438  
439 return false;
440 }
441  
442 // -----------------------------------------------------------------
443 /// <summary>
444 ///
445 /// </summary>
446 // -----------------------------------------------------------------
447 public int GetArrayLength(UUID storeID, string path)
448 {
449 if (! m_enabled) return -1;
450  
451 JsonStore map = null;
452 lock (m_JsonValueStore)
453 {
454 if (! m_JsonValueStore.TryGetValue(storeID,out map))
455 return -1;
456 }
457  
458 try
459 {
460 lock (map)
461 {
462 return map.ArrayLength(path);
463 }
464 }
465 catch (Exception e)
466 {
467 m_log.Error("[JsonStore]: unable to retrieve value", e);
468 }
469  
470 return -1;
471 }
472  
473 // -----------------------------------------------------------------
474 /// <summary>
475 ///
476 /// </summary>
477 // -----------------------------------------------------------------
478 public bool GetValue(UUID storeID, string path, bool useJson, out string value)
479 {
480 value = String.Empty;
481  
482 if (! m_enabled) return false;
483  
484 JsonStore map = null;
485 lock (m_JsonValueStore)
486 {
487 if (! m_JsonValueStore.TryGetValue(storeID,out map))
488 return false;
489 }
490  
491 try
492 {
493 lock (map)
494 {
495 return map.GetValue(path, out value, useJson);
496 }
497 }
498 catch (Exception e)
499 {
500 m_log.Error("[JsonStore]: unable to retrieve value", e);
501 }
502  
503 return false;
504 }
505  
506 // -----------------------------------------------------------------
507 /// <summary>
508 ///
509 /// </summary>
510 // -----------------------------------------------------------------
511 public void TakeValue(UUID storeID, string path, bool useJson, TakeValueCallback cback)
512 {
513 if (! m_enabled)
514 {
515 cback(String.Empty);
516 return;
517 }
518  
519 JsonStore map = null;
520 lock (m_JsonValueStore)
521 {
522 if (! m_JsonValueStore.TryGetValue(storeID,out map))
523 {
524 cback(String.Empty);
525 return;
526 }
527 }
528  
529 try
530 {
531 lock (map)
532 {
533 map.TakeValue(path, useJson, cback);
534 return;
535 }
536 }
537 catch (Exception e)
538 {
539 m_log.Error("[JsonStore] unable to retrieve value", e);
540 }
541  
542 cback(String.Empty);
543 }
544  
545 // -----------------------------------------------------------------
546 /// <summary>
547 ///
548 /// </summary>
549 // -----------------------------------------------------------------
550 public void ReadValue(UUID storeID, string path, bool useJson, TakeValueCallback cback)
551 {
552 if (! m_enabled)
553 {
554 cback(String.Empty);
555 return;
556 }
557  
558 JsonStore map = null;
559 lock (m_JsonValueStore)
560 {
561 if (! m_JsonValueStore.TryGetValue(storeID,out map))
562 {
563 cback(String.Empty);
564 return;
565 }
566 }
567  
568 try
569 {
570 lock (map)
571 {
572 map.ReadValue(path, useJson, cback);
573 return;
574 }
575 }
576 catch (Exception e)
577 {
578 m_log.Error("[JsonStore]: unable to retrieve value", e);
579 }
580  
581 cback(String.Empty);
582 }
583  
584 #endregion
585 }
586 }