aboutsummaryrefslogtreecommitdiff
path: root/src/Probe.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/Probe.java')
-rw-r--r--src/Probe.java125
1 files changed, 65 insertions, 60 deletions
diff --git a/src/Probe.java b/src/Probe.java
index c3f05c2..4425dd4 100644
--- a/src/Probe.java
+++ b/src/Probe.java
@@ -7,8 +7,12 @@
* (c) CENA
*
* Changelog:
+ * 1.2.14
+ * - uses the "new" for: loop construct of Java5
+ * - throws RuntimeException instead of System.exit(), allows code reuse
+ * - switch from gnu regexp (deprecated) to the built in java regexp
* 1.2.13
- * - adds support for RESyntaxException
+ * - adds support for RESyntaxException
* 1.2.12
* - .ping is back
* 1.2.9
@@ -59,7 +63,7 @@ import fr.dgac.ivy.* ;
import java.io.*;
import java.util.*;
import gnu.getopt.Getopt;
-import org.apache.regexp.*;
+import java.util.regex.*;
public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBindListener, Runnable {
@@ -110,14 +114,15 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
java.util.StringTokenizer classTok = new java.util.StringTokenizer(opt.getOptarg(),",");
messageClass=new String[classTok.countTokens()];
System.out.println("YANNNN "+messageClass.length);
- for (int i=0;classTok.hasMoreElements();) messageClass[i++]=new String((String)classTok.nextElement());
+ for (int i=0;classTok.hasMoreElements();)
+ messageClass[i++] = classTok.nextToken();
break;
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);
+ default: System.out.println(helpmsg); return;
}
Probe p = new Probe(new BufferedReader(new InputStreamReader(System.in)),timestamp,quiet,System.getProperty("IVY_DEBUG")!=null);
p.setExitOnDie(true);
@@ -126,7 +131,7 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
bus.sendToSelf(sendsToSelf);
if (messageClass!=null) {
System.out.println("using a message class filter of "+messageClass.length+" elements");
- for (int i=0;i<messageClass.length;i++) System.out.println(messageClass[i]);
+ for (String cls: messageClass) System.out.println(cls);
bus.setFilter(messageClass);
}
for (int i=opt.getOptind();i<args.length;i++) {
@@ -145,19 +150,17 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
private BufferedReader in;
private volatile Thread looperThread;
private Ivy bus;
- private boolean timestamp,quiet,debug,exitOnDie=false;
-
- private static RE directMsgRE, timeCountRE;
-
+ private boolean timestamp,quiet,debug,exitOnDie=false, encore=true;
+ private static Pattern directMsgRE, timeCountRE;
static {
try {
- directMsgRE = new RE("^\\.direct ([^ ]*) ([0-9]+) (.*)");
- timeCountRE = new RE("^\\.time (\\d+) (.*)");
- } catch ( RESyntaxException res ) {
+ directMsgRE = Pattern.compile("^\\.direct ([^ ]*) ([0-9]+) (.*)");
+ timeCountRE = Pattern.compile("^\\.time (\\d+) (.*)");
+ } catch ( PatternSyntaxException res ) {
res.printStackTrace();
System.out.println("Regular Expression bug in Ivy source code ... bailing out");
- System.exit(0);
+ throw new RuntimeException();
}
}
public Probe(BufferedReader in, boolean timestamp,boolean quiet,boolean debug) {
@@ -181,13 +184,14 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
traceDebug("Probe Thread started");
Thread thisThread=Thread.currentThread();
String s;
- println(bus.getSelfIvyClient().getApplicationName()+ " ready, type .help and return to get help");
+ SelfIvyClient sic = bus.getSelfIvyClient();
+ println(sic.getApplicationName()+ " ready, type .help and return to get help");
// "infinite" loop on keyboard input
- while (looperThread==thisThread) {
+ while (encore && looperThread==thisThread) {
try {
s=in.readLine();
if (s==null) break;
- parseCommand(s);
+ if (!parseCommand(s)) break;
} catch (NullPointerException e) {
// EOF triggered by a ^D, for instance
break;
@@ -201,35 +205,36 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
traceDebug("Probe Thread stopped");
}
- void parseCommand(String s) throws IOException {
+ boolean parseCommand(String s) throws IOException {
traceDebug("parsing the ["+s+"] (length "+s.length()+") string");
// crude parsing of the ".xyz" commands
+ Matcher m = directMsgRE.matcher(s);
+ Matcher mtime = timeCountRE.matcher(s);
if (s.length()==0) {
try {
println("-> Sent to " +bus.sendMsg(s)+" peers");
} catch (IvyException ie) {
println("-> not sent, the message contains incorrect characters");
}
- } else if (directMsgRE.match(s)) {
- String target = directMsgRE.getParen(1);
- int id = Integer.parseInt(directMsgRE.getParen(2));
- String message = directMsgRE.getParen(3);
- Vector v=bus.getIvyClientsByName(target);
+ } else if (m.matches()) {
+ String target = m.group(1);
+ int id = Integer.parseInt(m.group(2));
+ String message = m.group(3);
+ Vector<IvyClient>v=bus.getIvyClientsByName(target);
if (v.size()==0) println("no Ivy client with the name \""+target+"\"");
try {
- for (int i=0;i<v.size();i++) ((IvyClient)v.elementAt(i)).sendDirectMsg(id,message);
+ for (IvyClient ic : v) ic.sendDirectMsg(id,message);
} catch (IvyException ie) {
println("-> not sent, the message contains incorrect characters");
}
- return;
} else if (s.lastIndexOf(".dieall-yes-i-am-sure")>=0){
- Vector v=bus.getIvyClients();
- for (int i=0;i<v.size();i++) ((IvyClient)v.elementAt(i)).sendDie("java probe wants you to leave the bus");
+ Vector<IvyClient>v=bus.getIvyClients();
+ for (IvyClient ic: v) ic.sendDie("java probe wants you to leave the bus");
} else if (s.lastIndexOf(".die ")>=0){
String target=s.substring(5);
- Vector v=bus.getIvyClientsByName(target);
+ Vector<IvyClient>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("java probe wants you to leave the bus");
+ for (IvyClient ic: v) ic.sendDie("java probe wants you to leave the bus");
} else if (s.lastIndexOf(".unbind ")>=0){
String regexp=s.substring(8);
if (bus.unBindMsg(regexp)) {
@@ -238,14 +243,13 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
println("you can't unsubscribe to " + regexp + ", your're not subscribed to it");
}
} else if (s.lastIndexOf(".bound *")>=0){
- Vector v=bus.getIvyClients();
+ Vector<IvyClient>v=bus.getIvyClients();
int total=0;
int boundedtotal=0;
- for (int i=0;i<v.size();i++) {
- IvyClient ic=(IvyClient)v.elementAt(i);
- for (Enumeration e = ic.getRegexps();e.hasMoreElements();) {
+ for (IvyClient ic: v) {
+ for (Enumeration<String> e = ic.getRegexps();e.hasMoreElements();) {
total++;
- String r = (String)e.nextElement();
+ String r = e.nextElement();
if (r.startsWith("^")) boundedtotal++;
println(ic.getApplicationName()+" has subscribed to: "+r);
}
@@ -255,13 +259,12 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
int total=0;
int boundedtotal=0;
String target=s.substring(7);
- Vector v=bus.getIvyClientsByName(target);
+ Vector<IvyClient>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 ic=(IvyClient)v.elementAt(i);
- for (Enumeration e = ic.getRegexps();e.hasMoreElements();) {
+ for (IvyClient ic:v) {
+ for (Enumeration<String> e = ic.getRegexps();e.hasMoreElements();) {
total++;
- String r = (String)e.nextElement();
+ String r = e.nextElement();
if (r.startsWith("^")) boundedtotal++;
println(target+" has subscribed to: "+(String)e.nextElement());
}
@@ -269,8 +272,8 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
}
} else if (s.lastIndexOf(".bound")>=0){
println("you have subscribed to:");
- for (Enumeration e = bus.getSelfIvyClient().getRegexps();e.hasMoreElements();) {
- println("\t"+(String)e.nextElement());
+ for (Enumeration<String> e = bus.getSelfIvyClient().getRegexps();e.hasMoreElements();) {
+ println("\t"+e.nextElement());
}
} else if (s.lastIndexOf(".bind ")>=0){
String regexp=s.substring(6);
@@ -282,20 +285,18 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
}
} else if ( (s.lastIndexOf(".quit")>=0)||(s.lastIndexOf(".bye")>=0)){
bus.stop();
- System.exit(0);
+ return false;
} else if (s.lastIndexOf(".list")>=0) {
- Vector v = bus.getIvyClients();
+ Vector<IvyClient> v = bus.getIvyClients();
println(v.size()+" clients on the bus");
- for (int i=0;i<v.size();i++) {
- println("-> "+((IvyClient)v.elementAt(i)).getApplicationName());
- }
+ for (IvyClient ic: v) println("-> "+ic.getApplicationName());
} else if ( s.lastIndexOf(".ping ")>=0) {
String target=s.substring(6);
- Vector v=bus.getIvyClientsByName(target);
+ Vector<IvyClient>v=bus.getIvyClientsByName(target);
if (v.size()==0) println("no Ivy client with the name \""+target+"\"");
- for (int i=0;i<v.size();i++) {
+ for (IvyClient ic:v) {
try {
- ((IvyClient)v.elementAt(i)).ping(new PingCallback() {
+ ic.ping(new PingCallback() {
public void pongReceived(IvyClient ic,int elapsedTime){
System.out.println("round trip to "+ic.getApplicationName()+" "+elapsedTime+" ms");
}
@@ -306,17 +307,15 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
}
} else if ( s.lastIndexOf(".where ")>=0) {
String target=s.substring(7);
- Vector v=bus.getIvyClientsByName(target);
+ Vector<IvyClient>v=bus.getIvyClientsByName(target);
if (v.size()==0) println("no Ivy client with the name \""+target+"\"");
- for (int i=0;i<v.size();i++) {
- println(target+" runs on "+((IvyClient)v.elementAt(i)).getHostName());
- }
- } else if (timeCountRE.match(s)) {
+ for (IvyClient ic: v) println(target+" runs on "+ic.getHostName());
+ } else if (mtime.matches()) {
long before = new java.util.Date().getTime();
- int times=Integer.parseInt(timeCountRE.getParen(1));
+ int times=Integer.parseInt(mtime.group(1));
try {
int n=0;
- for (int i=0;i<times;i++) n=bus.sendMsg(timeCountRE.getParen(2));
+ for (int i=0;i<times;i++) n=bus.sendMsg(mtime.group(2));
long after = new java.util.Date().getTime();
println("-> it took "+(after-before)+"ms to send "+times+" to " +n+" peers");
} catch (IvyException ie) {
@@ -334,11 +333,12 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
println("-> not sent, the line contains incorrect characters");
}
}
+ return true;
} // parseCommand
public void bindPerformed(IvyClient client,int id,String re) {
String s="";
- if (!bus.CheckRegexp(re)) s=" WITH NO EFFECT";
+ if (!bus.checkRegexp(re)) s=" WITH NO EFFECT, because of the filters";
println(client.getApplicationName() + " subscribes to " +re+s);
}
@@ -358,7 +358,7 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
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 */
+ /* I cannot stop the readLine(), because it is native code ?! */
if (exitOnDie) System.exit(0);
}
@@ -367,9 +367,14 @@ public class Probe implements IvyApplicationListener, IvyMessageListener, IvyBin
}
public void receive(IvyClient client, String[] args) {
- String s=client.getApplicationName() + " sent";
- for (int i=0;i<args.length;i++) s+=" '"+args[i]+"'";
- println(s);
+ StringBuffer s=new StringBuffer(client.getApplicationName());
+ s.append(" sent");
+ for (String arg: args) {
+ s.append(" '");
+ s.append(arg);
+ s.append("'");
+ }
+ println(s.toString());
}
private void traceDebug(String s){ if (debug) System.out.println("-->Probe<-- "+s); }