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 | * MbPacketQueueHandler.java |
2 | * MbPacketQueueHandler.java |
| 3 | * |
3 | * |
| 4 | * Created on den 24 augusti 2003, 15:11 |
4 | * Created on den 24 augusti 2003, 15:11 |
| 5 | */ |
5 | */ |
| 6 | package Macbeth.System; |
6 | package Macbeth.System; |
| 7 | 7 | ||
| 8 | /** |
8 | /** |
| 9 | * A packet queue handler, which pulls packets from a packet |
9 | * A packet queue handler, which pulls packets from a packet |
| 10 | * queue, and handles over each pulled packet to a packet handler |
10 | * queue, and handles over each pulled packet to a packet handler |
| 11 | * object. |
11 | * object. |
| 12 | * @author Jimmy |
12 | * @author Jimmy |
| 13 | */ |
13 | */ |
| 14 | public class MbPacketQueueHandler extends Thread { |
14 | public class MbPacketQueueHandler extends Thread { |
| 15 | private MbPacketHandler packetHandler; |
15 | private MbPacketHandler packetHandler; |
| 16 | private MbPacketQueue packetQueue; |
16 | private MbPacketQueue packetQueue; |
| 17 | private boolean runThread; |
17 | private boolean runThread; |
| 18 | 18 | ||
| 19 | /** |
19 | /** |
| 20 | * Creates a new instance of MbPacketQueueHandler. |
20 | * Creates a new instance of MbPacketQueueHandler. |
| 21 | * @param packetqueue The packet queue to handle. |
21 | * @param packetqueue The packet queue to handle. |
| 22 | * @param packethandler The packet handler that should be |
22 | * @param packethandler The packet handler that should be |
| 23 | * invoked when packets need to be handled. |
23 | * invoked when packets need to be handled. |
| 24 | */ |
24 | */ |
| 25 | public MbPacketQueueHandler(MbPacketQueue packetqueue, MbPacketHandler packethandler) { |
25 | public MbPacketQueueHandler(MbPacketQueue packetqueue, MbPacketHandler packethandler) { |
| 26 | super("MbPacketQueueHandler"); |
26 | super("MbPacketQueueHandler"); |
| 27 | packetQueue = packetqueue; |
27 | packetQueue = packetqueue; |
| 28 | packetHandler = packethandler; |
28 | packetHandler = packethandler; |
| 29 | } |
29 | } |
| 30 | 30 | ||
| 31 | /** |
31 | /** |
| 32 | * Start working with the queue. |
32 | * Start working with the queue. |
| 33 | */ |
33 | */ |
| 34 | public void startWorking() { |
34 | public void startWorking() { |
| 35 | runThread = true; |
35 | runThread = true; |
| 36 | //start thread (it will call our run-method in a new thread) |
36 | //start thread (it will call our run-method in a new thread) |
| 37 | start(); |
37 | start(); |
| 38 | } |
38 | } |
| 39 | 39 | ||
| 40 | /** |
40 | /** |
| 41 | * Stop working with the queue. |
41 | * Stop working with the queue. |
| 42 | */ |
42 | */ |
| 43 | public void stopWorking() { |
43 | public void stopWorking() { |
| 44 | runThread = false; |
44 | runThread = false; |
| 45 | } |
45 | } |
| 46 | 46 | ||
| 47 | /** |
47 | /** |
| 48 | * This is called by the thread when running. |
48 | * This is called by the thread when running. |
| 49 | */ |
49 | */ |
| 50 | public void run() { |
50 | public void run() { |
| 51 | //loop while run-flag is set true |
51 | //loop while run-flag is set true |
| 52 | while (runThread) { |
52 | while (runThread) { |
| 53 | //if there are packets in the queue and packet handler is ready |
53 | //if there are packets in the queue and packet handler is ready |
| 54 | if (packetQueue.getSize()>0 && packetHandler.canHandlePacket()) { |
54 | if (packetQueue.getSize()>0 && packetHandler.canHandlePacket()) { |
| 55 | //pull packet from head of the queue and tell the packet handler to handle it |
55 | //pull packet from head of the queue and tell the packet handler to handle it |
| 56 | packetHandler.handlePacket(packetQueue.dequeue()); |
56 | packetHandler.handlePacket(packetQueue.dequeue()); |
| 57 | } else { |
57 | } else { |
| 58 | //if queue is empty of packet handler not ready yet, try to get some sleep=) |
58 | //if queue is empty of packet handler not ready yet, try to get some sleep=) |
| 59 | try { |
59 | try { |
| 60 | sleep(5); |
60 | sleep(5); |
| 61 | } catch (InterruptedException e) { |
61 | } catch (InterruptedException e) { |
| 62 | //don't care |
62 | //don't care |
| 63 | } |
63 | } |
| 64 | } |
64 | } |
| 65 | } |
65 | } |
| 66 | } |
66 | } |
| 67 | } |
67 | } |
| 68 | 68 | ||