Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * File created by Jimmy
  3.  * at 2003-dec-29 00:04:12
  4.  */
  5. package Macbeth.Transports;
  6.  
  7. import Macbeth.System.MbPacketTransport;
  8. import Macbeth.System.MbPacket;
  9. import Macbeth.System.MbModule;
  10. import Macbeth.System.MbKernel;
  11.  
  12. /**
  13.  * A local packet transport for the Macbeth kernel. This will
  14.  * deliver packets locally within the Macbeth kernel.
  15.  * @author Jimmy
  16.  */
  17. public class MbLocalTransport extends MbPacketTransport {
  18.  
  19.     /**
  20.      * Creates a new instance of MbLocalTransport.
  21.      * @param parentKernel Reference to the parent kernel object.
  22.      */
  23.     public MbLocalTransport(MbKernel parentKernel) {
  24.         super(parentKernel);
  25.     }
  26.  
  27.     /**
  28.      * Tries to deliver a packet locally.
  29.      * @param p The packet that needs to be delivered.
  30.      * @return True if the packet has been delivered successfully.
  31.      * False if this packet cannot be delivered locally (that is,
  32.      * if it has a destination kernel that differs from "self").
  33.      * @throws Macbeth.System.MbPacketTransport.MbPacketDeliveryException if the destination module
  34.      * couldn't be found while trying to deliver locally.
  35.      */
  36.     public boolean deliverPacket(MbPacket p) throws MbPacketTransport.MbPacketDeliveryException {
  37.         //we will only deliver locally if destination kernel is "self"
  38.         if (p.getDestination().getKernel().equals("self")) {
  39.             deliverPacketLocally(p);
  40.             return true;
  41.         } else {
  42.             //throw new MbPacketDeliveryException("Can only deliver to \"self\". Destination kernel was: \"" + p.getDestination().kernel + "\"");
  43.             return false;
  44.         }
  45.     }
  46.  
  47.     /**
  48.      * Returns the name of this transport.
  49.      * @return The name of this transport.
  50.      */
  51.     public String getName() {
  52.         return "Local transport";
  53.     }
  54.  
  55.     /**
  56.      * Delivers a packet to the specified destination module.
  57.      * This delivery will only be performed locally.
  58.      * @param p The packet that it to be delivered.
  59.      * @throws Macbeth.System.MbPacketTransport.MbPacketDeliveryException if the destination module
  60.      * couldn't be found.
  61.      */
  62.     protected void deliverPacketLocally(MbPacket p) throws MbPacketTransport.MbPacketDeliveryException {
  63.         String modulename = p.getDestination().getModule();
  64.         MbModule module = null;
  65.         //try to locate destination module locally
  66.         module = parentKernel.getModule(modulename);
  67.         //if module was found
  68.         if (module!=null) {
  69.             //tell module to receive this packet
  70.             module.packetReceived(p);
  71.         }
  72.         //if module wasn't found
  73.         else {
  74.             //throw exception
  75.             throw new MbPacketDeliveryException("Destination module '" + p.getDestination().getModule() + "' not found.");
  76.         }
  77.         //we have now delivered the packet successfully, so increase sent packets-counter
  78.         packetsSent++;
  79.     }
  80. }
  81.