RemoteConnectorCacheWrapper.cs 32 KB

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