📄 usbspotchecker.java
字号:
/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is * described in this document. In particular, and without limitation, these intellectual property rights may * include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents * or pending patent applications in the U.S. and in other countries. * * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. * standard license agreement and applicable provisions of the FAR and its supplements. * * Use is subject to license terms. * * This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo and * Java are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. * * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits r?serv?s. * * Sun Microsystems, Inc. d?tient les droits de propri?t? intellectuels relatifs ? la technologie incorpor?e dans * le produit qui est d?crit dans ce document. En particulier, et ce sans limitation, ces droits de propri?t? * intellectuelle peuvent inclure un ou plus des brevets am?ricains list?s ? l'adresse http://www.sun.com/patents * et un ou les brevets suppl?mentaires ou les applications de brevet en attente aux Etats - Unis et dans les * autres pays. * * L'utilisation est soumise aux termes du contrat de licence. * * Cette distribution peut comprendre des composants d?velopp?s par des tierces parties. * Sun, Sun Microsystems, le logo Sun et Java sont des marques de fabrique ou des marques d?pos?es de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */package com.sun.spot.spotworld.usb;import com.sun.spot.spotworld.common.LocaleUtil;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Hashtable;/** * NOT COMPLETELY IMPLEMENTED, AND NOT USED BY SPOTWORLD. THIS IS A POLACEHOLDER CLASS FOR NOW. * * To use this class, the idea is this. * You implement IStateListener. * You ask to listen to the sigleton instance of this class. * Your noteState(Object obj) method will be called with this single instance as the argument. * THIS CALLBACK HAPPENS WHEN THERE IS A CHANGE, PLUS THE FIRST TIME YOU SIGN UP AS A LISTENER. * Inside your noteState() method, ask this instance about its state. * * For Example * * public class MyClass implements IStateListener { * * public void init(){ * USBSpotChecker.getInstance().addListener(this); * } * * public void noteState(Object obj){ * USBSpotChecker x = (USBSpotChecker) obj; * port = x.getUSBPortForAddress(myAddressStringInDottedHex); * enumeration<String> bases = x.getBasestationAddresses(); * } * } * * NOTE: even though the protocol suggests this can find multiple basestations if there happen to be * many attached, it really only finds at most one. * * NOTE ON SEMANTICS * * This object uses 3 sources of information to help determine which SPOT is connected to which port. * * The bin/spotfinder application can generate a list of USB ports and the spot (IEEE address) attached there. * This is pretty good, but it doesn't distinguish basestations from nonbasestation spots. * * To do that, you can run the SpotSelector appliation which activeley probes the SPOT through the given port to * determine if it is a basestation or regular SPOT. However, this is VERY slow, so we have to resort to -- sigh -- complexity. * * Consequntly we ASUUME that once a basestation, always a basestation, and keep a persistent list un the SPOTWorld properties file of IEEE addresses * that have been encountered, and weather or not that SPOT is a basestion. You can see these entries in the properties file. * * So for now, IF YOU EVER CHANGE A SPOT INTO BEING A BASESTATION (or visa versa) simply edit the Spotworld properties file and remove the associated entry. The * system will find the thing, run the probe, and make a new entry accordingly. * * @author randy */public class USBSpotChecker { public USBSpotChecker() { init(); }//// private static USBSpotChecker soleInstance;//// private Thread pollThread;// private boolean running = false;// private Vector<IStateListener> listeners = new Vector<IStateListener>();// private int checkInterval = 7000; //Milliseconds// private Hashtable<String, String> addrToUSBPortMap; //Dictionary stores IEEE adress strings mapped to USB port addresses for ALL SPOTs (regular and basestation). private Hashtable<String, String> addrToUSBPortMapBS; //Dictionary stores IEEE adress strings mapped to USB port addresses for known basestations only.// private boolean firstTime; // The first time thcheck happens this is true, else false//// public static USBSpotChecker getInstance(){// if(soleInstance == null){// soleInstance = new USBSpotChecker();// }// return soleInstance;// }//// /************* RECOMMENDED PUBLIC CALLS ************///// /*// * Pass in a SPOT address, and get back null (if not on USB) or the port// */// public String getUSBPortForAddress(String addr){// return addrToUSBPortMap.get(addr); //the get() API claims to return null if absent.// }//// /*// * Get an enumeration of all basestations IEEE addresses. Each address can// * be used to find the port, via getUSBPortForAddress().// */// public Enumeration<String> getBasestationAddresses(){// return (Enumeration<String>) (addrToUSBPortMapBS.keys());// }//// /*// * Get an enumeration of all USB SPOT IEEE addresses. Each address can// * be used to find the port, via getUSBPortForAddress().// */// public Enumeration<String> getAllUSBSpotAddresses(){// return (Enumeration<String>) (addrToUSBPortMap.keys());// }//// /************* END RECOMMENDED PUBLIC CALLS ************/// public void init(){ addrToUSBPortMap = new Hashtable<String, String>(); addrToUSBPortMapBS = new Hashtable<String, String>(); // doCheck(); //set initial state firstTime = true; }//// public void start(){// createAndStartPollThread();// }//// public void addListener(IStateListener obj){// listeners.add(obj);// obj.noteState(this);// }//// public void removeLisetener(IStateListener obj){// listeners.remove(obj);// }//// public void notifyListeners(){// for(IStateListener obj : listeners){// obj.noteState(this);// }// }//// public Vector<String> getKnownBasestationAddresses(){// Vector<String> r = new Vector<String>();// int i = 0;// String addr = "";// while(addr != null){// addr = SpotWorld.getSPOTWorldProperties().getProperty("known.basestation." + i);// if(addr != null) r.add(addr);// i++;// }// return r;// }//// public Vector<String> getKnownNonBasestationAddresses(){// Vector<String> r = new Vector<String>();// int i = 0;// String addr = "";// while(addr != null){// addr = SpotWorld.getSPOTWorldProperties().getProperty("known.nonbasestation." + i);// if(addr != null) r.add(addr);// i++;// }// return r;// }//// public Vector<String> getKnownAddresses(){// Vector<String> r = new Vector<String>();// int i = 0;// String addr = "";// while(addr != null){// addr = SpotWorld.getSPOTWorldProperties().getProperty("known.nonbasestation." + i);// if(addr != null) r.add(addr);// addr = SpotWorld.getSPOTWorldProperties().getProperty("known.basestation." + i);// if(addr != null) r.add(addr);// i++;// }// return r;// }//// public void createAndStartPollThread(){// Runnable r = new Runnable(){// public void run() {// long t;// while(isRunning()){// t = System.currentTimeMillis();// Hashtable<String, String> oldMap = (Hashtable<String, String>) addrToUSBPortMap.clone();// if(updateAndCheckForChange(oldMap)){// notifyListeners();// }// t = System.currentTimeMillis() - t;// System.out.println("[" + getClass().getSimpleName() +// "] " + LocaleUtil.getString("check took") + " " + ((double) t / 1000.0 ) + " " +// LocaleUtil.getString("seconds") + ":" + LocaleUtil.getString("basestations") + " / " +// LocaleUtil.getString("total SPOTs") + " = " +// addrToUSBPortMapBS.size() + " / " + addrToUSBPortMap.size());// Utils.sleep(checkInterval);// }// }// };// pollThread = new Thread(r);// setRunning(true);// pollThread.start();// }// /*// * This is a heavy lifting method.// * 1] Update tha value of addrToUSBPortMap, and// * 2] See if there are any never-before-seen spots (not "known" to be basestaions or nonbasestations).// * 3] If there are new ones, make new entries (properties) as basestation or not.// * 4] Finally, return true if the argument oldMap is not equal to the new addrToUSBPortMap.// */// public boolean updateAndCheckForChange(Hashtable<String, String> oldMap){// //1] update addrToUSBPortMap and addrToUSBPortMapBS// Vector<String> allAddrs = getKnownAddresses();// Vector<String> bsAddrs = getKnownBasestationAddresses();// addrToUSBPortMap = probeForAddrsAndPorts();// System.out.println(":::::::::::::::::: light weight probe gives hashtable :::::::::::: " + addrToUSBPortMap);// for(String bsa : bsAddrs){// if(addrToUSBPortMap.containsKey(bsa)) addrToUSBPortMapBS.put(bsa, addrToUSBPortMap.get(bsa));// }// // 2] Check addrToUSBPortMap keys, and addrToUSBPortMapBS keys for never-before-know-to-exist SPOTs.// for(String k : addrToUSBPortMap.keySet()){// if ( (! allAddrs.contains(k)) ){// System.out.println("New spot, addr = " + k + ", not in known (recorded) spots list, which = " + allAddrs );// recordNewSPOT(k, addrToUSBPortMap.get(k));// }// }// if(firstTime){// firstTime = false;// return true;// }// //If there are more or fewer basestations or ports, return true// if(oldMap.size() != addrToUSBPortMap.size()) return true;// // If a key is not in the old but not new, return true, or if the keys value is different// for(String k : oldMap.keySet()){// if(! addrToUSBPortMap.containsKey(k)) return true;// if(! addrToUSBPortMap.get(k).equals(oldMap.get(k))) return true;// }// // The previous two checks make this check redundant, but I don't quite trust my logic'in saying that.// for(String k : addrToUSBPortMap.keySet()){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -