import java.io.*; import java.net.*; /* Gotta have access to the following - socket for receiving 802.11 message - UltrasoundListener object for getting boolean decision as to the presence of ultrasound. - Parent object for calling update() method when there is a positive decision. */ /** * The Listener class is a thread that first listens for an 802.11 * message, then listens for a certain period of time for an ultrasound * pulse. Before terminating, the Listener thread calls an update method * to let the caller class know that there is new room information. The * Listener class has a reference to the object that instantiated it so * that it has access to the update method. The Listener class also has * a reference to the UltrasoundListener object and the DatagramSocket * object of the object that instantiated it so that the Listener thread * has access to the appropriate communication and ultrasound detection * systems. * * @author Tony Offer * @author Chris Palistrant * @version 1.0 */ public class Listener extends Thread { // Reference to the object that instantiated this thread. The object // should have an update() method so that the Listener thread can // notify the object of a newly received 802.11/ultrasound pair. private Object notifyObj; // Reference to the socket that this Listener thread will use to // receive 802.11 messages. private DatagramSocket socket; // Reference to the UltrasoundListener object that is being used // to listen for ultrasound. private UltrasoundListener ultrasound; /** * This is the constructor for a Listener object. In order for a * Listener to function, it must have access to some external * objects since a Listener is designed mostly to service the * external object that created it. * * @param outsideObj Reference to the external object that created * this Listener thread. The external object * must have an update() method. * @param outsideSocket Reference to the datagram socket this * Listener thread should use to receive * 802.11 messages. * @param outsideUltrasound Reference to the UltrasoundListener * object this Listener thread should * use to listen for ultrasound pulses. */ public Listener(Object outsideObj, DatagramSocket outsideSocket, UltrasoundListener outisdeUltrasound) { notifyObj = outsideObj; socket = outsideSocket; ultrasound = outsideUltrasound; // Initialize 'buf' with the maximum amount of possible bytes // that could be received from this socket. try { buf = new byte[socket.getSendBufferSize()]; } catch (SocketException se) { se.printStackTrace(); } } /** * All the processing for a Listener object is performed here. * The thread first waits to receive an 802.11 message, then starts * another Listener thread, then listens for ultrasound, then * finally calls the notifyObj's update() method with the new * information it just received. */ public void run() { String msg = null; boolean detected; // Create a new packet for receiving data from the socket and // block on the socket.receive() method until an 802.11 message // is received, storing the bytes as a string in 'msg'. try { // Initialize 'buf' with the maximum amount of possible bytes // that could be received from this socket. byte [] buf = new byte[socket.getSendBufferSize()]; DatagramPacket packet = new DatagramPacket(buf, buf.length); System.out.println("Receiving... "); socket.receive(packet); msg = new String(packet.getData()); } catch (IOException e) { e.printStackTrace(); } catch (SocketException se) { se.printStackTrace(); } // Instantiate and run a new Listener thread. This is done so // that any more incoming 802.11 messages will not be ignored // while this thread is listening for ultrasound. new Listener(notifyObj, socket, ultrasound).start(); // Listen for ultrasound. This method will listen (and block) // for a certain amount of time to the sound being recorded from // the microphone. The 'analyze' method returns a boolean based // on whether or not ultrasound was detected in the recording. ultrasound.listen(); detected = ultrasound.analyze(); // Notify the external object of new information pertaining to // the presence or absence of ultrasound in relation to the // received 802.11 message. notifyObj.update(msg, detected); } }