More goodies

jjp32 [2002-03-31 23:24:46]
More goodies
Filename
siena/benchmarks/README.txt
siena/examples/Metaparser.java
siena/examples/Oracle.java
siena/examples/SmartEvent.java
siena/examples/WGC.java
siena/utils/AllSubscriber.java
siena/utils/TestPublisher.java
diff --git a/siena/benchmarks/README.txt b/siena/benchmarks/README.txt
new file mode 100644
index 0000000..657568f
--- /dev/null
+++ b/siena/benchmarks/README.txt
@@ -0,0 +1,5 @@
+See $CVSROOT/psl/events/benchmarks/siena for the benchmarking code.
+
+thanks,
+
+--janak
\ No newline at end of file
diff --git a/siena/examples/Metaparser.java b/siena/examples/Metaparser.java
new file mode 100644
index 0000000..5e3126e
--- /dev/null
+++ b/siena/examples/Metaparser.java
@@ -0,0 +1,126 @@
+import java.io.*;
+import siena.*;
+
+/**
+ * Example "stub" metaparser that (a) listens to the Siena for results
+ * from the "Oracle"; and (b) listens from the bus about SmartEvents
+ * to "parse".
+ *
+ * @author Phil Gross (png3@cs.columbia.edu), with modifications by
+ * Janak J Parekh (jjp32@cs.columbia.edu)
+ * @version 1.0
+ */
+class Metaparser implements Runnable, Notifiable {
+  public static final String me = "Metaparser";
+  Siena s = null;
+
+  public Metaparser(Siena s) {
+    this.s = s;
+  }
+
+  public static void main(String args[]) {
+    String master = "senp://localhost:31337";
+
+    if (args.length > 0) {
+      master = args[0];
+    }
+
+    HierarchicalDispatcher h = new HierarchicalDispatcher();
+    try {
+      h.setMaster(master);
+      System.out.println(me + ": master is " + master);
+    } catch (siena.InvalidHandlerException ihe) {
+      ihe.printStackTrace();
+    } catch (IOException ioe) {
+      ioe.printStackTrace();
+    }
+
+    Metaparser m = new Metaparser(h);
+    Thread t = new Thread(m);
+    t.start();
+  }
+
+  public void run() {
+    Filter f = new Filter();
+    f.addConstraint("source", "Oracle");
+    f.addConstraint("type", "queryResult");
+    try {
+      // create a listener (implementation of Notifiable) to
+      // specifically handle queryResults.
+      s.subscribe(f, new Notifiable() {
+	  public void notify(Notification n) {
+	    // if we get here, it means that source=Oracle and
+	    // type=queryResult, as per the filter.
+	    AttributeValue av = n.getAttribute("queryResult");
+	    if (av != null) {
+	      String res = av.stringValue();
+	      System.out.println(me + ": got queryResult " + res);
+	      processOracleReply(res);
+	    } else {
+	      System.out.println(me + " Error: oracle w/o queryResult");
+	    }
+	  }
+
+	  // We don't care about sequences here.
+	  public void notify(Notification[] n) { ; }
+	});
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " subscribed to " + f);
+
+    f.clear();
+    f.addConstraint("source", "Smart");
+    try {
+      // handle this one internally.  Why not?
+      s.subscribe(f, this);
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " subscribed to " + f);
+  }
+
+  public void processOracleReply(String reply) {
+    System.out.println(me + ": I'm processing this oracle reply.  Whoopee!");
+    System.out.println(me + ": reply was " + reply);
+    Notification n = new Notification();
+    n.putAttribute("source", "Metaparser");
+    n.putAttribute("type", "finalResult");
+    n.putAttribute("finalResult", "Happy Happy Joy Joy");
+    try {
+      s.publish(n);
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " published " + n);
+  }
+
+  public void processSmartEvent(String xmlFrag) {
+    System.out.println(me + "processSmartEvent deciding to ask Oracle");
+    Notification n = new Notification();
+    n.putAttribute("source", "Metaparser");
+    n.putAttribute("type", "query");
+    n.putAttribute("query", xmlFrag);
+    try {
+      s.publish(n);
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " published " + n);
+  }
+
+  public void notify(Notification n) {
+    // Smart event.  Pass fragment on to Oracle
+    AttributeValue av = n.getAttribute("xmlFrag");
+    if (av != null) {
+      String frag = av.stringValue();
+      System.out.println(me + " got fragment " + frag);
+      processSmartEvent(frag);
+
+    } else {
+      System.out.println(me + " Error: Smart without fragment");
+    }
+  }
+
+  public void notify(Notification[] e) { ; }
+}
diff --git a/siena/examples/Oracle.java b/siena/examples/Oracle.java
new file mode 100644
index 0000000..be99a7d
--- /dev/null
+++ b/siena/examples/Oracle.java
@@ -0,0 +1,141 @@
+import java.io.*;
+import siena.*;
+
+/**
+ * Sample "Oracle" class that answers queries from the Metaparser.
+ *
+ * @author Phil Gross (png3@cs.columbia.edu), with modifications by
+ * Janak J Parekh (jjp32@cs.columbia.edu)
+ * @version 1.0
+ */
+class Oracle implements Runnable, Notifiable {
+
+  public static final String me = "Oracle";
+  Siena s = null;
+
+
+  public Oracle(Siena s) {
+    this.s = s;
+  }
+
+  public static void main(String args[]) {
+
+    String master = "senp://localhost:31337";
+
+    if (args.length > 0) {
+      master = args[0];
+    }
+
+    HierarchicalDispatcher h = new HierarchicalDispatcher();
+    try {
+      h.setMaster(master);
+      System.out.println(me + ": master is " + master);
+    } catch (siena.InvalidHandlerException ihe) {
+      ihe.printStackTrace();
+    } catch (IOException ioe) {
+      ioe.printStackTrace();
+    }
+
+    Oracle m = new Oracle(h);
+    Thread t = new Thread(m);
+    t.start();
+
+  }
+
+  public void run() {
+
+    Filter f = new Filter();
+    f.addConstraint("source", "Metaparser");
+    f.addConstraint("type", "query");
+    try {
+      // handle internally
+      s.subscribe(f, this);
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " subscribed to " + f);
+
+    f.clear();
+    f.addConstraint("source", "WGC");
+    try {
+      // handle externally
+      s.subscribe(f, new Notifiable() {
+	  public void notify(Notification n) {
+	    // if we get here, it means that source=WGC and type=cacheResult,
+	    // as per the filter.
+	    AttributeValue av = n.getAttribute("cacheResult");
+	    if (av != null) {
+	      boolean res = av.booleanValue();
+	      System.out.println(me + " got cacheResult " + res);
+
+	      if (res) { // we got a cache hit
+		av = n.getAttribute("value");
+		if (av != null) {
+		  String val = av.stringValue();
+		  System.out.println(me + " got value " + val);
+		  processCacheResult(val);
+		} else {
+		  System.out.println(me + " Error: true result without value");
+		}
+	      } else { // result was false, no hit
+		processCacheResult("Too bad, so sad");
+	      }
+	    } else {
+	      System.out.println(me + " Error: cacheResult w/o payload");
+	    }
+	  }
+
+	  public void notify(Notification[] n) { }
+	});
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " subscribed to " + f);
+  }
+
+  public void processMPQuery(String xmlFrag) {
+    System.out.println(me + ": I'm processing this XML Fragment.  Yahoo!");
+    System.out.println(me + ": Deciding to ask WGC");
+    Notification n = new Notification();
+    n.putAttribute("source", "Oracle");
+    n.putAttribute("type", "cacheLookup");
+    n.putAttribute("key", "GoPatGo");
+    try {
+      s.publish(n);
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " published " + n);
+  }
+
+  public void processCacheResult(String xmlFrag) {
+    System.out.println(me + " got a Cache Result. I'm so excited!");
+    Notification n = new Notification();
+    n.putAttribute("source", "Oracle");
+    n.putAttribute("type", "queryResult");
+    n.putAttribute("queryResult", "the Answer to Life, the Universe, etc.");
+    try {
+      s.publish(n);
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " published " + n);
+  }
+
+  public void notify(Notification n) {
+
+    // Metaparser query
+    AttributeValue av = n.getAttribute("query");
+    if (av != null) {
+      String query = av.stringValue();
+      System.out.println(me + " got query " + query);
+      processMPQuery(query);
+
+    } else {
+      System.out.println(me + " Error: metaparser query without query");
+    }
+  }
+
+  public void notify(Notification[] e) {}
+
+}
diff --git a/siena/examples/SmartEvent.java b/siena/examples/SmartEvent.java
new file mode 100644
index 0000000..8ef745d
--- /dev/null
+++ b/siena/examples/SmartEvent.java
@@ -0,0 +1,78 @@
+import java.io.*;
+import siena.*;
+
+/**
+ * Example "SmartEvent tester program".
+ *
+ * @author Phil Gross (png3@cs.columbia.edu)
+ * @version 1.0
+ */
+class SmartEvent implements Runnable, Notifiable {
+
+  public static final String me = "SmartEvent";
+  Siena s = null;
+
+  public SmartEvent(Siena s) {
+    this.s = s;
+  }
+
+  public static void main(String args[]) {
+    String master = "senp://localhost:31337";
+
+    if (args.length > 0) {
+      master = args[0];
+    }
+
+    HierarchicalDispatcher h = new HierarchicalDispatcher();
+    try {
+      h.setMaster(master);
+    } catch (siena.InvalidHandlerException ihe) {
+      ihe.printStackTrace();
+    } catch (IOException ioe) {
+      ioe.printStackTrace();
+    }
+
+    SmartEvent m = new SmartEvent(h);
+    Thread t = new Thread(m);
+    t.start();
+
+  }
+
+  public void run() {
+
+    Filter f1 = new Filter();
+    f1.addConstraint("source", "Metaparser");
+    f1.addConstraint("type", "finalResult");
+    try {
+      // handle internally.
+      s.subscribe(f1, this);
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " subscribed to " + f1);
+
+    Notification n = new Notification();
+    n.putAttribute("source", "Smart");
+    n.putAttribute("xmlFrag", "<We love XML/>");
+    try {
+      s.publish(n);
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " published " + n);
+  }
+
+  public void notify(Notification n) {
+    // Smart event.  Pass fragment on to Oracle
+    AttributeValue av = n.getAttribute("finalResult");
+    if (av != null) {
+      String res = av.stringValue();
+      System.out.println(me + " got finalResult " + res);
+      System.out.println(me + ":  Peace on earth, etc.");
+    } else {
+      System.out.println(me + " Error: finalResult w/o payload");
+    }
+  }
+
+  public void notify(Notification[] e) {}
+}
diff --git a/siena/examples/WGC.java b/siena/examples/WGC.java
new file mode 100644
index 0000000..f386e11
--- /dev/null
+++ b/siena/examples/WGC.java
@@ -0,0 +1,99 @@
+import java.io.*;
+import siena.*;
+
+/**
+ * Sample stub workgroup cache.  Expects "requests" from the "Oracle".
+ *
+ * @author Phil Gross (png3@cs.columbia.edu)
+ * @version 1.0
+ */
+class WGC implements Runnable, Notifiable {
+
+  public static final String me = "WGC";
+  Siena s = null;
+  boolean success = false;
+
+
+  public WGC(Siena s, boolean success) {
+    this.s = s;
+    this.success = success;
+  }
+
+  /** if two parameters given, lookup will succeed. */
+  public static void main(String args[]) {
+
+    String master = "senp://localhost:31337";
+
+    if (args.length > 0) {
+      master = args[0];
+    }
+
+    HierarchicalDispatcher h = new HierarchicalDispatcher();
+    try {
+      h.setMaster(master);
+      System.out.println(me + ": master is " + master);
+    } catch (siena.InvalidHandlerException ihe) {
+      ihe.printStackTrace();
+    } catch (IOException ioe) {
+      ioe.printStackTrace();
+    }
+
+    WGC m = new WGC(h, (args.length > 1) ? true : false);
+
+    Thread t = new Thread(m);
+    t.start();
+
+  }
+
+  public void run() {
+
+    Filter f1 = new Filter();
+    f1.addConstraint("source", "Oracle");
+    f1.addConstraint("type", "cacheLookup");
+    try {
+      // handle internally
+      s.subscribe(f1, this);
+    } catch (siena.SienaException se) {
+      se.printStackTrace();
+    }
+    System.out.println(me + " subscribed to " + f1);
+
+  }
+
+  public void notify(Notification n) {
+
+    // Metaparser query
+    AttributeValue av = n.getAttribute("key");
+    if (av != null) {
+      String key = av.stringValue();
+      System.out.println(me + " got key " + key);
+      System.out.println(me + " success = " + success);
+
+      // sophisticated cache lookup scheme
+      /*
+      java.util.Random r = new java.util.Random();
+      if (r.nextBoolean()) {
+      */
+      // only generate event if success
+      if (success) {
+	n = new Notification();
+	n.putAttribute("source", "WGC");
+	n.putAttribute("type", "cacheResult");
+	n.putAttribute("cacheResult", true);
+	n.putAttribute("value", "Secret Egg Salad Recipe");
+	try {
+	  s.publish(n);
+	} catch (siena.SienaException se) {
+	  se.printStackTrace();
+	}
+	System.out.println(me + " published " + n);
+      }
+
+    } else {
+      System.out.println(me + " Error: Oracle lookup without key");
+    }
+  }
+
+  public void notify(Notification[] e) {}
+
+}
diff --git a/siena/utils/AllSubscriber.java b/siena/utils/AllSubscriber.java
new file mode 100644
index 0000000..5463e63
--- /dev/null
+++ b/siena/utils/AllSubscriber.java
@@ -0,0 +1,71 @@
+
+package psl.events.siena.utils;
+
+import siena.*;
+
+/**
+ * Universal subscription-and-printout service, useful for debugging.
+ * Note that even though the class is not a Runnable, it doesn't
+ * really matter since Siena will do all the thread work for us.
+ *
+ * TODO: Convert to a standardized logger
+ *
+ * @author Janak J Parekh
+ * @version 1.0
+ */
+public class AllSubscriber implements Notifiable {
+  private HierarchicalDispatcher hd = null;
+
+  public static void main(String[] args) {
+    if(args.length != 1) {
+      System.err.println("usage: java AllSubscriber <SENP URL>");
+      System.exit(-1);
+    }
+    new AllSubscriber(args[0]);
+  }
+
+  public AllSubscriber(String sienaHost) {
+    hd = new HierarchicalDispatcher();
+    try {
+      hd.setReceiver(new TCPPacketReceiver(0));
+      hd.setMaster(sienaHost);
+    } catch(Exception e) {
+      e.printStackTrace();
+    }
+
+    // Now super-subscribe
+    Filter f = new Filter();
+
+    try {
+      hd.subscribe(f, this);
+    } catch(Exception e) {
+      e.printStackTrace();
+    }
+
+    // Cleanup
+    Runtime.getRuntime().addShutdownHook(new Thread() {
+	public void run() {
+	  System.err.println("Shutting down..");
+	  hd.shutdown();
+	  return;
+	}
+      });
+
+    System.out.println("Ready.");
+  }
+
+  /**
+   * Get the hierarchicalDispatcher instance we use.
+   *
+   * @return The Hierarchical Dispatcher we're listening to.
+   */
+  public HierarchicalDispatcher getSiena() {
+    return hd;
+  }
+
+  public void notify(Notification n) {
+    System.out.println("Received event:" + n);
+  }
+
+  public void notify(Notification[] s) { ; }
+}
diff --git a/siena/utils/TestPublisher.java b/siena/utils/TestPublisher.java
new file mode 100644
index 0000000..ee5c709
--- /dev/null
+++ b/siena/utils/TestPublisher.java
@@ -0,0 +1,49 @@
+
+package psl.events.siena.utils;
+
+import siena.*;
+
+/**
+ * Simple test publisher, useful for testing a Siena.
+ *
+ * @author Janak J Parekh
+ * @version 1.0
+ */
+public class TestPublisher {
+  private HierarchicalDispatcher hd = null;
+
+  public static void main(String[] args) {
+    new TestPublisher(args);
+  }
+
+  public TestPublisher(String[] args) {
+    if(args.length==0) {
+      System.err.println("usage: java TestPublisher <SENP URL>");
+      System.exit(-1);
+    }
+    hd = new HierarchicalDispatcher();
+    try {
+      hd.setReceiver(new TCPPacketReceiver(0));
+      hd.setMaster(args[0]);
+    } catch(Exception e) {
+      e.printStackTrace();
+    }
+
+    System.out.println("publishing...");
+
+    // Now publish
+    Notification n = new Notification();
+    n.putAttribute("foo","bar");
+
+    try {
+      hd.publish(n);
+    } catch(Exception e) { e.printStackTrace(); }
+
+    System.out.println("shutting down...");
+
+    // If we don't sleep, Siena throws an exception... blargh
+    try { Thread.sleep(2000); } catch(Exception e) { ; }
+
+    hd.shutdown();
+  }
+}