JsonStoreModule.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 e)
  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. return true;
  246. }
  247. // -----------------------------------------------------------------
  248. /// <summary>
  249. ///
  250. /// </summary>
  251. // -----------------------------------------------------------------
  252. public bool TestStore(UUID storeID)
  253. {
  254. if (! m_enabled) return false;
  255. lock (m_JsonValueStore)
  256. return m_JsonValueStore.ContainsKey(storeID);
  257. }
  258. // -----------------------------------------------------------------
  259. /// <summary>
  260. ///
  261. /// </summary>
  262. // -----------------------------------------------------------------
  263. public JsonStoreNodeType GetNodeType(UUID storeID, string path)
  264. {
  265. if (! m_enabled) return JsonStoreNodeType.Undefined;
  266. JsonStore map = null;
  267. lock (m_JsonValueStore)
  268. {
  269. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  270. {
  271. m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
  272. return JsonStoreNodeType.Undefined;
  273. }
  274. }
  275. try
  276. {
  277. lock (map)
  278. return map.GetNodeType(path);
  279. }
  280. catch (Exception e)
  281. {
  282. m_log.Error(string.Format("[JsonStore]: Path test failed for {0} in {1}", path, storeID), e);
  283. }
  284. return JsonStoreNodeType.Undefined;
  285. }
  286. // -----------------------------------------------------------------
  287. /// <summary>
  288. ///
  289. /// </summary>
  290. // -----------------------------------------------------------------
  291. public JsonStoreValueType GetValueType(UUID storeID, string path)
  292. {
  293. if (! m_enabled) return JsonStoreValueType.Undefined;
  294. JsonStore map = null;
  295. lock (m_JsonValueStore)
  296. {
  297. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  298. {
  299. m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
  300. return JsonStoreValueType.Undefined;
  301. }
  302. }
  303. try
  304. {
  305. lock (map)
  306. return map.GetValueType(path);
  307. }
  308. catch (Exception e)
  309. {
  310. m_log.Error(string.Format("[JsonStore]: Path test failed for {0} in {1}", path, storeID), e);
  311. }
  312. return JsonStoreValueType.Undefined;
  313. }
  314. // -----------------------------------------------------------------
  315. /// <summary>
  316. ///
  317. /// </summary>
  318. // -----------------------------------------------------------------
  319. public bool SetValue(UUID storeID, string path, string value, bool useJson)
  320. {
  321. if (! m_enabled) return false;
  322. JsonStore map = null;
  323. lock (m_JsonValueStore)
  324. {
  325. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  326. {
  327. m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
  328. return false;
  329. }
  330. }
  331. try
  332. {
  333. lock (map)
  334. {
  335. if (map.StringSpace > m_maxStringSpace)
  336. {
  337. m_log.WarnFormat("[JsonStore] {0} exceeded string size; {1} bytes used of {2} limit",
  338. storeID,map.StringSpace,m_maxStringSpace);
  339. return false;
  340. }
  341. return map.SetValue(path,value,useJson);
  342. }
  343. }
  344. catch (Exception e)
  345. {
  346. m_log.Error(string.Format("[JsonStore]: Unable to assign {0} to {1} in {2}", value, path, storeID), e);
  347. }
  348. return false;
  349. }
  350. // -----------------------------------------------------------------
  351. /// <summary>
  352. ///
  353. /// </summary>
  354. // -----------------------------------------------------------------
  355. public bool RemoveValue(UUID storeID, string path)
  356. {
  357. if (! m_enabled) return false;
  358. JsonStore map = null;
  359. lock (m_JsonValueStore)
  360. {
  361. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  362. {
  363. m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
  364. return false;
  365. }
  366. }
  367. try
  368. {
  369. lock (map)
  370. return map.RemoveValue(path);
  371. }
  372. catch (Exception e)
  373. {
  374. m_log.Error(string.Format("[JsonStore]: Unable to remove {0} in {1}", path, storeID), e);
  375. }
  376. return false;
  377. }
  378. // -----------------------------------------------------------------
  379. /// <summary>
  380. ///
  381. /// </summary>
  382. // -----------------------------------------------------------------
  383. public int GetArrayLength(UUID storeID, string path)
  384. {
  385. if (! m_enabled) return -1;
  386. JsonStore map = null;
  387. lock (m_JsonValueStore)
  388. {
  389. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  390. return -1;
  391. }
  392. try
  393. {
  394. lock (map)
  395. {
  396. return map.ArrayLength(path);
  397. }
  398. }
  399. catch (Exception e)
  400. {
  401. m_log.Error("[JsonStore]: unable to retrieve value", e);
  402. }
  403. return -1;
  404. }
  405. // -----------------------------------------------------------------
  406. /// <summary>
  407. ///
  408. /// </summary>
  409. // -----------------------------------------------------------------
  410. public bool GetValue(UUID storeID, string path, bool useJson, out string value)
  411. {
  412. value = String.Empty;
  413. if (! m_enabled) return false;
  414. JsonStore map = null;
  415. lock (m_JsonValueStore)
  416. {
  417. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  418. return false;
  419. }
  420. try
  421. {
  422. lock (map)
  423. {
  424. return map.GetValue(path, out value, useJson);
  425. }
  426. }
  427. catch (Exception e)
  428. {
  429. m_log.Error("[JsonStore]: unable to retrieve value", e);
  430. }
  431. return false;
  432. }
  433. // -----------------------------------------------------------------
  434. /// <summary>
  435. ///
  436. /// </summary>
  437. // -----------------------------------------------------------------
  438. public void TakeValue(UUID storeID, string path, bool useJson, TakeValueCallback cback)
  439. {
  440. if (! m_enabled)
  441. {
  442. cback(String.Empty);
  443. return;
  444. }
  445. JsonStore map = null;
  446. lock (m_JsonValueStore)
  447. {
  448. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  449. {
  450. cback(String.Empty);
  451. return;
  452. }
  453. }
  454. try
  455. {
  456. lock (map)
  457. {
  458. map.TakeValue(path, useJson, cback);
  459. return;
  460. }
  461. }
  462. catch (Exception e)
  463. {
  464. m_log.Error("[JsonStore] unable to retrieve value", e);
  465. }
  466. cback(String.Empty);
  467. }
  468. // -----------------------------------------------------------------
  469. /// <summary>
  470. ///
  471. /// </summary>
  472. // -----------------------------------------------------------------
  473. public void ReadValue(UUID storeID, string path, bool useJson, TakeValueCallback cback)
  474. {
  475. if (! m_enabled)
  476. {
  477. cback(String.Empty);
  478. return;
  479. }
  480. JsonStore map = null;
  481. lock (m_JsonValueStore)
  482. {
  483. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  484. {
  485. cback(String.Empty);
  486. return;
  487. }
  488. }
  489. try
  490. {
  491. lock (map)
  492. {
  493. map.ReadValue(path, useJson, cback);
  494. return;
  495. }
  496. }
  497. catch (Exception e)
  498. {
  499. m_log.Error("[JsonStore]: unable to retrieve value", e);
  500. }
  501. cback(String.Empty);
  502. }
  503. #endregion
  504. }
  505. }