Subversion Repositories HomeAutomation

Rev

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