Files
briar/components/net/sf/briar/transport/FrameScheduler.java
2011-10-27 17:52:03 +01:00

45 lines
1.3 KiB
Java

package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A thread that calls the writeFullFrame() method of a PaddedConnectionWriter
* at regular intervals. The interval between calls is determined by a target
* output rate. If the underlying output stream cannot accept data at the
* target rate, calls will be made as frequently as the output stream allows.
*/
class FrameScheduler extends Thread {
private static final Logger LOG =
Logger.getLogger(FrameScheduler.class.getName());
private final PaddedConnectionWriter writer;
private final int millisPerFrame;
FrameScheduler(PaddedConnectionWriter writer, int bytesPerSecond) {
this.writer = writer;
millisPerFrame = bytesPerSecond * 1000 / MAX_FRAME_LENGTH;
}
@Override
public void run() {
long lastCall = System.currentTimeMillis();
while(true) {
long now = System.currentTimeMillis();
long nextCall = lastCall + millisPerFrame;
if(nextCall > now) {
try {
Thread.sleep(nextCall - now);
} catch(InterruptedException e) {
if(LOG.isLoggable(Level.WARNING))
LOG.warning(e.getMessage());
}
}
lastCall = System.currentTimeMillis();
if(!writer.writeFullFrame()) return;
}
}
}