package mil.navy.nps.logger; import java.awt.*; import java.net.*; import java.io.*; import sun.net.*; //import java.util.*; import java.awt.event.*; import mil.navy.nps.dis.*; import mil.navy.nps.util.*; import mil.navy.nps.testing.*; import mil.navy.nps.disEnumerations.*; /** * Plays back pdus in the array at normal speed. * @authors: Millie Ives and David Holland, DMcG * @version Version 3 */ public class Play implements Runnable { PduPlayer pduPlayer; // ptr back to pdu player object InetAddress address; // address we send to int port; // port we send to int ttl; // ttl for multicast String fileName; // file name of file we read from boolean runDone = false; boolean looping = false; // loop to start of pdus when you reach the end? BehaviorStreamBufferUDP bsbUDP; public void setRunDone(boolean pStatus) { runDone = pStatus; } /** * Constructor for Play Thread */ public Play(PduPlayer pPlayer, // ptr pack to player String intAddress, // string format inet address int portNumber, // string format port int pTtl) // string format ttl { try { pduPlayer = pPlayer; address = InetAddress.getByName(intAddress); port = portNumber; ttl = pTtl; bsbUDP = new BehaviorStreamBufferUDP(intAddress, port); bsbUDP.suspendReading(); // we only want to send, don't care about reading bsbUDP.setTTL((byte)ttl); } catch(Exception e) { System.out.println("Failed to create Player object " + e); } } /** * Called by the thread to start the process. Loops until told to stop */ public void run() { ProtocolDataUnit pdu; int maxIndex; long previousTime = 0; // guaranteed to always be before now long thisTime = 0; // timestamp of this pdu long difference; // time delta between this pdu and previous one boolean firstTime = true; // hack int index = 0; maxIndex = pduPlayer.getPduBuffer().size(); while(!runDone) { index = pduPlayer.getPduIndex(); //System.out.println("got index of " + index); // Check for whether we're at the end if(index >= maxIndex) { if(looping == true) { pduPlayer.setPduIndex(0); index = 0; firstTime = true; continue; } else { runDone = true; //System.out.println("Exiting from play run"); continue; } } pdu = (ProtocolDataUnit)pduPlayer.getPduBuffer().elementAt(index); // Find the timestamp on the current PDU thisTime = pdu.getTimestamp().longValue(); // Bad hack if(firstTime) { firstTime = false; previousTime = thisTime; } // How much time between the last one and this one? We should wait that amount // of time. difference = thisTime - previousTime; //System.out.println(pdu); //System.out.println("Time difference is " + difference + " index is " + index); // Possiblities of screw-ups: non-sequential timestamps. if(difference < 0) difference = 0; try { Thread.sleep(difference); } catch(Exception e) { System.out.println(e); } // The time of this pdu is now the time of the previous pdu previousTime = thisTime; bsbUDP.sendPdu(pdu); pduPlayer.setPduIndex(index+1); } // end while // We're done playing; take ball and go home. But first notify the // pdu player that we're done, so the button will switch to some // neutral position. // Clean up threads and sockets we created bsbUDP.shutdown(); bsbUDP.cleanup(); pduPlayer.performStopButtonHit(); } // end run }