]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/endpoint/endpointMeter/OutboundMetric.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / endpoint / endpointMeter / OutboundMetric.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 message queue to for outbound messengers based upon an endpoint address
70  **/
71 public class OutboundMetric implements DocumentSerializable {
72     private EndpointAddress endpointAddress;
73
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;
83
84     public OutboundMetric(OutboundMeter outboundMeter) {
85         this.endpointAddress = outboundMeter.getEndpointAddress();
86     }
87
88     public OutboundMetric(OutboundMetric prototype) {
89         this.endpointAddress = prototype.getEndpointAddress();  
90     }
91
92     public OutboundMetric() {} 
93
94     /** The Endpoint address for this outbound message queue **/
95     public EndpointAddress getEndpointAddress() {
96         return endpointAddress;
97     }
98         
99     /** The Number of Outbound Messages Queued **/
100     public int getNumOutboundQueued() {
101         return numOutboundQueued;
102     }
103
104     /** The Number of Outbound Messages Dropped from Queue **/
105     public int getNumOutboundDropped() {
106         return numOutboundDropped;
107     }
108
109     /** The Sum of the times in queue for all dropped messages  **/
110     public long getTimeToDropOutbound() {
111         return timeToDropOutbound;
112     }
113
114     /** The Number of Outbound Messages DeQueued **/
115     public int getNumOutboundDeQueued() {
116         return numOutboundDeQueued;
117     }
118
119     /** The Sum of the times in queue for all messages  **/
120     public long getTimeInOutboundQueue() {
121         return timeInOutboundQueue;
122     }
123
124     /** The Number of Outbound Messages Processed Successfully **/
125     public int getNumOutboundProcessed() {
126         return numOutboundProcessed;
127     }
128
129     /** The Sum of the times from sending to handling by messenger **/
130     public long getTimeToProcessOutbound() {
131         return timeToProcessOutbound;
132     }
133
134     /** The Number of Outbound Messages Failed in sending **/
135     public int getNumOutboundFailed() {
136         return numOutboundFailed;
137     }
138
139     /** The Sum of the times in queue for all failed messages  **/
140     public long getTimeOutboundToFail() {
141         return timeOutboundToFail;
142     }
143
144     /** The Average of the times in queue for all messages  **/
145     public long getAverageTimeInOutboundQueue() {
146         return  (numOutboundDeQueued == 0) ? 0 : (timeInOutboundQueue / numOutboundDeQueued);
147     }
148
149     /** The Average of the times in queue for all dropped messages  **/
150     public long getAverageOutboundDropTime() {
151         return  (numOutboundDropped == 0) ? 0 : (timeToDropOutbound / numOutboundDropped);
152     }
153
154     /** The Average of the times from sending to handling by messenger **/
155     public long getAverageOutboundProcessTime() {
156         return  (numOutboundProcessed == 0) ? 0 : (timeToProcessOutbound / numOutboundProcessed);
157     }
158         
159     @Override
160     public boolean equals(Object obj) {
161                 
162         if (obj instanceof OutboundMetric) {
163             OutboundMetric other = (OutboundMetric) obj;
164                         
165             return endpointAddress.equals(other.endpointAddress);
166         } else {
167             return false;
168         }
169     }
170
171     public boolean matches(EndpointAddress otherAddress) {
172         return getEndpointAddress().equals(otherAddress);
173     }
174
175     @Override
176     public int hashCode() {
177         return endpointAddress.hashCode();
178     }    
179
180     void outboundMessageQueued(Message message) {
181         numOutboundQueued++;
182     }
183
184     void outboundMessageDropped(Message message, long time) {
185         numOutboundDropped++;
186         timeToDropOutbound += time;
187     }
188
189     void outboundMessageFailed(Message message, long time) {
190         numOutboundFailed++;
191         timeOutboundToFail += time;
192     }
193
194     void outboundMessageDeQueued(Message message, long time) {
195         numOutboundDeQueued++;
196         timeInOutboundQueue += time;
197     }
198
199     void outboundMessageProcessed(Message message, long time) { 
200         numOutboundProcessed++;
201         timeToProcessOutbound += time;
202     }
203
204     public void mergeMetrics(OutboundMetric other) {
205         
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;
215     }   
216
217     public void serializeTo(Element element) throws DocumentSerializationException {
218
219         DocumentSerializableUtilities.addString(element, "endpointAddress", endpointAddress.toString());
220
221         if (numOutboundQueued != 0) {
222             DocumentSerializableUtilities.addInt(element, "numOutboundQueued", numOutboundQueued);
223         }
224                                 
225         if (numOutboundDropped != 0) {
226             DocumentSerializableUtilities.addInt(element, "numOutboundDropped", numOutboundDropped);
227         }
228
229         if (timeToDropOutbound != 0) {
230             DocumentSerializableUtilities.addLong(element, "timeToDropOutbound", timeToDropOutbound);
231         }
232                 
233         if (numOutboundDeQueued != 0) {
234             DocumentSerializableUtilities.addInt(element, "numOutboundDeQueued", numOutboundDeQueued);
235         }
236                 
237         if (timeInOutboundQueue != 0) {
238             DocumentSerializableUtilities.addLong(element, "timeInOutboundQueue", timeInOutboundQueue);
239         }
240                 
241         if (numOutboundProcessed != 0) {
242             DocumentSerializableUtilities.addInt(element, "numOutboundProcessed", numOutboundProcessed);
243         }
244                                 
245         if (timeToProcessOutbound != 0) {
246             DocumentSerializableUtilities.addLong(element, "timeToProcessOutbound", timeToProcessOutbound);
247         }
248                 
249         if (numOutboundFailed != 0) {
250             DocumentSerializableUtilities.addInt(element, "numOutboundFailed", numOutboundFailed);
251         }
252
253         if (timeOutboundToFail != 0) {
254             DocumentSerializableUtilities.addLong(element, "timeOutboundToFail", timeOutboundToFail);
255         }
256                                 
257     }
258
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();
263                         
264             if (tagName.equals("endpointAddress")) {
265                 String endpointAddressString = DocumentSerializableUtilities.getString(childElement);   
266
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);
286             }
287         }
288     }
289 }