aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs
diff options
context:
space:
mode:
authorMelanie2012-04-18 00:28:33 +0100
committerMelanie2012-04-18 00:28:33 +0100
commitfc9f244a7da6f5b4507278988259cfc54e0dd375 (patch)
tree0ee0202e5f6acf9ebfbd1fd0442a0c6b9f1e3fcc /OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs
parentMerge branch 'master' into careminster (diff)
parentMerge branch 'master' of ssh://opensimulator.org/var/git/opensim (diff)
downloadopensim-SC_OLD-fc9f244a7da6f5b4507278988259cfc54e0dd375.zip
opensim-SC_OLD-fc9f244a7da6f5b4507278988259cfc54e0dd375.tar.gz
opensim-SC_OLD-fc9f244a7da6f5b4507278988259cfc54e0dd375.tar.bz2
opensim-SC_OLD-fc9f244a7da6f5b4507278988259cfc54e0dd375.tar.xz
Merge branch 'master' into careminster
Diffstat (limited to 'OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs')
-rw-r--r--OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs490
1 files changed, 490 insertions, 0 deletions
diff --git a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs
new file mode 100644
index 0000000..eda2aef
--- /dev/null
+++ b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs
@@ -0,0 +1,490 @@
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 */
27using Mono.Addins;
28
29using System;
30using System.Reflection;
31using System.Threading;
32using System.Text;
33using System.Net;
34using System.Net.Sockets;
35using log4net;
36using Nini.Config;
37using OpenMetaverse;
38using OpenMetaverse.StructuredData;
39using OpenSim.Framework;
40using OpenSim.Region.Framework.Interfaces;
41using OpenSim.Region.Framework.Scenes;
42using System.Collections.Generic;
43using System.Text.RegularExpressions;
44
45namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
46{
47 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "JsonStoreScriptModule")]
48
49 public class JsonStoreScriptModule : INonSharedRegionModule
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 Scene m_scene = null;
57
58 private IScriptModuleComms m_comms;
59 private IJsonStoreModule m_store;
60
61#region IRegionModule Members
62
63 // -----------------------------------------------------------------
64 /// <summary>
65 /// Name of this shared module is it's class name
66 /// </summary>
67 // -----------------------------------------------------------------
68 public string Name
69 {
70 get { return this.GetType().Name; }
71 }
72
73 // -----------------------------------------------------------------
74 /// <summary>
75 /// Initialise this shared module
76 /// </summary>
77 /// <param name="scene">this region is getting initialised</param>
78 /// <param name="source">nini config, we are not using this</param>
79 // -----------------------------------------------------------------
80 public void Initialise(IConfigSource config)
81 {
82 try
83 {
84 if ((m_config = config.Configs["JsonStore"]) == null)
85 {
86 // There is no configuration, the module is disabled
87 // m_log.InfoFormat("[JsonStoreScripts] no configuration info");
88 return;
89 }
90
91 m_enabled = m_config.GetBoolean("Enabled", m_enabled);
92 }
93 catch (Exception e)
94 {
95 m_log.ErrorFormat("[JsonStoreScripts] initialization error: {0}",e.Message);
96 return;
97 }
98
99 if (m_enabled)
100 m_log.DebugFormat("[JsonStoreScripts] module is enabled");
101 }
102
103 // -----------------------------------------------------------------
104 /// <summary>
105 /// everything is loaded, perform post load configuration
106 /// </summary>
107 // -----------------------------------------------------------------
108 public void PostInitialise()
109 {
110 }
111
112 // -----------------------------------------------------------------
113 /// <summary>
114 /// Nothing to do on close
115 /// </summary>
116 // -----------------------------------------------------------------
117 public void Close()
118 {
119 }
120
121 // -----------------------------------------------------------------
122 /// <summary>
123 /// </summary>
124 // -----------------------------------------------------------------
125 public void AddRegion(Scene scene)
126 {
127 }
128
129 // -----------------------------------------------------------------
130 /// <summary>
131 /// </summary>
132 // -----------------------------------------------------------------
133 public void RemoveRegion(Scene scene)
134 {
135 // need to remove all references to the scene in the subscription
136 // list to enable full garbage collection of the scene object
137 }
138
139 // -----------------------------------------------------------------
140 /// <summary>
141 /// Called when all modules have been added for a region. This is
142 /// where we hook up events
143 /// </summary>
144 // -----------------------------------------------------------------
145 public void RegionLoaded(Scene scene)
146 {
147 if (m_enabled)
148 {
149 m_scene = scene;
150 m_comms = m_scene.RequestModuleInterface<IScriptModuleComms>();
151 if (m_comms == null)
152 {
153 m_log.ErrorFormat("[JsonStoreScripts] ScriptModuleComms interface not defined");
154 m_enabled = false;
155 return;
156 }
157
158 m_store = m_scene.RequestModuleInterface<IJsonStoreModule>();
159 if (m_store == null)
160 {
161 m_log.ErrorFormat("[JsonStoreScripts] JsonModule interface not defined");
162 m_enabled = false;
163 return;
164 }
165
166 m_comms.RegisterScriptInvocation(this,"JsonCreateStore");
167 m_comms.RegisterScriptInvocation(this,"JsonDestroyStore");
168
169 m_comms.RegisterScriptInvocation(this,"JsonReadNotecard");
170 m_comms.RegisterScriptInvocation(this,"JsonWriteNotecard");
171
172 m_comms.RegisterScriptInvocation(this,"JsonTestPath");
173 m_comms.RegisterScriptInvocation(this,"JsonTestPathJson");
174
175 m_comms.RegisterScriptInvocation(this,"JsonGetValue");
176 m_comms.RegisterScriptInvocation(this,"JsonGetValueJson");
177
178 m_comms.RegisterScriptInvocation(this,"JsonTakeValue");
179 m_comms.RegisterScriptInvocation(this,"JsonTakeValueJson");
180
181 m_comms.RegisterScriptInvocation(this,"JsonReadValue");
182 m_comms.RegisterScriptInvocation(this,"JsonReadValueJson");
183
184 m_comms.RegisterScriptInvocation(this,"JsonSetValue");
185 m_comms.RegisterScriptInvocation(this,"JsonSetValueJson");
186
187 m_comms.RegisterScriptInvocation(this,"JsonRemoveValue");
188 }
189 }
190
191 /// -----------------------------------------------------------------
192 /// <summary>
193 /// </summary>
194 // -----------------------------------------------------------------
195 public Type ReplaceableInterface
196 {
197 get { return null; }
198 }
199
200#endregion
201
202#region ScriptInvocationInteface
203 // -----------------------------------------------------------------
204 /// <summary>
205 ///
206 /// </summary>
207 // -----------------------------------------------------------------
208 protected void GenerateRuntimeError(string msg)
209 {
210 throw new Exception("JsonStore Runtime Error: " + msg);
211 }
212
213 // -----------------------------------------------------------------
214 /// <summary>
215 ///
216 /// </summary>
217 // -----------------------------------------------------------------
218 protected UUID JsonCreateStore(UUID hostID, UUID scriptID, string value)
219 {
220 UUID uuid = UUID.Zero;
221 if (! m_store.CreateStore(value, out uuid))
222 GenerateRuntimeError("Failed to create Json store");
223
224 return uuid;
225 }
226
227 // -----------------------------------------------------------------
228 /// <summary>
229 ///
230 /// </summary>
231 // -----------------------------------------------------------------
232 protected int JsonDestroyStore(UUID hostID, UUID scriptID, UUID storeID)
233 {
234 return m_store.DestroyStore(storeID) ? 1 : 0;
235 }
236
237 // -----------------------------------------------------------------
238 /// <summary>
239 ///
240 /// </summary>
241 // -----------------------------------------------------------------
242 protected UUID JsonReadNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, UUID assetID)
243 {
244 UUID reqID = UUID.Random();
245 Util.FireAndForget(delegate(object o) { DoJsonReadNotecard(reqID,hostID,scriptID,storeID,path,assetID); });
246 return reqID;
247 }
248
249 // -----------------------------------------------------------------
250 /// <summary>
251 ///
252 /// </summary>
253 // -----------------------------------------------------------------
254 protected UUID JsonWriteNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, string name)
255 {
256 UUID reqID = UUID.Random();
257 Util.FireAndForget(delegate(object o) { DoJsonWriteNotecard(reqID,hostID,scriptID,storeID,path,name); });
258 return reqID;
259 }
260
261 // -----------------------------------------------------------------
262 /// <summary>
263 ///
264 /// </summary>
265 // -----------------------------------------------------------------
266 protected int JsonTestPath(UUID hostID, UUID scriptID, UUID storeID, string path)
267 {
268 return m_store.TestPath(storeID,path,false) ? 1 : 0;
269 }
270
271 protected int JsonTestPathJson(UUID hostID, UUID scriptID, UUID storeID, string path)
272 {
273 return m_store.TestPath(storeID,path,true) ? 1 : 0;
274 }
275
276 // -----------------------------------------------------------------
277 /// <summary>
278 ///
279 /// </summary>
280 // -----------------------------------------------------------------
281 protected int JsonSetValue(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
282 {
283 return m_store.SetValue(storeID,path,value,false) ? 1 : 0;
284 }
285
286 protected int JsonSetValueJson(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
287 {
288 return m_store.SetValue(storeID,path,value,true) ? 1 : 0;
289 }
290
291 // -----------------------------------------------------------------
292 /// <summary>
293 ///
294 /// </summary>
295 // -----------------------------------------------------------------
296 protected int JsonRemoveValue(UUID hostID, UUID scriptID, UUID storeID, string path)
297 {
298 return m_store.RemoveValue(storeID,path) ? 1 : 0;
299 }
300
301 // -----------------------------------------------------------------
302 /// <summary>
303 ///
304 /// </summary>
305 // -----------------------------------------------------------------
306 protected string JsonGetValue(UUID hostID, UUID scriptID, UUID storeID, string path)
307 {
308 string value = String.Empty;
309 m_store.GetValue(storeID,path,false,out value);
310 return value;
311 }
312
313 protected string JsonGetValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
314 {
315 string value = String.Empty;
316 m_store.GetValue(storeID,path,true, out value);
317 return value;
318 }
319
320 // -----------------------------------------------------------------
321 /// <summary>
322 ///
323 /// </summary>
324 // -----------------------------------------------------------------
325 protected UUID JsonTakeValue(UUID hostID, UUID scriptID, UUID storeID, string path)
326 {
327 UUID reqID = UUID.Random();
328 Util.FireAndForget(delegate(object o) { DoJsonTakeValue(scriptID,reqID,storeID,path,false); });
329 return reqID;
330 }
331
332 protected UUID JsonTakeValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
333 {
334 UUID reqID = UUID.Random();
335 Util.FireAndForget(delegate(object o) { DoJsonTakeValue(scriptID,reqID,storeID,path,true); });
336 return reqID;
337 }
338
339 private void DoJsonTakeValue(UUID scriptID, UUID reqID, UUID storeID, string path, bool useJson)
340 {
341 try
342 {
343 m_store.TakeValue(storeID,path,useJson,delegate(string value) { DispatchValue(scriptID,reqID,value); });
344 return;
345 }
346 catch (Exception e)
347 {
348 m_log.InfoFormat("[JsonStoreScripts] unable to retrieve value; {0}",e.ToString());
349 }
350
351 DispatchValue(scriptID,reqID,String.Empty);
352 }
353
354
355 // -----------------------------------------------------------------
356 /// <summary>
357 ///
358 /// </summary>
359 // -----------------------------------------------------------------
360 protected UUID JsonReadValue(UUID hostID, UUID scriptID, UUID storeID, string path)
361 {
362 UUID reqID = UUID.Random();
363 Util.FireAndForget(delegate(object o) { DoJsonReadValue(scriptID,reqID,storeID,path,false); });
364 return reqID;
365 }
366
367 protected UUID JsonReadValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
368 {
369 UUID reqID = UUID.Random();
370 Util.FireAndForget(delegate(object o) { DoJsonReadValue(scriptID,reqID,storeID,path,true); });
371 return reqID;
372 }
373
374 private void DoJsonReadValue(UUID scriptID, UUID reqID, UUID storeID, string path, bool useJson)
375 {
376 try
377 {
378 m_store.ReadValue(storeID,path,useJson,delegate(string value) { DispatchValue(scriptID,reqID,value); });
379 return;
380 }
381 catch (Exception e)
382 {
383 m_log.InfoFormat("[JsonStoreScripts] unable to retrieve value; {0}",e.ToString());
384 }
385
386 DispatchValue(scriptID,reqID,String.Empty);
387 }
388
389#endregion
390
391 // -----------------------------------------------------------------
392 /// <summary>
393 ///
394 /// </summary>
395 // -----------------------------------------------------------------
396 protected void DispatchValue(UUID scriptID, UUID reqID, string value)
397 {
398 m_comms.DispatchReply(scriptID,1,value,reqID.ToString());
399 }
400
401 // -----------------------------------------------------------------
402 /// <summary>
403 ///
404 /// </summary>
405 // -----------------------------------------------------------------
406 private void DoJsonReadNotecard(UUID reqID, UUID hostID, UUID scriptID, UUID storeID, string path, UUID assetID)
407 {
408 AssetBase a = m_scene.AssetService.Get(assetID.ToString());
409 if (a == null)
410 GenerateRuntimeError(String.Format("Unable to find notecard asset {0}",assetID));
411
412 if (a.Type != (sbyte)AssetType.Notecard)
413 GenerateRuntimeError(String.Format("Invalid notecard asset {0}",assetID));
414
415 m_log.DebugFormat("[JsonStoreScripts] read notecard in context {0}",storeID);
416
417 try
418 {
419 System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
420 string jsondata = SLUtil.ParseNotecardToString(enc.GetString(a.Data));
421 int result = m_store.SetValue(storeID,path,jsondata,true) ? 1 : 0;
422 m_comms.DispatchReply(scriptID,result,"",reqID.ToString());
423 return;
424 }
425 catch (Exception e)
426 {
427 m_log.WarnFormat("[JsonStoreScripts] Json parsing failed; {0}",e.Message);
428 }
429
430 GenerateRuntimeError(String.Format("Json parsing failed for {0}",assetID.ToString()));
431 m_comms.DispatchReply(scriptID,0,"",reqID.ToString());
432 }
433
434 // -----------------------------------------------------------------
435 /// <summary>
436 ///
437 /// </summary>
438 // -----------------------------------------------------------------
439 private void DoJsonWriteNotecard(UUID reqID, UUID hostID, UUID scriptID, UUID storeID, string path, string name)
440 {
441 string data;
442 if (! m_store.GetValue(storeID,path,true, out data))
443 {
444 m_comms.DispatchReply(scriptID,0,UUID.Zero.ToString(),reqID.ToString());
445 return;
446 }
447
448 SceneObjectPart host = m_scene.GetSceneObjectPart(hostID);
449
450 // Create new asset
451 UUID assetID = UUID.Random();
452 AssetBase asset = new AssetBase(assetID, name, (sbyte)AssetType.Notecard, host.OwnerID.ToString());
453 asset.Description = "Json store";
454
455 int textLength = data.Length;
456 data = "Linden text version 2\n{\nLLEmbeddedItems version 1\n{\ncount 0\n}\nText length "
457 + textLength.ToString() + "\n" + data + "}\n";
458
459 asset.Data = Util.UTF8.GetBytes(data);
460 m_scene.AssetService.Store(asset);
461
462 // Create Task Entry
463 TaskInventoryItem taskItem = new TaskInventoryItem();
464
465 taskItem.ResetIDs(host.UUID);
466 taskItem.ParentID = host.UUID;
467 taskItem.CreationDate = (uint)Util.UnixTimeSinceEpoch();
468 taskItem.Name = asset.Name;
469 taskItem.Description = asset.Description;
470 taskItem.Type = (int)AssetType.Notecard;
471 taskItem.InvType = (int)InventoryType.Notecard;
472 taskItem.OwnerID = host.OwnerID;
473 taskItem.CreatorID = host.OwnerID;
474 taskItem.BasePermissions = (uint)PermissionMask.All;
475 taskItem.CurrentPermissions = (uint)PermissionMask.All;
476 taskItem.EveryonePermissions = 0;
477 taskItem.NextPermissions = (uint)PermissionMask.All;
478 taskItem.GroupID = host.GroupID;
479 taskItem.GroupPermissions = 0;
480 taskItem.Flags = 0;
481 taskItem.PermsGranter = UUID.Zero;
482 taskItem.PermsMask = 0;
483 taskItem.AssetID = asset.FullID;
484
485 host.Inventory.AddInventoryItem(taskItem, false);
486
487 m_comms.DispatchReply(scriptID,1,assetID.ToString(),reqID.ToString());
488 }
489 }
490}