corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
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 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27  
28 using System;
29 using System.Text;
30 using System.Threading;
31 using System.Collections.Generic;
32 using OpenMetaverse.Packets;
33  
34 namespace OpenMetaverse.Stats
35 {
36 public enum Type
37 {
38 Packet,
39 Message
40 }
41 public class UtilizationStatistics
42 {
43  
44 public class Stat
45 {
46 public Type Type;
47 public long TxCount;
48 public long RxCount;
49 public long TxBytes;
50 public long RxBytes;
51  
52 public Stat(Type type, long txCount, long rxCount, long txBytes, long rxBytes)
53 {
54 this.Type = type;
55 this.TxCount = txCount;
56 this.RxCount = rxCount;
57 this.TxBytes = txBytes;
58 this.RxBytes = rxBytes;
59 }
60 }
61  
62 private Dictionary<string, Stat> m_StatsCollection;
63  
64 public UtilizationStatistics()
65 {
66 m_StatsCollection = new Dictionary<string, Stat>();
67 }
68  
69 internal void Update(string key, Type Type, long txBytes, long rxBytes)
70 {
71 lock (m_StatsCollection)
72 {
73 if(m_StatsCollection.ContainsKey(key))
74 {
75 Stat stat = m_StatsCollection[key];
76 if (rxBytes > 0)
77 {
78 Interlocked.Increment(ref stat.RxCount);
79 Interlocked.Add(ref stat.RxBytes, rxBytes);
80 }
81  
82 if (txBytes > 0)
83 {
84 Interlocked.Increment(ref stat.TxCount);
85 Interlocked.Add(ref stat.TxBytes, txBytes);
86 }
87  
88 } else {
89 Stat stat;
90 if (txBytes > 0)
91 stat = new Stat(Type, 1, 0, txBytes, 0);
92 else
93 stat = new Stat(Type, 0, 1, 0, rxBytes);
94  
95 m_StatsCollection.Add(key, stat);
96 }
97 }
98 }
99  
100 public Dictionary<string, Stat> GetStatistics()
101 {
102 lock(m_StatsCollection)
103 {
104 return new Dictionary<string, Stat>(m_StatsCollection);
105 }
106 }
107 }
108 }