View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
4    * 
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: RemoteDeviceInfo.java 2471 2008-12-01 03:44:20Z skarzhevskyy $
24   */
25  package net.sf.bluecove;
26  
27  import java.util.Hashtable;
28  
29  import javax.bluetooth.RemoteDevice;
30  import javax.bluetooth.ServiceRecord;
31  
32  import net.sf.bluecove.util.TimeStatistic;
33  
34  /**
35   * 
36   */
37  public class RemoteDeviceInfo {
38  
39  	public static Hashtable devices = new Hashtable();
40  
41  	public static Hashtable services = new Hashtable();
42  
43  	public String name;
44  
45  	public RemoteDevice remoteDevice;
46  
47  	public int discoveredCount;
48  
49  	public long discoveredFirstTime;
50  
51  	public long discoveredLastTime;
52  
53  	private TimeStatistic deviceDiscovery = new TimeStatistic();
54  
55  	public static TimeStatistic deviceInquiryDuration = new TimeStatistic();
56  
57  	private TimeStatistic serviceSearch = new TimeStatistic();
58  
59  	public static TimeStatistic allServiceSearch = new TimeStatistic();
60  
61  	public long serviceDiscoveredFirstTime;
62  
63  	public long serviceDiscoveredLastTime;
64  
65  	public TimeStatistic serviceDiscovered = new TimeStatistic();
66  
67  	public long variableData;
68  
69  	public long variableDataCheckLastTime;
70  
71  	public boolean variableDataUpdated = false;
72  
73  	public static synchronized void clear() {
74  		devices = new Hashtable();
75  		services = new Hashtable();
76  		allServiceSearch.clear();
77  		deviceInquiryDuration.clear();
78  	}
79  
80  	public static synchronized RemoteDeviceInfo getDevice(RemoteDevice remoteDevice) {
81  		String addr = remoteDevice.getBluetoothAddress().toUpperCase();
82  		RemoteDeviceInfo devInfo = (RemoteDeviceInfo) devices.get(addr);
83  		if (devInfo == null) {
84  			devInfo = new RemoteDeviceInfo();
85  			devInfo.name = TestResponderClient.niceDeviceName(addr);
86  			devInfo.remoteDevice = remoteDevice;
87  			devices.put(addr, devInfo);
88  		}
89  		return devInfo;
90  	}
91  
92  	public static synchronized void deviceFound(RemoteDevice remoteDevice) {
93  		RemoteDeviceInfo devInfo = getDevice(remoteDevice);
94  		long now = System.currentTimeMillis();
95  		if (devInfo.discoveredCount == 0) {
96  			devInfo.discoveredFirstTime = now;
97  			devInfo.deviceDiscovery.add(0);
98  		} else {
99  			devInfo.deviceDiscovery.add(now - devInfo.discoveredLastTime);
100 		}
101 		devInfo.remoteDevice = remoteDevice;
102 		devInfo.discoveredCount++;
103 		devInfo.discoveredLastTime = now;
104 	}
105 
106 	public static synchronized void deviceServiceFound(RemoteDevice remoteDevice, long variableData) {
107 		RemoteDeviceInfo devInfo = getDevice(remoteDevice);
108 		long now = System.currentTimeMillis();
109 		if (devInfo.serviceDiscovered.count == 0) {
110 			devInfo.serviceDiscoveredFirstTime = now;
111 			devInfo.serviceDiscovered.add(0);
112 		} else {
113 			devInfo.serviceDiscovered.add(now - devInfo.serviceDiscoveredLastTime);
114 		}
115 		devInfo.remoteDevice = remoteDevice;
116 		devInfo.serviceDiscoveredLastTime = now;
117 		// if (variableData != 0) {
118 		// long frequencyMSec = now - devInfo.variableDataCheckLastTime;
119 		// if ((devInfo.variableData != 0) && (frequencyMSec > 1000 * 120)) {
120 		// devInfo.variableDataCheckLastTime = now;
121 		// boolean er = false;
122 		// if (variableData == devInfo.variableData) {
123 		// Logger.warn("not updated " + variableData);
124 		// TestResponderClient.failure.addFailure("not updated " + variableData
125 		// + " on " + devInfo.name);
126 		// er = true;
127 		// }
128 		// if (!er) {
129 		// devInfo.variableDataUpdated = true;
130 		// Logger.info("Var info updated, " + variableData);
131 		// }
132 		// } else if (devInfo.variableData != variableData) {
133 		// if (devInfo.variableData == 0) {
134 		// Logger.info("Var info set, " + variableData);
135 		// } else {
136 		// Logger.info("Var info updated, " + variableData);
137 		// }
138 		// devInfo.variableData = variableData;
139 		// devInfo.variableDataCheckLastTime = now;
140 		// devInfo.variableDataUpdated = true;
141 		// }
142 		// }
143 	}
144 
145 	public static synchronized void searchServices(RemoteDevice remoteDevice, boolean found, long servicesSearch) {
146 		RemoteDeviceInfo devInfo = getDevice(remoteDevice);
147 		devInfo.serviceSearch.add(servicesSearch);
148 		allServiceSearch.add(servicesSearch);
149 	}
150 
151 	public static void discoveryInquiryFinished(long discoveryInquiry) {
152 		deviceInquiryDuration.add(discoveryInquiry);
153 	}
154 
155 	public static long allAvgDeviceInquiryDurationSec() {
156 		return deviceInquiryDuration.avgSec();
157 	}
158 
159 	public static long allAvgServiceSearchDurationSec() {
160 		return allServiceSearch.avgSec();
161 	}
162 
163 	public long avgDiscoveryFrequencySec() {
164 		return deviceDiscovery.avgSec();
165 	}
166 
167 	public long avgServiceDiscoveryFrequencySec() {
168 		return serviceDiscovered.avgSec();
169 	}
170 
171 	public long avgServiceSearchDurationSec() {
172 		return serviceSearch.durationMaxSec();
173 	}
174 
175 	public long serviceSearchSuccessPrc() {
176 		if ((serviceSearch.count) == 0) {
177 			return 0;
178 		}
179 		return (100 * serviceDiscovered.count) / (serviceSearch.count);
180 	}
181 
182 	public static void saveServiceURL(ServiceRecord serviceRecord) {
183 		services.put(serviceRecord.getConnectionURL(Configuration.getRequiredSecurity(), false), serviceRecord);
184 	}
185 }