package mil.navy.nps.logger; import java.awt.*; import java.net.*; import java.io.*; 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.*; /** * Fast forwards a thread. Plays items in a vector at top speed.

* * We have some PDUs loaded in memory. The ffwd button runs through those * pdus at top speed, without regard to the timestamps on the PDUs, sending * them as quickly as possible.

* * This is very similar to "play". The only difference is that this pays no * attention to the timestamps saved in the PDUs. * * @authors: Millie Ives and David Holland, DMcG * @version Version 3 */ public class FFwd extends Object implements Runnable { public static final int DEFAULT_SPEEDUP = 10; 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 FFwd(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; port = portNumber; ttl = pTtl; address = InetAddress.getByName(intAddress); bsbUDP = new BehaviorStreamBufferUDP(intAddress, portNumber); 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, or until it * runs of of PDUs. */ public void run() { ProtocolDataUnit pdu; int index = 0; 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 maxIndex = pduPlayer.getPduBuffer().size(); while(!runDone) { index = pduPlayer.getPduIndex(); System.out.println("index is " + index); // Check for whether we're at the end if(index >= maxIndex) { if(looping == true) { pduPlayer.setPduIndex(0); } 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; difference = difference / DEFAULT_SPEEDUP; // accelerated speed 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 }