Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
15 arune 1
/**
2
 * File created by Jimmy
3
 * at 2003-dec-28 19:50:12
4
 */
5
package Macbeth.System;
6
 
7
import java.io.PrintStream;
8
 
9
/**
10
 * A packet transport plugs directly into the kernel and helps
11
 * it delivering packets via different transports. For instance,
12
 * a local transport might deliver a packet directly to the
13
 * destination module within the local kernel, while an internet
14
 * transport might send the packet to a kernel on a remote location.
15
 * Packet transports primarily send packets, but they can of
16
 * course also receive packets on their own, and when they do, they
17
 * can simply tell the kernel to put the new, received, packet onto
18
 * the standard packet queue by calling the packetReceived-method
19
 * declared in this class.
20
 */
21
public abstract class MbPacketTransport {
22
    //reference to our parent kernel object
23
    protected MbKernel parentKernel;
24
    //references to output streams
25
    protected PrintStream _debug,_info,_errors;
26
 
27
    //counts how many packets this transport has sent/received
28
    public int packetsSent = 0;
29
    public int packetsReceived = 0;
30
 
31
    /**
32
     * Creates a new instance of MbPacketTransport.
33
     * @param parentKernel Reference to the parent kernel object.
34
     */
35
    public MbPacketTransport(MbKernel parentKernel) {
36
        this.parentKernel = parentKernel;
37
    }
38
 
39
    /**
40
     * Will be called by the kernel when it has a new packet that
41
     * needs to be delivered. The kernel has a list of packet
42
     * transport systems, and typically it will try to invoke them
43
     * in the same order as they were added to that list. In other
44
     * words, there is no way you can tell whether one packet
45
     * transport will be invoked before another one. Therefore, it is
46
     * very important that you return false here if you cannot deliver
47
     * the current packet. The kernel will then try with the next
48
     * transport in list. If, and only if, none of the transports
49
     * could deliver the packet, the kernel will drop it.
50
     * @param p The packet that needs to be delivered.
51
     * @return False if this packet transport cannot perform delivery
52
     * on the specified packet. True if the packets has been delivered
53
     * successfully. When you return true, the packet is considered
54
     * delivered, and no further transports will be invoked.
55
     * @throws MbPacketDeliveryException if an error occured while trying
56
     * to deliver the packet.
57
     */
58
    public abstract boolean deliverPacket(MbPacket p) throws MbPacketDeliveryException;
59
 
60
    /**
61
     * Override this if you need to perform anything particular
62
     * during kernel startup.
63
     */
64
    public void startup() {
65
        //keep references to output streams
66
        _debug = parentKernel._debug;
67
        _info = parentKernel._info;
68
        _errors = parentKernel._errors;
69
    }
70
 
71
    /**
72
     * Override this if you need to perform anything particular
73
     * during kernel shutdown.
74
     */
75
    public void shutdown() {}
76
 
77
    /**
78
     * Returns the name of this transport. This is only used by
79
     * kernel to show user-friendly information.
80
     * @return The name of this transport.
81
     */
82
    public abstract String getName();
83
 
84
    /**
85
     * Call this method if you have received a new, incoming,
86
     * packet via your transport and need to handle it over to
87
     * the kernel. This is useful for bidirectional delivery
88
     * systems.
89
     * @param p The new packet that you have received.
90
     */
91
    protected void packetReceived(MbPacket p) {
92
        //put this packet onto the kernel packet queue
93
        parentKernel.packetReceived(p);
94
        //increase packets received-counter
95
        packetsReceived++;
96
    }
97
 
98
    /**
99
     * Thrown when a packet couldn't be delivered properly.
100
     */
101
    public class MbPacketDeliveryException extends Exception {
102
        /**
103
         * Creates a new instance of MbPacketDeliveryException
104
         * @param s The reason of this exception.
105
         */
106
        public MbPacketDeliveryException(String s) {
107
            super(s);
108
        }
109
    }
110
}