aboutsummaryrefslogtreecommitdiff
path: root/src/Probe.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/Probe.java')
-rw-r--r--src/Probe.java130
1 files changed, 101 insertions, 29 deletions
diff --git a/src/Probe.java b/src/Probe.java
index fdb102e..a0a2ed0 100644
--- a/src/Probe.java
+++ b/src/Probe.java
@@ -1,5 +1,6 @@
package fr.dgac.ivy ;
import java.io.*;
+import java.util.*;
import gnu.getopt.Getopt;
/**
@@ -11,25 +12,38 @@ import gnu.getopt.Getopt;
*
*
* Changelog:
+ * 1.0.12
+ * - Probe can now send empty strings on keyboard input
+ * - rewritten with a looping thread on stdin to allow a cleaner exit on die
+ * message : not very good
+ * - processes .help, .die , .quit and .bye commands
+ * - it is possible to rename the JPROBE on the bus with the -n switch, it can
+ * circumvent name collisions during tests
+ * e.g: java fr.dgac.ivy.Probe -n JPROBE2
* 1.0.10
* exits on end of user input
* handles multiple domains - it was a IvyWatcher problem -
*/
-class Probe implements IvyApplicationListener, IvyMessageListener {
+class Probe implements IvyApplicationListener, IvyMessageListener, Runnable {
/**
* help message for the standalone program
*/
- public static final String helpmsg = "usage: java fr.dgac.ivy.Probe [options] [regexp]\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\n\t regexp is a Perl5 compatible regular expression";
+ public static final String helpCommands = "Available commands:\n.die CLIENTNAME sends a die message\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";
+ 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-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,"b:dh");
+ Getopt opt = new Getopt("Probe",args,"n:b:dh");
int c;
String domain=Ivy.getDomain(null);
+ String name="JPROBE";
while ((c = opt.getopt()) != -1) switch (c) {
case 'b':
domain=opt.getOptarg();
break;
+ case 'n':
+ name=opt.getOptarg();
+ break;
case 'd':
System.setProperty("IVY_DEBUG","yes");
break;
@@ -39,39 +53,95 @@ class Probe implements IvyApplicationListener, IvyMessageListener {
System.exit(0);
} // getopt
Probe p = new Probe();
- Ivy bus=new Ivy("JPROBE","JPROBE ready",p);
- try {
- for (int i=opt.getOptind();i<args.length;i++) {
- System.out.println("you want to subscribe to " + args[i]);
- bus.bindMsg(args[i],p);
- }
- System.out.println("broadcasting on "+domain);
- bus.start(domain);
- String s;
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ Ivy bus=new Ivy(name,name+" ready",p);
+ for (int i=opt.getOptind();i<args.length;i++) {
+ System.out.println("you want to subscribe to " + args[i]);
+ bus.bindMsg(args[i],p);
+ }
+ System.out.println("broadcasting on "+domain);
+ bus.start(domain);
+ p.start(bus);
+ }
+
+ private BufferedReader in;
+ private volatile Thread looperThread;
+ private Ivy bus;
+
+ Probe() {
+ in = new BufferedReader(new InputStreamReader(System.in));
+ looperThread=new Thread(this);
+ }
+
+ public void start(Ivy bus) {
+ this.bus=bus;
+ looperThread.start();
+ }
+
+ public void run() {
+ Thread thisThread=Thread.currentThread();
+ String s;
+ while (looperThread==thisThread) {
// infinite loop on keyboard input
try {
- while (p.looping()) {
- if ( (s=in.readLine()).length()!=0) {
- System.out.println("-> Sent to " +bus.sendMsg(s)+" peers");
- }
- }
+ s=in.readLine();
+ parseCommand(s);
} catch (NullPointerException e) {
// EOF triggered by a ^D, for instance
- bus.stop();
+ bus.stop();
} catch (IOException e) {
System.out.println("ioe ?");
e.printStackTrace();
- bus.stop();
+ bus.stop();
+ } catch (InterruptedException ie) {
+ System.out.println("allo ?");
}
- } catch (IvyException ie) {
- System.out.println("Caught an exception. quitting. "+ie.getMessage());
- }
- System.exit(0); // quit !
+ } //while
+ System.out.println("End of looping");
+ System.exit(0);
}
- private boolean looping=true;
- public boolean looping() { return looping ; } // accessor
+ void parseCommand(String s) throws IOException, InterruptedException {
+ // System.out.println("parsing the ["+s+"] (length "+s.length()+") string");
+ // crude parsing of the ".xyz" commands
+ // TODO use regexps instends of String.lastIndexOf(String)
+ if (s.length()==0) {
+ System.out.println("-> Sent to " +bus.sendMsg(s)+" peers");
+ } else if (s.lastIndexOf(".die ")>=0){
+ String target=s.substring(5);
+ Vector v=bus.getIvyClientsByName(target);
+ if (v.size()==0) {
+ System.out.println("no Ivy client with the name \""+target+"\"");
+ }
+ for (int i=0;i<v.size();i++) {
+ ((IvyClient)v.elementAt(i)).sendDie();
+ }
+ } else if (s.lastIndexOf(".ping ")>=0){
+ String target=s.substring(6);
+ Vector v=bus.getIvyClientsByName(target);
+ if (v.size()==0) {
+ System.out.println("no Ivy client with the name \""+target+"\"");
+ }
+ for (int i=0;i<v.size();i++) {
+ ((IvyClient)v.elementAt(i)).sendPing("test");
+ }
+ } else if ( (s.lastIndexOf(".quit")>=0)||(s.lastIndexOf(".bye")>=0)){
+ bus.stop();
+ System.exit(0);
+ } else if (s.lastIndexOf(".list")>=0) {
+ Vector v = bus.getIvyClients();
+ System.out.println(v.size()+" clients on the bus");
+ for (int i=0;i<v.size();i++) {
+ System.out.println(" -> "+((IvyClient)v.elementAt(i)).getApplicationName());
+ }
+ } else if ( s.lastIndexOf(".help")>=0) {
+ System.out.println(helpCommands);
+ } else if ( s.charAt(0)=='.') {
+ System.out.println("this command is not recognized");
+ System.out.println(helpCommands);
+ } else {
+ System.out.println("-> Sent to " +bus.sendMsg(s)+" peers");
+ }
+ } // parseCommand
public void connect(IvyClient client) {
System.out.println(client.getApplicationName() + " connected " );
@@ -85,9 +155,11 @@ class Probe implements IvyApplicationListener, IvyMessageListener {
}
public void die(IvyClient client, int id) {
- looping=false;
- System.out.println("received die msg from " + client.getApplicationName() );
- System.exit(0);
+ System.out.println("received die msg from " + client.getApplicationName() );
+ Thread t = looperThread;
+ looperThread = null;
+ t.interrupt(); // TODO does'nt work
+ System.exit(0);
}
public void directMessage(IvyClient client, int id, String arg) {