/* The Java RTP/I library, Version 0.1 alpha.
*
* This library provides the functionality of RTP/I as it is specified in
* Internet Draft draft-mauve-rtpi-00.txt.
*
* Copyright (C) 2000 Martin Mauve
* University of Mannheim / Germany
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Martin Mauve
*
* e-mail:
* mauve@informatik.uni-mannheim.de
*
* paper mail:
* Martin Mauve
* University of Mannheim
* Lehrstuhl Praktische Informatik IV
* L15, 16
* 68131 Mannheim
* Germany
*/
package rtpi.reliability.unreliableUdpMulticast;
import rtpi.packets.RtpiDataPacket;
import java.util.LinkedList;
import java.util.ListIterator;
// We expect that the first packet of an ADU starts with fragment count 0 and the the
// fragment count is increasing by one for each packet towards the last packet of an ADU!
class RtpiDataPacketBuffer {
LinkedList buffer = new LinkedList();
long lastPacketReceivedAt=0;
RtpiDataPacketBuffer(RtpiDataPacket packet) {
lastPacketReceivedAt = System.currentTimeMillis();
buffer.add(packet);
}
void put(RtpiDataPacket packet) {
lastPacketReceivedAt = System.currentTimeMillis();
RtpiDataPacket packet2 = (RtpiDataPacket) buffer.getLast();
if (packet2.getFragmentCount() < packet.getFragmentCount()) { // this should be the most common case!
buffer.add(packet);
return;
}
int position=buffer.size();
do {
position--;
packet2 = (RtpiDataPacket) buffer.get(position);
} while (packet2.getFragmentCount() > packet.getFragmentCount() && position!=0);
if (position==0 && packet2.getFragmentCount() > packet.getFragmentCount()) {
position--;
}
buffer.add(position+1, packet);
}
boolean timedOut(int millis) {
if (System.currentTimeMillis()-lastPacketReceivedAt>millis) {
return true;
}
return false;
}
boolean isComplete() {
ListIterator it = buffer.listIterator(0);
if (!it.hasNext()) {
return false;
}
RtpiDataPacket packet=(RtpiDataPacket) it.next();
int fragmentCount=packet.getFragmentCount();
if (fragmentCount!=0) {
return false;
}
while(it.hasNext()) {
packet = (RtpiDataPacket)it.next();
if (packet.getFragmentCount()!=fragmentCount+1) {
return false;
}
fragmentCount++;
}
if (packet.getEnd()!=1) {
return false;
}
return true;
}
LinkedList getPackets() {
return buffer;
}
}