]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/endpoint/transportMeter/TransportBindingMeter.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 / TransportBindingMeter.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.peer.PeerID;
61 import net.jxta.endpoint.*;
62
63
64 public class TransportBindingMeter {
65     private PeerID peerID;
66     private EndpointAddress endpointAddress;
67
68     private TransportBindingMetric cumulativeMetrics;
69     private TransportBindingMetric deltaMetrics;
70
71     public TransportBindingMeter(PeerID peerID, EndpointAddress endpointAddress) {
72         this(peerID, endpointAddress, false, false);
73     }
74         
75     public TransportBindingMeter(PeerID peerID, EndpointAddress endpointAddress, boolean initiatorConnected, boolean acceptorConnected) {
76         this.peerID = peerID;
77         this.endpointAddress = endpointAddress;
78         cumulativeMetrics = new TransportBindingMetric(this, initiatorConnected, acceptorConnected);
79     }
80
81     @Override
82     public String toString() {
83         return "TransportBindingMeter(" + endpointAddress + ";" + peerID + ")";
84     }
85
86     public synchronized TransportBindingMetric collectMetrics() {
87         TransportBindingMetric prevDelta = deltaMetrics;
88
89         deltaMetrics = null;
90         return prevDelta;
91     }
92                 
93     private void createDeltaMetric() {
94         deltaMetrics = new TransportBindingMetric(cumulativeMetrics);
95     }
96
97     public TransportBindingMetric getCumulativeMetrics() {
98         return cumulativeMetrics;
99     }
100
101     public PeerID getPeerID() {
102         return peerID;
103     }
104
105     public EndpointAddress getEndpointAddress() {
106         return endpointAddress;
107     }
108
109     public void setPeerID(PeerID peerID) { 
110         this.peerID = peerID; 
111         cumulativeMetrics.setPeerID(peerID);
112                 
113         if (deltaMetrics != null) {     
114             deltaMetrics.setPeerID(peerID);
115         }
116     }
117
118     public synchronized void connectionEstablished(boolean initator, long timeToConnect) {              
119         if (deltaMetrics == null) {     
120             createDeltaMetric();
121         }
122                         
123         long now = System.currentTimeMillis();
124
125         deltaMetrics.connectionEstablished(initator, timeToConnect, now);
126         cumulativeMetrics.connectionEstablished(initator, timeToConnect, now);
127     }
128
129     public synchronized void connectionFailed(boolean initator, long timeToConnect) {
130         if (deltaMetrics == null) {     
131             createDeltaMetric();
132         }
133                         
134         long now = System.currentTimeMillis();
135
136         deltaMetrics.connectionFailed(initator, timeToConnect, now);
137         cumulativeMetrics.connectionFailed(initator, timeToConnect, now);
138     }
139         
140     public synchronized void connectionClosed(boolean initator, long connectionLife) {
141         if (deltaMetrics == null) {     
142             createDeltaMetric();
143         }
144                         
145         long now = System.currentTimeMillis();
146
147         deltaMetrics.connectionClosed(initator, now);
148         cumulativeMetrics.connectionClosed(initator, now);
149     }
150         
151     public synchronized void connectionDropped(boolean initator, long connectionLife) {
152         if (deltaMetrics == null) {     
153             createDeltaMetric();
154         }
155                         
156         long now = System.currentTimeMillis();
157
158         deltaMetrics.connectionDropped(initator, now);
159         cumulativeMetrics.connectionDropped(initator, now);
160     }
161
162     public synchronized void pingReceived() {
163         if (deltaMetrics == null) {     
164             createDeltaMetric();
165         }
166                         
167         deltaMetrics.pingReceived();
168         cumulativeMetrics.pingReceived();
169     }
170         
171     public synchronized void ping(long time) {
172         if (deltaMetrics == null) {     
173             createDeltaMetric();
174         }
175                         
176         deltaMetrics.ping(time);
177         cumulativeMetrics.ping(time);
178     }
179         
180     public synchronized void pingFailed(long time) {
181         if (deltaMetrics == null) {     
182             createDeltaMetric();
183         }
184                         
185         deltaMetrics.pingFailed(time);
186         cumulativeMetrics.pingFailed(time);
187     }
188
189     public synchronized void dataReceived(boolean initator, int size) {
190         if (deltaMetrics == null) {     
191             createDeltaMetric();
192         }
193                         
194         deltaMetrics.dataReceived(initator, size);
195         cumulativeMetrics.dataReceived(initator, size);
196     }
197
198     public synchronized void messageReceived(boolean initator, Message message, long time, long size) {
199         if (deltaMetrics == null) {     
200             createDeltaMetric();
201         }
202                         
203         deltaMetrics.messageReceived(initator, message, time, size);
204         cumulativeMetrics.messageReceived(initator, message, time, size);
205     }
206
207     public synchronized void receiveFailure(boolean initator, long time, long size) {
208         if (deltaMetrics == null) {     
209             createDeltaMetric();
210         }
211                         
212         deltaMetrics.receiveFailure(initator, time, size);
213         cumulativeMetrics.receiveFailure(initator, time, size);
214     }
215
216     public synchronized void dataSent(boolean initator, long size) {
217         if (deltaMetrics == null) {     
218             createDeltaMetric();
219         }
220                         
221         deltaMetrics.dataSent(initator, size);
222         cumulativeMetrics.dataSent(initator, size);
223     }           
224         
225     public synchronized void sendFailure(boolean initator, Message message, long time, long size) {
226         if (deltaMetrics == null) {     
227             createDeltaMetric();
228         }
229                         
230         deltaMetrics.sendFailure(initator, message, time, size);
231         cumulativeMetrics.sendFailure(initator, message, time, size);
232     }
233
234     public synchronized void messageSent(boolean initator, Message message, long time, long size) {
235         if (deltaMetrics == null) {     
236             createDeltaMetric();
237         }
238                         
239         deltaMetrics.messageSent(initator, message, time, size);
240         cumulativeMetrics.messageSent(initator, message, time, size);
241     }           
242 }