Finished testing code for peppo

jjp32 [2002-05-26 20:16:24]
Finished testing code for peppo
Filename
siena/tests/ordering/FourHDTwoPublisher.java
siena/tests/ordering/OneHDOneThin.java
siena/tests/ordering/ThreeHD.java
siena/tests/ordering/TwoHD.java
diff --git a/siena/tests/ordering/FourHDTwoPublisher.java b/siena/tests/ordering/FourHDTwoPublisher.java
new file mode 100644
index 0000000..bf58cc6
--- /dev/null
+++ b/siena/tests/ordering/FourHDTwoPublisher.java
@@ -0,0 +1,103 @@
+package psl.events.siena.tests.ordering;
+
+import siena.*;
+
+/**
+ * Ordering test for four HD's (two publishers, one subscriber, one master).
+ *
+ * @author Janak J Parekh
+ * @version $Revision$
+ */
+public class FourHDTwoPublisher implements Notifiable, Runnable {
+  public static final int numNotifications = 500;
+  private HierarchicalDispatcher hd = null;
+  private long publisherID = -2;
+  private long lastReceived[] = {-1, -1};
+
+  public static void main(String[] args) {
+    if(args.length != 2) {
+      System.err.println("usage: java psl.events.siena.tests.ordering.TwoHD " +
+      "<-p SENP URL of master|-s SENP URL of master>");
+      System.err.println("\t-p implies publishers");
+      System.err.println("\t-s implies subscriber");
+      System.exit(-1);
+    }
+
+    if(args[0].equals("-p")) {
+      Thread t1 = new Thread(new FourHDTwoPublisher(args[1], 0));
+      Thread t2 = new Thread(new FourHDTwoPublisher(args[1], 1));
+      t1.start();
+      t2.start();
+    } else { // Subscriber
+      new FourHDTwoPublisher(args[1], -1);
+    }
+  }
+
+  /**
+   * CTOR.
+   */
+  public FourHDTwoPublisher(String master, long publisherID) {
+    try {
+      hd = new HierarchicalDispatcher();
+      hd.setReceiver(new TCPPacketReceiver(0),1);
+      hd.setMaster(master);
+    } catch(Exception e) { e.printStackTrace(); }
+
+    this.publisherID = publisherID;
+
+    if(publisherID == -1) {
+      subscribe();
+    } /* Else the run method for the publisher */
+  }
+
+  /**
+   * Publisher mode
+   */
+  public void run() {
+    Notification n = null;
+    for(long i = 0; i < numNotifications; i++) {
+      n = new Notification();
+      n.putAttribute("Source", publisherID);
+      n.putAttribute("Count", i);
+      n.putAttribute("Junk", "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.");
+      try { hd.publish(n); } catch(Exception e) { e.printStackTrace(); }
+    }
+  }
+
+  /**
+   * Subscriber mode
+   */
+  public void subscribe() {
+    Runtime.getRuntime().addShutdownHook(new Thread() {
+      public void run() {
+        hd.shutdown();
+      }
+    });
+
+    Filter f = new Filter();
+    try { hd.subscribe(f, this); } catch(Exception e) { e.printStackTrace(); }
+    System.err.println("Listening.");
+  }
+
+  /**
+   * Receive mechanism
+   */
+  public void notify(Notification n) {
+    int index = (int)n.getAttribute("Source").longValue();
+    // get the count
+    if(n.getAttribute("Count").longValue() != (++lastReceived[index])) {
+      System.err.println("FAIL: Received " +
+      n.getAttribute("Count").longValue() + " for source " + index +
+      ", was expecting " + (lastReceived[index] - 1));
+      System.exit(-1);
+    }
+    // if a multiple of 100 print
+    if(n.getAttribute("Count").longValue() % 10 == 0) {
+      System.out.println("Received " + n.getAttribute("Count").longValue() +
+      "th event from source " + index);
+    }
+  }
+
+  public void notify(Notification[] n) { ; }
+
+}
diff --git a/siena/tests/ordering/OneHDOneThin.java b/siena/tests/ordering/OneHDOneThin.java
index f5912c7..56fbaa3 100644
--- a/siena/tests/ordering/OneHDOneThin.java
+++ b/siena/tests/ordering/OneHDOneThin.java
@@ -54,6 +54,7 @@ public class OneHDOneThin implements Notifiable {
     final HierarchicalDispatcher hd = new HierarchicalDispatcher();

     try {
+      // NB: we are using 1 receiver thread below, doesn't work otherwise
       hd.setReceiver(new TCPPacketReceiver(port),1);
     } catch(Exception e) { e.printStackTrace(); }

diff --git a/siena/tests/ordering/ThreeHD.java b/siena/tests/ordering/ThreeHD.java
new file mode 100644
index 0000000..4a8e26a
--- /dev/null
+++ b/siena/tests/ordering/ThreeHD.java
@@ -0,0 +1,97 @@
+package psl.events.siena.tests.ordering;
+
+import siena.*;
+
+/**
+ * Ordering test for three HD's (one publisher, one subscriber, one master).
+ *
+ * @author Janak J Parekh
+ * @version $Revision$
+ */
+public class ThreeHD implements Notifiable {
+  public static final int numNotifications = 5000;
+  public long lastReceived = -1;
+
+  public static void main(String[] args) {
+    if(args.length != 2) {
+      System.err.println("usage: java psl.events.siena.tests.ordering.TwoHD " +
+      "<-p SENP URL of master|-s SENP URL of master>");
+      System.err.println("\t-p implies publisher");
+      System.err.println("\t-s implies subscriber");
+      System.exit(-1);
+    }
+
+    if(args[0].equals("-p")) {
+      new ThreeHD(args[1], true);
+    } else { // Subscriber
+      new ThreeHD(args[1], false);
+    }
+  }
+
+  /**
+   * CTOR.
+   */
+  public ThreeHD(String master, boolean publisher) {
+    HierarchicalDispatcher hd = null;
+    try {
+      hd = new HierarchicalDispatcher();
+      hd.setReceiver(new TCPPacketReceiver(0),1);
+      hd.setMaster(master);
+    } catch(Exception e) { e.printStackTrace(); }
+
+    if(publisher == true) {
+      publish(hd);
+    } else {
+      subscribe(hd);
+    }
+  }
+
+  /**
+   * Publisher mode
+   */
+  public void publish(HierarchicalDispatcher hd) {
+    Notification n = null;
+    for(long i = 0; i < 5000; i++) {
+      n = new Notification();
+      n.putAttribute("Count", i);
+      n.putAttribute("Junk", "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.");
+      try { hd.publish(n); } catch(Exception e) { e.printStackTrace(); }
+    }
+  }
+
+  /**
+   * Subscriber mode
+   */
+  public void subscribe(final HierarchicalDispatcher hd) {
+    Runtime.getRuntime().addShutdownHook(new Thread() {
+      public void run() {
+        hd.shutdown();
+      }
+    });
+
+    Filter f = new Filter();
+    try { hd.subscribe(f, this); } catch(Exception e) { e.printStackTrace(); }
+    System.err.println("Listening.");
+  }
+
+  /**
+   * Receive mechanism
+   */
+  public void notify(Notification n) {
+    // get the count
+    if(n.getAttribute("Count").longValue() != (++lastReceived)) {
+      System.err.println("FAIL: Received " +
+      n.getAttribute("Count").longValue() + ", was expecting " +
+      (lastReceived - 1));
+      System.exit(-1);
+    }
+    // if a multiple of 100 print
+    if(n.getAttribute("Count").longValue() % 100 == 0) {
+      System.out.println("Received " + n.getAttribute("Count").longValue() +
+      "th event");
+    }
+  }
+
+  public void notify(Notification[] n) { ; }
+
+}
diff --git a/siena/tests/ordering/TwoHD.java b/siena/tests/ordering/TwoHD.java
new file mode 100644
index 0000000..54d053f
--- /dev/null
+++ b/siena/tests/ordering/TwoHD.java
@@ -0,0 +1,93 @@
+package psl.events.siena.tests.ordering;
+
+import siena.*;
+
+/**
+ * Ordering test for two HD's (one publisher, one subscriber).
+ *
+ * @author Janak J Parekh
+ * @version $Revision$
+ */
+public class TwoHD implements Notifiable {
+  public static final int numNotifications = 5000;
+  public long lastReceived = -1;
+
+  public static void main(String[] args) {
+    if(args.length == 0) {
+      System.err.println("usage: java psl.events.siena.tests.ordering.TwoHD " +
+      "<-p SENP URL of subscriber|-s port>");
+      System.err.println("\t-p implies publisher");
+      System.err.println("\t-s implies subscriber");
+      System.exit(-1);
+    }
+
+    if(args[0].equals("-p")) {
+      new TwoHD(args[1]);
+    } else {
+      new TwoHD(Short.parseShort(args[1]));
+    }
+  }
+
+  /**
+   * CTOR for publisher mode
+   */
+  public TwoHD(String subscriberHost) {
+    HierarchicalDispatcher hd = null;
+    try {
+      hd = new HierarchicalDispatcher();
+      hd.setReceiver(new TCPPacketReceiver(0));
+      hd.setMaster(subscriberHost);
+    } catch(Exception e) { e.printStackTrace(); }
+
+    Notification n = null;
+    for(long i = 0; i < 5000; i++) {
+      n = new Notification();
+      n.putAttribute("Count", i);
+      n.putAttribute("Junk", "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.");
+      try { hd.publish(n); } catch(Exception e) { e.printStackTrace(); }
+    }
+  }
+
+  /**
+   * CTOR for subscriber mode
+   */
+  public TwoHD(short port) {
+    final HierarchicalDispatcher hd = new HierarchicalDispatcher();
+
+    try {
+      // NB: we are using 1 receiver thread below, doesn't work otherwise
+      hd.setReceiver(new TCPPacketReceiver(port),1);
+    } catch(Exception e) { e.printStackTrace(); }
+
+    Runtime.getRuntime().addShutdownHook(new Thread() {
+      public void run() {
+        hd.shutdown();
+      }
+    });
+
+    Filter f = new Filter();
+    try { hd.subscribe(f, this); } catch(Exception e) { e.printStackTrace(); }
+    System.err.println("Listening on port " + port);
+  }
+
+  /**
+   * Receive mechanism
+   */
+  public void notify(Notification n) {
+    // get the count
+    if(n.getAttribute("Count").longValue() != (++lastReceived)) {
+      System.err.println("FAIL: Received " +
+      n.getAttribute("Count").longValue() + ", was expecting " +
+      (lastReceived - 1));
+      System.exit(-1);
+    }
+    // if a multiple of 100 print
+    if(n.getAttribute("Count").longValue() % 100 == 0) {
+      System.out.println("Received " + n.getAttribute("Count").longValue() +
+      "th event");
+    }
+  }
+
+  public void notify(Notification[] n) { ; }
+
+}