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.*; public class Rewind 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 Rewind(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 Rewind 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 startIndex; 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 runDone = false; System.out.println("****Rewinding"); while(!runDone) { index = pduPlayer.getPduIndex(); //System.out.println("index is " + index); // Check for whether we're at the end if(index <= 1) { runDone = true; //System.out.println("Exiting from rewind run"); continue; } pdu = (ProtocolDataUnit)pduPlayer.getPduBuffer().elementAt(index-1); // 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); } // 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 }