MessageFilter f = MessageFilter.create(INITIAL_RECEIPT_TIMEOUT, DMT.packetTransmit)
.setField(DMT.UID, _uid)
.addType(DMT.allSent)
.addType(DMT.sendAborted);
while (!_prb.allReceived()) {
Message m1 = _usm.waitFor(f);
// Get ready for the next packet
f = MessageFilter.create(RECEIPT_TIMEOUT, DMT.packetTransmit)
.setField(DMT.UID, _uid)
.addType(DMT.allSent)
.addType(DMT.sendAborted);
// The faster we finish the rest of this loop the better!
if ((m1 != null) && (m1.getSpec().equals(DMT.packetTransmit))) {
consecutiveMissingPacketReports = 0;
// packetTransmit received
int packetNo = m1.getInt(DMT.PACKET_NO);
BitArray sent = (BitArray) m1.getObject(DMT.SENT);
Buffer data = (Buffer) m1.getObject(DMT.DATA);
_prb.addPacket(packetNo, data);
// Remove it from rrmp if its in there
_recentlyReportedMissingPackets.remove(new Integer(packetNo));
// Skip checks except for 1 out of 16 packets until we get to the end
if ((_prb.numReceived() < (_prb.getNumPackets() - 16))
&& (packetNo % 16 != 0))
continue;
// Check that we have what the sender thinks we have
LinkedList missing = new LinkedList();
for (int x = 0; x < sent.getSize(); x++) {
if (sent.bitAt(x) && !_prb.isReceived(x)) {
// Sender thinks we have a block which we don't, but have we already
// re-requested it recently?
Long resendTime = (Long) _recentlyReportedMissingPackets.get(new Integer(x));
if ((resendTime == null) || (System.currentTimeMillis() > resendTime.longValue())) {
// Make a note of the earliest time we should resend this,
// based on the number of other packets we are already waiting for
long resendWait = System.currentTimeMillis()
+ (10*MAX_ROUND_TRIP_TIME + (_recentlyReportedMissingPackets.size() * MAX_SEND_INTERVAL));
_recentlyReportedMissingPackets.put(new Integer(x), (new Long(resendWait)));
missing.add(new Integer(x));
}
}
}
if (missing.size() > 0) {
Message mn = DMT.createMissingPacketNotification(_uid, missing);
_usm.send(_sender, mn);
consecutiveMissingPacketReports++;
if (missing.size() > 50) {
Logger.warning("Excessive packet loss : " + mn);
}
}
continue;
}
if ((m1 != null) && m1.getSpec().equals(DMT.sendAborted)) {
_prb.abort(m1.getInt(DMT.REASON), m1.getString(DMT.DESCRIPTION));
throw new RetrievalException(m1.getInt(DMT.REASON), m1.getString(DMT.DESCRIPTION));
}
if ((m1 == null) || (m1.getSpec().equals(DMT.allSent))) {
if (consecutiveMissingPacketReports >= MAX_CONSECUTIVE_MISSING_PACKET_REPORTS) {
_prb.abort(RetrievalException.SENDER_DIED, "Sender unresponsive to resend requests");
LinkedList rem = new LinkedList();
rem.add(_sender);
// TODO: This is a stupid work around for BlockTransferTest and needs to be fixed
if (RoutingTable.getRoutingTable() != null)
RoutingTable.getRoutingTable().removePeers(rem, "Failed to send data after acking request");
throw new RetrievalException(RetrievalException.SENDER_DIED,
"Sender unresponsive to resend requests");
}
LinkedList missing = new LinkedList();
for (int x = 0; x < _prb.getNumPackets(); x++) {
if (!_prb.isReceived(x)) {
missing.addFirst(new Integer(x));
}
}
Message mn = DMT.createMissingPacketNotification(_uid, missing);
_usm.send(_sender, mn);
consecutiveMissingPacketReports++;
if (missing.size() > 50) {
Logger.warning("Sending large missingPacketNotification to " + _sender
+ " due to packet receiver timeout after " + RECEIPT_TIMEOUT + "ms having received "