1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/HttpConnectionMetricsImpl.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl; 1.32 + 1.33 +import java.util.HashMap; 1.34 +import ch.boye.httpclientandroidlib.HttpConnectionMetrics; 1.35 +import ch.boye.httpclientandroidlib.io.HttpTransportMetrics; 1.36 + 1.37 +/** 1.38 + * Default implementation of the {@link HttpConnectionMetrics} interface. 1.39 + * 1.40 + * @since 4.0 1.41 + */ 1.42 +public class HttpConnectionMetricsImpl implements HttpConnectionMetrics { 1.43 + 1.44 + public static final String REQUEST_COUNT = "http.request-count"; 1.45 + public static final String RESPONSE_COUNT = "http.response-count"; 1.46 + public static final String SENT_BYTES_COUNT = "http.sent-bytes-count"; 1.47 + public static final String RECEIVED_BYTES_COUNT = "http.received-bytes-count"; 1.48 + 1.49 + private final HttpTransportMetrics inTransportMetric; 1.50 + private final HttpTransportMetrics outTransportMetric; 1.51 + private long requestCount = 0; 1.52 + private long responseCount = 0; 1.53 + 1.54 + /** 1.55 + * The cache map for all metrics values. 1.56 + */ 1.57 + private HashMap metricsCache; 1.58 + 1.59 + public HttpConnectionMetricsImpl( 1.60 + final HttpTransportMetrics inTransportMetric, 1.61 + final HttpTransportMetrics outTransportMetric) { 1.62 + super(); 1.63 + this.inTransportMetric = inTransportMetric; 1.64 + this.outTransportMetric = outTransportMetric; 1.65 + } 1.66 + 1.67 + /* ------------------ Public interface method -------------------------- */ 1.68 + 1.69 + public long getReceivedBytesCount() { 1.70 + if (this.inTransportMetric != null) { 1.71 + return this.inTransportMetric.getBytesTransferred(); 1.72 + } else { 1.73 + return -1; 1.74 + } 1.75 + } 1.76 + 1.77 + public long getSentBytesCount() { 1.78 + if (this.outTransportMetric != null) { 1.79 + return this.outTransportMetric.getBytesTransferred(); 1.80 + } else { 1.81 + return -1; 1.82 + } 1.83 + } 1.84 + 1.85 + public long getRequestCount() { 1.86 + return this.requestCount; 1.87 + } 1.88 + 1.89 + public void incrementRequestCount() { 1.90 + this.requestCount++; 1.91 + } 1.92 + 1.93 + public long getResponseCount() { 1.94 + return this.responseCount; 1.95 + } 1.96 + 1.97 + public void incrementResponseCount() { 1.98 + this.responseCount++; 1.99 + } 1.100 + 1.101 + public Object getMetric(final String metricName) { 1.102 + Object value = null; 1.103 + if (this.metricsCache != null) { 1.104 + value = this.metricsCache.get(metricName); 1.105 + } 1.106 + if (value == null) { 1.107 + if (REQUEST_COUNT.equals(metricName)) { 1.108 + value = new Long(requestCount); 1.109 + } else if (RESPONSE_COUNT.equals(metricName)) { 1.110 + value = new Long(responseCount); 1.111 + } else if (RECEIVED_BYTES_COUNT.equals(metricName)) { 1.112 + if (this.inTransportMetric != null) { 1.113 + return new Long(this.inTransportMetric.getBytesTransferred()); 1.114 + } else { 1.115 + return null; 1.116 + } 1.117 + } else if (SENT_BYTES_COUNT.equals(metricName)) { 1.118 + if (this.outTransportMetric != null) { 1.119 + return new Long(this.outTransportMetric.getBytesTransferred()); 1.120 + } else { 1.121 + return null; 1.122 + } 1.123 + } 1.124 + } 1.125 + return value; 1.126 + } 1.127 + 1.128 + public void setMetric(final String metricName, Object obj) { 1.129 + if (this.metricsCache == null) { 1.130 + this.metricsCache = new HashMap(); 1.131 + } 1.132 + this.metricsCache.put(metricName, obj); 1.133 + } 1.134 + 1.135 + public void reset() { 1.136 + if (this.outTransportMetric != null) { 1.137 + this.outTransportMetric.reset(); 1.138 + } 1.139 + if (this.inTransportMetric != null) { 1.140 + this.inTransportMetric.reset(); 1.141 + } 1.142 + this.requestCount = 0; 1.143 + this.responseCount = 0; 1.144 + this.metricsCache = null; 1.145 + } 1.146 + 1.147 +}