Rev 15 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 15 | Rev 73 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * MbPacketQueue.java |
2 | * MbPacketQueue.java |
| 3 | * |
3 | * |
| 4 | * Created on den 24 augusti 2003, 13:03 |
4 | * Created on den 24 augusti 2003, 13:03 |
| 5 | */ |
5 | */ |
| 6 | package Macbeth.System; |
6 | package Macbeth.System; |
| 7 | 7 | ||
| 8 | import java.util.*; |
8 | import java.util.*; |
| 9 | 9 | ||
| 10 | /** |
10 | /** |
| 11 | * This class represents a packet-queue, onto which incoming |
11 | * This class represents a packet-queue, onto which incoming |
| 12 | * packets will be put, and from which kernels will fetch |
12 | * packets will be put, and from which kernels will fetch |
| 13 | * packets for processing. |
13 | * packets for processing. |
| 14 | * @author Jimmy |
14 | * @author Jimmy |
| 15 | */ |
15 | */ |
| 16 | public class MbPacketQueue { |
16 | public class MbPacketQueue { |
| 17 | //queue is implemented using a linked list |
17 | //queue is implemented using a linked list |
| 18 | private LinkedList queue; |
18 | private LinkedList queue; |
| 19 | 19 | ||
| 20 | /** |
20 | /** |
| 21 | * Creates a new instance of MbPacketQueue. |
21 | * Creates a new instance of MbPacketQueue. |
| 22 | */ |
22 | */ |
| 23 | public MbPacketQueue() { |
23 | public MbPacketQueue() { |
| 24 | queue = new LinkedList(); |
24 | queue = new LinkedList(); |
| 25 | } |
25 | } |
| 26 | 26 | ||
| 27 | /** |
27 | /** |
| 28 | * Gets the size (=number of elements) of the queue. |
28 | * Gets the size (=number of elements) of the queue. |
| 29 | * @return The number of elements currently enqueued. |
29 | * @return The number of elements currently enqueued. |
| 30 | */ |
30 | */ |
| 31 | synchronized public int getSize() { |
31 | synchronized public int getSize() { |
| 32 | return queue.size(); |
32 | return queue.size(); |
| 33 | } |
33 | } |
| 34 | 34 | ||
| 35 | /** |
35 | /** |
| 36 | * Adds a packet to the queue. |
36 | * Adds a packet to the queue. |
| 37 | * @param p The packet that should be added. In case of null, packet is ignored. |
37 | * @param p The packet that should be added. In case of null, packet is ignored. |
| 38 | */ |
38 | */ |
| 39 | synchronized public void enqueue(MbPacket p) { |
39 | synchronized public void enqueue(MbPacket p) { |
| 40 | if (p!=null) { |
40 | if (p!=null) { |
| 41 | queue.add(p); |
41 | queue.add(p); |
| 42 | } |
42 | } |
| 43 | } |
43 | } |
| 44 | 44 | ||
| 45 | /** |
45 | /** |
| 46 | * Extracts and removes a packet from the queue. |
46 | * Extracts and removes a packet from the queue. |
| 47 | * @return The extracted packet. If queue is empty, return value is null. |
47 | * @return The extracted packet. If queue is empty, return value is null. |
| 48 | */ |
48 | */ |
| 49 | synchronized public MbPacket dequeue() { |
49 | synchronized public MbPacket dequeue() { |
| 50 | if (getSize()>0) { |
50 | if (getSize()>0) { |
| 51 | return (MbPacket)queue.removeFirst(); |
51 | return (MbPacket)queue.removeFirst(); |
| 52 | } else { |
52 | } else { |
| 53 | return null; |
53 | return null; |
| 54 | } |
54 | } |
| 55 | } |
55 | } |
| 56 | } |
56 | } |
| 57 | 57 | ||