|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.impl; |
|
29 |
|
30 import java.util.HashMap; |
|
31 import ch.boye.httpclientandroidlib.HttpConnectionMetrics; |
|
32 import ch.boye.httpclientandroidlib.io.HttpTransportMetrics; |
|
33 |
|
34 /** |
|
35 * Default implementation of the {@link HttpConnectionMetrics} interface. |
|
36 * |
|
37 * @since 4.0 |
|
38 */ |
|
39 public class HttpConnectionMetricsImpl implements HttpConnectionMetrics { |
|
40 |
|
41 public static final String REQUEST_COUNT = "http.request-count"; |
|
42 public static final String RESPONSE_COUNT = "http.response-count"; |
|
43 public static final String SENT_BYTES_COUNT = "http.sent-bytes-count"; |
|
44 public static final String RECEIVED_BYTES_COUNT = "http.received-bytes-count"; |
|
45 |
|
46 private final HttpTransportMetrics inTransportMetric; |
|
47 private final HttpTransportMetrics outTransportMetric; |
|
48 private long requestCount = 0; |
|
49 private long responseCount = 0; |
|
50 |
|
51 /** |
|
52 * The cache map for all metrics values. |
|
53 */ |
|
54 private HashMap metricsCache; |
|
55 |
|
56 public HttpConnectionMetricsImpl( |
|
57 final HttpTransportMetrics inTransportMetric, |
|
58 final HttpTransportMetrics outTransportMetric) { |
|
59 super(); |
|
60 this.inTransportMetric = inTransportMetric; |
|
61 this.outTransportMetric = outTransportMetric; |
|
62 } |
|
63 |
|
64 /* ------------------ Public interface method -------------------------- */ |
|
65 |
|
66 public long getReceivedBytesCount() { |
|
67 if (this.inTransportMetric != null) { |
|
68 return this.inTransportMetric.getBytesTransferred(); |
|
69 } else { |
|
70 return -1; |
|
71 } |
|
72 } |
|
73 |
|
74 public long getSentBytesCount() { |
|
75 if (this.outTransportMetric != null) { |
|
76 return this.outTransportMetric.getBytesTransferred(); |
|
77 } else { |
|
78 return -1; |
|
79 } |
|
80 } |
|
81 |
|
82 public long getRequestCount() { |
|
83 return this.requestCount; |
|
84 } |
|
85 |
|
86 public void incrementRequestCount() { |
|
87 this.requestCount++; |
|
88 } |
|
89 |
|
90 public long getResponseCount() { |
|
91 return this.responseCount; |
|
92 } |
|
93 |
|
94 public void incrementResponseCount() { |
|
95 this.responseCount++; |
|
96 } |
|
97 |
|
98 public Object getMetric(final String metricName) { |
|
99 Object value = null; |
|
100 if (this.metricsCache != null) { |
|
101 value = this.metricsCache.get(metricName); |
|
102 } |
|
103 if (value == null) { |
|
104 if (REQUEST_COUNT.equals(metricName)) { |
|
105 value = new Long(requestCount); |
|
106 } else if (RESPONSE_COUNT.equals(metricName)) { |
|
107 value = new Long(responseCount); |
|
108 } else if (RECEIVED_BYTES_COUNT.equals(metricName)) { |
|
109 if (this.inTransportMetric != null) { |
|
110 return new Long(this.inTransportMetric.getBytesTransferred()); |
|
111 } else { |
|
112 return null; |
|
113 } |
|
114 } else if (SENT_BYTES_COUNT.equals(metricName)) { |
|
115 if (this.outTransportMetric != null) { |
|
116 return new Long(this.outTransportMetric.getBytesTransferred()); |
|
117 } else { |
|
118 return null; |
|
119 } |
|
120 } |
|
121 } |
|
122 return value; |
|
123 } |
|
124 |
|
125 public void setMetric(final String metricName, Object obj) { |
|
126 if (this.metricsCache == null) { |
|
127 this.metricsCache = new HashMap(); |
|
128 } |
|
129 this.metricsCache.put(metricName, obj); |
|
130 } |
|
131 |
|
132 public void reset() { |
|
133 if (this.outTransportMetric != null) { |
|
134 this.outTransportMetric.reset(); |
|
135 } |
|
136 if (this.inTransportMetric != null) { |
|
137 this.inTransportMetric.reset(); |
|
138 } |
|
139 this.requestCount = 0; |
|
140 this.responseCount = 0; |
|
141 this.metricsCache = null; |
|
142 } |
|
143 |
|
144 } |