]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/endpoint/endpointMeter/PropagationMetric.java
8bf71be1e02db7e16dedebd453e0724afb30ffef
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / endpoint / endpointMeter / PropagationMetric.java
1 /*
2  * Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56
57 package net.jxta.impl.endpoint.endpointMeter;
58
59
60 import net.jxta.endpoint.*;
61 import net.jxta.impl.endpoint.*;
62 import net.jxta.util.documentSerializable.*;
63 import net.jxta.document.*;
64
65 import java.util.*;
66
67
68 /**
69  *    Metric corresponding to a propagated messages 
70  *    Meter corresponding to propagated to a ServiceName/ServiceParam pair
71  **/
72 public class PropagationMetric implements DocumentSerializable {
73     private String serviceName;
74     private String serviceParameter;
75
76     private String serviceIdString; // for Hashing
77     int numPropagations;
78     int numPropagatedTo;
79     int numFilteredOut;
80     int numErrorsPropagated;
81     long propagationTime;
82
83     public PropagationMetric() {
84         serviceIdString = serviceName + serviceParameter;               
85     }
86
87     public PropagationMetric(PropagationMeter propagationMeter) {
88         this.serviceName = propagationMeter.getServiceName();
89         this.serviceParameter = propagationMeter.getServiceParameter();
90
91         serviceIdString = serviceName + serviceParameter;               
92     }
93
94     public PropagationMetric(PropagationMetric prototype) {
95         this.serviceName = prototype.getServiceName();
96         this.serviceParameter = prototype.getServiceParameter();
97
98         serviceIdString = serviceName + serviceParameter;               
99     }
100
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;
107     }
108
109     /** The Endpoint address for this outbound message queue **/
110     public String getServiceName() {
111         return serviceName;
112     }
113
114     /** The Endpoint address for this outbound message queue **/
115     public String getServiceParameter() {
116         return serviceParameter;
117     }
118
119     /** The Number of Propagated Messages  **/
120     public int getNumPropagations() {
121         return numPropagations;
122     }
123
124     /** Total number of transports messagess were propagated to  **/
125     public int getNumPropagatedTo() {
126         return numPropagatedTo;
127     }
128
129     /** The Number of Filtered out  Messages  **/
130     
131     /** The Average of number of Transports propagated To from propagation to transport **/
132     public int getAverageNumTransports() {
133         return (numPropagatedTo == 0) ? 0 : (numPropagations / numPropagatedTo);
134     }
135
136     public int getNumFilteredOut() {
137         return numFilteredOut;
138     }
139
140     /** The Number of Errors propagating Messages  **/
141     public int getNumErrorsPropagated() {
142         return numErrorsPropagated;
143     }
144
145     /** The Sum of (clock) times from propagation to transport **/
146     public long getPropagationTime() {
147         return propagationTime;
148     }
149
150     /** The Average of (clock) times from propagation to transport **/
151     public long getAveragePropagationTime() {
152         return (numPropagatedTo == 0) ? 0 : (propagationTime / numPropagatedTo);
153     }
154
155     @Override
156     public boolean equals(Object obj) {
157                 
158         if (obj instanceof PropagationMetric) {
159             PropagationMetric other = (PropagationMetric) obj;
160                         
161             return serviceIdString.equals(other.serviceIdString);
162         } else {
163             return false;
164         }
165     }
166
167     public boolean matches(String serviceName, String serviceParam) {
168         if (serviceName.equals(getServiceName())) {
169             if (serviceParam == null && getServiceParameter() == null) {
170                 return true;
171             } else if (serviceParam != null && getServiceParameter() != null) {
172                 return serviceParam.equals(getServiceParameter());
173             }
174         }
175         return false;
176     }
177         
178     @Override
179     public int hashCode() {
180         return serviceIdString.hashCode();
181     }
182
183     String getServiceIdString() {
184         return serviceIdString;
185     }   
186
187     public void mergeMetrics(PropagationMetric other) {
188         numPropagatedTo += other.numPropagatedTo;
189         numFilteredOut += other.numFilteredOut;
190         numErrorsPropagated += other.numErrorsPropagated;
191         propagationTime += other.propagationTime;
192     }   
193
194     public void serializeTo(Element element) throws DocumentSerializationException {
195
196         DocumentSerializableUtilities.addString(element, "serviceName", serviceName);
197         DocumentSerializableUtilities.addString(element, "serviceParam", serviceParameter);
198
199         if (numPropagations != 0) {
200             DocumentSerializableUtilities.addInt(element, "numPropagations", numPropagatedTo);
201         }
202                 
203         if (numPropagatedTo != 0) {
204             DocumentSerializableUtilities.addInt(element, "numPropagatedTo", numPropagatedTo);
205         }
206                 
207         if (numFilteredOut != 0) {
208             DocumentSerializableUtilities.addInt(element, "numFilteredOut", numFilteredOut);
209         }
210                 
211         if (numErrorsPropagated != 0) {
212             DocumentSerializableUtilities.addInt(element, "numErrorsPropagated", numErrorsPropagated);
213         }
214                 
215         if (propagationTime != 0) {
216             DocumentSerializableUtilities.addLong(element, "propagationTime", propagationTime);
217         }
218     }
219
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();
224                         
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);
239             }
240         }
241
242         serviceIdString = serviceName + serviceParameter;               
243     }
244 }