JsonStoreModule.cs 19 KB

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