The ClientlibExample.java file contains all the source code for the sample application. For convenience, we list the source code here. The code includes instructive comments that explains how to use this template to create your own Cricket application.
package clientlibexample; import gnu.getopt.*; // OPTIONAL: package that process command line args import java.util.*; // All Cricket apps should include the following two paths import cricketdaemon.clientlib.*; import cricketdaemon.clientlib.data.*; /** * An example of how to use the CricketDaemon Java clientlib. * * It simply prints a line containing space and position * information whenever they are updated by the CricketDaemon * processing stack. * * @author Allen Miu */ // A Cricket callback handler class implements // the Callback interface class ClientlibExample implements Callback { /** * Handle to cricket */ Broker cricket; /** * Data structures */ String currentSpace = null; Position currentPosition = null; public ClientlibExample() { /** * Established TCP connection to the CricketDaemon running on * localhost. The ServerBroker will trigger a callback * whenever the CricketDaemon decides to push information out to * the clients. An application may the alternate * ServerBroker constructor: * * public ServerBroker(String addr, int portNum) * * to connect to a CricketDaemon running on <code>addr</code> * (may be numeric IP or hostname), using portNum (normally, * should use 5001). */ cricket = new ServerBroker(); /** * Create a bitmask specifying the type of Cricket information * that should trigger a callback on this object. The following * table shows all the valid bit types: * ALL selects all types * SPACE current spatial location * DEVICECOORD current position coordinates * BEACONSHEARDLIST set of beacons heard in history window * BEACONSTAT distance measurement to a beacon * POSERR exceptions in position computation */ BitSet mask = new BitSet(); mask.set(Broker.SPACE); mask.set(Broker.DEVICECOORD); mask.set(Broker.POSERR); /** * Register this object's callback handler with ServerBroker. * An application can create and register multiple handlers. * Here, we register only one handler. */ cricket.register(this, mask); cricket.start(); // fork thread, runs forever System.out.println("ClientlibExample created"); } /** * The ServerBroker object invokes the callback handler * with location updates contained in <code>data</code> and * <code>mask</code> indicates which location type()s is(are) * readable in \texttt{CricketData}. All Cricket callback handler * classes must implement this method. */ synchronized public void callback(CricketData data, BitSet mask) { boolean update = false; // check for space updates if(mask.get(Broker.SPACE)) { BeaconRecord cs = data.getCurrentSpaceObject(); if(cs != null && cs.uniqueName != null) { if(currentSpace == null || !(currentSpace.equals(cs.uniqueName))) { // if space string is different from before, update it update = true; currentSpace = cs.uniqueName; } } } // check for coordinate updates if(mask.get(Broker.DEVICECOORD) || mask.get(Broker.POSERR)) { Position p = data.getDevicePosition(); if(p != null) { if(currentPosition == null || !(currentPosition.equals(p))) { if(p.invalid()) { // CricketDaemon cannot produce a position estimate // because it does not hear enough different beacons System.err.println("ignoring invalid position"); } else { // if the coordinates are different from before, // update them update = true; currentPosition = p; } } } } if(update) printState(); } /** * Prints the current space and coorinates of the Cricket listener */ private void printState() { System.out.println("space="+currentSpace+" pos="+currentPosition); } public static void main(String[] args) { /* OPTIONAL: example of gnu-style getopt to parse cmd line args */ Getopt g = new Getopt("ClientlibExample", args, "c:"); int c; try { while((c = g.getopt()) != -1) { switch(c) { case 'c': System.out.println("wassup! "+g.getOptarg()); break; } } } catch (Exception e) { //usage(); } // The ClientlibExample constructor launches the ServerBroker // thread so all we have to do is to invoke it. ClientlibExample client = new ClientlibExample(); } }