aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/Counter.java96
-rw-r--r--examples/Makefile16
-rw-r--r--examples/TestIvy.java127
-rw-r--r--examples/TestIvySwing.java243
4 files changed, 482 insertions, 0 deletions
diff --git a/examples/Counter.java b/examples/Counter.java
new file mode 100644
index 0000000..bc23938
--- /dev/null
+++ b/examples/Counter.java
@@ -0,0 +1,96 @@
+/**
+ * a software bus package geiger counter
+ *
+ * @author Yannick Jestin
+ * @author <a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
+ *
+ */
+import fr.dgac.ivy.* ;
+import gnu.getopt.Getopt ;
+
+
+/**
+ * A program to count to the Ivy software bus messages.
+ * The class itself can be used to collect data and send them on the terminal
+ * or on the bus.
+ */
+public class Counter implements IvyMessageListener, Runnable {
+
+ private Ivy bus ;
+ private int[] secCount = new int[60];
+ private int totalminute=0;
+ private int totaldix=0;
+ private int counter=0;
+ private int moindix=secCount.length-10;
+ private int moinune=1;
+ private Thread thread;
+ boolean isRunning=false;
+ boolean quiet=false;
+
+ public static final String helpmsg = "usage: java Counter -[options]\n\t-b BUS\tspecifies the Ivy bus domain\n\t-q\tquiet, no tty output\n\t-d\tdebug\n\t-h\thelp\n";
+
+ public Counter(String domain,boolean quiet) {
+ this.quiet=quiet;
+ for (int j=0;j<secCount.length;j++) {secCount[j]=0;}
+ bus = new Ivy("Counter","Counter ready",null);
+ bus.bindMsg(".*",this);
+ bus.bindMsg("^EXHAUSTED$",new IvyMessageListener(){
+ public void receive(IvyClient client,String[] args) {
+ isRunning=false;
+ }
+ });
+ thread = new Thread(this);
+ isRunning=true;
+ thread.start();
+ try {
+ bus.start(domain);
+ } catch (IvyException ie) {
+ ie.printStackTrace();
+ }
+ }
+
+ // implements the Runnable interface
+ public void run() {
+ while (isRunning) {
+ try {
+ thread.sleep(1000);
+ } catch (InterruptedException ie) {
+ }
+ totalminute+=secCount[counter]-secCount[moinune];
+ totaldix+=secCount[counter]-secCount[moindix];
+ String s = "MessageCount "+ secCount[counter]+" "+totaldix+" "+totalminute;
+ if (!quiet) { System.out.println(s); }
+ bus.sendMsg(s);
+ moinune=(moinune+1)%secCount.length;
+ moindix=(moindix+1)%secCount.length;
+ counter=(counter+1)%secCount.length;
+ secCount[counter]=0;
+ }
+ }
+
+ public void receive(IvyClient client,String[] args) { secCount[counter]++; }
+
+ public static void main(String[] args) {
+ String domain="127.255.255.255:2010";
+ Getopt opt = new Getopt("Counter",args,"b:dhq");
+ int c;
+ boolean quiet=false;
+ while ((c=opt.getopt()) != -1 ) switch(c) {
+ case 'q':
+ quiet=true;
+ break;
+ case 'b':
+ domain=opt.getOptarg();
+ break;
+ case 'd':
+ System.setProperty("IVY_DEBUG","yesla!");
+ break;
+ case 'h':
+ default:
+ System.out.println(helpmsg);
+ System.exit(0);
+ }
+ new Counter(domain,quiet);
+ } // main
+
+} // class Counter
diff --git a/examples/Makefile b/examples/Makefile
new file mode 100644
index 0000000..4c317f3
--- /dev/null
+++ b/examples/Makefile
@@ -0,0 +1,16 @@
+# Be sure to set this before compiling ...
+# JIKESPATH = /usr/lib/j2re1.3/lib/rt.jar:/usr/share/java/repository
+JAVACOPTS = -deprecation
+.SUFFIXES: .java .class
+ SRCS = TestIvy.java TestIvySwing.java Counter.java ivyTranslater.java
+ OBJS = $(SRCS:.java=.class)
+ JAVAC = jikes
+ #JAVAC = javac
+
+.java.class:
+ $(JAVAC) $<
+
+all: $(OBJS)
+
+clean:
+ /bin/rm -f -- $(OBJS) *~ *.bak
diff --git a/examples/TestIvy.java b/examples/TestIvy.java
new file mode 100644
index 0000000..802f982
--- /dev/null
+++ b/examples/TestIvy.java
@@ -0,0 +1,127 @@
+package fr.dgac.ivy ;
+
+import java.awt.* ;
+import java.awt.event.* ;
+
+/**
+ * toy tool to probe the Ivy software bus.
+ * it relies on the AWT, and is less useable than TestIvySwing, which should
+ * be preferred.
+ *
+ * @see fr.dgac.ivy.TestIvySwing
+ * @author François-Régis Colin
+ * @author Yannick Jestin
+ * @author <a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
+ *
+ * CHANGELOG
+ * 1.0.10:
+ * - System.exit on bus die
+ */
+
+class TestIvy extends Panel implements IvyApplicationListener {
+ private Ivy bus ;
+ private String regexp="";
+ private TextField tfRegex, tfSend ;
+ private TextArea ta ;
+ private Button buApply, buSend, buClear ;
+ private java.text.SimpleDateFormat format = new
+ java.text.SimpleDateFormat("hh:mm:ss");
+ private int regexp_id;
+
+ public TestIvy() throws IvyException {
+ super(new BorderLayout());
+ ta = new TextArea();
+ ta.setEditable(false);
+ add(ta,BorderLayout.CENTER);
+ Panel p = new Panel(new BorderLayout());
+ p.add(new Label("Regex:"),BorderLayout.WEST);
+ tfRegex = new TextField(regexp);
+ tfRegex.addActionListener(new REGCB());
+ p.add(tfRegex,BorderLayout.CENTER);
+ add(p,BorderLayout.NORTH);
+ p = new Panel(new BorderLayout());
+ p.add(new Label("Msg:"),BorderLayout.WEST);
+ tfSend = new TextField("");
+ tfSend.addActionListener(new SENDCB());
+ p.add(tfSend,BorderLayout.CENTER);
+ add(p,BorderLayout.SOUTH);
+ bus = new Ivy("JAVATESTBUS","Testbus is ready",this);
+ bus.start(null);
+ append( "Ivy Domain: "+ bus.getDomain(null) );
+ }
+
+ public static Frame f;
+
+ public static void main(String[] args) throws IvyException {
+ TestIvy tb = new TestIvy();
+ f = new Frame("TestIvy");
+ f.addWindowListener( tb.new WCCB(f,tb)) ;
+ f.add(tb, BorderLayout.CENTER);
+ f.pack();
+ f.setVisible(true);
+ }
+
+ public void connect(IvyClient client) {
+ append(client.getApplicationName() + " connected " );
+ }
+
+ public void disconnect(IvyClient client) {
+ append(client.getApplicationName() + " disconnected " );
+ }
+ public void die(IvyClient client, int id) {
+ f.dispose();
+ System.exit(0);
+ }
+ public void directMessage(IvyClient client, int id, String arg) {
+ append(client.getApplicationName() + " direct Message "+ id + arg );
+ }
+
+ class REGCB implements ActionListener, IvyMessageListener {
+ public void actionPerformed(ActionEvent e) {
+ try {
+ bus.unBindMsg(regexp_id);
+ } catch (IvyException ie) {
+ System.out.println("Big badaboum"); // this should not happen
+ }
+ regexp=tfRegex.getText();
+ regexp.trim();
+ regexp_id = bus.bindMsg(regexp,this);
+ tfRegex.setText("");
+ }
+ public void receive(IvyClient client, String[] args) {
+ String out="client " + client.getApplicationName() + " envoie: [ ";
+ for (int i=0;i<args.length;i++)
+ out=out+args[i]+ ((i<args.length-1)?" , ":"");
+ out = out + " ]";
+ append(out);
+ }
+ }
+
+ class SENDCB implements ActionListener {
+ public void actionPerformed(ActionEvent e) {
+ int count;
+ String tosend = tfSend.getText();
+ tfSend.setText("");
+ count = bus.sendMsg(tosend);
+ append("Sending '" + tosend + "' count " + count );
+ }
+ }
+
+ private void append(String s) {
+ ta.append("[" + format.format(new java.util.Date()) + "] "+ s + "\n");
+ }
+
+ class WCCB extends WindowAdapter {
+ private Frame f;
+ public WCCB(Frame f, TestIvy b) { this.f=f; }
+ public void windowClosing(WindowEvent e) {
+ f.setVisible(false);
+ bus.stop();
+ bus = null;
+ f.dispose();
+ System.exit(0);
+ }
+ }
+
+} // class TestIvy
+// EOF
diff --git a/examples/TestIvySwing.java b/examples/TestIvySwing.java
new file mode 100644
index 0000000..ed61741
--- /dev/null
+++ b/examples/TestIvySwing.java
@@ -0,0 +1,243 @@
+package fr.dgac.ivy ;
+
+import java.awt.BorderLayout;
+import java.awt.event.*;
+import javax.swing.* ;
+import gnu.getopt.Getopt ;
+
+/**
+ * toy tool to probe the Ivy software bus.
+ * it relies on the Swing toolkit, which is not standard on jdk1.1 platform.
+ * if you don't have jdk1.2 or swing, consider downloading it. You can also
+ * use TestIvy
+ *
+ * @see fr.dgac.ivy.TestIvy
+ * @author François-Régis Colin
+ * @author Yannick Jestin
+ * @author <a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
+ */
+class TestIvySwing extends JPanel implements IvyApplicationListener {
+
+ /**
+ * help message for the standalone program
+ */
+ public static final String helpmsg = "usage: java fr.dgac.ivy.TestIvySwing [options]\n\t-b BUS\tspecifies the Ivy bus domain\n\t-q\tquiet, no tty output\n\t-d\tdebug\n\t-h\thelp\n";
+ public static final int WIDTH=30;
+ public static final int HEIGHT=30;
+
+ private static int index;
+ private static int nbTIS=0;
+ private String localname;
+ private Ivy bus ;
+ private String domain;
+ private String regexp = "(.*)";
+ private JLabel laRegex;
+ private JTextArea ta ;
+ private JTextField tfRegex, tfSend ;
+ private JButton buApply, buSend, buClear ;
+ private JComboBox ports;
+ private int regexp_id;
+ private REGCB reg;
+ private java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("hh:mm:ss");
+ private static String[] startDomainList = {
+ "127.255.255.255:2010",
+ "10.192.36:3110",
+ "10.0.0:54321",
+ "228.1.2.4:4567",
+ };
+ private static java.util.Vector domainList;
+
+ static {
+ // initialize the domainlist
+ domainList = new java.util.Vector();
+ for (int i = 0; i<startDomainList.length;i++)
+ domainList.add(startDomainList[i]);
+ }
+
+ public static void main(String[] args) throws IvyException {
+ String domain="127.255.255.255:2010";
+ Getopt opt = new Getopt("Counter",args,"b:dhq");
+ int c;
+ boolean quiet=false;
+ while ((c=opt.getopt()) != -1 ) switch(c) {
+ case 'q':
+ quiet=true;
+ break;
+ case 'b':
+ domain=opt.getOptarg();
+ break;
+ case 'd':
+ System.setProperty("IVY_DEBUG","yesla!");
+ break;
+ case 'h':
+ default:
+ System.out.println(helpmsg);
+ System.exit(0);
+ }
+ newTestIvy(domain);
+ }
+
+ private TestIvySwing(String domain) throws IvyException {
+ super(new BorderLayout());
+ this.domain=domain;
+ nbTIS++;
+ ta = new JTextArea(WIDTH,HEIGHT);
+ ta.setEditable(false);
+ add(new JScrollPane(ta),BorderLayout.CENTER);
+ JPanel p = new JPanel(new BorderLayout());
+ p.add(new JLabel("Regex:"),BorderLayout.WEST);
+ tfRegex = new JTextField();
+ tfRegex.addActionListener(reg=new REGCB());
+ p.add(tfRegex,BorderLayout.CENTER);
+ p.add(laRegex=new JLabel(regexp),BorderLayout.EAST);
+ add(p,BorderLayout.NORTH);
+ JPanel p2 = new JPanel(new BorderLayout());
+ p2.add(new JLabel("Msg:"),BorderLayout.WEST);
+ tfSend = new JTextField("");
+ tfSend.addActionListener(new SENDCB());
+ p2.add(tfSend,BorderLayout.CENTER);
+ p.add(p2,BorderLayout.SOUTH);
+ JButton tmpb ;
+ (p = new JPanel()).add(tmpb=new JButton("spawn"));
+ tmpb.addActionListener(new SPAWN(domain));
+ p.add(tmpb=new JButton("clear"));
+ tmpb.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ ta.setText("");
+ }
+ });
+ ports=new JComboBox();
+ ports.setEditable(true);
+ int index=0;
+ for (java.util.Enumeration e=domainList.elements();e.hasMoreElements();index++) {
+ String port = (String) e.nextElement();
+ ports.addItem(port);
+ if (port == domain ) { ports.setSelectedIndex(index); }
+ }
+ ports.addActionListener(new ComboCB());
+ p.add(ports);
+ add(p,BorderLayout.SOUTH);
+ tfRegex.setNextFocusableComponent(tfSend);
+ tfSend.setNextFocusableComponent(tfRegex);
+ tfSend.setRequestFocusEnabled(true);
+ localname = "TestIvySwing "+Ivy.libVersion+" ("+index+")";
+ index++;
+ bus = new Ivy(localname,localname+" ready",this);
+ regexp_id = bus.bindMsg(regexp,reg);
+ bus.start(domain);
+ append( "Ivy Domain: "+ bus.getDomain(domain) );
+ }
+
+ public void connect(IvyClient client) {
+ append(client.getApplicationName() + " connected " );
+ }
+
+ public void disconnect(IvyClient client) {
+ append(client.getApplicationName() + " disconnected " );
+ }
+
+ public void die(IvyClient client, int id) {
+ append(client.getApplicationName() + " die "+ id );
+ }
+
+ public void directMessage(IvyClient client, int id, String arg) {
+ append(client.getApplicationName() + " direct Message "+ id + arg );
+ }
+
+ private class ComboCB implements ActionListener {
+ public void actionPerformed(ActionEvent e) {
+ String newDomain=(String)ports.getSelectedItem();
+ if (newDomain == domain) {
+ // if it's the same domain, don't do anything
+ return;
+ }
+ domain=newDomain;
+ try {
+ append( "deconnexion from domain "+ bus.getDomain(null));
+ } catch ( IvyException ie ) {
+ System.err.println("deconnexion failed");
+ }
+ bus.stop();
+ try {
+ bus.start(newDomain);
+ append( "Ivy Domain: "+ newDomain );
+ } catch (IvyException ie ) {
+ System.err.println("auuuugh "+newDomain);
+ }
+ }
+ } // ComboCB
+
+ private class REGCB implements ActionListener, IvyMessageListener {
+ public void actionPerformed(ActionEvent e) {
+ try {
+ bus.unBindMsg(regexp_id);
+ } catch (IvyException ie) {
+ System.out.println("big badaboum"); // this cannot happen
+ }
+ regexp=tfRegex.getText();
+ regexp.trim();
+ regexp_id = bus.bindMsg(regexp,this);
+ tfRegex.setText("");
+ laRegex.setText(regexp);
+ }
+ public void receive(IvyClient client, String[] args) {
+ String out="client " + client.getApplicationName() + " envoie: [ ";
+ for (int i=0;i<args.length;i++) {
+ out+=args[i]+ ((i<args.length-1)?" , ":"");
+ }
+ out+=" ]";
+ append(out);
+ }
+ }
+
+ private class SENDCB implements ActionListener {
+ public void actionPerformed(ActionEvent e) {
+ int count;
+ String tosend = tfSend.getText();
+ tfSend.setText("");
+ if ( (count = bus.sendMsg(tosend)) != 0 )
+ append("Sending '" + tosend + "' count " + count );
+ else
+ append("not Sending '" + tosend + "' nobody cares");
+ }
+ }
+
+ private void append(String s) {
+ ta.insert("[" + format.format(new java.util.Date()) + "] "+ s + "\n",0);
+ }
+
+ private static void newTestIvy(String domain) throws IvyException {
+ TestIvySwing tb = new TestIvySwing(domain);
+ JFrame f = new JFrame(tb.localname);
+ f.addWindowListener( tb.new WCCB(f,tb)) ;
+ f.getContentPane().add(tb, BorderLayout.CENTER);
+ f.pack();
+ f.setVisible(true);
+ }
+
+ private class WCCB extends WindowAdapter {
+ private JFrame f;
+ public WCCB(JFrame f, TestIvySwing b) { this.f=f; }
+ public void windowClosing(WindowEvent e) {
+ bus.stop();
+ f.dispose();
+ // I leave when the last TestIvySwing exits
+ if (--nbTIS == 0) System.exit(0);
+ }
+ public void windowActivated(WindowEvent e) {tfSend.grabFocus();}
+ }
+
+ private class SPAWN implements ActionListener {
+ private String domain;
+ public SPAWN(String domain) {this.domain=domain;}
+ public void actionPerformed(ActionEvent e) {
+ try {
+ newTestIvy(domain);
+ } catch (IvyException ie) {
+ ie.printStackTrace();
+ }
+ }
+ }
+
+} // class TestIvySwing
+// EOF