1
0

RemoteConnectorCacheWrapper.cs 32 KB

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