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.transportMeter;
60 import net.jxta.peer.PeerID;
61 import net.jxta.endpoint.*;
64 public class TransportBindingMeter {
65 private PeerID peerID;
66 private EndpointAddress endpointAddress;
68 private TransportBindingMetric cumulativeMetrics;
69 private TransportBindingMetric deltaMetrics;
71 public TransportBindingMeter(PeerID peerID, EndpointAddress endpointAddress) {
72 this(peerID, endpointAddress, false, false);
75 public TransportBindingMeter(PeerID peerID, EndpointAddress endpointAddress, boolean initiatorConnected, boolean acceptorConnected) {
77 this.endpointAddress = endpointAddress;
78 cumulativeMetrics = new TransportBindingMetric(this, initiatorConnected, acceptorConnected);
82 public String toString() {
83 return "TransportBindingMeter(" + endpointAddress + ";" + peerID + ")";
86 public synchronized TransportBindingMetric collectMetrics() {
87 TransportBindingMetric prevDelta = deltaMetrics;
93 private void createDeltaMetric() {
94 deltaMetrics = new TransportBindingMetric(cumulativeMetrics);
97 public TransportBindingMetric getCumulativeMetrics() {
98 return cumulativeMetrics;
101 public PeerID getPeerID() {
105 public EndpointAddress getEndpointAddress() {
106 return endpointAddress;
109 public void setPeerID(PeerID peerID) {
110 this.peerID = peerID;
111 cumulativeMetrics.setPeerID(peerID);
113 if (deltaMetrics != null) {
114 deltaMetrics.setPeerID(peerID);
118 public synchronized void connectionEstablished(boolean initator, long timeToConnect) {
119 if (deltaMetrics == null) {
123 long now = System.currentTimeMillis();
125 deltaMetrics.connectionEstablished(initator, timeToConnect, now);
126 cumulativeMetrics.connectionEstablished(initator, timeToConnect, now);
129 public synchronized void connectionFailed(boolean initator, long timeToConnect) {
130 if (deltaMetrics == null) {
134 long now = System.currentTimeMillis();
136 deltaMetrics.connectionFailed(initator, timeToConnect, now);
137 cumulativeMetrics.connectionFailed(initator, timeToConnect, now);
140 public synchronized void connectionClosed(boolean initator, long connectionLife) {
141 if (deltaMetrics == null) {
145 long now = System.currentTimeMillis();
147 deltaMetrics.connectionClosed(initator, now);
148 cumulativeMetrics.connectionClosed(initator, now);
151 public synchronized void connectionDropped(boolean initator, long connectionLife) {
152 if (deltaMetrics == null) {
156 long now = System.currentTimeMillis();
158 deltaMetrics.connectionDropped(initator, now);
159 cumulativeMetrics.connectionDropped(initator, now);
162 public synchronized void pingReceived() {
163 if (deltaMetrics == null) {
167 deltaMetrics.pingReceived();
168 cumulativeMetrics.pingReceived();
171 public synchronized void ping(long time) {
172 if (deltaMetrics == null) {
176 deltaMetrics.ping(time);
177 cumulativeMetrics.ping(time);
180 public synchronized void pingFailed(long time) {
181 if (deltaMetrics == null) {
185 deltaMetrics.pingFailed(time);
186 cumulativeMetrics.pingFailed(time);
189 public synchronized void dataReceived(boolean initator, int size) {
190 if (deltaMetrics == null) {
194 deltaMetrics.dataReceived(initator, size);
195 cumulativeMetrics.dataReceived(initator, size);
198 public synchronized void messageReceived(boolean initator, Message message, long time, long size) {
199 if (deltaMetrics == null) {
203 deltaMetrics.messageReceived(initator, message, time, size);
204 cumulativeMetrics.messageReceived(initator, message, time, size);
207 public synchronized void receiveFailure(boolean initator, long time, long size) {
208 if (deltaMetrics == null) {
212 deltaMetrics.receiveFailure(initator, time, size);
213 cumulativeMetrics.receiveFailure(initator, time, size);
216 public synchronized void dataSent(boolean initator, long size) {
217 if (deltaMetrics == null) {
221 deltaMetrics.dataSent(initator, size);
222 cumulativeMetrics.dataSent(initator, size);
225 public synchronized void sendFailure(boolean initator, Message message, long time, long size) {
226 if (deltaMetrics == null) {
230 deltaMetrics.sendFailure(initator, message, time, size);
231 cumulativeMetrics.sendFailure(initator, message, time, size);
234 public synchronized void messageSent(boolean initator, Message message, long time, long size) {
235 if (deltaMetrics == null) {
239 deltaMetrics.messageSent(initator, message, time, size);
240 cumulativeMetrics.messageSent(initator, message, time, size);