Rev 641 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 15 | arune | 1 | /* |
| 2 | * MbComponent.java |
||
| 3 | * |
||
| 4 | * Created on den 23 augusti 2003, 20:56 |
||
| 5 | */ |
||
| 6 | package Macbeth.System; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * This class represents a Macbeth component. Components |
||
| 10 | * are parts of the Macbeth system, each having its own |
||
| 11 | * _unique_ name, a packet queue and a packet queue handler. |
||
| 12 | * Extend this class further to create kernels or modules. |
||
| 13 | * @author Jimmy |
||
| 14 | */ |
||
| 15 | abstract public class MbComponent implements MbPacketHandler,MbPacketReceiver { |
||
| 16 | //A packet queue in which all packets will be put before they are processed. |
||
| 17 | protected MbPacketQueue packetQueue; |
||
| 18 | //A packet processor that takes care of packets in the queue. |
||
| 19 | protected MbPacketQueueHandler packetQueueHandler; |
||
| 20 | //is this component started? |
||
| 21 | private boolean started; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Creates a new instance of MbComponent. |
||
| 25 | */ |
||
| 26 | public MbComponent() { |
||
| 27 | //create a new packet queue for us |
||
| 28 | packetQueue = new MbPacketQueue(); |
||
| 29 | //create a handler for the packet queue |
||
| 30 | packetQueueHandler = new MbPacketQueueHandler(packetQueue,this); |
||
| 31 | //we're not started yet |
||
| 32 | started = false; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Gets the name of the component. |
||
| 37 | * @return The name of the component. |
||
| 38 | */ |
||
| 39 | abstract public String name(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Gets a short description of the component. This should |
||
| 43 | * be kept short (1-2 lines) and should be formatted as |
||
| 44 | * plain text. |
||
| 45 | * @return A short description of the component. |
||
| 46 | */ |
||
| 47 | abstract public String description(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Returns true if this component is started. |
||
| 51 | * @return true if this component is started. |
||
| 52 | */ |
||
| 53 | public boolean isStarted() { |
||
| 54 | return started; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Will be called when this component should start up. Most |
||
| 59 | * initialization should be done here (rather than in the |
||
| 60 | * component's constructor). |
||
| 61 | * @throws MbStartupException When this component couldn't |
||
| 62 | * be started up for some reason. |
||
| 63 | */ |
||
| 64 | public void startup() throws MbStartupException { |
||
| 65 | //start packet queue handler (it will be run in a separate thread) |
||
| 66 | packetQueueHandler.startWorking(); |
||
| 67 | //this component is started now! |
||
| 68 | started = true; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Will be called when this component should shut down itself. |
||
| 73 | * Default behaviour is to shut down immediately, deleting any |
||
| 74 | * eventual packets waiting in our queue. If you want to change |
||
| 75 | * this behaviour, just override this method. Keep in mind though, |
||
| 76 | * that you cannot send any packets to other components here, they |
||
| 77 | * might have been shut down already! |
||
| 78 | */ |
||
| 79 | public void shutdown() { |
||
| 80 | //shut down packet queue handler |
||
| 81 | packetQueueHandler.stopWorking(); |
||
| 82 | //this component is no longer started! |
||
| 83 | started = false; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Will be called when someone sends a packet to us. Default behaviour |
||
| 88 | * is to simply put the packet onto our packet queue. You could override |
||
| 89 | * this if you, for instance, want to filter out certain packets before |
||
| 90 | * putting them onto the queue. Note that you should not do the actual |
||
| 91 | * packet handling here. That's what the handlePacket-method is for. |
||
| 92 | * @param p The packet we're receiving. |
||
| 93 | */ |
||
| 94 | public void packetReceived(MbPacket p) { |
||
| 95 | packetQueue.enqueue(p); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Checks if packet handler is ready. |
||
| 100 | * @return True if we're ready and it's OK to call handlePacket, false otherwise. |
||
| 101 | */ |
||
| 102 | public boolean canHandlePacket() { |
||
| 103 | return true; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Will be called by the packet queue handler for each packet it |
||
| 108 | * takes off the queue. This means it is time to parse and take |
||
| 109 | * care of this packet. |
||
| 110 | * @param p The packet we need to handle. |
||
| 111 | */ |
||
| 112 | abstract public void handlePacket(MbPacket p); |
||
| 113 | |||
| 114 | } |