stateblock TrackSendRate augments RNetwork {
requires SystemTime;
addstate RNetwork.bytesSent: int = 0;
addstate RNetwork.timeQuantum: long;
addstate RNetwork.timeStart: long;
helper updateTimer () {
if (naccio.library.Time.getCurrentTime () - timeStart > timeQuantum) {
// The current time quantum is finished, reset
// Note: doesn't deal with number wrap araound!
bytesSent = 0;
timeStart = naccio.library.Time.getCurrentTime ();
// debugMessage ("Update timer: " + timeStart);
} else {
// debugMessage ("Update timer. Not a new quantum.");
}
}
helper waitForQuantum () {
debugMessage ("Waiting for time quantum loop: " + timeStart + " / " +
naccio.library.Time.getCurrentTime ());
while (naccio.library.Time.getCurrentTime () - timeStart < timeQuantum) {
long sleeptime;
sleeptime = timeQuantum - (naccio.library.Time.getCurrentTime () - timeStart);
naccio.library.Time.sleep (sleeptime);
// debugMessage ("Waiting for time quantum: " + sleeptime + " / " +
// naccio.library.Time.getCurrentTime ());
}
updateTimer ();
util.Assert.assert (bytesSent == 0);
}
precode initialize () {
timeStart = RSystem.getCurrentTime ();
}
precode postSend (remote: RNetAddress, nbytes: int) {
updateTimer ();
// debugMessage ("Network send: " + nbytes);
bytesSent += nbytes;
}
}
Naccio Home Page
University of Virginia, Computer Science