]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/endpoint/transportMeter/TransportBindingMetric.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / endpoint / transportMeter / TransportBindingMetric.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.transportMeter;
58
59
60 import net.jxta.document.Element;
61 import net.jxta.document.TextElement;
62 import net.jxta.endpoint.EndpointAddress;
63 import net.jxta.endpoint.Message;
64 import net.jxta.impl.meter.MetricUtilities;
65 import net.jxta.peer.PeerID;
66 import net.jxta.util.documentSerializable.DocumentSerializable;
67 import net.jxta.util.documentSerializable.DocumentSerializableUtilities;
68 import net.jxta.util.documentSerializable.DocumentSerializationException;
69
70 import java.util.Enumeration;
71
72
73 public class TransportBindingMetric implements DocumentSerializable {
74     public static final String CONNECTED = "connected";
75     public static final String CLOSED = "closed";
76     public static final String DROPPED = "dropped";
77     public static final String FAILED = "failed";
78
79     private PeerID peerID;
80     private EndpointAddress endpointAddress;
81
82     private String initiatorState = null;
83     private String acceptorState = null;
84     private long initiatorTransitionTime;
85     private long acceptorTransitionTime;
86
87     private int acceptorBytesReceived;
88     private int acceptorBytesSent;
89     private int acceptorConnections;
90     private int acceptorConnectionsClosed;
91     private int acceptorConnectionsDropped;
92     private int acceptorConnectionsFailed;
93     private int acceptorMessagesReceived;
94     private int acceptorMessagesSent;
95     private long acceptorReceiveFailureProcessingTime;
96     private int acceptorReceiveFailures;
97     private long acceptorReceiveProcessingTime;
98     private long acceptorSendFailureProcessingTime;
99     private int acceptorSendFailures;
100     private long acceptorSendProcessingTime;
101     private long acceptorTotalTimeConnected;
102     private long acceptorTimeToConnect;
103     private long acceptorTimeToFail;
104     private int initiatorBytesReceived;
105     private int initiatorBytesSent;
106     private long initiatorTotalTimeConnected;
107     private int initiatorConnections;
108     private int initiatorConnectionsClosed;
109     private int initiatorConnectionsDropped;
110     private int initiatorConnectionsFailed;
111     private int initiatorMessagesReceived;
112     private int initiatorMessagesSent;
113     private long initiatorReceiveFailureProcessingTime;
114     private int initiatorReceiveFailures;
115     private long initiatorReceiveProcessingTime;
116     private long initiatorSendFailureProcessingTime;
117     private int initiatorSendFailures;
118     private long initiatorSendProcessingTime;
119     private long initiatorTimeToConnect;
120     private long initiatorTimeToFail;
121
122     private int numPings;
123     private int numFailedPings;
124     private long pingTime;
125     private long pingFailedTime;
126     private int numPingsReceived;
127
128     public TransportBindingMetric(TransportBindingMeter transportBindingMeter, boolean initiatorConnected, boolean acceptorConnected) {
129         this.peerID = transportBindingMeter.getPeerID();
130         this.endpointAddress = transportBindingMeter.getEndpointAddress();
131         this.initiatorState = initiatorConnected ? CONNECTED : CLOSED;
132         this.acceptorState = acceptorConnected ? CONNECTED : CLOSED;
133     }
134
135     public TransportBindingMetric() {}
136
137     public TransportBindingMetric(TransportBindingMetric prototype) {
138         this.peerID = prototype.peerID;
139         this.endpointAddress = prototype.endpointAddress;
140         this.initiatorState = prototype.initiatorState;
141         this.acceptorState = prototype.acceptorState;
142         this.initiatorTransitionTime = prototype.initiatorTransitionTime;
143         this.acceptorTransitionTime = prototype.acceptorTransitionTime;
144     }
145
146     @Override
147     public boolean equals(Object obj) {
148         if (obj instanceof TransportBindingMetric) {
149             TransportBindingMetric other = (TransportBindingMetric) obj;
150
151             return endpointAddress.equals(other.endpointAddress);
152         } else {
153             return false;
154         }
155     }
156
157     @Override
158     public int hashCode() {
159         return peerID.hashCode() + endpointAddress.hashCode();
160     }
161
162     public PeerID getPeerID() {
163         return peerID;
164     }
165
166     public void setPeerID(PeerID peerID) {
167         this.peerID = peerID;
168     }
169
170     public EndpointAddress getEndpointAddress() {
171         return endpointAddress;
172     }
173
174     /**
175      * State of this Initiator Binding
176      *
177      * @return TransportBindingMetric.CONNECTED, TransportBindingMetric.DISCONNECTED or TransportBindingMetric.FAILED
178      */
179     public String getInitiatorState() {
180         return initiatorState;
181     }
182
183     /**
184      * State of this Acceptor Binding
185      *
186      * @return TransportBindingMetric.CONNECTED, TransportBindingMetric.DISCONNECTED or TransportBindingMetric.FAILED
187      */
188     public String getAcceptorState() {
189         return acceptorState;
190     }
191
192     /**
193      * Get the time that it entered the current state
194      *
195      * @return transition time in ms since January 1, 1970, 00:00:00 GMT
196      */
197     public long getInitiatorTransitionTime() {
198         return initiatorTransitionTime;
199     }
200
201     /**
202      * Get the time that it entered the current state
203      *
204      * @return transition time in ms since January 1, 1970, 00:00:00 GMT
205      */
206     public long getAcceptorTransitionTime() {
207         return acceptorTransitionTime;
208     }
209
210     public boolean isAcceptorConnected() {
211         return (acceptorState != null) && acceptorState.equals(CONNECTED);
212     }
213
214     public boolean isInitiatorConnected() {
215         return (initiatorState != null) && initiatorState.equals(CONNECTED);
216     }
217
218     public long getTimeAcceptorConnectionEstablished() {
219         return isAcceptorConnected() ? acceptorTransitionTime : 0;
220     }
221
222     public long getTimeInitiatorConnectionEstablished() {
223         return isInitiatorConnected() ? initiatorTransitionTime : 0;
224     }
225
226     public int getAcceptorBytesReceived() {
227         return acceptorBytesReceived;
228     }
229
230     public int getAcceptorBytesSent() {
231         return acceptorBytesSent;
232     }
233
234     public int getAcceptorConnections() {
235         return acceptorConnections;
236     }
237
238     public int getAcceptorConnectionsClosed() {
239         return acceptorConnectionsClosed;
240     }
241
242     public int getAcceptorConnectionsDropped() {
243         return acceptorConnectionsDropped;
244     }
245
246     public int getAcceptorConnectionsFailed() {
247         return acceptorConnectionsFailed;
248     }
249
250     public int getAcceptorMessagesReceived() {
251         return acceptorMessagesReceived;
252     }
253
254     public int getAcceptorMessagesSent() {
255         return acceptorMessagesSent;
256     }
257
258     public long getAcceptorReceiveFailureProcessingTime() {
259         return acceptorReceiveFailureProcessingTime;
260     }
261
262     public int getAcceptorReceiveFailures() {
263         return acceptorReceiveFailures;
264     }
265
266     public long getAcceptorReceiveProcessingTime() {
267         return acceptorReceiveProcessingTime;
268     }
269
270     public long getAcceptorSendFailureProcessingTime() {
271         return acceptorSendFailureProcessingTime;
272     }
273
274     public int getAcceptorSendFailures() {
275         return acceptorSendFailures;
276     }
277
278     public long getAcceptorSendProcessingTime() {
279         return acceptorSendProcessingTime;
280     }
281
282     public long getAcceptorTimeToConnect() {
283         return acceptorTimeToConnect;
284     }
285
286     public long getAcceptorTimeToFail() {
287         return acceptorTimeToFail;
288     }
289
290     public int getInitiatorBytesReceived() {
291         return initiatorBytesReceived;
292     }
293
294     public int getInitiatorBytesSent() {
295         return initiatorBytesSent;
296     }
297
298     public int getInitiatorConnections() {
299         return initiatorConnections;
300     }
301
302     public int getInitiatorConnectionsClosed() {
303         return initiatorConnectionsClosed;
304     }
305
306     public int getInitiatorConnectionsDropped() {
307         return initiatorConnectionsDropped;
308     }
309
310     public int getInitiatorConnectionsFailed() {
311         return initiatorConnectionsFailed;
312     }
313
314     public int getInitiatorMessagesReceived() {
315         return initiatorMessagesReceived;
316     }
317
318     public int getInitiatorMessagesSent() {
319         return initiatorMessagesSent;
320     }
321
322     public long getInitiatorReceiveFailureProcessingTime() {
323         return initiatorReceiveFailureProcessingTime;
324     }
325
326     public int getInitiatorReceiveFailures() {
327         return initiatorReceiveFailures;
328     }
329
330     public long getInitiatorReceiveProcessingTime() {
331         return initiatorReceiveProcessingTime;
332     }
333
334     public long getInitiatorSendFailureProcessingTime() {
335         return initiatorSendFailureProcessingTime;
336     }
337
338     public int getInitiatorSendFailures() {
339         return initiatorSendFailures;
340     }
341
342     public long getInitiatorSendProcessingTime() {
343         return initiatorSendProcessingTime;
344     }
345
346     public long getInitiatorTimeToConnect() {
347         return initiatorTimeToConnect;
348     }
349
350     public long getInitiatorTimeToFail() {
351         return initiatorTimeToFail;
352     }
353
354     public int getNumPings() {
355         return numPings;
356     }
357
358     public int getNumFailedPings() {
359         return numFailedPings;
360     }
361
362     public long getPingTime() {
363         return pingTime;
364     }
365
366     public long getPingFailedTime() {
367         return pingFailedTime;
368     }
369
370     public int getNumPingsReceived() {
371         return numPingsReceived;
372     }
373
374     public int getBytesReceived() {
375         return acceptorBytesReceived + initiatorBytesReceived;
376     }
377
378     public int getBytesSent() {
379         return acceptorBytesSent + initiatorBytesSent;
380     }
381
382     public int getConnections() {
383         return acceptorConnections + initiatorConnections;
384     }
385
386     public int getConnectionsClosed() {
387         return acceptorConnectionsClosed + initiatorConnectionsClosed;
388     }
389
390     public int getConnectionsDropped() {
391         return acceptorConnectionsDropped + initiatorConnectionsDropped;
392     }
393
394     public int getConnectionsFailed() {
395         return acceptorConnectionsFailed + initiatorConnectionsFailed;
396     }
397
398     public int getMessagesReceived() {
399         return acceptorMessagesReceived + initiatorMessagesReceived;
400     }
401
402     public int getMessagesSent() {
403         return acceptorMessagesSent + initiatorMessagesSent;
404     }
405
406     public long getReceiveFailureProcessingTime() {
407         return acceptorReceiveFailureProcessingTime + initiatorReceiveFailureProcessingTime;
408     }
409
410     public int getReceiveFailures() {
411         return acceptorReceiveFailures + initiatorReceiveFailures;
412     }
413
414     public long getReceiveProcessingTime() {
415         return acceptorReceiveProcessingTime + initiatorReceiveProcessingTime;
416     }
417
418     public long getSendFailureProcessingTime() {
419         return acceptorSendFailureProcessingTime + initiatorSendFailureProcessingTime;
420     }
421
422     public int getSendFailures() {
423         return acceptorSendFailures + initiatorSendFailures;
424     }
425
426     public long getSendProcessingTime() {
427         return acceptorSendProcessingTime + initiatorSendProcessingTime;
428     }
429
430     public long getTotalTimeConnected() {
431         return acceptorTotalTimeConnected + initiatorTotalTimeConnected;
432     }
433
434     public long getTimeToConnect() {
435         return acceptorTimeToConnect + initiatorTimeToConnect;
436     }
437
438     public long getTimeToFail() {
439         return acceptorTimeToFail + initiatorTimeToFail;
440     }
441
442     public int getAveragePingTime() {
443         return (int) ((numPings != 0) ? (pingTime / numPings) : 0);
444     }
445
446     public int getAveragePingFailedTime() {
447         return (int) ((numFailedPings != 0) ? (pingFailedTime / numFailedPings) : 0);
448     }
449
450     public int getAverageAcceptorReceiveFailureProcessingTime() {
451         return (int) ((acceptorReceiveFailures != 0) ? (acceptorReceiveFailureProcessingTime / acceptorReceiveFailures) : 0);
452     }
453
454     public int getAverageAcceptorReceiveProcessingTime() {
455         return (int) ((acceptorMessagesReceived != 0) ? (acceptorReceiveProcessingTime / acceptorMessagesReceived) : 0);
456     }
457
458     public int getAverageAcceptorSendFailureProcessingTime() {
459         return (int) ((acceptorSendFailures != 0) ? (acceptorSendFailureProcessingTime / acceptorSendFailures) : 0);
460     }
461
462     public int getAverageAcceptorSendProcessingTime() {
463         return (int) ((acceptorMessagesSent != 0) ? (acceptorSendProcessingTime / acceptorMessagesSent) : 0);
464     }
465
466     public int getAverageAcceptorTimeToConnect() {
467         return (int) ((acceptorConnections != 0) ? (acceptorTimeToConnect / acceptorConnections) : 0);
468     }
469
470     public int getAverageAcceptorTimeToFail() {
471         return (int) ((acceptorConnectionsFailed != 0) ? (acceptorTimeToFail / acceptorConnectionsFailed) : 0);
472     }
473
474     public int getAverageInitiatorReceiveFailureProcessingTime() {
475         return (int) ((initiatorReceiveFailures != 0) ? (initiatorReceiveFailureProcessingTime / initiatorReceiveFailures) : 0);
476     }
477
478     public int getAverageInitiatorReceiveProcessingTime() {
479         return (int) ((initiatorMessagesReceived != 0) ? (initiatorReceiveProcessingTime / initiatorMessagesReceived) : 0);
480     }
481
482     public int getAverageInitiatorSendFailureProcessingTime() {
483         return (int) ((initiatorSendFailures != 0) ? (initiatorSendFailureProcessingTime / initiatorSendFailures) : 0);
484     }
485
486     public int getAverageInitiatorSendProcessingTime() {
487         return (int) ((initiatorMessagesSent != 0) ? (initiatorSendProcessingTime / initiatorMessagesSent) : 0);
488     }
489
490     public int getAverageInitiatorTimeToConnect() {
491         return (int) ((initiatorConnections != 0) ? (initiatorTimeToConnect / initiatorConnections) : 0);
492     }
493
494     public int getAverageInitiatorTimeToFail() {
495         return (int) ((initiatorConnectionsFailed != 0) ? (initiatorTimeToFail / initiatorConnectionsFailed) : 0);
496     }
497
498     public int getAverageReceiveFailureProcessingTime() {
499         return (int) (((initiatorReceiveFailures + acceptorReceiveFailures) != 0)
500                 ? ((initiatorReceiveFailureProcessingTime + acceptorReceiveFailureProcessingTime)
501                         / (initiatorReceiveFailures + acceptorReceiveFailures))
502                         : 0);
503     }
504
505     public int getAverageReceiveProcessingTime() {
506         return (int) (((initiatorMessagesReceived + acceptorMessagesReceived) != 0)
507                 ? ((initiatorReceiveProcessingTime + acceptorReceiveProcessingTime)
508                         / (initiatorMessagesReceived + acceptorMessagesReceived))
509                         : 0);
510     }
511
512     public int getAverageSendFailureProcessingTime() {
513         return (int) (((initiatorSendFailures + acceptorSendFailures) != 0)
514                 ? ((initiatorSendFailureProcessingTime + acceptorSendFailureProcessingTime)
515                         / (initiatorSendFailures + acceptorSendFailures))
516                         : 0);
517     }
518
519     public int getAverageSendProcessingTime() {
520         return (int) (((initiatorMessagesSent + acceptorMessagesSent) != 0)
521                 ? ((initiatorSendProcessingTime + acceptorSendProcessingTime) / (initiatorMessagesSent + acceptorMessagesSent))
522                 : 0);
523     }
524
525     public int getAverageTimeToConnect() {
526         return (int) (((initiatorConnections + acceptorConnections) != 0)
527                 ? ((initiatorTimeToConnect + acceptorTimeToConnect) / (initiatorConnections + acceptorConnections))
528                 : 0);
529     }
530
531     public int getAverageTimeToFail() {
532         return (int) (((initiatorConnectionsFailed + acceptorConnectionsFailed) != 0)
533                 ? ((initiatorTimeToFail + acceptorTimeToFail) / (initiatorConnectionsFailed + acceptorConnectionsFailed))
534                 : 0);
535     }
536
537     /**
538      * Get the total time this intiated connection has been connected.
539      * <BR><BR>
540      * <B>Note:</B> This does not include the current time connected (if it is currently connected)
541      *
542      * @return time in ms (see note above)
543      * @see #getTotalTimeConnected()
544      */
545     public long getInitiatorTotalTimeConnected() {
546         return initiatorTotalTimeConnected;
547     }
548
549     /**
550      * Get the total time this initiating connection has been connected.  If it is currently
551      * connected, then the total time is adjusted to include the time since the transition time
552      * to become connected until the provided time
553      *
554      * @param adjustmentTime The time of this metric will be adjusted to
555      * @return time in ms (see note above)
556      * @see #getTotalTimeConnected()
557      */
558     public long getInitiatorTotalTimeConnected(long adjustmentTime) {
559         long result = initiatorTotalTimeConnected;
560
561         if (isInitiatorConnected()) {
562             result += (adjustmentTime - this.initiatorTransitionTime);
563         }
564
565         return result;
566     }
567
568     /**
569      * Get the duration of current connection relative to local clock (from transition time)
570      * <BR><BR>
571      * <B>Note:</B> This assumes the clocks are in sync with the reporting peer
572      *
573      * @return time in ms (see note above) or 0 if not connected
574      * @see #getTotalTimeConnected()
575      */
576     public long getInitiatorTimeConnected() {
577         return getInitiatorTimeConnected(System.currentTimeMillis());
578     }
579
580     /**
581      * Get the duration of current connection until the specified time
582      *
583      * @param adjustmentTime The time of this metric will be computed until
584      * @return time in ms (see note above) or 0 if not connected
585      */
586     public long getInitiatorTimeConnected(long adjustmentTime) {
587
588         if (isInitiatorConnected()) {
589             return (adjustmentTime - this.initiatorTransitionTime);
590         } else {
591             return 0;
592         }
593     }
594
595     /**
596      * Get the total time this intiated connection has been connected.
597      * <BR><BR>
598      * <B>Note:</B> This does not include the current time connected (if it is currently connected)
599      *
600      * @return time in ms (see note above)
601      * @see #getTotalTimeConnected()
602      */
603     public long getAcceptorTotalTimeConnected() {
604         return acceptorTotalTimeConnected;
605     }
606
607     /**
608      * Get the total time this initiating connection has been connected.  If it is currently
609      * connected, then the total time is adjusted to include the time since the transition time
610      * to become connected until the provided time
611      *
612      * @param adjustmentTime The time of this metric will be adjusted to
613      * @return time in ms (see note above)
614      * @see #getTotalTimeConnected()
615      */
616     public long getAcceptorTotalTimeConnected(long adjustmentTime) {
617         long result = acceptorTotalTimeConnected;
618
619         if (isAcceptorConnected()) {
620             result += (adjustmentTime - this.acceptorTransitionTime);
621         }
622
623         return result;
624     }
625
626     /**
627      * Get the duration of current connection relative to local clock (from transition time)
628      * <BR><BR>
629      * <B>Note:</B> This assumes the clocks are in sync with the reporting peer
630      *
631      * @return time in ms (see note above) or 0 if not connected
632      * @see #getTotalTimeConnected()
633      */
634     public long getAcceptorTimeConnected() {
635         return getAcceptorTimeConnected(System.currentTimeMillis());
636     }
637
638     /**
639      * Get the duration of current connection until the specified time
640      *
641      * @param adjustmentTime The time of this metric will be computed until
642      * @return time in ms (see note above) or 0 if not connected
643      */
644     public long getAcceptorTimeConnected(long adjustmentTime) {
645
646         if (isAcceptorConnected()) {
647             return (adjustmentTime - this.acceptorTransitionTime);
648         } else {
649             return 0;
650         }
651     }
652
653     void resetInitiatorState(String state, long transitionTime) {
654         if (isInitiatorConnected()) {
655             acceptorTotalTimeConnected += (System.currentTimeMillis() - this.initiatorTransitionTime);
656         }
657
658         this.initiatorState = state;
659         this.initiatorTransitionTime = transitionTime;
660         // System.out.println("initiatorState: " + initiatorState + " " + endpointAddress);
661     }
662
663     void resetAcceptorState(String state, long transitionTime) {
664         if (isAcceptorConnected()) {
665             initiatorTotalTimeConnected += (System.currentTimeMillis() - this.acceptorTransitionTime);
666         }
667
668         this.acceptorState = state;
669         this.acceptorTransitionTime = transitionTime;
670         // System.out.println("acceptorState: " + acceptorState + " " + endpointAddress);
671     }
672
673     void connectionEstablished(boolean initiator, long timeToConnect, long transitionTime) {
674         if (initiator) {
675             resetInitiatorState(CONNECTED, transitionTime);
676             initiatorConnections++;
677             initiatorTimeToConnect += timeToConnect;
678         } else {
679             resetAcceptorState(CONNECTED, transitionTime);
680             acceptorConnections++;
681             acceptorTimeToConnect += timeToConnect;
682         }
683
684     }
685
686     void connectionFailed(boolean initiator, long timeToConnect, long transitionTime) {
687         if (initiator) {
688             resetInitiatorState(FAILED, transitionTime);
689             initiatorConnectionsFailed++;
690             initiatorTimeToFail += timeToConnect;
691         } else {
692             resetAcceptorState(FAILED, transitionTime);
693             acceptorConnectionsFailed++;
694             acceptorTimeToFail += timeToConnect;
695         }
696     }
697
698     void connectionClosed(boolean initiator, long transitionTime) {
699         if (initiator) {
700             resetInitiatorState(CLOSED, transitionTime);
701             initiatorConnectionsClosed++;
702         } else {
703             resetAcceptorState(CLOSED, transitionTime);
704             acceptorConnectionsClosed++;
705         }
706     }
707
708     void connectionDropped(boolean initiator, long transitionTime) {
709         if (initiator) {
710             resetInitiatorState(DROPPED, transitionTime);
711             initiatorConnectionsDropped++;
712         } else {
713             resetAcceptorState(DROPPED, transitionTime);
714             acceptorConnectionsDropped++;
715         }
716     }
717
718     void pingReceived() {
719         numPingsReceived++;
720     }
721
722     void ping(long time) {
723         numPings++;
724         pingTime += time;
725     }
726
727     void pingFailed(long time) {
728         numFailedPings++;
729         pingFailedTime += time;
730     }
731
732     void dataReceived(boolean initiator, int size) {
733         if (initiator) {
734             initiatorBytesReceived += size;
735         } else {
736             acceptorBytesReceived += size;
737         }
738     }
739
740     void messageReceived(boolean initiator, Message message, long time, long size) {
741         if (initiator) {
742             initiatorMessagesReceived++;
743             initiatorReceiveProcessingTime += time;
744             initiatorBytesReceived += size;
745         } else {
746             acceptorMessagesReceived++;
747             acceptorReceiveProcessingTime += time;
748             acceptorBytesReceived += size;
749         }
750     }
751
752     void receiveFailure(boolean initiator, long time, long size) {
753         if (initiator) {
754             initiatorReceiveFailures++;
755             initiatorReceiveFailureProcessingTime += time;
756             initiatorBytesReceived += size;
757         } else {
758             acceptorReceiveFailures++;
759             acceptorReceiveFailureProcessingTime += time;
760             acceptorBytesReceived += size;
761         }
762     }
763
764     void dataSent(boolean initiator, long size) {
765         if (initiator) {
766             initiatorBytesSent += size;
767         } else {
768             acceptorBytesSent += size;
769         }
770     }
771
772     void sendFailure(boolean initiator, Message message, long time, long size) {
773         if (initiator) {
774             initiatorSendFailures++;
775             initiatorSendFailureProcessingTime += time;
776             initiatorBytesSent += size;
777         } else {
778             acceptorSendFailures++;
779             acceptorSendFailureProcessingTime += time;
780             acceptorBytesSent += size;
781         }
782     }
783
784     void messageSent(boolean initiator, Message message, long time, long size) {
785         if (initiator) {
786             initiatorMessagesSent++;
787             initiatorSendProcessingTime += time;
788             initiatorBytesSent += size;
789         } else {
790             acceptorMessagesSent++;
791             acceptorSendProcessingTime += time;
792             acceptorBytesSent += size;
793         }
794     }
795
796     public void mergeMetrics(TransportBindingMetric other) {
797         peerID = other.peerID;
798
799         if (other.initiatorState != null) {
800             initiatorState = other.initiatorState;
801         }
802
803         if (other.initiatorTransitionTime != 0) {
804             initiatorTransitionTime = other.initiatorTransitionTime;
805         }
806
807         if (other.acceptorState != null) {
808             acceptorState = other.acceptorState;
809         }
810
811         if (other.initiatorTransitionTime != 0) {
812             acceptorTransitionTime = other.acceptorTransitionTime;
813         }
814
815         acceptorBytesReceived += other.acceptorBytesReceived;
816         acceptorBytesSent += other.acceptorBytesSent;
817         acceptorConnections += other.acceptorConnections;
818         acceptorConnectionsClosed += other.acceptorConnectionsClosed;
819         acceptorConnectionsDropped += other.acceptorConnectionsDropped;
820         acceptorConnectionsFailed += other.acceptorConnectionsFailed;
821         acceptorMessagesReceived += other.acceptorMessagesReceived;
822         acceptorMessagesSent += other.acceptorMessagesSent;
823         acceptorReceiveFailureProcessingTime += other.acceptorReceiveFailureProcessingTime;
824         acceptorReceiveFailures += other.acceptorReceiveFailures;
825         acceptorReceiveProcessingTime += other.acceptorReceiveProcessingTime;
826         acceptorSendFailureProcessingTime += other.acceptorSendFailureProcessingTime;
827         acceptorSendFailures += other.acceptorSendFailures;
828         acceptorSendProcessingTime += other.acceptorSendProcessingTime;
829         acceptorTotalTimeConnected += other.acceptorTotalTimeConnected;
830         acceptorTimeToConnect += other.acceptorTimeToConnect;
831         acceptorTimeToFail += other.acceptorTimeToFail;
832         initiatorBytesReceived += other.initiatorBytesReceived;
833         initiatorBytesSent += other.initiatorBytesSent;
834         initiatorTotalTimeConnected += other.initiatorTotalTimeConnected;
835         initiatorConnections += other.initiatorConnections;
836         initiatorConnectionsClosed += other.initiatorConnectionsClosed;
837         initiatorConnectionsDropped += other.initiatorConnectionsDropped;
838         initiatorConnectionsFailed += other.initiatorConnectionsFailed;
839         initiatorMessagesReceived += other.initiatorMessagesReceived;
840         initiatorMessagesSent += other.initiatorMessagesSent;
841         initiatorReceiveFailureProcessingTime += other.initiatorReceiveFailureProcessingTime;
842         initiatorReceiveFailures += other.initiatorReceiveFailures;
843         initiatorReceiveProcessingTime += other.initiatorReceiveProcessingTime;
844         initiatorSendFailureProcessingTime += other.initiatorSendFailureProcessingTime;
845         initiatorSendFailures += other.initiatorSendFailures;
846         initiatorSendProcessingTime += other.initiatorSendProcessingTime;
847         initiatorTimeToConnect += other.initiatorTimeToConnect;
848         initiatorTimeToFail += other.initiatorTimeToFail;
849         numPings += other.numPings;
850         numFailedPings += other.numFailedPings;
851         pingTime += other.pingTime;
852         pingFailedTime += other.pingFailedTime;
853         numPingsReceived += other.numPingsReceived;
854
855     }
856
857     public void serializeTo(Element element) throws DocumentSerializationException {
858
859         DocumentSerializableUtilities.addString(element, "peerID", peerID.toString());
860         DocumentSerializableUtilities.addString(element, "endpointAddress", endpointAddress.toString());
861
862         if (initiatorState != null) {
863             DocumentSerializableUtilities.addString(element, "initiatorState", initiatorState);
864         }
865         if (initiatorTransitionTime != 0) {
866             DocumentSerializableUtilities.addLong(element, "initiatorTransitionTime", initiatorTransitionTime);
867         }
868
869         if (acceptorState != null) {
870             DocumentSerializableUtilities.addString(element, "acceptorState", acceptorState);
871         }
872         if (acceptorTransitionTime != 0) {
873             DocumentSerializableUtilities.addLong(element, "acceptorTransitionTime", acceptorTransitionTime);
874         }
875
876         if (acceptorBytesReceived != 0) {
877             DocumentSerializableUtilities.addInt(element, "acceptorBytesReceived", acceptorBytesReceived);
878         }
879
880         if (acceptorBytesSent != 0) {
881             DocumentSerializableUtilities.addInt(element, "acceptorBytesSent", acceptorBytesSent);
882         }
883
884         if (acceptorConnections != 0) {
885             DocumentSerializableUtilities.addInt(element, "acceptorConnections", acceptorConnections);
886         }
887
888         if (acceptorConnectionsClosed != 0) {
889             DocumentSerializableUtilities.addInt(element, "acceptorConnectionsClosed", acceptorConnectionsClosed);
890         }
891
892         if (acceptorConnectionsDropped != 0) {
893             DocumentSerializableUtilities.addInt(element, "acceptorConnectionsDropped", acceptorConnectionsDropped);
894         }
895
896         if (acceptorConnectionsFailed != 0) {
897             DocumentSerializableUtilities.addInt(element, "acceptorConnectionsFailed", acceptorConnectionsFailed);
898         }
899
900         if (acceptorMessagesReceived != 0) {
901             DocumentSerializableUtilities.addInt(element, "acceptorMessagesReceived", acceptorMessagesReceived);
902         }
903
904         if (acceptorMessagesSent != 0) {
905             DocumentSerializableUtilities.addInt(element, "acceptorMessagesSent", acceptorMessagesSent);
906         }
907
908         if (acceptorReceiveFailureProcessingTime != 0) {
909             DocumentSerializableUtilities.addLong(element, "acceptorReceiveFailureProcessingTime"
910                     ,
911                     acceptorReceiveFailureProcessingTime);
912         }
913
914         if (acceptorReceiveFailures != 0) {
915             DocumentSerializableUtilities.addInt(element, "acceptorReceiveFailures", acceptorReceiveFailures);
916         }
917
918         if (acceptorReceiveProcessingTime != 0) {
919             DocumentSerializableUtilities.addLong(element, "acceptorReceiveProcessingTime", acceptorReceiveProcessingTime);
920         }
921
922         if (acceptorSendFailureProcessingTime != 0) {
923             DocumentSerializableUtilities.addLong(element, "acceptorSendFailureProcessingTime", acceptorSendFailureProcessingTime);
924         }
925
926         if (acceptorSendFailures != 0) {
927             DocumentSerializableUtilities.addInt(element, "acceptorSendFailures", acceptorSendFailures);
928         }
929
930         if (acceptorSendProcessingTime != 0) {
931             DocumentSerializableUtilities.addLong(element, "acceptorSendProcessingTime", acceptorSendProcessingTime);
932         }
933
934         if (acceptorTotalTimeConnected != 0) {
935             DocumentSerializableUtilities.addLong(element, "acceptorTotalTimeConnected", acceptorTotalTimeConnected);
936         }
937
938         if (acceptorTimeToConnect != 0) {
939             DocumentSerializableUtilities.addLong(element, "acceptorTimeToConnect", acceptorTimeToConnect);
940         }
941
942         if (acceptorTimeToFail != 0) {
943             DocumentSerializableUtilities.addLong(element, "acceptorTimeToFail", acceptorTimeToFail);
944         }
945
946         if (initiatorBytesReceived != 0) {
947             DocumentSerializableUtilities.addInt(element, "initiatorBytesReceived", initiatorBytesReceived);
948         }
949
950         if (initiatorBytesSent != 0) {
951             DocumentSerializableUtilities.addInt(element, "initiatorBytesSent", initiatorBytesSent);
952         }
953
954         if (initiatorTotalTimeConnected != 0) {
955             DocumentSerializableUtilities.addLong(element, "initiatorTotalTimeConnected", initiatorTotalTimeConnected);
956         }
957
958         if (initiatorConnections != 0) {
959             DocumentSerializableUtilities.addInt(element, "initiatorConnections", initiatorConnections);
960         }
961
962         if (initiatorConnectionsClosed != 0) {
963             DocumentSerializableUtilities.addInt(element, "initiatorConnectionsClosed", initiatorConnectionsClosed);
964         }
965
966         if (initiatorConnectionsDropped != 0) {
967             DocumentSerializableUtilities.addInt(element, "initiatorConnectionsDropped", initiatorConnectionsDropped);
968         }
969
970         if (initiatorConnectionsFailed != 0) {
971             DocumentSerializableUtilities.addInt(element, "initiatorConnectionsFailed", initiatorConnectionsFailed);
972         }
973
974         if (initiatorMessagesReceived != 0) {
975             DocumentSerializableUtilities.addInt(element, "initiatorMessagesReceived", initiatorMessagesReceived);
976         }
977
978         if (initiatorMessagesSent != 0) {
979             DocumentSerializableUtilities.addInt(element, "initiatorMessagesSent", initiatorMessagesSent);
980         }
981
982         if (initiatorReceiveFailureProcessingTime != 0) {
983             DocumentSerializableUtilities.addLong(element, "initiatorReceiveFailureProcessingTime"
984                     ,
985                     initiatorReceiveFailureProcessingTime);
986         }
987
988         if (initiatorReceiveFailures != 0) {
989             DocumentSerializableUtilities.addInt(element, "initiatorReceiveFailures", initiatorReceiveFailures);
990         }
991
992         if (initiatorReceiveProcessingTime != 0) {
993             DocumentSerializableUtilities.addLong(element, "initiatorReceiveProcessingTime", initiatorReceiveProcessingTime);
994         }
995
996         if (initiatorSendFailureProcessingTime != 0) {
997             DocumentSerializableUtilities.addLong(element, "initiatorSendFailureProcessingTime"
998                     ,
999                     initiatorSendFailureProcessingTime);
1000         }
1001
1002         if (initiatorSendFailures != 0) {
1003             DocumentSerializableUtilities.addInt(element, "initiatorSendFailures", initiatorSendFailures);
1004         }
1005
1006         if (initiatorSendProcessingTime != 0) {
1007             DocumentSerializableUtilities.addLong(element, "initiatorSendProcessingTime", initiatorSendProcessingTime);
1008         }
1009
1010         if (initiatorTimeToConnect != 0) {
1011             DocumentSerializableUtilities.addLong(element, "initiatorTimeToConnect", initiatorTimeToConnect);
1012         }
1013
1014         if (initiatorTimeToFail != 0) {
1015             DocumentSerializableUtilities.addLong(element, "initiatorTimeToFail", initiatorTimeToFail);
1016         }
1017
1018         if (numPings != 0) {
1019             DocumentSerializableUtilities.addInt(element, "numPings", numPings);
1020         }
1021
1022         if (numFailedPings != 0) {
1023             DocumentSerializableUtilities.addInt(element, "numFailedPings", numFailedPings);
1024         }
1025
1026         if (pingTime != 0) {
1027             DocumentSerializableUtilities.addLong(element, "pingTime", pingTime);
1028         }
1029
1030         if (pingFailedTime != 0) {
1031             DocumentSerializableUtilities.addLong(element, "pingFailedTime", pingFailedTime);
1032         }
1033
1034         if (initiatorTimeToFail != 0) {
1035             DocumentSerializableUtilities.addInt(element, "numPingsReceived", numPingsReceived);
1036         }
1037
1038     }
1039
1040     public void initializeFrom(Element element) throws DocumentSerializationException {
1041         for (Enumeration e = element.getChildren(); e.hasMoreElements();) {
1042             Element childElement = (TextElement) e.nextElement();
1043             String tagName = (String) childElement.getKey();
1044
1045             if (tagName.equals("peerID")) {
1046                 String peerIdString = DocumentSerializableUtilities.getString(childElement);
1047
1048                 peerID = MetricUtilities.getPeerIdFromString(peerIdString);
1049             }
1050             if (tagName.equals("endpointAddress")) {
1051                 String endpointAddressString = DocumentSerializableUtilities.getString(childElement);
1052
1053                 endpointAddress = new EndpointAddress(endpointAddressString);
1054             } else if (tagName.equals("acceptorBytesReceived")) {
1055                 acceptorBytesReceived = DocumentSerializableUtilities.getInt(childElement);
1056             } else if (tagName.equals("acceptorBytesSent")) {
1057                 acceptorBytesSent = DocumentSerializableUtilities.getInt(childElement);
1058             } else if (tagName.equals("acceptorConnections")) {
1059                 acceptorConnections = DocumentSerializableUtilities.getInt(childElement);
1060             } else if (tagName.equals("acceptorConnectionsClosed")) {
1061                 acceptorConnectionsClosed = DocumentSerializableUtilities.getInt(childElement);
1062             } else if (tagName.equals("acceptorConnectionsDropped")) {
1063                 acceptorConnectionsDropped = DocumentSerializableUtilities.getInt(childElement);
1064             } else if (tagName.equals("acceptorConnectionsFailed")) {
1065                 acceptorConnectionsFailed = DocumentSerializableUtilities.getInt(childElement);
1066             } else if (tagName.equals("acceptorMessagesReceived")) {
1067                 acceptorMessagesReceived = DocumentSerializableUtilities.getInt(childElement);
1068             } else if (tagName.equals("acceptorMessagesSent")) {
1069                 acceptorMessagesSent = DocumentSerializableUtilities.getInt(childElement);
1070             } else if (tagName.equals("acceptorReceiveFailureProcessingTime")) {
1071                 acceptorReceiveFailureProcessingTime = DocumentSerializableUtilities.getLong(childElement);
1072             } else if (tagName.equals("acceptorReceiveFailures")) {
1073                 acceptorReceiveFailures = DocumentSerializableUtilities.getInt(childElement);
1074             } else if (tagName.equals("acceptorReceiveProcessingTime")) {
1075                 acceptorReceiveProcessingTime = DocumentSerializableUtilities.getLong(childElement);
1076             } else if (tagName.equals("acceptorSendFailureProcessingTime")) {
1077                 acceptorSendFailureProcessingTime = DocumentSerializableUtilities.getLong(childElement);
1078             } else if (tagName.equals("acceptorSendFailures")) {
1079                 acceptorSendFailures = DocumentSerializableUtilities.getInt(childElement);
1080             } else if (tagName.equals("acceptorSendProcessingTime")) {
1081                 acceptorSendProcessingTime = DocumentSerializableUtilities.getLong(childElement);
1082             } else if (tagName.equals("acceptorTotalTimeConnected")) {
1083                 acceptorTotalTimeConnected = DocumentSerializableUtilities.getLong(childElement);
1084             } else if (tagName.equals("acceptorTimeToConnect")) {
1085                 acceptorTimeToConnect = DocumentSerializableUtilities.getLong(childElement);
1086             } else if (tagName.equals("acceptorTimeToFail")) {
1087                 acceptorTimeToFail = DocumentSerializableUtilities.getLong(childElement);
1088             } else if (tagName.equals("initiatorBytesReceived")) {
1089                 initiatorBytesReceived = DocumentSerializableUtilities.getInt(childElement);
1090             } else if (tagName.equals("initiatorBytesSent")) {
1091                 initiatorBytesSent = DocumentSerializableUtilities.getInt(childElement);
1092             } else if (tagName.equals("initiatorTotalTimeConnected")) {
1093                 initiatorTotalTimeConnected = DocumentSerializableUtilities.getLong(childElement);
1094             } else if (tagName.equals("initiatorConnections")) {
1095                 initiatorConnections = DocumentSerializableUtilities.getInt(childElement);
1096             } else if (tagName.equals("initiatorConnectionsClosed")) {
1097                 initiatorConnectionsClosed = DocumentSerializableUtilities.getInt(childElement);
1098             } else if (tagName.equals("initiatorConnectionsDropped")) {
1099                 initiatorConnectionsDropped = DocumentSerializableUtilities.getInt(childElement);
1100             } else if (tagName.equals("initiatorConnectionsFailed")) {
1101                 initiatorConnectionsFailed = DocumentSerializableUtilities.getInt(childElement);
1102             } else if (tagName.equals("initiatorMessagesReceived")) {
1103                 initiatorMessagesReceived = DocumentSerializableUtilities.getInt(childElement);
1104             } else if (tagName.equals("initiatorMessagesSent")) {
1105                 initiatorMessagesSent = DocumentSerializableUtilities.getInt(childElement);
1106             } else if (tagName.equals("initiatorReceiveFailureProcessingTime")) {
1107                 initiatorReceiveFailureProcessingTime = DocumentSerializableUtilities.getLong(childElement);
1108             } else if (tagName.equals("initiatorReceiveFailures")) {
1109                 initiatorReceiveFailures = DocumentSerializableUtilities.getInt(childElement);
1110             } else if (tagName.equals("initiatorReceiveProcessingTime")) {
1111                 initiatorReceiveProcessingTime = DocumentSerializableUtilities.getLong(childElement);
1112             } else if (tagName.equals("initiatorSendFailureProcessingTime")) {
1113                 initiatorSendFailureProcessingTime = DocumentSerializableUtilities.getLong(childElement);
1114             } else if (tagName.equals("initiatorSendFailures")) {
1115                 initiatorSendFailures = DocumentSerializableUtilities.getInt(childElement);
1116             } else if (tagName.equals("initiatorSendProcessingTime")) {
1117                 initiatorSendProcessingTime = DocumentSerializableUtilities.getLong(childElement);
1118             } else if (tagName.equals("initiatorTimeToConnect")) {
1119                 initiatorTimeToConnect = DocumentSerializableUtilities.getLong(childElement);
1120             } else if (tagName.equals("initiatorTimeToFail")) {
1121                 initiatorTimeToFail = DocumentSerializableUtilities.getLong(childElement);
1122             } else if (tagName.equals("numPingsReceived")) {
1123                 numPingsReceived = DocumentSerializableUtilities.getInt(childElement);
1124             } else if (tagName.equals("numPings")) {
1125                 numPings = DocumentSerializableUtilities.getInt(childElement);
1126             } else if (tagName.equals("numFailedPings")) {
1127                 numFailedPings = DocumentSerializableUtilities.getInt(childElement);
1128             } else if (tagName.equals("pingTime")) {
1129                 pingTime = DocumentSerializableUtilities.getLong(childElement);
1130             } else if (tagName.equals("pingFailedTime")) {
1131                 pingFailedTime = DocumentSerializableUtilities.getLong(childElement);
1132             }
1133         }
1134     }
1135 }