RemoteConnectorCacheWrapper.cs 31 KB

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