diff options
author | BlueWall | 2011-02-09 14:45:40 -0500 |
---|---|---|
committer | BlueWall | 2011-02-09 14:45:40 -0500 |
commit | 0f736e5601a738b4da435b392774ba211647da9c (patch) | |
tree | edb1f9d90c01841eea86b2ce0b2fbf410dc416ee /OpenSim/Region/CoreModules/World/Land/PrimCountModule.cs | |
parent | Merge branch 'master' of /home/opensim/src/OpenSim/Core (diff) | |
parent | Flash out the prim count module (diff) | |
download | opensim-SC_OLD-0f736e5601a738b4da435b392774ba211647da9c.zip opensim-SC_OLD-0f736e5601a738b4da435b392774ba211647da9c.tar.gz opensim-SC_OLD-0f736e5601a738b4da435b392774ba211647da9c.tar.bz2 opensim-SC_OLD-0f736e5601a738b4da435b392774ba211647da9c.tar.xz |
Merge branch 'master' of /home/opensim/src/OpenSim/Core
Diffstat (limited to 'OpenSim/Region/CoreModules/World/Land/PrimCountModule.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/World/Land/PrimCountModule.cs | 406 |
1 files changed, 406 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/World/Land/PrimCountModule.cs b/OpenSim/Region/CoreModules/World/Land/PrimCountModule.cs new file mode 100644 index 0000000..34ef67f --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Land/PrimCountModule.cs | |||
@@ -0,0 +1,406 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Diagnostics; | ||
32 | using System.Reflection; | ||
33 | using log4net; | ||
34 | using Nini.Config; | ||
35 | using OpenMetaverse; | ||
36 | using OpenSim.Framework; | ||
37 | using OpenSim.Region.Framework.Interfaces; | ||
38 | using OpenSim.Region.Framework.Scenes; | ||
39 | using OpenSim.Services.Interfaces; | ||
40 | |||
41 | namespace OpenSim.Region.CoreModules.World.Land | ||
42 | { | ||
43 | public class ParcelCounts | ||
44 | { | ||
45 | public int Owner = 0; | ||
46 | public int Group = 0; | ||
47 | public int Others = 0; | ||
48 | public Dictionary <UUID, int> Users = | ||
49 | new Dictionary <UUID, int>(); | ||
50 | } | ||
51 | |||
52 | public class PrimCountModule : IPrimCountModule, INonSharedRegionModule | ||
53 | { | ||
54 | private static readonly ILog m_log = | ||
55 | LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
56 | |||
57 | private Scene m_Scene; | ||
58 | private Dictionary<UUID, PrimCounts> m_PrimCounts = | ||
59 | new Dictionary<UUID, PrimCounts>(); | ||
60 | private Dictionary<UUID, UUID> m_OwnerMap = | ||
61 | new Dictionary<UUID, UUID>(); | ||
62 | private Dictionary<UUID, int> m_SimwideCounts = | ||
63 | new Dictionary<UUID, int>(); | ||
64 | private Dictionary<UUID, ParcelCounts> m_ParcelCounts = | ||
65 | new Dictionary<UUID, ParcelCounts>(); | ||
66 | |||
67 | // For now, a simple simwide taint to get this up. Later parcel based | ||
68 | // taint to allow recounting a parcel if only ownership has changed | ||
69 | // without recounting the whole sim. | ||
70 | private bool m_Tainted = true; | ||
71 | private Object m_TaintLock = new Object(); | ||
72 | |||
73 | public Type ReplaceableInterface | ||
74 | { | ||
75 | get { return null; } | ||
76 | } | ||
77 | |||
78 | public void Initialise(IConfigSource source) | ||
79 | { | ||
80 | } | ||
81 | |||
82 | public void AddRegion(Scene scene) | ||
83 | { | ||
84 | m_Scene = scene; | ||
85 | |||
86 | m_Scene.EventManager.OnParcelPrimCountAdd += | ||
87 | OnParcelPrimCountAdd; | ||
88 | m_Scene.EventManager.OnObjectBeingRemovedFromScene += | ||
89 | OnObjectBeingRemovedFromScene; | ||
90 | m_Scene.EventManager.OnParcelPrimCountTainted += | ||
91 | OnParcelPrimCountTainted; | ||
92 | } | ||
93 | |||
94 | public void RegionLoaded(Scene scene) | ||
95 | { | ||
96 | } | ||
97 | |||
98 | public void RemoveRegion(Scene scene) | ||
99 | { | ||
100 | } | ||
101 | |||
102 | public void Close() | ||
103 | { | ||
104 | } | ||
105 | |||
106 | public string Name | ||
107 | { | ||
108 | get { return "PrimCountModule"; } | ||
109 | } | ||
110 | |||
111 | private void OnParcelPrimCountAdd(SceneObjectGroup obj) | ||
112 | { | ||
113 | // If we're tainted already, don't bother to add. The next | ||
114 | // access will cause a recount anyway | ||
115 | lock (m_TaintLock) | ||
116 | { | ||
117 | if (!m_Tainted) | ||
118 | AddObject(obj); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | private void OnObjectBeingRemovedFromScene(SceneObjectGroup obj) | ||
123 | { | ||
124 | // Don't bother to update tainted counts | ||
125 | lock (m_TaintLock) | ||
126 | { | ||
127 | if (!m_Tainted) | ||
128 | RemoveObject(obj); | ||
129 | } | ||
130 | } | ||
131 | |||
132 | private void OnParcelPrimCountTainted() | ||
133 | { | ||
134 | lock (m_TaintLock) | ||
135 | m_Tainted = true; | ||
136 | } | ||
137 | |||
138 | public void TaintPrimCount(ILandObject land) | ||
139 | { | ||
140 | lock (m_TaintLock) | ||
141 | m_Tainted = true; | ||
142 | } | ||
143 | |||
144 | public void TaintPrimCount(int x, int y) | ||
145 | { | ||
146 | lock (m_TaintLock) | ||
147 | m_Tainted = true; | ||
148 | } | ||
149 | |||
150 | public void TaintPrimCount() | ||
151 | { | ||
152 | lock (m_TaintLock) | ||
153 | m_Tainted = true; | ||
154 | } | ||
155 | |||
156 | // NOTE: Call under Taint Lock | ||
157 | private void AddObject(SceneObjectGroup obj) | ||
158 | { | ||
159 | if (obj.IsAttachment) | ||
160 | return; | ||
161 | if (((obj.RootPart.Flags & PrimFlags.TemporaryOnRez) != 0)) | ||
162 | return; | ||
163 | |||
164 | Vector3 pos = obj.AbsolutePosition; | ||
165 | ILandObject landObject = m_Scene.LandChannel.GetLandObject(pos.X, pos.Y); | ||
166 | LandData landData = landObject.LandData; | ||
167 | |||
168 | ParcelCounts parcelCounts; | ||
169 | if (m_ParcelCounts.TryGetValue(landData.GlobalID, out parcelCounts)) | ||
170 | { | ||
171 | UUID landOwner = landData.OwnerID; | ||
172 | int partCount = obj.Parts.Length; | ||
173 | |||
174 | m_SimwideCounts[landOwner] += partCount; | ||
175 | if (parcelCounts.Users.ContainsKey(obj.OwnerID)) | ||
176 | parcelCounts.Users[obj.OwnerID] += partCount; | ||
177 | else | ||
178 | parcelCounts.Users[obj.OwnerID] = partCount; | ||
179 | |||
180 | if (landData.IsGroupOwned) | ||
181 | { | ||
182 | if (obj.OwnerID == landData.GroupID) | ||
183 | parcelCounts.Owner += partCount; | ||
184 | else if (obj.GroupID == landData.GroupID) | ||
185 | parcelCounts.Group += partCount; | ||
186 | else | ||
187 | parcelCounts.Others += partCount; | ||
188 | } | ||
189 | else | ||
190 | { | ||
191 | if (obj.OwnerID == landData.OwnerID) | ||
192 | parcelCounts.Owner += partCount; | ||
193 | else if (obj.GroupID == landData.GroupID) | ||
194 | parcelCounts.Group += partCount; | ||
195 | else | ||
196 | parcelCounts.Others += partCount; | ||
197 | } | ||
198 | } | ||
199 | } | ||
200 | |||
201 | // NOTE: Call under Taint Lock | ||
202 | private void RemoveObject(SceneObjectGroup obj) | ||
203 | { | ||
204 | } | ||
205 | |||
206 | public IPrimCounts GetPrimCounts(UUID parcelID) | ||
207 | { | ||
208 | PrimCounts primCounts; | ||
209 | |||
210 | lock (m_PrimCounts) | ||
211 | { | ||
212 | if (m_PrimCounts.TryGetValue(parcelID, out primCounts)) | ||
213 | return primCounts; | ||
214 | |||
215 | primCounts = new PrimCounts(parcelID, this); | ||
216 | m_PrimCounts[parcelID] = primCounts; | ||
217 | } | ||
218 | return primCounts; | ||
219 | } | ||
220 | |||
221 | public int GetOwnerCount(UUID parcelID) | ||
222 | { | ||
223 | lock (m_TaintLock) | ||
224 | { | ||
225 | if (m_Tainted) | ||
226 | Recount(); | ||
227 | |||
228 | ParcelCounts counts; | ||
229 | if (m_ParcelCounts.TryGetValue(parcelID, out counts)) | ||
230 | return counts.Owner; | ||
231 | } | ||
232 | return 0; | ||
233 | } | ||
234 | |||
235 | public int GetGroupCount(UUID parcelID) | ||
236 | { | ||
237 | lock (m_TaintLock) | ||
238 | { | ||
239 | if (m_Tainted) | ||
240 | Recount(); | ||
241 | |||
242 | ParcelCounts counts; | ||
243 | if (m_ParcelCounts.TryGetValue(parcelID, out counts)) | ||
244 | return counts.Group; | ||
245 | } | ||
246 | return 0; | ||
247 | } | ||
248 | |||
249 | public int GetOthersCount(UUID parcelID) | ||
250 | { | ||
251 | lock (m_TaintLock) | ||
252 | { | ||
253 | if (m_Tainted) | ||
254 | Recount(); | ||
255 | |||
256 | ParcelCounts counts; | ||
257 | if (m_ParcelCounts.TryGetValue(parcelID, out counts)) | ||
258 | return counts.Others; | ||
259 | } | ||
260 | return 0; | ||
261 | } | ||
262 | |||
263 | public int GetSimulatorCount(UUID parcelID) | ||
264 | { | ||
265 | lock (m_TaintLock) | ||
266 | { | ||
267 | if (m_Tainted) | ||
268 | Recount(); | ||
269 | |||
270 | UUID owner; | ||
271 | if (m_OwnerMap.TryGetValue(parcelID, out owner)) | ||
272 | { | ||
273 | int val; | ||
274 | if (m_SimwideCounts.TryGetValue(owner, out val)) | ||
275 | return val; | ||
276 | } | ||
277 | } | ||
278 | return 0; | ||
279 | } | ||
280 | |||
281 | public int GetUserCount(UUID parcelID, UUID userID) | ||
282 | { | ||
283 | lock (m_TaintLock) | ||
284 | { | ||
285 | if (m_Tainted) | ||
286 | Recount(); | ||
287 | |||
288 | ParcelCounts counts; | ||
289 | if (m_ParcelCounts.TryGetValue(parcelID, out counts)) | ||
290 | { | ||
291 | int val; | ||
292 | if (counts.Users.TryGetValue(userID, out val)) | ||
293 | return val; | ||
294 | } | ||
295 | } | ||
296 | return 0; | ||
297 | } | ||
298 | |||
299 | // NOTE: This method MUST be called while holding the taint lock! | ||
300 | private void Recount() | ||
301 | { | ||
302 | m_OwnerMap.Clear(); | ||
303 | m_SimwideCounts.Clear(); | ||
304 | m_ParcelCounts.Clear(); | ||
305 | |||
306 | List<ILandObject> land = m_Scene.LandChannel.AllParcels(); | ||
307 | |||
308 | foreach (ILandObject l in land) | ||
309 | { | ||
310 | LandData landData = l.LandData; | ||
311 | |||
312 | m_OwnerMap[landData.GlobalID] = landData.OwnerID; | ||
313 | m_SimwideCounts[landData.OwnerID] = 0; | ||
314 | m_ParcelCounts[landData.GlobalID] = new ParcelCounts(); | ||
315 | } | ||
316 | |||
317 | m_Scene.ForEachSOG(AddObject); | ||
318 | |||
319 | List<UUID> primcountKeys = new List<UUID>(m_PrimCounts.Keys); | ||
320 | foreach (UUID k in primcountKeys) | ||
321 | { | ||
322 | if (!m_OwnerMap.ContainsKey(k)) | ||
323 | m_PrimCounts.Remove(k); | ||
324 | } | ||
325 | m_Tainted = false; | ||
326 | } | ||
327 | } | ||
328 | |||
329 | public class PrimCounts : IPrimCounts | ||
330 | { | ||
331 | private PrimCountModule m_Parent; | ||
332 | private UUID m_ParcelID; | ||
333 | private UserPrimCounts m_UserPrimCounts; | ||
334 | |||
335 | public PrimCounts (UUID parcelID, PrimCountModule parent) | ||
336 | { | ||
337 | m_ParcelID = parcelID; | ||
338 | m_Parent = parent; | ||
339 | |||
340 | m_UserPrimCounts = new UserPrimCounts(this); | ||
341 | } | ||
342 | |||
343 | public int Owner | ||
344 | { | ||
345 | get | ||
346 | { | ||
347 | return m_Parent.GetOwnerCount(m_ParcelID); | ||
348 | } | ||
349 | } | ||
350 | |||
351 | public int Group | ||
352 | { | ||
353 | get | ||
354 | { | ||
355 | return m_Parent.GetGroupCount(m_ParcelID); | ||
356 | } | ||
357 | } | ||
358 | |||
359 | public int Others | ||
360 | { | ||
361 | get | ||
362 | { | ||
363 | return m_Parent.GetOthersCount(m_ParcelID); | ||
364 | } | ||
365 | } | ||
366 | |||
367 | public int Simulator | ||
368 | { | ||
369 | get | ||
370 | { | ||
371 | return m_Parent.GetSimulatorCount(m_ParcelID); | ||
372 | } | ||
373 | } | ||
374 | |||
375 | public IUserPrimCounts Users | ||
376 | { | ||
377 | get | ||
378 | { | ||
379 | return m_UserPrimCounts; | ||
380 | } | ||
381 | } | ||
382 | |||
383 | public int GetUserCount(UUID userID) | ||
384 | { | ||
385 | return m_Parent.GetUserCount(m_ParcelID, userID); | ||
386 | } | ||
387 | } | ||
388 | |||
389 | public class UserPrimCounts : IUserPrimCounts | ||
390 | { | ||
391 | private PrimCounts m_Parent; | ||
392 | |||
393 | public UserPrimCounts(PrimCounts parent) | ||
394 | { | ||
395 | m_Parent = parent; | ||
396 | } | ||
397 | |||
398 | public int this[UUID userID] | ||
399 | { | ||
400 | get | ||
401 | { | ||
402 | return m_Parent.GetUserCount(userID); | ||
403 | } | ||
404 | } | ||
405 | } | ||
406 | } | ||