aboutsummaryrefslogtreecommitdiff
path: root/examples/Counter.java
blob: a1dffd1952000b97db8e3138d057161ccb3a18b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
 * Yet another Ivy java program example
 *
 * a software bus message "geiger counter" displaying each and every second
 * the number of messages sent on the bus during the past second, the past ten
 * seconds and the past minute.
 *
 * @author	Yannick Jestin
 * @author	<a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
 *
 * (c) CENA 1998-2003
 * This program is provided as is, under the LGPL licence with the ivy-java
 * package.
 *
 */
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<secCount.length;j++) {secCount[j]=0;}
    bus = new Ivy("Counter","Counter ready",null);
    System.out.println(bus.domains(domain));
    System.out.println("stats:\t1s\t10s\t1m");
    bus.bindMsg(".*",this);
    bus.bindMsg("^EXHAUSTED$",new IvyMessageListener(){
      public void receive(IvyClient client,String[] args) {
	isRunning=false;
      }
    });
    thread = new Thread(this);
    isRunning=true;
    thread.start();
    try {
      bus.start(domain);
    } catch (IvyException ie) {
      ie.printStackTrace();
    }
  }

  // implements the Runnable interface
  public void run() {
    while (isRunning) {
      try {
	thread.sleep(1000);
      } catch (InterruptedException ie) {
      }
      totalminute+=secCount[counter]-secCount[moinune];
      totaldix+=secCount[counter]-secCount[moindix];
      String s = "stats:\t"+ secCount[counter]+"\t"+totaldix+"\t"+totalminute;
      if (!quiet) { System.out.println(s); }
      bus.sendMsg(s);
      moinune=(moinune+1)%secCount.length;
      moindix=(moindix+1)%secCount.length;
      counter=(counter+1)%secCount.length;
      secCount[counter]=0;
    }
  }

  public void receive(IvyClient client,String[] args) { secCount[counter]++; }

  public static void main(String[] args) {
    String domain=Ivy.getDomain(null);
    Getopt opt = new Getopt("Counter",args,"b:dhq");
    int c;
    boolean quiet=false;
    while ((c=opt.getopt()) != -1 ) switch(c) {
      case 'q':
	quiet=true;
	break;
      case 'b':
	domain=opt.getOptarg();
	break;
      case 'd':
	System.setProperty("IVY_DEBUG","yesla!");
	break;
      case 'h':
      default:
	System.out.println(helpmsg);
	System.exit(0);
    }
    new Counter(domain,quiet);
  } // main

} // class Counter