diff options
Diffstat (limited to 'src/Probe.java')
-rw-r--r-- | src/Probe.java | 73 |
1 files changed, 53 insertions, 20 deletions
diff --git a/src/Probe.java b/src/Probe.java index bd37089..8e4f9c2 100644 --- a/src/Probe.java +++ b/src/Probe.java @@ -7,6 +7,9 @@ * (c) CENA * * Changelog: + * 1.2.4 + * - now uses the bindListener paradigm to display the binding/unbinding dynamically + * - adds the -s (send to self) command line switch * 1.2.3 * - now allows directMessages with the .direct command * - the parseMsg is being rewritten with regexps @@ -40,17 +43,18 @@ import java.util.*; import gnu.getopt.Getopt; import gnu.regexp.*; -public class Probe implements IvyApplicationListener, IvyMessageListener, Runnable { +public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBindListener, Runnable { public static final String helpCommands = "Available commands:\n.die CLIENTNAME sends a die message\n.direct CLIENTNAME ID MESSAGE sends the direct message to the client, with a message id set to the numerical ID\n.bye quits the application\n.quit idem\n.list lists the available clients\n.ping sends a ping request if IVY_PING is enabled\n.bind REGEXP binds to a regexp at runtime\n.unbind REGEXP unbinds to a regexp at runtime"; - public static final String helpmsg = "usage: java fr.dgac.ivy.Probe [options] [regexp]\n\t-b BUS\tspecifies the Ivy bus domain\n\t-n ivyname (default JPROBE)\n\t-q\tquiet, no tty output\n\t-d\tdebug\n\t-t\ttime stamp each message\n\t-h\thelp\n\n\t regexp is a Perl5 compatible regular expression"; + public static final String helpmsg = "usage: java fr.dgac.ivy.Probe [options] [regexp]\n\t-b BUS\tspecifies the Ivy bus domain\n\t-n ivyname (default JPROBE)\n\t-q\tquiet, no tty output\n\t-d\tdebug\n\t-t\ttime stamp each message\n\t-s\tsends to self\n\t-h\thelp\n\n\t regexp is a Perl5 compatible regular expression"; public static void main(String[] args) throws IvyException { - Getopt opt = new Getopt("Probe",args,"n:b:dqht"); + Getopt opt = new Getopt("Probe",args,"n:b:dqsht"); int c; boolean timestamp=false; boolean quiet=false; + boolean sendsToSelf=false; String domain=Ivy.getDomain(null); String name="JPROBE"; while ((c = opt.getopt()) != -1) switch (c) { @@ -62,17 +66,24 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, Runnab case 'n': name=opt.getOptarg(); break; case 'q': quiet=true; break; case 't': timestamp=true; break; + case 's': sendsToSelf=true; break; case 'h': default: System.out.println(helpmsg); System.exit(0); } Probe p = new Probe(new BufferedReader(new InputStreamReader(System.in)),timestamp,quiet,System.getProperty("IVY_DEBUG")!=null); p.setExitOnDie(true); Ivy bus=new Ivy(name,name+" ready",null); + bus.addBindListener(p); + bus.sendToSelf(sendsToSelf); for (int i=opt.getOptind();i<args.length;i++) { - if (!quiet) System.out.println("you want to subscribe to " + args[i]); - bus.bindMsg(args[i],p); + try { + bus.bindMsg(args[i],p); + if (!quiet) System.out.println("you have subscribed to " + args[i]); + } catch (IvyException ie) { + System.out.println("you have not subscribed to " + args[i]+ ", this regexp is invalid"); + } } - if (!quiet) System.out.println(bus.domains(domain)); + if (!quiet) System.out.println("broadcasting on "+bus.domains(domain)); bus.start(domain); p.start(bus); } @@ -135,34 +146,44 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, Runnab REMatch result; traceDebug("parsing the ["+s+"] (length "+s.length()+") string"); // crude parsing of the ".xyz" commands - // TODO use regexps instends of String.lastIndexOf(String). Example - // provided with .direct ! if (s.length()==0) { - println("-> Sent to " +bus.sendMsg(s)+" peers"); + try { + println("-> Sent to " +bus.sendMsg(s)+" peers"); + } catch (IvyException ie) { + println("-> not sent, the message contains incorrect characters"); + } } else if ((result=directMsgRE.getMatch(s))!=null) { String target = result.toString(1); int id = Integer.parseInt(result.toString(2)); String message = result.toString(3); Vector v=bus.getIvyClientsByName(target); if (v.size()==0) println("no Ivy client with the name \""+target+"\""); - for (int i=0;i<v.size();i++) ((IvyClient)v.elementAt(i)).sendDirectMsg(id,message); + try { + for (int i=0;i<v.size();i++) ((IvyClient)v.elementAt(i)).sendDirectMsg(id,message); + } catch (IvyException ie) { + println("-> not sent, the message contains incorrect characters"); + } return; } else if (s.lastIndexOf(".die ")>=0){ String target=s.substring(5); Vector v=bus.getIvyClientsByName(target); if (v.size()==0) println("no Ivy client with the name \""+target+"\""); - for (int i=0;i<v.size();i++) ((IvyClient)v.elementAt(i)).sendDie(); + for (int i=0;i<v.size();i++) ((IvyClient)v.elementAt(i)).sendDie("java probe wants you to leave the bus"); } else if (s.lastIndexOf(".unbind ")>=0){ String regexp=s.substring(8); if (bus.unBindMsg(regexp)) { - println("you want to unsubscribe to " + regexp); + println("you have unsubscribed to " + regexp); } else { println("you can't unsubscribe to " + regexp + ", your're not subscribed to it"); } } else if (s.lastIndexOf(".bind ")>=0){ String regexp=s.substring(6); - println("you want to subscribe to " + regexp); - bus.bindMsg(regexp,this); + try { + bus.bindMsg(regexp,this); + println("you have now subscribed to " + regexp); + } catch (IvyException ie) { + System.out.println("warning, the regular expression '" + regexp + "' is invalid. Not bound !"); + } } else if (s.lastIndexOf(".ping ")>=0){ String target=s.substring(6); Vector v=bus.getIvyClientsByName(target); @@ -187,28 +208,40 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, Runnab println("this command is not recognized"); println(helpCommands); } else { - println("-> Sent to " +bus.sendMsg(s)+" peers"); + try { + println("-> Sent to " +bus.sendMsg(s)+" peers"); + } catch (IvyException ie) { + println("-> not sent, the line contains incorrect characters"); + } } } // parseCommand + public void bindPerformed(IvyClient client,int id,String re) { + println(client.getApplicationName() + " subscribes to " +re ); + } + + public void unbindPerformed(IvyClient client,int id,String re) { + println(client.getApplicationName() + " unsubscribes to " +re ); + } + public void connect(IvyClient client) { println(client.getApplicationName() + " connected " ); - for (java.util.Enumeration e=client.getRegexps();e.hasMoreElements();) - println(client.getApplicationName() + " subscribes to " +e.nextElement() ); + // for (java.util.Enumeration e=client.getRegexps();e.hasMoreElements();) + // println(client.getApplicationName() + " subscribes to " +e.nextElement() ); } public void disconnect(IvyClient client) { println(client.getApplicationName() + " disconnected " ); } - public void die(IvyClient client, int id) { - println("received die msg from " + client.getApplicationName() +" good bye"); + public void die(IvyClient client, int id,String msgarg) { + println("received die msg from " + client.getApplicationName() +" with the message: "+msgarg+", good bye"); /* I cannot stop the readLine(), because it is native code */ if (exitOnDie) System.exit(0); } public void directMessage(IvyClient client, int id, String arg) { - println(client.getApplicationName() + " direct Message "+ id + arg ); + println(client.getApplicationName() + " sent the direct Message, id: "+ id + ", arg: "+ arg ); } public void receive(IvyClient client, String[] args) { |