import twinfeats.io.*; import java.util.*; import java.io.*; import twinfeats.conf.core.*; public class ConfReplay extends twinfeats.io.WorkThread implements twinfeats.io.Work { String conf; String user; long stamp; boolean playing = true; long speed; FileInputStream fis; ObjectInputStream ois; // DataInputStream dis; boolean stop; Comm comm; long timeZero; public ConfReplay(String fn,String un, Comm c) throws FileNotFoundException, IOException { user = un; comm = c; fis = new FileInputStream(fn); ois = new ObjectInputStream(fis); // dis = new DataInputStream(fis); conf = fn; setWork(this); start(); } public void doWork() { CommMessage msg; CommMessageHeader hdr=null; long lasttime=0; long thistime; long delay; // int dl; // byte[] b; while(!stop) { synchronized(this) { try { while(!playing && !stop) wait(); } catch (InterruptedException ie) { ie.printStackTrace(); } } if (stop) return; try { /* dl = dis.readInt(); b = new byte[dl]; dis.readFully(b); hdr = (CommMessageHeader)CommMessageHeader.fromBytes(b); */ hdr = (CommMessageHeader)ois.readObject(); if (hdr == null) { stopReplay(); return; } /* dl = dis.readInt(); b = new byte[dl]; dis.readFully(b); msg = (CommMessage)CommMessage.fromBytes(b); */ msg = (CommMessage)ois.readObject(); if (msg == null) { stopReplay(); return; } hdr.setDest(user); msg.setHeader(hdr); thistime = msg.getStamp(); if (lasttime == 0) { lasttime = thistime; timeZero = thistime; } delay = thistime-lasttime; if (speed > 0) delay /= (speed+1); else if (speed < 0) delay *= (1-speed); if (delay > 0) { try { Thread.sleep(delay); } catch (InterruptedException ie) { ie.printStackTrace(); } } msg.setStamp(thistime-timeZero); lasttime = thistime; comm.send(msg); msg = new CommMessage("ConfReplay",user,ConfBean.VCR,new Long(thistime-timeZero)); comm.send(msg); } catch (EOFException eof) { stopReplay(); return; } catch (Exception e) { e.printStackTrace(); stopReplay(); return; } } } public synchronized void pauseReplay() { playing = false; } public synchronized void fastForwardReplay() { speed++; } public synchronized void slowReplay() { speed--; } public synchronized void resumeReplay() { playing = true; notify(); } public synchronized void stopReplay() { CommMessage msg = new CommMessage("ConfReplay",user,ConfBean.VCR,"End"); comm.send(msg); stop = true; notify(); } }