2011-05-13 04:24:42 -04:00
|
|
|
package org.moparscape.msc.ls.net;
|
2011-04-27 01:44:26 -04:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronized packet queue
|
|
|
|
*/
|
|
|
|
public class PacketQueue<T extends Packet> {
|
2011-06-25 01:44:53 -04:00
|
|
|
/**
|
|
|
|
* The list of packets in the queue
|
|
|
|
*/
|
|
|
|
private ArrayList<T> packets = new ArrayList<T>();
|
2011-04-27 01:44:26 -04:00
|
|
|
|
2011-06-25 01:44:53 -04:00
|
|
|
/**
|
|
|
|
* Adds a packet to the queue
|
|
|
|
*/
|
|
|
|
public void add(T p) {
|
|
|
|
synchronized (packets) {
|
|
|
|
packets.add(p);
|
|
|
|
}
|
2011-04-27 01:44:26 -04:00
|
|
|
}
|
|
|
|
|
2011-06-25 01:44:53 -04:00
|
|
|
/**
|
|
|
|
* Returns the packets currently in the list and removes them from the
|
|
|
|
* backing store
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public List<T> getPackets() {
|
|
|
|
List<T> tmpList;
|
|
|
|
synchronized (packets) {
|
|
|
|
tmpList = (List<T>) packets.clone();
|
|
|
|
packets.clear();
|
|
|
|
}
|
|
|
|
return tmpList;
|
2011-04-27 01:44:26 -04:00
|
|
|
}
|
|
|
|
|
2011-06-25 01:44:53 -04:00
|
|
|
/**
|
|
|
|
* Returns if there is packets to process
|
|
|
|
*/
|
|
|
|
public boolean hasPackets() {
|
|
|
|
return !packets.isEmpty();
|
|
|
|
}
|
2011-04-27 01:44:26 -04:00
|
|
|
}
|