Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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