}
long startWindow = currentTimeMillis();
int payLoadSize = 0;
Payload p = new Payload(m == null ? heartBeat : m);
if (wait) {
while (!queue.isEmpty() && payLoadSize < MAXIMUM_PAYLOAD_SIZE
&& (currentTimeMillis() - startWindow) < transmissionWindow) {
p.addMessage(queue.poll());
payLoadSize++;
}
if ((lastTransmission = currentTimeMillis()) - lastEnqueue > transmissionWindow) {
transmissionWindow = (lastTransmission - lastEnqueue);
} else {
transmissionWindow = DEFAULT_TRANSMISSION_WINDOW;
}
}
return p;
}
catch (InterruptedException e) {
return new Payload(heartBeat);
}
}