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 message queue to for outbound messengers based upon an endpoint address
71 public class OutboundMetric implements DocumentSerializable {
72 private EndpointAddress endpointAddress;
74 private int numOutboundQueued;
75 private int numOutboundDropped;
76 private long timeToDropOutbound;
77 private int numOutboundDeQueued;
78 private long timeInOutboundQueue;
79 private int numOutboundProcessed;
80 private long timeToProcessOutbound;
81 private int numOutboundFailed;
82 private long timeOutboundToFail;
84 public OutboundMetric(OutboundMeter outboundMeter) {
85 this.endpointAddress = outboundMeter.getEndpointAddress();
88 public OutboundMetric(OutboundMetric prototype) {
89 this.endpointAddress = prototype.getEndpointAddress();
92 public OutboundMetric() {}
94 /** The Endpoint address for this outbound message queue **/
95 public EndpointAddress getEndpointAddress() {
96 return endpointAddress;
99 /** The Number of Outbound Messages Queued **/
100 public int getNumOutboundQueued() {
101 return numOutboundQueued;
104 /** The Number of Outbound Messages Dropped from Queue **/
105 public int getNumOutboundDropped() {
106 return numOutboundDropped;
109 /** The Sum of the times in queue for all dropped messages **/
110 public long getTimeToDropOutbound() {
111 return timeToDropOutbound;
114 /** The Number of Outbound Messages DeQueued **/
115 public int getNumOutboundDeQueued() {
116 return numOutboundDeQueued;
119 /** The Sum of the times in queue for all messages **/
120 public long getTimeInOutboundQueue() {
121 return timeInOutboundQueue;
124 /** The Number of Outbound Messages Processed Successfully **/
125 public int getNumOutboundProcessed() {
126 return numOutboundProcessed;
129 /** The Sum of the times from sending to handling by messenger **/
130 public long getTimeToProcessOutbound() {
131 return timeToProcessOutbound;
134 /** The Number of Outbound Messages Failed in sending **/
135 public int getNumOutboundFailed() {
136 return numOutboundFailed;
139 /** The Sum of the times in queue for all failed messages **/
140 public long getTimeOutboundToFail() {
141 return timeOutboundToFail;
144 /** The Average of the times in queue for all messages **/
145 public long getAverageTimeInOutboundQueue() {
146 return (numOutboundDeQueued == 0) ? 0 : (timeInOutboundQueue / numOutboundDeQueued);
149 /** The Average of the times in queue for all dropped messages **/
150 public long getAverageOutboundDropTime() {
151 return (numOutboundDropped == 0) ? 0 : (timeToDropOutbound / numOutboundDropped);
154 /** The Average of the times from sending to handling by messenger **/
155 public long getAverageOutboundProcessTime() {
156 return (numOutboundProcessed == 0) ? 0 : (timeToProcessOutbound / numOutboundProcessed);
160 public boolean equals(Object obj) {
162 if (obj instanceof OutboundMetric) {
163 OutboundMetric other = (OutboundMetric) obj;
165 return endpointAddress.equals(other.endpointAddress);
171 public boolean matches(EndpointAddress otherAddress) {
172 return getEndpointAddress().equals(otherAddress);
176 public int hashCode() {
177 return endpointAddress.hashCode();
180 void outboundMessageQueued(Message message) {
184 void outboundMessageDropped(Message message, long time) {
185 numOutboundDropped++;
186 timeToDropOutbound += time;
189 void outboundMessageFailed(Message message, long time) {
191 timeOutboundToFail += time;
194 void outboundMessageDeQueued(Message message, long time) {
195 numOutboundDeQueued++;
196 timeInOutboundQueue += time;
199 void outboundMessageProcessed(Message message, long time) {
200 numOutboundProcessed++;
201 timeToProcessOutbound += time;
204 public void mergeMetrics(OutboundMetric other) {
206 numOutboundQueued += other.numOutboundQueued;
207 numOutboundDropped += other.numOutboundDropped;
208 timeToDropOutbound += other.timeToDropOutbound;
209 numOutboundDeQueued += other.numOutboundDeQueued;
210 timeInOutboundQueue += other.timeInOutboundQueue;
211 numOutboundProcessed += other.numOutboundProcessed;
212 timeToProcessOutbound += other.timeToProcessOutbound;
213 numOutboundFailed += other.numOutboundFailed;
214 timeOutboundToFail += other.timeOutboundToFail;
217 public void serializeTo(Element element) throws DocumentSerializationException {
219 DocumentSerializableUtilities.addString(element, "endpointAddress", endpointAddress.toString());
221 if (numOutboundQueued != 0) {
222 DocumentSerializableUtilities.addInt(element, "numOutboundQueued", numOutboundQueued);
225 if (numOutboundDropped != 0) {
226 DocumentSerializableUtilities.addInt(element, "numOutboundDropped", numOutboundDropped);
229 if (timeToDropOutbound != 0) {
230 DocumentSerializableUtilities.addLong(element, "timeToDropOutbound", timeToDropOutbound);
233 if (numOutboundDeQueued != 0) {
234 DocumentSerializableUtilities.addInt(element, "numOutboundDeQueued", numOutboundDeQueued);
237 if (timeInOutboundQueue != 0) {
238 DocumentSerializableUtilities.addLong(element, "timeInOutboundQueue", timeInOutboundQueue);
241 if (numOutboundProcessed != 0) {
242 DocumentSerializableUtilities.addInt(element, "numOutboundProcessed", numOutboundProcessed);
245 if (timeToProcessOutbound != 0) {
246 DocumentSerializableUtilities.addLong(element, "timeToProcessOutbound", timeToProcessOutbound);
249 if (numOutboundFailed != 0) {
250 DocumentSerializableUtilities.addInt(element, "numOutboundFailed", numOutboundFailed);
253 if (timeOutboundToFail != 0) {
254 DocumentSerializableUtilities.addLong(element, "timeOutboundToFail", timeOutboundToFail);
259 public void initializeFrom(Element element) throws DocumentSerializationException {
260 for (Enumeration e = element.getChildren(); e.hasMoreElements();) {
261 Element childElement = (TextElement) e.nextElement();
262 String tagName = (String) childElement.getKey();
264 if (tagName.equals("endpointAddress")) {
265 String endpointAddressString = DocumentSerializableUtilities.getString(childElement);
267 endpointAddress = new EndpointAddress(endpointAddressString);
268 } else if (tagName.equals("numOutboundQueued")) {
269 numOutboundQueued = DocumentSerializableUtilities.getInt(childElement);
270 } else if (tagName.equals("numOutboundDropped")) {
271 numOutboundDropped = DocumentSerializableUtilities.getInt(childElement);
272 } else if (tagName.equals("timeToDropOutbound")) {
273 timeToDropOutbound = DocumentSerializableUtilities.getLong(childElement);
274 } else if (tagName.equals("numOutboundDeQueued")) {
275 numOutboundDeQueued = DocumentSerializableUtilities.getInt(childElement);
276 } else if (tagName.equals("timeInOutboundQueue")) {
277 timeInOutboundQueue = DocumentSerializableUtilities.getLong(childElement);
278 } else if (tagName.equals("numOutboundProcessed")) {
279 numOutboundProcessed = DocumentSerializableUtilities.getInt(childElement);
280 } else if (tagName.equals("timeToProcessOutbound")) {
281 timeToProcessOutbound = DocumentSerializableUtilities.getLong(childElement);
282 } else if (tagName.equals("numOutboundFailed")) {
283 numOutboundFailed = DocumentSerializableUtilities.getInt(childElement);
284 } else if (tagName.equals("timeOutboundToFail")) {
285 timeOutboundToFail = DocumentSerializableUtilities.getLong(childElement);