2 * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment: "This product includes software
18 * developed by Sun Microsystems, Inc. for JXTA(TM) technology."
19 * Alternately, this acknowledgment may appear in the software itself, if
20 * and wherever such third-party acknowledgments normally appear.
22 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
23 * not be used to endorse or promote products derived from this software
24 * without prior written permission. For written permission, please contact
25 * Project JXTA at http://www.jxta.org.
27 * 5. Products derived from this software may not be called "JXTA", nor may
28 * "JXTA" appear in their name, without prior written permission of Sun.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN
33 * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
44 * Please see the license information page at :
45 * <http://www.jxta.org/project/www/license.html> for instructions on use of
46 * the license in source files.
48 * ====================================================================
50 * This software consists of voluntary contributions made by many individuals
51 * on behalf of Project JXTA. For more information on Project JXTA, please see
52 * http://www.jxta.org.
54 * This license is based on the BSD license adopted by the Apache Foundation.
57 package net.jxta.impl.endpoint.endpointMeter;
60 import net.jxta.endpoint.*;
61 import net.jxta.impl.endpoint.*;
62 import net.jxta.util.documentSerializable.*;
63 import net.jxta.document.*;
69 * Metric corresponding to a propagated messages
70 * Meter corresponding to propagated to a ServiceName/ServiceParam pair
72 public class PropagationMetric implements DocumentSerializable {
73 private String serviceName;
74 private String serviceParameter;
76 private String serviceIdString; // for Hashing
80 int numErrorsPropagated;
83 public PropagationMetric() {
84 serviceIdString = serviceName + serviceParameter;
87 public PropagationMetric(PropagationMeter propagationMeter) {
88 this.serviceName = propagationMeter.getServiceName();
89 this.serviceParameter = propagationMeter.getServiceParameter();
91 serviceIdString = serviceName + serviceParameter;
94 public PropagationMetric(PropagationMetric prototype) {
95 this.serviceName = prototype.getServiceName();
96 this.serviceParameter = prototype.getServiceParameter();
98 serviceIdString = serviceName + serviceParameter;
101 void registerPropagateMessageStats(int numPropagatedTo, int numFilteredOut, int numErrorsPropagated, long propagationTime) {
102 this.numPropagations++;
103 this.numPropagatedTo += numPropagatedTo;
104 this.numFilteredOut += numFilteredOut;
105 this.numErrorsPropagated += numErrorsPropagated;
106 this.propagationTime += propagationTime;
109 /** The Endpoint address for this outbound message queue **/
110 public String getServiceName() {
114 /** The Endpoint address for this outbound message queue **/
115 public String getServiceParameter() {
116 return serviceParameter;
119 /** The Number of Propagated Messages **/
120 public int getNumPropagations() {
121 return numPropagations;
124 /** Total number of transports messagess were propagated to **/
125 public int getNumPropagatedTo() {
126 return numPropagatedTo;
129 /** The Number of Filtered out Messages **/
131 /** The Average of number of Transports propagated To from propagation to transport **/
132 public int getAverageNumTransports() {
133 return (numPropagatedTo == 0) ? 0 : (numPropagations / numPropagatedTo);
136 public int getNumFilteredOut() {
137 return numFilteredOut;
140 /** The Number of Errors propagating Messages **/
141 public int getNumErrorsPropagated() {
142 return numErrorsPropagated;
145 /** The Sum of (clock) times from propagation to transport **/
146 public long getPropagationTime() {
147 return propagationTime;
150 /** The Average of (clock) times from propagation to transport **/
151 public long getAveragePropagationTime() {
152 return (numPropagatedTo == 0) ? 0 : (propagationTime / numPropagatedTo);
156 public boolean equals(Object obj) {
158 if (obj instanceof PropagationMetric) {
159 PropagationMetric other = (PropagationMetric) obj;
161 return serviceIdString.equals(other.serviceIdString);
167 public boolean matches(String serviceName, String serviceParam) {
168 if (serviceName.equals(getServiceName())) {
169 if (serviceParam == null && getServiceParameter() == null) {
171 } else if (serviceParam != null && getServiceParameter() != null) {
172 return serviceParam.equals(getServiceParameter());
179 public int hashCode() {
180 return serviceIdString.hashCode();
183 String getServiceIdString() {
184 return serviceIdString;
187 public void mergeMetrics(PropagationMetric other) {
188 numPropagatedTo += other.numPropagatedTo;
189 numFilteredOut += other.numFilteredOut;
190 numErrorsPropagated += other.numErrorsPropagated;
191 propagationTime += other.propagationTime;
194 public void serializeTo(Element element) throws DocumentSerializationException {
196 DocumentSerializableUtilities.addString(element, "serviceName", serviceName);
197 DocumentSerializableUtilities.addString(element, "serviceParam", serviceParameter);
199 if (numPropagations != 0) {
200 DocumentSerializableUtilities.addInt(element, "numPropagations", numPropagatedTo);
203 if (numPropagatedTo != 0) {
204 DocumentSerializableUtilities.addInt(element, "numPropagatedTo", numPropagatedTo);
207 if (numFilteredOut != 0) {
208 DocumentSerializableUtilities.addInt(element, "numFilteredOut", numFilteredOut);
211 if (numErrorsPropagated != 0) {
212 DocumentSerializableUtilities.addInt(element, "numErrorsPropagated", numErrorsPropagated);
215 if (propagationTime != 0) {
216 DocumentSerializableUtilities.addLong(element, "propagationTime", propagationTime);
220 public void initializeFrom(Element element) throws DocumentSerializationException {
221 for (Enumeration e = element.getChildren(); e.hasMoreElements();) {
222 Element childElement = (TextElement) e.nextElement();
223 String tagName = (String) childElement.getKey();
225 if (tagName.equals("serviceName")) {
226 serviceName = DocumentSerializableUtilities.getString(childElement);
227 } else if (tagName.equals("serviceParam")) {
228 serviceParameter = DocumentSerializableUtilities.getString(childElement);
229 } else if (tagName.equals("numPropagations")) {
230 numPropagations = DocumentSerializableUtilities.getInt(childElement);
231 } else if (tagName.equals("numPropagatedTo")) {
232 numPropagatedTo = DocumentSerializableUtilities.getInt(childElement);
233 } else if (tagName.equals("numFilteredOut")) {
234 numFilteredOut = DocumentSerializableUtilities.getInt(childElement);
235 } else if (tagName.equals("propagationTime")) {
236 propagationTime = DocumentSerializableUtilities.getLong(childElement);
237 } else if (tagName.equals("numErrorsPropagated")) {
238 numErrorsPropagated = DocumentSerializableUtilities.getInt(childElement);
242 serviceIdString = serviceName + serviceParameter;