JsonStoreModule.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. }
  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. /// <summary>
  140. /// Called when all modules have been added for a region. This is
  141. /// where we hook up events
  142. /// </summary>
  143. // -----------------------------------------------------------------
  144. public void RegionLoaded(Scene scene)
  145. {
  146. if (m_enabled) {}
  147. }
  148. /// -----------------------------------------------------------------
  149. /// <summary>
  150. /// </summary>
  151. // -----------------------------------------------------------------
  152. public Type ReplaceableInterface
  153. {
  154. get { return null; }
  155. }
  156. #endregion
  157. #region ScriptInvocationInteface
  158. // -----------------------------------------------------------------
  159. /// <summary>
  160. ///
  161. /// </summary>
  162. // -----------------------------------------------------------------
  163. public bool AttachObjectStore(UUID objectID)
  164. {
  165. if (! m_enabled) return false;
  166. if (! m_enableObjectStore) return false;
  167. SceneObjectPart sop = m_scene.GetSceneObjectPart(objectID);
  168. if (sop == null)
  169. {
  170. m_log.ErrorFormat("[JsonStore] unable to attach to unknown object; {0}", objectID);
  171. return false;
  172. }
  173. lock (m_JsonValueStore)
  174. {
  175. if (m_JsonValueStore.ContainsKey(objectID))
  176. return true;
  177. JsonStore map = new JsonObjectStore(m_scene,objectID);
  178. m_JsonValueStore.Add(objectID,map);
  179. }
  180. return true;
  181. }
  182. // -----------------------------------------------------------------
  183. /// <summary>
  184. ///
  185. /// </summary>
  186. // -----------------------------------------------------------------
  187. public bool CreateStore(string value, ref UUID result)
  188. {
  189. if (result == UUID.Zero)
  190. result = UUID.Random();
  191. JsonStore map = null;
  192. if (! m_enabled) return false;
  193. try
  194. {
  195. map = new JsonStore(value);
  196. }
  197. catch (Exception e)
  198. {
  199. m_log.ErrorFormat("[JsonStore]: Unable to initialize store from {0}", value);
  200. return false;
  201. }
  202. lock (m_JsonValueStore)
  203. m_JsonValueStore.Add(result,map);
  204. return true;
  205. }
  206. // -----------------------------------------------------------------
  207. /// <summary>
  208. ///
  209. /// </summary>
  210. // -----------------------------------------------------------------
  211. public bool DestroyStore(UUID storeID)
  212. {
  213. if (! m_enabled) return false;
  214. lock (m_JsonValueStore)
  215. return m_JsonValueStore.Remove(storeID);
  216. return true;
  217. }
  218. // -----------------------------------------------------------------
  219. /// <summary>
  220. ///
  221. /// </summary>
  222. // -----------------------------------------------------------------
  223. public bool TestStore(UUID storeID)
  224. {
  225. if (! m_enabled) return false;
  226. lock (m_JsonValueStore)
  227. return m_JsonValueStore.ContainsKey(storeID);
  228. }
  229. // -----------------------------------------------------------------
  230. /// <summary>
  231. ///
  232. /// </summary>
  233. // -----------------------------------------------------------------
  234. public JsonStoreNodeType GetNodeType(UUID storeID, string path)
  235. {
  236. if (! m_enabled) return JsonStoreNodeType.Undefined;
  237. JsonStore map = null;
  238. lock (m_JsonValueStore)
  239. {
  240. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  241. {
  242. m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
  243. return JsonStoreNodeType.Undefined;
  244. }
  245. }
  246. try
  247. {
  248. lock (map)
  249. return map.GetNodeType(path);
  250. }
  251. catch (Exception e)
  252. {
  253. m_log.Error(string.Format("[JsonStore]: Path test failed for {0} in {1}", path, storeID), e);
  254. }
  255. return JsonStoreNodeType.Undefined;
  256. }
  257. // -----------------------------------------------------------------
  258. /// <summary>
  259. ///
  260. /// </summary>
  261. // -----------------------------------------------------------------
  262. public JsonStoreValueType GetValueType(UUID storeID, string path)
  263. {
  264. if (! m_enabled) return JsonStoreValueType.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 JsonStoreValueType.Undefined;
  272. }
  273. }
  274. try
  275. {
  276. lock (map)
  277. return map.GetValueType(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 JsonStoreValueType.Undefined;
  284. }
  285. // -----------------------------------------------------------------
  286. /// <summary>
  287. ///
  288. /// </summary>
  289. // -----------------------------------------------------------------
  290. public bool SetValue(UUID storeID, string path, string value, bool useJson)
  291. {
  292. if (! m_enabled) return false;
  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 false;
  300. }
  301. }
  302. try
  303. {
  304. lock (map)
  305. {
  306. if (map.StringSpace > m_maxStringSpace)
  307. {
  308. m_log.WarnFormat("[JsonStore] {0} exceeded string size; {1} bytes used of {2} limit",
  309. storeID,map.StringSpace,m_maxStringSpace);
  310. return false;
  311. }
  312. return map.SetValue(path,value,useJson);
  313. }
  314. }
  315. catch (Exception e)
  316. {
  317. m_log.Error(string.Format("[JsonStore]: Unable to assign {0} to {1} in {2}", value, path, storeID), e);
  318. }
  319. return false;
  320. }
  321. // -----------------------------------------------------------------
  322. /// <summary>
  323. ///
  324. /// </summary>
  325. // -----------------------------------------------------------------
  326. public bool RemoveValue(UUID storeID, string path)
  327. {
  328. if (! m_enabled) return false;
  329. JsonStore map = null;
  330. lock (m_JsonValueStore)
  331. {
  332. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  333. {
  334. m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
  335. return false;
  336. }
  337. }
  338. try
  339. {
  340. lock (map)
  341. return map.RemoveValue(path);
  342. }
  343. catch (Exception e)
  344. {
  345. m_log.Error(string.Format("[JsonStore]: Unable to remove {0} in {1}", path, storeID), e);
  346. }
  347. return false;
  348. }
  349. // -----------------------------------------------------------------
  350. /// <summary>
  351. ///
  352. /// </summary>
  353. // -----------------------------------------------------------------
  354. public int GetArrayLength(UUID storeID, string path)
  355. {
  356. if (! m_enabled) return -1;
  357. JsonStore map = null;
  358. lock (m_JsonValueStore)
  359. {
  360. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  361. return -1;
  362. }
  363. try
  364. {
  365. lock (map)
  366. {
  367. return map.ArrayLength(path);
  368. }
  369. }
  370. catch (Exception e)
  371. {
  372. m_log.Error("[JsonStore]: unable to retrieve value", e);
  373. }
  374. return -1;
  375. }
  376. // -----------------------------------------------------------------
  377. /// <summary>
  378. ///
  379. /// </summary>
  380. // -----------------------------------------------------------------
  381. public bool GetValue(UUID storeID, string path, bool useJson, out string value)
  382. {
  383. value = String.Empty;
  384. if (! m_enabled) return false;
  385. JsonStore map = null;
  386. lock (m_JsonValueStore)
  387. {
  388. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  389. return false;
  390. }
  391. try
  392. {
  393. lock (map)
  394. {
  395. return map.GetValue(path, out value, useJson);
  396. }
  397. }
  398. catch (Exception e)
  399. {
  400. m_log.Error("[JsonStore]: unable to retrieve value", e);
  401. }
  402. return false;
  403. }
  404. // -----------------------------------------------------------------
  405. /// <summary>
  406. ///
  407. /// </summary>
  408. // -----------------------------------------------------------------
  409. public void TakeValue(UUID storeID, string path, bool useJson, TakeValueCallback cback)
  410. {
  411. if (! m_enabled)
  412. {
  413. cback(String.Empty);
  414. return;
  415. }
  416. JsonStore map = null;
  417. lock (m_JsonValueStore)
  418. {
  419. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  420. {
  421. cback(String.Empty);
  422. return;
  423. }
  424. }
  425. try
  426. {
  427. lock (map)
  428. {
  429. map.TakeValue(path, useJson, cback);
  430. return;
  431. }
  432. }
  433. catch (Exception e)
  434. {
  435. m_log.Error("[JsonStore] unable to retrieve value", e);
  436. }
  437. cback(String.Empty);
  438. }
  439. // -----------------------------------------------------------------
  440. /// <summary>
  441. ///
  442. /// </summary>
  443. // -----------------------------------------------------------------
  444. public void ReadValue(UUID storeID, string path, bool useJson, TakeValueCallback cback)
  445. {
  446. if (! m_enabled)
  447. {
  448. cback(String.Empty);
  449. return;
  450. }
  451. JsonStore map = null;
  452. lock (m_JsonValueStore)
  453. {
  454. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  455. {
  456. cback(String.Empty);
  457. return;
  458. }
  459. }
  460. try
  461. {
  462. lock (map)
  463. {
  464. map.ReadValue(path, useJson, cback);
  465. return;
  466. }
  467. }
  468. catch (Exception e)
  469. {
  470. m_log.Error("[JsonStore]: unable to retrieve value", e);
  471. }
  472. cback(String.Empty);
  473. }
  474. #endregion
  475. }
  476. }