Subversion Repositories HomeAutomation

Rev

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

  1. /*
  2.  * modLoopback.java
  3.  *
  4.  * Created on den 23 augusti 2003, 14:10
  5.  */
  6. package Macbeth.Modules.modLoopback;
  7.  
  8. import java.util.*;
  9. import Macbeth.System.*;
  10. import Macbeth.Utilities.MySystem;
  11. import Macbeth.XML.XMLDataHandler;
  12.  
  13. /**
  14.  * Macbeth module for performance loopback tests.
  15.  * @author Jimmy
  16.  */
  17. public class modLoopback extends MbModule {
  18.  
  19.     private int nrPacketBounces = 0;
  20.  
  21.     /**
  22.      * Creates a new instance of modLoopback.
  23.      */
  24.     public modLoopback() {
  25.         //construct MbModule
  26.         super();
  27.         //register our packet data handler
  28.         setPacketDataHandler(new PacketDataHandler());
  29.         //we have a html-interface, so lets register it
  30.         setHTMLInterface(new HTMLInterface());
  31.     }
  32.  
  33.     /**
  34.      * Gets the name of the component.
  35.      * @return The name of the component.
  36.      */
  37.     public String name() {
  38.         return "Loopback";
  39.     }
  40.  
  41.     /**
  42.      * Gets the description of the component.
  43.      * @return The description of the component.
  44.      */
  45.     public String description() {
  46.         return "Simple loopback test module";
  47.     }
  48.  
  49.     /**
  50.      * This method sets default values on all _required_ data
  51.      * fields in the data repository.
  52.      */
  53.     protected void initDataFields(){
  54.         //this module doesn't require any data fields
  55.     }
  56.  
  57.     /**
  58.      * Starts up this module.
  59.      */
  60.     public void startup() throws MbStartupException {
  61.         //start up MbModule
  62.         super.startup();
  63.         MbPacket p = new MbPacket();
  64.         p.setContents("<returntosource/>");
  65.         p.setDestination(new MbLocation("self",name()));
  66.         for (int i=0; i<1000; i++) {
  67.             sendPacket(p);
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * Shuts down this module.
  73.      */
  74.     public void shutdown() {
  75.         //shut down MbModule
  76.         super.shutdown();
  77.     }
  78.  
  79.     /**
  80.      * A HTML-interface for this module.
  81.      */
  82.     private class HTMLInterface implements MbHTMLInterface {
  83.         /**
  84.          * Retreives the full code for your HTML-page. This is
  85.          * formatted as plain text and must be valid HTML-code.
  86.          * @return Full HTML-code for the page.
  87.          */
  88.         public String getHTMLPage() {
  89.             String s = "<html><body>" + MySystem.lineBreak;
  90.             s += "<h1>Loopback module</h1>" + MySystem.lineBreak;
  91.             s += "<p>This module sends packets to itself in order to put kernel on heavy workload</p>" + MySystem.lineBreak;
  92.             s += "<p>So far, a total of <b>" + Integer.toString(nrPacketBounces) + "</b> packets have been bounced back and forth to kernel";
  93.             s += "</p></body></html>";
  94.             return s;
  95.         }
  96.  
  97.         /**
  98.          * This method is invoked when a link is pressed on your
  99.          * HTML-page.
  100.          * @param href The href-attribute of the link.
  101.          */
  102.         public void linkPressed(String href) {
  103.             _info.println("Link pressed: " + href);
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Takes care of XML-data found in incoming packets.
  109.      */
  110.     private class PacketDataHandler implements XMLDataHandler {
  111.         /**
  112.          * Called when start of a new element is found in the XML-data.
  113.          * Ex: <name attr1="value1" attr2="value2">
  114.          * Element name would then be "name" and attribute list
  115.          * would contain "value1" and "value2" mapped to the attribute
  116.          * names "attr1" and "attr2".
  117.          * @param element The name of the element.
  118.          * @param attributes The element attributes.
  119.          */
  120.         public void XMLstartElement(String element, HashMap attributes) {
  121.             /**
  122.              * Ex: <returntosource/>
  123.              * Tells us to return this packet to where it came from.
  124.              */
  125.             if (element.equals("returntosource")) {
  126.                 MbPacket p = currentPacket;
  127.                 p.setDestinationModule(p.getSource().getModule());
  128.                 p.setDestinationKernel("self");
  129.                 sendPacket(p);
  130.                 nrPacketBounces++;
  131.             }
  132.         }
  133.  
  134.         public void XMLendElement(String element) {}
  135.         public void XMLelementData(String data) {}
  136.         public void XMLdocumentStart() {}
  137.         public void XMLdocumentEnd() {}
  138.     }
  139. }
  140.