import gnu.regexp.*; import gnu.getopt.*; import fr.dgac.ivy.*; import java.io.*; /* * a program with 2 busses testing * - the rendez vous * - the IvyMessageListener interface * - disconnect part of the IvyApplicationListener interface */ class BenchLocal { public static final String helpmsg = "usage: java TestLocal [options]\n\t-b domain\n\t-d delay (in ms)\n\t-t test number\n\t-h\thelp\n\n"; public static void main(String[] args) { Getopt opt = new Getopt("BenchLocal",args,"t:b:d:h"); String domain=Ivy.getDomain(null); // default bus int delay=2000; int c; int testtoperform=1; while ((c = opt.getopt()) != -1) switch (c) { case 'b': domain=opt.getOptarg(); break; case 'd': delay=Integer.parseInt(opt.getOptarg()); break; case 't': testtoperform=Integer.parseInt(opt.getOptarg()); break; case 'h': default: System.out.println(helpmsg); System.exit(0); } new BenchLocal(testtoperform,domain,delay); } public BenchLocal(int testtoperform,String domain,int delay) { try { switch (testtoperform) { case 2: testRegex(domain,delay); break; case 1: default: test2bus(domain,delay); break; } } catch (IvyException ie) { fail(ie.getMessage()); } } void fail(String msg) { System.out.println("failed: "+msg); System.exit(-1); } void sleep(int delay) { System.out.println("waiting "+delay+" ms"); try { Thread.sleep(delay); } catch (InterruptedException ie) { } } public void testRegex(String domain,int delay) throws IvyException { Ivy bus1,bus2; IAL ial=new IAL(); bus1=new Ivy("BUS1","Bus1 ready",ial); bus2=new Ivy("BUS2","Bus2 ready",ial); ial.setBusses(bus1,bus2); bus1.bindMsg("^Bus2 ready",new RML(bus1,delay)); SuccessStory success=new SuccessStory(bus1,bus2); bus2.bindMsg("^([^ ]*) ([^ ]*) ([^ ]*)$",new RMLAnswer1(success)); bus2.bindMsg("(.*)",new RMLAnswer2(success)); bus2.bindMsg("y=([^ ]*)",new RMLAnswer3(success)); bus1.start(domain); bus2.start(domain); } private class RML implements IvyMessageListener { Ivy b; int delay; public RML(Ivy b,int delay) { this.b=b;this.delay=delay; } public void receive(IvyClient c,String[] args) { b.sendMsg("a b c"); sleep(delay); b.sendMsg(""); sleep(delay); b.sendMsg("x=1 y=2 z=3"); b.stop(); } } private class SuccessStory { int i=0; Ivy b1,b2; public SuccessStory(Ivy b1,Ivy b2){this.b1=b1;this.b2=b2;} public void incr(){ i++; System.out.println("regex "+i+" successful"); if (i==3) { System.out.println("quitting the bus"); b1.stop();b2.stop(); } } } private class RMLAnswer1 implements IvyMessageListener { SuccessStory ss; public RMLAnswer1(SuccessStory ss) {this.ss=ss;} public void receive(IvyClient c,String[] args) { if ( (args.length==3) && (args[0].compareTo("a")==0) && (args[1].compareTo("b")==0) && (args[2].compareTo("c")==0) ) ss.incr(); } } private class RMLAnswer2 implements IvyMessageListener { SuccessStory ss; public RMLAnswer2(SuccessStory ss) {this.ss=ss;} public void receive(IvyClient c,String[] args) { if ( (args.length==1) && (args[0].compareTo("")==0) ) ss.incr(); } } private class RMLAnswer3 implements IvyMessageListener { SuccessStory ss; public RMLAnswer3(SuccessStory ss) {this.ss=ss;} public void receive(IvyClient c,String[] args) { if ( (args.length==1) && (args[0].compareTo("2")==0) ) ss.incr(); } } public void test2bus(String domain,int delay) throws IvyException { Ivy bus1,bus2; IAL ial=new IAL(); System.out.println("starting with delay="+delay+" ms"); bus1=new Ivy("BUS1","Bus1 ready",ial); bus2=new Ivy("BUS2","Bus2 ready",ial); ial.setBusses(bus1,bus2); bus1.bindMsg("^Bus2 ready",new SENDOUT(bus1)); bus2.bindMsg("^out$",new DIE(bus2)); System.out.println("starting Bus1"); bus1.start(domain); sleep(delay); System.out.println("starting Bus2"); bus2.start(domain); } private class SENDOUT implements IvyMessageListener { Ivy b; public SENDOUT(Ivy b) {this.b=b;} public void receive(IvyClient client,String[] arg) { System.out.println("received ready message"); b.sendMsg("out"); b.stop(); } } private class IAL implements IvyApplicationListener { Ivy b1,b2; int count=0; public void setBusses(Ivy b1,Ivy b2) { this.b1=b1; this.b2=b2; } public void disconnect(IvyClient c) { if ( (c.getApplicationName().compareTo("BUS1")==0) ||(c.getApplicationName().compareTo("BUS2")==0) ) { count++; System.out.println(c.getApplicationName()+" left"); if (count==2) {System.exit(0);} } } public void connect(IvyClient c) { } public void directMessage(IvyClient c,int id,String arg) { } public void die(IvyClient c,int id) { } } private class DIE implements IvyMessageListener { Ivy b; public DIE(Ivy b) {this.b=b; } public void receive(IvyClient client,String[] arg) { System.out.println("received out message"); b.stop(); } } }