1
0

RemoteConnectorCacheWrapper.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  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 OpenSimulator 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 System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Threading;
  31. using OpenSim.Framework;
  32. //using OpenSim.Region.Framework.Interfaces;
  33. using OpenSim.Services.Interfaces;
  34. using OpenMetaverse;
  35. namespace OpenSim.Groups
  36. {
  37. public delegate ExtendedGroupRecord GroupRecordDelegate();
  38. public delegate GroupMembershipData GroupMembershipDelegate();
  39. public delegate List<GroupMembershipData> GroupMembershipListDelegate();
  40. public delegate List<ExtendedGroupMembersData> GroupMembersListDelegate();
  41. public delegate List<GroupRolesData> GroupRolesListDelegate();
  42. public delegate List<ExtendedGroupRoleMembersData> RoleMembersListDelegate();
  43. public delegate GroupNoticeInfo NoticeDelegate();
  44. public delegate List<ExtendedGroupNoticeData> NoticeListDelegate();
  45. public delegate void VoidDelegate();
  46. public delegate bool BooleanDelegate();
  47. public class RemoteConnectorCacheWrapper
  48. {
  49. private ForeignImporter m_ForeignImporter;
  50. private Dictionary<string, bool> m_ActiveRequests = new Dictionary<string, bool>();
  51. private const int GROUPS_CACHE_TIMEOUT = 1 * 60; // 1 minutes
  52. // This all important cache cahces objects of different types:
  53. // group-<GroupID> or group-<Name> => ExtendedGroupRecord
  54. // active-<AgentID> => GroupMembershipData
  55. // membership-<AgentID>-<GroupID> => GroupMembershipData
  56. // memberships-<AgentID> => List<GroupMembershipData>
  57. // members-<RequestingAgentID>-<GroupID> => List<ExtendedGroupMembersData>
  58. // role-<RoleID> => GroupRolesData
  59. // roles-<GroupID> => List<GroupRolesData> ; all roles in the group
  60. // roles-<GroupID>-<AgentID> => List<GroupRolesData> ; roles that the agent has
  61. // rolemembers-<RequestingAgentID>-<GroupID> => List<ExtendedGroupRoleMembersData>
  62. // notice-<noticeID> => GroupNoticeInfo
  63. // notices-<GroupID> => List<ExtendedGroupNoticeData>
  64. private ExpiringCache<string, object> m_Cache = new ExpiringCache<string, object>();
  65. public RemoteConnectorCacheWrapper(IUserManagement uman)
  66. {
  67. m_ForeignImporter = new ForeignImporter(uman);
  68. }
  69. public UUID CreateGroup(UUID RequestingAgentID, GroupRecordDelegate d)
  70. {
  71. //m_log.DebugFormat("[Groups.RemoteConnector]: Creating group {0}", name);
  72. //reason = string.Empty;
  73. //ExtendedGroupRecord group = m_GroupsService.CreateGroup(RequestingAgentID.ToString(), name, charter, showInList, insigniaID,
  74. // membershipFee, openEnrollment, allowPublish, maturePublish, founderID, out reason);
  75. ExtendedGroupRecord group = d();
  76. if (group == null)
  77. return UUID.Zero;
  78. if (group.GroupID != UUID.Zero)
  79. lock (m_Cache)
  80. {
  81. m_Cache.Add("group-" + group.GroupID.ToString(), group, GROUPS_CACHE_TIMEOUT);
  82. if (m_Cache.Contains("memberships-" + RequestingAgentID.ToString()))
  83. m_Cache.Remove("memberships-" + RequestingAgentID.ToString());
  84. }
  85. return group.GroupID;
  86. }
  87. public bool UpdateGroup(UUID groupID, GroupRecordDelegate d)
  88. {
  89. //reason = string.Empty;
  90. //ExtendedGroupRecord group = m_GroupsService.UpdateGroup(RequestingAgentID, groupID, charter, showInList, insigniaID, membershipFee, openEnrollment, allowPublish, maturePublish);
  91. ExtendedGroupRecord group = d();
  92. if (group != null && group.GroupID != UUID.Zero)
  93. lock (m_Cache)
  94. m_Cache.AddOrUpdate("group-" + group.GroupID.ToString(), group, GROUPS_CACHE_TIMEOUT);
  95. return true;
  96. }
  97. public ExtendedGroupRecord GetGroupRecord(string RequestingAgentID, UUID GroupID, string GroupName, GroupRecordDelegate d)
  98. {
  99. //if (GroupID == UUID.Zero && (GroupName == null || GroupName != null && GroupName == string.Empty))
  100. // return null;
  101. object group = null;
  102. bool firstCall = false;
  103. string cacheKey = "group-";
  104. if (GroupID != UUID.Zero)
  105. cacheKey += GroupID.ToString();
  106. else
  107. cacheKey += GroupName;
  108. //m_log.DebugFormat("[XXX]: GetGroupRecord {0}", cacheKey);
  109. while (true)
  110. {
  111. lock (m_Cache)
  112. {
  113. if (m_Cache.TryGetValue(cacheKey, out group))
  114. {
  115. //m_log.DebugFormat("[XXX]: GetGroupRecord {0} cached!", cacheKey);
  116. return (ExtendedGroupRecord)group;
  117. }
  118. // not cached
  119. if (!m_ActiveRequests.ContainsKey(cacheKey))
  120. {
  121. m_ActiveRequests.Add(cacheKey, true);
  122. firstCall = true;
  123. }
  124. }
  125. if (firstCall)
  126. {
  127. try
  128. {
  129. //group = m_GroupsService.GetGroupRecord(RequestingAgentID, GroupID, GroupName);
  130. group = d();
  131. lock (m_Cache)
  132. {
  133. m_Cache.AddOrUpdate(cacheKey, group, GROUPS_CACHE_TIMEOUT);
  134. return (ExtendedGroupRecord)group;
  135. }
  136. }
  137. finally
  138. {
  139. m_ActiveRequests.Remove(cacheKey);
  140. }
  141. }
  142. else
  143. Thread.Sleep(50);
  144. }
  145. }
  146. public bool AddAgentToGroup(string RequestingAgentID, string AgentID, UUID GroupID, GroupMembershipDelegate d)
  147. {
  148. GroupMembershipData membership = d();
  149. if (membership == null)
  150. return false;
  151. lock (m_Cache)
  152. {
  153. // first, remove everything! add a user is a heavy-duty op
  154. m_Cache.Clear();
  155. m_Cache.AddOrUpdate("active-" + AgentID.ToString(), membership, GROUPS_CACHE_TIMEOUT);
  156. m_Cache.AddOrUpdate("membership-" + AgentID.ToString() + "-" + GroupID.ToString(), membership, GROUPS_CACHE_TIMEOUT);
  157. }
  158. return true;
  159. }
  160. public void RemoveAgentFromGroup(string RequestingAgentID, string AgentID, UUID GroupID, VoidDelegate d)
  161. {
  162. d();
  163. lock (m_Cache)
  164. {
  165. string cacheKey = "active-" + AgentID.ToString();
  166. if (m_Cache.Contains(cacheKey))
  167. m_Cache.Remove(cacheKey);
  168. cacheKey = "memberships-" + AgentID.ToString();
  169. if (m_Cache.Contains(cacheKey))
  170. m_Cache.Remove(cacheKey);
  171. cacheKey = "membership-" + AgentID.ToString() + "-" + GroupID.ToString();
  172. if (m_Cache.Contains(cacheKey))
  173. m_Cache.Remove(cacheKey);
  174. cacheKey = "members-" + RequestingAgentID.ToString() + "-" + GroupID.ToString();
  175. if (m_Cache.Contains(cacheKey))
  176. m_Cache.Remove(cacheKey);
  177. cacheKey = "roles-" + "-" + GroupID.ToString() + "-" + AgentID.ToString();
  178. if (m_Cache.Contains(cacheKey))
  179. m_Cache.Remove(cacheKey);
  180. }
  181. }
  182. public void SetAgentActiveGroup(string AgentID, GroupMembershipDelegate d)
  183. {
  184. GroupMembershipData activeGroup = d();
  185. string cacheKey = "active-" + AgentID.ToString();
  186. lock (m_Cache)
  187. if (m_Cache.Contains(cacheKey))
  188. m_Cache.AddOrUpdate(cacheKey, activeGroup, GROUPS_CACHE_TIMEOUT);
  189. }
  190. public ExtendedGroupMembershipData GetAgentActiveMembership(string AgentID, GroupMembershipDelegate d)
  191. {
  192. object membership = null;
  193. bool firstCall = false;
  194. string cacheKey = "active-" + AgentID.ToString();
  195. //m_log.DebugFormat("[XXX]: GetAgentActiveMembership {0}", cacheKey);
  196. while (true)
  197. {
  198. lock (m_Cache)
  199. {
  200. if (m_Cache.TryGetValue(cacheKey, out membership))
  201. {
  202. //m_log.DebugFormat("[XXX]: GetAgentActiveMembership {0} cached!", cacheKey);
  203. return (ExtendedGroupMembershipData)membership;
  204. }
  205. // not cached
  206. if (!m_ActiveRequests.ContainsKey(cacheKey))
  207. {
  208. m_ActiveRequests.Add(cacheKey, true);
  209. firstCall = true;
  210. }
  211. }
  212. if (firstCall)
  213. {
  214. try
  215. {
  216. membership = d();
  217. lock (m_Cache)
  218. {
  219. m_Cache.AddOrUpdate(cacheKey, membership, GROUPS_CACHE_TIMEOUT);
  220. return (ExtendedGroupMembershipData)membership;
  221. }
  222. }
  223. finally
  224. {
  225. m_ActiveRequests.Remove(cacheKey);
  226. }
  227. }
  228. else
  229. Thread.Sleep(50);
  230. }
  231. }
  232. public ExtendedGroupMembershipData GetAgentGroupMembership(string AgentID, UUID GroupID, GroupMembershipDelegate d)
  233. {
  234. object membership = null;
  235. bool firstCall = false;
  236. string cacheKey = "membership-" + AgentID.ToString() + "-" + GroupID.ToString();
  237. //m_log.DebugFormat("[XXX]: GetAgentGroupMembership {0}", cacheKey);
  238. while (true)
  239. {
  240. lock (m_Cache)
  241. {
  242. if (m_Cache.TryGetValue(cacheKey, out membership))
  243. {
  244. //m_log.DebugFormat("[XXX]: GetAgentGroupMembership {0}", cacheKey);
  245. return (ExtendedGroupMembershipData)membership;
  246. }
  247. // not cached
  248. if (!m_ActiveRequests.ContainsKey(cacheKey))
  249. {
  250. m_ActiveRequests.Add(cacheKey, true);
  251. firstCall = true;
  252. }
  253. }
  254. if (firstCall)
  255. {
  256. try
  257. {
  258. membership = d();
  259. lock (m_Cache)
  260. {
  261. m_Cache.AddOrUpdate(cacheKey, membership, GROUPS_CACHE_TIMEOUT);
  262. return (ExtendedGroupMembershipData)membership;
  263. }
  264. }
  265. finally
  266. {
  267. m_ActiveRequests.Remove(cacheKey);
  268. }
  269. }
  270. else
  271. Thread.Sleep(50);
  272. }
  273. }
  274. public List<GroupMembershipData> GetAgentGroupMemberships(string AgentID, GroupMembershipListDelegate d)
  275. {
  276. object memberships = null;
  277. bool firstCall = false;
  278. string cacheKey = "memberships-" + AgentID.ToString();
  279. //m_log.DebugFormat("[XXX]: GetAgentGroupMemberships {0}", cacheKey);
  280. while (true)
  281. {
  282. lock (m_Cache)
  283. {
  284. if (m_Cache.TryGetValue(cacheKey, out memberships))
  285. {
  286. //m_log.DebugFormat("[XXX]: GetAgentGroupMemberships {0} cached!", cacheKey);
  287. return (List<GroupMembershipData>)memberships;
  288. }
  289. // not cached
  290. if (!m_ActiveRequests.ContainsKey(cacheKey))
  291. {
  292. m_ActiveRequests.Add(cacheKey, true);
  293. firstCall = true;
  294. }
  295. }
  296. if (firstCall)
  297. {
  298. try
  299. {
  300. memberships = d();
  301. lock (m_Cache)
  302. {
  303. m_Cache.AddOrUpdate(cacheKey, memberships, GROUPS_CACHE_TIMEOUT);
  304. return (List<GroupMembershipData>)memberships;
  305. }
  306. }
  307. finally
  308. {
  309. m_ActiveRequests.Remove(cacheKey);
  310. }
  311. }
  312. else
  313. Thread.Sleep(50);
  314. }
  315. }
  316. public List<GroupMembersData> GetGroupMembers(string RequestingAgentID, UUID GroupID, GroupMembersListDelegate d)
  317. {
  318. object members = null;
  319. bool firstCall = false;
  320. // we need to key in also on the requester, because different ppl have different view privileges
  321. string cacheKey = "members-" + RequestingAgentID.ToString() + "-" + GroupID.ToString();
  322. //m_log.DebugFormat("[XXX]: GetGroupMembers {0}", cacheKey);
  323. while (true)
  324. {
  325. lock (m_Cache)
  326. {
  327. if (m_Cache.TryGetValue(cacheKey, out members))
  328. {
  329. List<ExtendedGroupMembersData> xx = (List<ExtendedGroupMembersData>)members;
  330. return xx.ConvertAll<GroupMembersData>(new Converter<ExtendedGroupMembersData, GroupMembersData>(m_ForeignImporter.ConvertGroupMembersData));
  331. }
  332. // not cached
  333. if (!m_ActiveRequests.ContainsKey(cacheKey))
  334. {
  335. m_ActiveRequests.Add(cacheKey, true);
  336. firstCall = true;
  337. }
  338. }
  339. if (firstCall)
  340. {
  341. try
  342. {
  343. List<ExtendedGroupMembersData> _members = d();
  344. if (_members != null && _members.Count > 0)
  345. members = _members.ConvertAll<GroupMembersData>(new Converter<ExtendedGroupMembersData, GroupMembersData>(m_ForeignImporter.ConvertGroupMembersData));
  346. else
  347. members = new List<GroupMembersData>();
  348. lock (m_Cache)
  349. {
  350. //m_Cache.AddOrUpdate(cacheKey, members, GROUPS_CACHE_TIMEOUT);
  351. m_Cache.AddOrUpdate(cacheKey, _members, GROUPS_CACHE_TIMEOUT);
  352. return (List<GroupMembersData>)members;
  353. }
  354. }
  355. finally
  356. {
  357. m_ActiveRequests.Remove(cacheKey);
  358. }
  359. }
  360. else
  361. Thread.Sleep(50);
  362. }
  363. }
  364. public bool AddGroupRole(UUID groupID, UUID roleID, string description, string name, ulong powers, string title, BooleanDelegate d)
  365. {
  366. if (d())
  367. {
  368. GroupRolesData role = new GroupRolesData();
  369. role.Description = description;
  370. role.Members = 0;
  371. role.Name = name;
  372. role.Powers = powers;
  373. role.RoleID = roleID;
  374. role.Title = title;
  375. lock (m_Cache)
  376. {
  377. m_Cache.AddOrUpdate("role-" + roleID.ToString(), role, GROUPS_CACHE_TIMEOUT);
  378. // also remove this list
  379. if (m_Cache.Contains("roles-" + groupID.ToString()))
  380. m_Cache.Remove("roles-" + groupID.ToString());
  381. }
  382. return true;
  383. }
  384. return false;
  385. }
  386. public bool UpdateGroupRole(UUID groupID, UUID roleID, string name, string description, string title, ulong powers, BooleanDelegate d)
  387. {
  388. if (d())
  389. {
  390. object role;
  391. lock (m_Cache)
  392. if (m_Cache.TryGetValue("role-" + roleID.ToString(), out role))
  393. {
  394. GroupRolesData r = (GroupRolesData)role;
  395. r.Description = description;
  396. r.Name = name;
  397. r.Powers = powers;
  398. r.Title = title;
  399. m_Cache.Update("role-" + roleID.ToString(), r, GROUPS_CACHE_TIMEOUT);
  400. }
  401. return true;
  402. }
  403. else
  404. {
  405. lock (m_Cache)
  406. {
  407. if (m_Cache.Contains("role-" + roleID.ToString()))
  408. m_Cache.Remove("role-" + roleID.ToString());
  409. // also remove these lists, because they will have an outdated role
  410. if (m_Cache.Contains("roles-" + groupID.ToString()))
  411. m_Cache.Remove("roles-" + groupID.ToString());
  412. }
  413. return false;
  414. }
  415. }
  416. public void RemoveGroupRole(string RequestingAgentID, UUID groupID, UUID roleID, VoidDelegate d)
  417. {
  418. d();
  419. lock (m_Cache)
  420. {
  421. if (m_Cache.Contains("role-" + roleID.ToString()))
  422. m_Cache.Remove("role-" + roleID.ToString());
  423. // also remove the list, because it will have an removed role
  424. if (m_Cache.Contains("roles-" + groupID.ToString()))
  425. m_Cache.Remove("roles-" + groupID.ToString());
  426. if (m_Cache.Contains("roles-" + groupID.ToString() + "-" + RequestingAgentID.ToString()))
  427. m_Cache.Remove("roles-" + groupID.ToString() + "-" + RequestingAgentID.ToString());
  428. if (m_Cache.Contains("rolemembers-" + RequestingAgentID.ToString() + "-" + groupID.ToString()))
  429. m_Cache.Remove("rolemembers-" + RequestingAgentID.ToString() + "-" + groupID.ToString());
  430. }
  431. }
  432. public List<GroupRolesData> GetGroupRoles(string RequestingAgentID, UUID GroupID, GroupRolesListDelegate d)
  433. {
  434. object roles = null;
  435. bool firstCall = false;
  436. string cacheKey = "roles-" + GroupID.ToString();
  437. while (true)
  438. {
  439. lock (m_Cache)
  440. {
  441. if (m_Cache.TryGetValue(cacheKey, out roles))
  442. return (List<GroupRolesData>)roles;
  443. // not cached
  444. if (!m_ActiveRequests.ContainsKey(cacheKey))
  445. {
  446. m_ActiveRequests.Add(cacheKey, true);
  447. firstCall = true;
  448. }
  449. }
  450. if (firstCall)
  451. {
  452. try
  453. {
  454. roles = d();
  455. if (roles != null)
  456. {
  457. lock (m_Cache)
  458. {
  459. m_Cache.AddOrUpdate(cacheKey, roles, GROUPS_CACHE_TIMEOUT);
  460. return (List<GroupRolesData>)roles;
  461. }
  462. }
  463. }
  464. finally
  465. {
  466. m_ActiveRequests.Remove(cacheKey);
  467. }
  468. }
  469. else
  470. Thread.Sleep(50);
  471. }
  472. }
  473. public List<GroupRoleMembersData> GetGroupRoleMembers(string RequestingAgentID, UUID GroupID, RoleMembersListDelegate d)
  474. {
  475. object rmembers = null;
  476. bool firstCall = false;
  477. // we need to key in also on the requester, because different ppl have different view privileges
  478. string cacheKey = "rolemembers-" + RequestingAgentID.ToString() + "-" + GroupID.ToString();
  479. //m_log.DebugFormat("[XXX]: GetGroupRoleMembers {0}", cacheKey);
  480. while (true)
  481. {
  482. lock (m_Cache)
  483. {
  484. if (m_Cache.TryGetValue(cacheKey, out rmembers))
  485. {
  486. List<ExtendedGroupRoleMembersData> xx = (List<ExtendedGroupRoleMembersData>)rmembers;
  487. return xx.ConvertAll<GroupRoleMembersData>(m_ForeignImporter.ConvertGroupRoleMembersData);
  488. }
  489. // not cached
  490. if (!m_ActiveRequests.ContainsKey(cacheKey))
  491. {
  492. m_ActiveRequests.Add(cacheKey, true);
  493. firstCall = true;
  494. }
  495. }
  496. if (firstCall)
  497. {
  498. try
  499. {
  500. List<ExtendedGroupRoleMembersData> _rmembers = d();
  501. if (_rmembers != null && _rmembers.Count > 0)
  502. rmembers = _rmembers.ConvertAll<GroupRoleMembersData>(new Converter<ExtendedGroupRoleMembersData, GroupRoleMembersData>(m_ForeignImporter.ConvertGroupRoleMembersData));
  503. else
  504. rmembers = new List<GroupRoleMembersData>();
  505. lock (m_Cache)
  506. {
  507. // For some strange reason, when I cache the list of GroupRoleMembersData,
  508. // it gets emptied out. The TryGet gets an empty list...
  509. //m_Cache.AddOrUpdate(cacheKey, rmembers, GROUPS_CACHE_TIMEOUT);
  510. // Caching the list of ExtendedGroupRoleMembersData doesn't show that issue
  511. // I don't get it.
  512. m_Cache.AddOrUpdate(cacheKey, _rmembers, GROUPS_CACHE_TIMEOUT);
  513. return (List<GroupRoleMembersData>)rmembers;
  514. }
  515. }
  516. finally
  517. {
  518. m_ActiveRequests.Remove(cacheKey);
  519. }
  520. }
  521. else
  522. Thread.Sleep(50);
  523. }
  524. }
  525. public void AddAgentToGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID, BooleanDelegate d)
  526. {
  527. if (d())
  528. {
  529. lock (m_Cache)
  530. {
  531. // update the cached role
  532. string cacheKey = "role-" + RoleID.ToString();
  533. object obj;
  534. if (m_Cache.TryGetValue(cacheKey, out obj))
  535. {
  536. GroupRolesData r = (GroupRolesData)obj;
  537. r.Members++;
  538. }
  539. // add this agent to the list of role members
  540. cacheKey = "rolemembers-" + RequestingAgentID.ToString() + "-" + GroupID.ToString();
  541. if (m_Cache.TryGetValue(cacheKey, out obj))
  542. {
  543. try
  544. {
  545. // This may throw an exception, in which case the agentID is not a UUID but a full ID
  546. // In that case, let's just remove the whoe things from the cache
  547. UUID id = new UUID(AgentID);
  548. List<ExtendedGroupRoleMembersData> xx = (List<ExtendedGroupRoleMembersData>)obj;
  549. List<GroupRoleMembersData> rmlist = xx.ConvertAll<GroupRoleMembersData>(m_ForeignImporter.ConvertGroupRoleMembersData);
  550. GroupRoleMembersData rm = new GroupRoleMembersData();
  551. rm.MemberID = id;
  552. rm.RoleID = RoleID;
  553. rmlist.Add(rm);
  554. }
  555. catch
  556. {
  557. m_Cache.Remove(cacheKey);
  558. }
  559. }
  560. // Remove the cached info about this agent's roles
  561. // because we don't have enough local info about the new role
  562. cacheKey = "roles-" + GroupID.ToString() + "-" + AgentID.ToString();
  563. if (m_Cache.Contains(cacheKey))
  564. m_Cache.Remove(cacheKey);
  565. }
  566. }
  567. }
  568. public void RemoveAgentFromGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID, BooleanDelegate d)
  569. {
  570. if (d())
  571. {
  572. lock (m_Cache)
  573. {
  574. // update the cached role
  575. string cacheKey = "role-" + RoleID.ToString();
  576. object obj;
  577. if (m_Cache.TryGetValue(cacheKey, out obj))
  578. {
  579. GroupRolesData r = (GroupRolesData)obj;
  580. r.Members--;
  581. }
  582. cacheKey = "roles-" + GroupID.ToString() + "-" + AgentID.ToString();
  583. if (m_Cache.Contains(cacheKey))
  584. m_Cache.Remove(cacheKey);
  585. cacheKey = "rolemembers-" + RequestingAgentID.ToString() + "-" + GroupID.ToString();
  586. if (m_Cache.Contains(cacheKey))
  587. m_Cache.Remove(cacheKey);
  588. }
  589. }
  590. }
  591. public List<GroupRolesData> GetAgentGroupRoles(string RequestingAgentID, string AgentID, UUID GroupID, GroupRolesListDelegate d)
  592. {
  593. object roles = null;
  594. bool firstCall = false;
  595. string cacheKey = "roles-" + GroupID.ToString() + "-" + AgentID.ToString();
  596. //m_log.DebugFormat("[XXX]: GetAgentGroupRoles {0}", cacheKey);
  597. while (true)
  598. {
  599. lock (m_Cache)
  600. {
  601. if (m_Cache.TryGetValue(cacheKey, out roles))
  602. {
  603. //m_log.DebugFormat("[XXX]: GetAgentGroupRoles {0} cached!", cacheKey);
  604. return (List<GroupRolesData>)roles;
  605. }
  606. // not cached
  607. if (!m_ActiveRequests.ContainsKey(cacheKey))
  608. {
  609. m_ActiveRequests.Add(cacheKey, true);
  610. firstCall = true;
  611. }
  612. }
  613. if (firstCall)
  614. {
  615. try
  616. {
  617. roles = d();
  618. lock (m_Cache)
  619. {
  620. m_Cache.AddOrUpdate(cacheKey, roles, GROUPS_CACHE_TIMEOUT);
  621. m_ActiveRequests.Remove(cacheKey);
  622. return (List<GroupRolesData>)roles;
  623. }
  624. }
  625. finally
  626. {
  627. m_ActiveRequests.Remove(cacheKey);
  628. }
  629. }
  630. else
  631. Thread.Sleep(50);
  632. }
  633. }
  634. public void SetAgentActiveGroupRole(string AgentID, UUID GroupID, VoidDelegate d)
  635. {
  636. d();
  637. lock (m_Cache)
  638. {
  639. // Invalidate cached info, because it has ActiveRoleID and Powers
  640. string cacheKey = "membership-" + AgentID.ToString() + "-" + GroupID.ToString();
  641. if (m_Cache.Contains(cacheKey))
  642. m_Cache.Remove(cacheKey);
  643. cacheKey = "memberships-" + AgentID.ToString();
  644. if (m_Cache.Contains(cacheKey))
  645. m_Cache.Remove(cacheKey);
  646. }
  647. }
  648. public void UpdateMembership(string AgentID, UUID GroupID, bool AcceptNotices, bool ListInProfile, VoidDelegate d)
  649. {
  650. d();
  651. lock (m_Cache)
  652. {
  653. string cacheKey = "membership-" + AgentID.ToString() + "-" + GroupID.ToString();
  654. if (m_Cache.Contains(cacheKey))
  655. m_Cache.Remove(cacheKey);
  656. cacheKey = "memberships-" + AgentID.ToString();
  657. if (m_Cache.Contains(cacheKey))
  658. m_Cache.Remove(cacheKey);
  659. cacheKey = "active-" + AgentID.ToString();
  660. object m = null;
  661. if (m_Cache.TryGetValue(cacheKey, out m))
  662. {
  663. GroupMembershipData membership = (GroupMembershipData)m;
  664. membership.ListInProfile = ListInProfile;
  665. membership.AcceptNotices = AcceptNotices;
  666. }
  667. }
  668. }
  669. public bool AddGroupNotice(UUID groupID, UUID noticeID, GroupNoticeInfo notice, BooleanDelegate d)
  670. {
  671. if (d())
  672. {
  673. lock (m_Cache)
  674. {
  675. m_Cache.AddOrUpdate("notice-" + noticeID.ToString(), notice, GROUPS_CACHE_TIMEOUT);
  676. string cacheKey = "notices-" + groupID.ToString();
  677. if (m_Cache.Contains(cacheKey))
  678. m_Cache.Remove(cacheKey);
  679. }
  680. return true;
  681. }
  682. return false;
  683. }
  684. public GroupNoticeInfo GetGroupNotice(UUID noticeID, NoticeDelegate d)
  685. {
  686. object notice = null;
  687. bool firstCall = false;
  688. string cacheKey = "notice-" + noticeID.ToString();
  689. //m_log.DebugFormat("[XXX]: GetAgentGroupRoles {0}", cacheKey);
  690. while (true)
  691. {
  692. lock (m_Cache)
  693. {
  694. if (m_Cache.TryGetValue(cacheKey, out notice))
  695. {
  696. return (GroupNoticeInfo)notice;
  697. }
  698. // not cached
  699. if (!m_ActiveRequests.ContainsKey(cacheKey))
  700. {
  701. m_ActiveRequests.Add(cacheKey, true);
  702. firstCall = true;
  703. }
  704. }
  705. if (firstCall)
  706. {
  707. try
  708. {
  709. GroupNoticeInfo _notice = d();
  710. lock (m_Cache)
  711. {
  712. m_Cache.AddOrUpdate(cacheKey, _notice, GROUPS_CACHE_TIMEOUT);
  713. return _notice;
  714. }
  715. }
  716. finally
  717. {
  718. m_ActiveRequests.Remove(cacheKey);
  719. }
  720. }
  721. else
  722. Thread.Sleep(50);
  723. }
  724. }
  725. public List<ExtendedGroupNoticeData> GetGroupNotices(UUID GroupID, NoticeListDelegate d)
  726. {
  727. object notices = null;
  728. bool firstCall = false;
  729. string cacheKey = "notices-" + GroupID.ToString();
  730. //m_log.DebugFormat("[XXX]: GetGroupNotices {0}", cacheKey);
  731. while (true)
  732. {
  733. lock (m_Cache)
  734. {
  735. if (m_Cache.TryGetValue(cacheKey, out notices))
  736. {
  737. //m_log.DebugFormat("[XXX]: GetGroupNotices {0} cached!", cacheKey);
  738. return (List<ExtendedGroupNoticeData>)notices;
  739. }
  740. // not cached
  741. if (!m_ActiveRequests.ContainsKey(cacheKey))
  742. {
  743. m_ActiveRequests.Add(cacheKey, true);
  744. firstCall = true;
  745. }
  746. }
  747. if (firstCall)
  748. {
  749. try
  750. {
  751. notices = d();
  752. lock (m_Cache)
  753. {
  754. m_Cache.AddOrUpdate(cacheKey, notices, GROUPS_CACHE_TIMEOUT);
  755. return (List<ExtendedGroupNoticeData>)notices;
  756. }
  757. }
  758. finally
  759. {
  760. m_ActiveRequests.Remove(cacheKey);
  761. }
  762. }
  763. else
  764. Thread.Sleep(50);
  765. }
  766. }
  767. }
  768. }