Source Code Cross Referenced for STATS.java in  » Net » JGroups-2.4.1-sp3 » org » jgroups » protocols » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » JGroups 2.4.1 sp3 » org.jgroups.protocols 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jgroups.protocols;
002:
003:        import org.jgroups.stack.Protocol;
004:        import org.jgroups.Event;
005:        import org.jgroups.Message;
006:        import org.jgroups.Address;
007:        import org.jgroups.View;
008:
009:        import java.util.*;
010:
011:        /**
012:         * Provides various stats
013:         * @author Bela Ban
014:         * @version $Id: STATS.java,v 1.3 2006/01/14 14:00:38 belaban Exp $
015:         */
016:        public class STATS extends Protocol {
017:            long sent_msgs, sent_bytes, sent_ucasts, sent_mcasts,
018:                    received_ucasts, received_mcasts;
019:            long received_msgs, received_bytes, sent_ucast_bytes,
020:                    sent_mcast_bytes, received_ucast_bytes,
021:                    received_mcast_bytes;
022:
023:            /** HashMap<Address,Entry>, maintains stats per target destination */
024:            HashMap sent = new HashMap();
025:
026:            /** HashMap<Address,Entry>, maintains stats per receiver */
027:            HashMap received = new HashMap();
028:
029:            static final short UP = 1;
030:            static final short DOWN = 2;
031:
032:            public String getName() {
033:                return "STATS";
034:            }
035:
036:            public boolean setProperties(Properties props) {
037:                super .setProperties(props);
038:                down_thread = false; // never use a down thread
039:                up_thread = false; // never use an up thread
040:
041:                if (props.size() > 0) {
042:                    log.error("the following properties are not recognized: "
043:                            + props);
044:                    return false;
045:                }
046:                return true;
047:            }
048:
049:            public void resetStats() {
050:                sent_msgs = sent_bytes = sent_ucasts = sent_mcasts = received_ucasts = received_mcasts = 0;
051:                received_msgs = received_bytes = sent_ucast_bytes = sent_mcast_bytes = received_ucast_bytes = received_mcast_bytes = 0;
052:                sent.clear();
053:                received.clear();
054:            }
055:
056:            public long getSentMessages() {
057:                return sent_msgs;
058:            }
059:
060:            public long getSentBytes() {
061:                return sent_bytes;
062:            }
063:
064:            public long getSentUnicastMessages() {
065:                return sent_ucasts;
066:            }
067:
068:            public long getSentUnicastBytes() {
069:                return sent_ucast_bytes;
070:            }
071:
072:            public long getSentMcastMessages() {
073:                return sent_mcasts;
074:            }
075:
076:            public long getSentMcastBytes() {
077:                return sent_mcast_bytes;
078:            }
079:
080:            public long getReceivedMessages() {
081:                return received_msgs;
082:            }
083:
084:            public long getReceivedBytes() {
085:                return received_bytes;
086:            }
087:
088:            public long getReceivedUnicastMessages() {
089:                return received_ucasts;
090:            }
091:
092:            public long getReceivedUnicastBytes() {
093:                return received_ucast_bytes;
094:            }
095:
096:            public long getReceivedMcastMessages() {
097:                return received_mcasts;
098:            }
099:
100:            public long getReceivedMcastBytes() {
101:                return received_mcast_bytes;
102:            }
103:
104:            public void up(Event evt) {
105:                if (evt.getType() == Event.MSG) {
106:                    Message msg = (Message) evt.getArg();
107:                    updateStats(msg, UP);
108:                } else if (evt.getType() == Event.VIEW_CHANGE) {
109:                    handleViewChange((View) evt.getArg());
110:                }
111:                passUp(evt);
112:            }
113:
114:            public void down(Event evt) {
115:                if (evt.getType() == Event.MSG) {
116:                    Message msg = (Message) evt.getArg();
117:                    updateStats(msg, DOWN);
118:                } else if (evt.getType() == Event.VIEW_CHANGE) {
119:                    handleViewChange((View) evt.getArg());
120:                }
121:                passDown(evt);
122:            }
123:
124:            public String printStats() {
125:                Map.Entry entry;
126:                Object key, val;
127:                StringBuffer sb = new StringBuffer();
128:                sb.append("sent:\n");
129:                for (Iterator it = sent.entrySet().iterator(); it.hasNext();) {
130:                    entry = (Map.Entry) it.next();
131:                    key = entry.getKey();
132:                    if (key == null)
133:                        key = "<mcast dest>";
134:                    val = entry.getValue();
135:                    sb.append(key).append(": ").append(val).append("\n");
136:                }
137:                sb.append("\nreceived:\n");
138:                for (Iterator it = received.entrySet().iterator(); it.hasNext();) {
139:                    entry = (Map.Entry) it.next();
140:                    key = entry.getKey();
141:                    val = entry.getValue();
142:                    sb.append(key).append(": ").append(val).append("\n");
143:                }
144:
145:                return sb.toString();
146:            }
147:
148:            private void handleViewChange(View view) {
149:                Vector members = view.getMembers();
150:                Set tmp = new LinkedHashSet(members);
151:                tmp.add(null); // for null destination (= mcast)
152:                sent.keySet().retainAll(tmp);
153:                received.keySet().retainAll(tmp);
154:            }
155:
156:            private void updateStats(Message msg, short direction) {
157:                int length;
158:                HashMap map;
159:                boolean mcast;
160:                Address dest, src;
161:
162:                if (msg == null)
163:                    return;
164:                length = msg.getLength();
165:                dest = msg.getDest();
166:                src = msg.getSrc();
167:                mcast = dest == null || dest.isMulticastAddress();
168:
169:                if (direction == UP) { // received
170:                    received_msgs++;
171:                    received_bytes += length;
172:                    if (mcast) {
173:                        received_mcasts++;
174:                        received_mcast_bytes += length;
175:                    } else {
176:                        received_ucasts++;
177:                        received_ucast_bytes += length;
178:                    }
179:                } else { // sent
180:                    sent_msgs++;
181:                    sent_bytes += length;
182:                    if (mcast) {
183:                        sent_mcasts++;
184:                        sent_mcast_bytes += length;
185:                    } else {
186:                        sent_ucasts++;
187:                        sent_ucast_bytes += length;
188:                    }
189:                }
190:
191:                Address key = direction == UP ? src : dest;
192:                map = direction == UP ? received : sent;
193:                Entry entry = (Entry) map.get(key);
194:                if (entry == null) {
195:                    entry = new Entry();
196:                    map.put(key, entry);
197:                }
198:                entry.msgs++;
199:                entry.bytes += length;
200:                if (mcast) {
201:                    entry.mcasts++;
202:                    entry.mcast_bytes += length;
203:                } else {
204:                    entry.ucasts++;
205:                    entry.ucast_bytes += length;
206:                }
207:            }
208:
209:            static class Entry {
210:                long msgs, bytes, ucasts, mcasts, ucast_bytes, mcast_bytes;
211:
212:                public String toString() {
213:                    StringBuffer sb = new StringBuffer();
214:                    sb.append(msgs).append(" (").append(bytes)
215:                            .append(" bytes)");
216:                    sb.append(": ").append(ucasts).append(" ucasts (").append(
217:                            ucast_bytes).append(" bytes), ");
218:                    sb.append(mcasts).append(" mcasts (").append(mcast_bytes)
219:                            .append(" bytes)");
220:                    return sb.toString();
221:                }
222:            }
223:
224:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.