From cb0e14bbd6882bed96642628ddc5e6d9c6c8baa4 Mon Sep 17 00:00:00 2001
From: jestin
Date: Wed, 6 Mar 2002 12:56:54 +0000
Subject: Counter counts an approximative number of messages going on the bus,
think xload, but for ivy messages. 1 second, 10 seconds, 1 minute
---
src/Counter.java | 98 ++++++++++++++++++
src/Ivy.java | 34 ++++---
src/IvyClient.java | 271 ++++++++++++++++++++++++++++----------------------
src/Makefile | 2 +-
src/Probe.java | 17 +++-
src/TestIvySwing.java | 85 +++++++++++-----
6 files changed, 345 insertions(+), 162 deletions(-)
create mode 100644 src/Counter.java
diff --git a/src/Counter.java b/src/Counter.java
new file mode 100644
index 0000000..7e355db
--- /dev/null
+++ b/src/Counter.java
@@ -0,0 +1,98 @@
+/**
+ * a software bus package geiger counter
+ *
+ * @author Yannick Jestin
+ * @author http://www.tls.cena.fr/products/ivy/
+ *
+ */
+package fr.dgac.ivy ;
+
+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> matching "+regexp.getNumSubs()+" blocks "+tmp);
send(Msg,key,regexp.getNumSubs(),result);
count++;
}
@@ -118,8 +116,6 @@ public class IvyClient extends Thread {
*/
void close(String msg) throws IOException {
traceDebug( msg );
- socket.close(); // TODO it seems it doesnt stop the thread
- out.close();
in.close();
gardefou=false;
}
@@ -143,108 +139,23 @@ public class IvyClient extends Thread {
public void run() {
String msg = null;
try {
- traceDebug("Connected from "+ socket.getInetAddress().getHostName()+
- ":"+socket.getPort());
+ traceDebug("Connected from "+ socket.getInetAddress().getHostName()+ ":"+socket.getPort());
+ /*
+ * TODO bug ?!
+ * sometime we're stuck in readline while gardefou is false
+ * we need non blocking IO, found in the next 1.4 jdk ...
+ */
while ( gardefou && ((msg=in.readLine()) != null )) {
- int msgtype; // type of the last message received
- Integer msgid; // ID of the last message received
- RE regexp = null; // compiled regexp
- String token = null;
- /*
- * First stage: extract the message
- */
- StringTokenizer st = new StringTokenizer(msg);
- if(!st.hasMoreTokens()){close("Bad format no type '"+msg+"'");break;}
- token=st.nextToken().trim();
- if (token.length()==0){close("Bad format no type '"+msg+"'");break;}
- try {
- msgtype = Integer.parseInt(token);
- } catch ( NumberFormatException e ) {
- close("Bad format error parsing type'"+msg+"'");
- break;
- }
- if(!st.hasMoreTokens()){close("Bad format no id '"+msg+"'");break;}
-
- token=st.nextToken(StartArg).trim();
- try {
- msgid=Integer.valueOf(token);
- } catch ( NumberFormatException e ) {
- close("Bad format error parsing id '"+token+"'");
- break;
- }
- String msgarg="";
- // if (st.hasMoreTokens()) msgarg=st.nextToken("\n").trim();
- if (st.hasMoreTokens()) msgarg=st.nextToken("\n");
- // TODO: here is a bug !
- // quick and dirty fix: I do the trimming by hand later on.
- // (cf ref: This method may be used to trim whitespace from the
- // beginning and end of a string; in fact, it trims all ASCII control characters as well.
-
- /*
- * second stage: process the message
- */
- switch (msgtype) {
- case Bye: break;
- case AddRegexp:
- msgarg.trim();
- if ( bus.CheckRegexp(msgarg) ) {
- try {
- regexp_in.put(msgid,new RE(msgarg));
- regexp_text.put(msgid,msgarg);
- } catch (REException e) {
- System.err.println("Bad pattern: "+e.getMessage());
- }
- } else {
- System.err.println(
- "Warning exp='"+msgarg+"' can't match removing from "+appName);
- }
- break;
- case DelRegexp:
- regexp_in.remove(msgid);
- regexp_text.remove(msgid);
- break;
- case EndRegexp:
- bus.connect(this);
- /* TODO
- * BUG ? the peer is perhaps not ready to handle this message
- * an assymetric processing should be written
- */
- if (bus.ready_message!=null) sendMsg(bus.ready_message);
- break;
- case Msg:
- try {
- bus.callCallback(this,msgid,msgarg.substring(1,msgarg.length()-1));
- } catch (IvyException ie) {
- // calling an inexistant callback
- System.err.println("calling an inexistant callback, the caller must be wrong !");
- }
- break;
- case Error:
- traceDebug("Error msg "+msgid+" "+msgarg);
- break;
- case SchizoToken:
- appName=msgarg;
- appPort=msgid.intValue();
- if ( bus.checkConnected(this) ) {
- close("Quitting Application already connected");
- System.err.println("Rare ! A concurrent connect occured");
- }
- break;
- case DirectMsg:
- msgarg.trim();
- bus.directMessage( this, msgid.intValue(), msgarg );
- break;
- case Die:
- bus.die( this,msgid.intValue());
- break;
- default:
- System.err.println("*** IvyClient *** unhandled msg type "+
- msgtype+" "+msgid+msgarg);
- break;
- } // switch
- } // while gardefou
- traceDebug("normally Disconnected from "+
- socket.getInetAddress().getHostName()+":"+socket.getPort());
+ try {
+ newParseMsg(msg);
+ } catch (IvyException ie) {
+ ie.printStackTrace();
+ }
+ }
+ traceDebug("normally Disconnected from "+ socket.getInetAddress().getHostName()+":"+socket.getPort());
+ socket.close();
+ out.close();
+ in.close();
} catch (IOException e) {
traceDebug("abnormally Disconnected from "+
socket.getInetAddress().getHostName()+":"+socket.getPort());
@@ -253,22 +164,24 @@ public class IvyClient extends Thread {
bus.removeClient( this );
}
- private void sendBuffer( String buffer ) {
+ private void sendBuffer( String buffer ) throws IvyException {
buffer += "\n";
try {
out.write(buffer.getBytes() );
out.flush();
} catch ( IOException e ) {
- /*
- * TODO
- * we should throw an exception here
- */
- System.err.println("IvyClient.sendBuffer.write failed. FIX ME");
+ throw new IvyException("IvyClient.sendBuffer.write failed: "+e.getMessage());
}
}
private void send(int type, int id, String arg) {
- sendBuffer(type+" "+id+StartArg+arg);
+ try {
+ sendBuffer(type+" "+id+StartArg+arg);
+ } catch (IvyException ie ) {
+ // TODO shoud fix the exception Handling here ...
+ System.err.println("received an exception: " + ie.getMessage());
+ ie.printStackTrace();
+ }
}
private void send(int type, Integer id, int nbsub, REMatch result) {
@@ -279,7 +192,127 @@ public class IvyClient extends Thread {
buffer += result.toString(sub)+EndArg;
}
}
- sendBuffer(buffer);
+ try {
+ sendBuffer(buffer);
+ } catch (IvyException ie ) {
+ // TODO shoud fix the exception Handling here ...
+ System.err.println("received an exception: " + ie.getMessage());
+ ie.printStackTrace();
+ }
+ }
+
+ private String dumpHex(String s) {
+ byte[] b = s.getBytes();
+ String out = "";
+ String zu = "\t";
+ for (int i=0;i15) ? c : 'X')+" ";
+ }
+ out += zu;
+ return out;
+ }
+
+ private String dumpMsg(String s) {
+ String deb = " \""+s+"\" "+s.length()+" cars, ";
+ for (int i=0;i=b.length) throw new IvyException("protocol error");
+ try {
+ msgType = Integer.parseInt(s.substring(from,to));
+ } catch (NumberFormatException nfe) {
+ throw new IvyException("protocol error on msgType");
+ }
+ from=to+1;
+ while ((to=b.length) throw new IvyException("protocol error");
+ try {
+ msgId = new Integer(s.substring(from,to));
+ } catch (NumberFormatException nfe) {
+ throw new IvyException("protocol error on identifier");
+ }
+ from=to+1;
+ switch (msgType) {
+ case Bye:
+ bus.die(this,msgId.intValue());
+ gardefou=false;
+ break;
+ case AddRegexp:
+ String regexp=s.substring(from,b.length);
+ if ( bus.CheckRegexp(regexp) ) {
+ try {
+ regexp_in.put(msgId,new RE(regexp));
+ regexp_text.put(msgId,regexp);
+ } catch (REException e) {
+ throw new IvyException("regexp error " +e.getMessage());
+ }
+ } else {
+ throw new IvyException("regexp Warning exp='"+regexp+"' can't match removing from "+appName);
+ }
+ break;
+ case DelRegexp:
+ regexp_in.remove(msgId);
+ regexp_text.remove(msgId);
+ break;
+ case EndRegexp:
+ bus.connect(this);
+ /* TODO
+ * BUG ? the peer is perhaps not ready to handle this message
+ * an assymetric processing should be written
+ */
+ if (bus.ready_message!=null) sendMsg(bus.ready_message);
+ break;
+ case Msg:
+ Vector v = new Vector();
+ while (to